technicianSelect.helpers.js
4.05 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
/**
* @file 技师选择弹层纯逻辑 — idKey 分支、列表映射、confirm 载荷
* 供 Artificer/Technician 合并后单测;组件内 setData 前调用。
*/
/**
* 从选中对象取 identity key
* @param {object} item
* @param {'technicianId'|'uuid'} idKey
* @returns {string|number|undefined}
*/
export function getItemKey(item, idKey) {
if (!item) return undefined
if (idKey === 'uuid') return item.uuid
return item.technicianId || item.id
}
/**
* 从 artificerSelectObj 构建选中 id 数组
* @param {object|object[]} artificerSelectObj
* @param {'technicianId'|'uuid'} idKey
* @returns {Array<string|number>}
*/
export function buildSelectArr(artificerSelectObj, idKey) {
if (Array.isArray(artificerSelectObj)) {
return artificerSelectObj.map(val => getItemKey(val, idKey)).filter(k => k != null)
}
const key = getItemKey(artificerSelectObj, idKey)
return key != null ? [key] : []
}
/**
* 选中项 shareRatio 映射
* @param {object[]} selectedList
* @param {'technicianId'|'uuid'} idKey
* @returns {Record<string|number, *>}
*/
export function buildRatioMap(selectedList, idKey) {
return selectedList.reduce((m, v) => {
const k = getItemKey(v, idKey)
if (k != null) m[k] = v.shareRatio
return m
}, {})
}
/**
* 打开弹层时映射技师列表 active/disabled/shareRatio
* @param {object[]} copyArtificerList
* @param {object} options
* @returns {object[]}
*/
export function mapArtificerList(copyArtificerList, options) {
const {
artificerSelectObj,
selectArr,
ratioMap,
idKey,
disabledSelectObj,
deputyFlag
} = options
const selectedList = Array.isArray(artificerSelectObj)
? artificerSelectObj
: artificerSelectObj
? [artificerSelectObj]
: []
let list = (copyArtificerList || []).map(i => {
let item = { ...i }
if (idKey === 'uuid' && selectedList.length) {
const matched = selectedList.find(v => v.uuid == i.uuid)
if (matched?.id !== undefined) item.id = matched.id
}
const itemKey = idKey === 'uuid' ? i.uuid : i.id
const active = selectArr?.includes(itemKey)
const disabled =
disabledSelectObj?.findIndex(val => val.uuid == i.uuid) > -1
const shareRatio = active ? ratioMap[itemKey] ?? '' : ''
return { ...item, active, disabled, shareRatio }
})
if (selectArr.length === 2 && deputyFlag) {
list = list.map(i => {
const itemKey = idKey === 'uuid' ? i.uuid : i.id
if (!selectArr.includes(itemKey)) return { ...i, disabled: true }
return i
})
}
return list
}
/**
* 计算主技师剩余分成比例
* @param {object[]} list
* @returns {number}
*/
export function calcShowMainShareRatio(list) {
const sum = (list || [])
.filter(i => i.active)
.map(i => parseInt(i.shareRatio || 0, 10))
.reduce((a, b) => a + (isNaN(b) ? 0 : b), 0)
let showMainShareRatio = 100 - sum
if (showMainShareRatio < 0) showMainShareRatio = 0
if (showMainShareRatio > 100) showMainShareRatio = 100
return showMainShareRatio
}
/**
* 确认时构建 payload;空选时 deputy 模式允许清空
* @param {object} params
* @returns {{ ok: boolean, reason?: string, payload?: object }}
*/
export function buildConfirmPayload({ artificerSelectObj, activeArr, idKey, deputyFlag }) {
if (!activeArr?.length) {
if (deputyFlag) {
return {
ok: true,
payload: {
...artificerSelectObj,
technicianUuid: '',
technicianName: '',
selectedTechnicians: []
}
}
}
return { ok: false, reason: 'empty' }
}
if (deputyFlag) {
const invalid = activeArr.some(
v =>
v.shareRatio === '' ||
v.shareRatio === undefined ||
v.shareRatio === null
)
if (invalid) return { ok: false, reason: 'invalid_ratio' }
}
const payload = {
...artificerSelectObj,
technicianUuid: activeArr[0]?.uuid || '',
technicianName: activeArr[0]?.staffName || '',
selectedTechnicians: activeArr
}
if (idKey === 'technicianId') {
payload.technicianId = activeArr[0]?.id || ''
}
return { ok: true, payload }
}