employeeRanking.js
6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// pages/tab/crew/components/employeeRanking/employeeRanking.js
const DEFAULT_CONFIG = {
performance: {
sortOptions: [
{ label: '产值', key: '1', type: 'number' },
{ label: '客单价', key: '2', type: 'number' },
{ label: '转化率', key: '3', type: 'percent' }
],
metrics: [
{ label: '个人产值', key: 'personalOutput', dotKey:'outputWarningLevel', type: 'number', width: '234rpx' },
{ label: '个人客单价', key: 'personalUnitPrice', dotKey:'unitPriceWarningLevel', type: 'number', width: '216rpx'},
{ label: '转化率', key: 'personalConversion', dotKey:'conversionWarningLevel', type: 'percent', width: '192rpx' }
]
},
behavior: {
sortOptions: [
{ label: '成交数', key: '4', type: 'number' },
{ label: '战败数', key: '5', type: 'number' },
{ label: '录音数', key: '6', type: 'number' }
],
metrics: [
{ label: '成交数', key: 'dealCount', type: 'number' },
{ label: '战败数', key: 'defeatCount', type: 'number' },
{ label: '录音数', key: 'recordingCount', type: 'number' },
{ label: '录音缺失率', key: 'missingRate', type: 'percent' }
]
},
training: {
sortOptions: [
{ label: '学习时长', key: '1', type: 'duration' },
{ label: '完成课程数', key: '2', type: 'number' },
{ label: '带教次数', key: '3', type: 'number' }
],
metrics: [
{ label: '学习时长', key: 'studyHours', type: 'duration', width: '234rpx' },
{ label: '完成课程数', key: 'completedCourses', type: 'number', width: '216rpx' },
{ label: '带教次数', key: 'coachingTimes', type: 'number', width: '192rpx' }
]
}
}
const toNumber = (v) => {
if (v === null || v === undefined) return null
if (typeof v === 'number') return isNaN(v) ? null : v
const s = String(v).trim()
if (!s) return null
const n = Number(s.replace(/[,%\s]/g, ''))
return isNaN(n) ? null : n
}
const formatThousands = (v) => {
const s = String(v)
if (!s) return s
const isNeg = s[0] === '-'
const raw = isNeg ? s.slice(1) : s
const [intPart, decPart] = raw.split('.')
const intFmt = String(intPart || '0').replace(/\B(?=(\d{3})+(?!\d))/g, ',')
const res = decPart ? `${intFmt}.${decPart}` : intFmt
return isNeg ? `-${res}` : res
}
const formatNumber = (v) => {
const n = toNumber(v)
if (n === null) return '-'
const str = String(n).includes('.') ? n.toFixed(2).replace(/\.?0+$/, '') : String(n)
return formatThousands(str)
}
const formatPercent = (v) => {
const n = toNumber(v)
if (n === null) return '-'
const str = n.toFixed(2).replace(/\.?0+$/, '')
return `${formatThousands(str)}%`
}
Component({
properties: {
title: { type: String, value: '员工排行' },
titleIcon: { type: String, value: '' },
mode: { type: String, value: 'performance' },
employeeList: { type: Array, value: [] },
initialSortKey: { type: String, value: '' },
initialSortOrder: { type: String, value: 'desc' },
localSort: { type: Boolean, value: false },
scene: Number
},
data: {
sortOptions: [],
metrics: [],
sortKey: '',
sortOrder: 'desc',
keyword: '',
displayList: [],
dotMaps: {
0: 'red',
1: 'yellow',
2: 'green'
}
},
observers: {
'mode, employeeList, initialSortKey, initialSortOrder': function () {
this.sync()
}
},
lifetimes: {
attached() {
this.sync()
}
},
methods: {
input(e) {
let { value } = e.detail
this.setData({ keyword: value })
},
search() {
this.applySearch()
},
clear() {
this.setData({keyword: ''}, () => {
this.applySearch()
})
},
initLoad() {
let { mode } = this.data
this.setData({
sortKey: mode == 'behavior' ? '4' : '1',
sortOrder: 'desc'
})
},
getConfig() {
return DEFAULT_CONFIG[this.data.mode] || DEFAULT_CONFIG.performance
},
sync() {
const cfg = this.getConfig()
const sortKey = this.data.sortKey || cfg.sortOptions[0].key
const sortOrder = this.data.sortOrder || this.data.initialSortOrder || 'desc'
const metrics = cfg.metrics || []
const metricWidth = `${(100 / Math.max(1, metrics.length)).toFixed(2)}%`
const displayList = this.buildDisplayList(this.getFilteredEmployeeList(this.properties.employeeList), metrics)
this.setData(
{
sortOptions: cfg.sortOptions || [],
metrics,
sortKey,
sortOrder,
metricWidth,
displayList,
}
)
},
openHelp() {
this.setData({ helpVisible: true })
},
closeHelp() {
this.setData({ helpVisible: false })
},
onTapSort(e) {
const key = e.currentTarget?.dataset?.key
if (!key) return
let nextOrder = 'desc'
if (this.data.sortKey == key) {
nextOrder = this.data.sortOrder === 'desc' ? 'asc' : 'desc'
}
this.setData(
{
sortKey: key,
sortOrder: nextOrder
},
() => {
this.triggerEvent('sortChange', { rankfield: key, rankType: nextOrder })
}
)
},
onTapEmployee(e) {
if (this.data.scene == 0) return // 全业务不支持跳转详情
const idx = e.currentTarget?.dataset?.index
const employee = Array.isArray(this.data.displayList) ? this.data.displayList[idx] : null
this.triggerEvent('employeeTap', { employee })
},
formatMetricValue(metric, employee) {
const v = employee?.[metric.key]
if (metric.type === 'percent') return formatPercent(v)
return formatNumber(v)
},
buildDisplayList(employeeList, metrics) {
if (!Array.isArray(employeeList) || !Array.isArray(metrics)) return []
return employeeList.map((emp) => {
const metricDisplay = {}
metrics.forEach((m) => {
metricDisplay[m.key] = this.formatMetricValue(m, emp)
})
return {
...emp,
metricDisplay,
}
})
},
applySearch() {
const metrics = Array.isArray(this.data.metrics) ? this.data.metrics : this.getConfig().metrics || []
const displayList = this.buildDisplayList(this.getFilteredEmployeeList(this.properties.employeeList), metrics)
this.setData({ displayList })
},
getFilteredEmployeeList(employeeList) {
if (!Array.isArray(employeeList)) return []
const keyword = String(this.data.keyword || '').trim()
if (!keyword) return employeeList
return employeeList.filter((emp) => {
const name = String(emp?.employeeName || '').trim()
return name.includes(keyword)
})
},
}
})