technicianSelect.helpers.js 4.05 KB
/**
 * @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 }
}