constantData.js 3.13 KB
/**
 * 工资申诉常量数据映射
 * 将API返回的数据映射到 salaryItems 数据结构
 */

// 工资项基础配置
const SALARY_ITEM_CONFIG = [
  {
    id: 'attendance',
    title: '实际出勤天数',
    centerLeft: '根据打卡数据产生',
    btnText: '申诉考勤',
    unit: '天',
    defaultValue: 0
  },
  {
    id: 'daily',
    title: '日报完成天数',
    centerLeft: '填写驻店日报天数',
    btnText: '去补日报',
    unit: '天',
    defaultValue: 0,
    hasMissingDates: true
  },
  {
    id: 'recording',
    title: '平板录音总数',
    centerLeft: '触客总数为',
    btnText: '申诉平板',
    unit: '条',
    defaultValue: 0
  }
]

// API字段映射配置
const API_FIELD_MAP = {
  attendance: {
    value: 'actualAttendanceDays',
    deduction: 'attendanceDeductionAmount',
    appealable: 'attendanceAppealable'
  },
  daily: {
    value: 'dailyReportDays',
    deduction: 'dailyReportDeductionAmount',
    appealable: 'dailyReportable',
    missingDates: 'missingDailyReportDates'
  },
  recording: {
    value: 'padRecordNum',
    deduction: 'padDeductionAmount',
    appealable: 'padAppealable'
  }
}

/**
 * 格式化扣款金额显示
 */
const formatDeductionAmount = (amount) => {
  if (!amount || amount === 0) return ''
  return amount > 0 ? `-${amount}元` : `${amount}元`
}

/**
 * 格式化缺失日期显示
 */
const formatMissingDates = (dates) => {
  if (!dates || !Array.isArray(dates) || dates.length === 0) return ''
  return dates.map(date => {
    if (date.includes('-')) {
      const day = date.split('-')[2]
      return day.replace(/^0+/, '')
    }
    return date
  }).join('、') + '号'
}

/**
 * 格式化数值显示
 */
const formatValue = (value, unit, defaultValue = 0) => {
  const num = value !== undefined && value !== null ? value : defaultValue
  return `${num}${unit}`
}

/**
 * 创建单个工资项数据
 */
const createSalaryItem = (config, apiData) => {
  const fieldMap = API_FIELD_MAP[config.id]
  const value = apiData[fieldMap.value]
  const deductionAmount = apiData[fieldMap.deduction]
  const appealable = apiData[fieldMap.appealable] || false
  const missingDates = config.hasMissingDates ? apiData[fieldMap.missingDates] : null

  return {
    id: config.id,
    title: config.title,
    value: formatValue(value, config.unit, config.defaultValue),
    centerLeft: config.centerLeft,
    centerLeftNum: '',
    centerRight: deductionAmount ? '扣款' : '',
    centerRightPrice: formatDeductionAmount(deductionAmount),
    bottomText: missingDates && missingDates.length > 0 ? '缺失天数为' : '',
    bottomDays: config.hasMissingDates ? formatMissingDates(missingDates) : '',
    btnText: appealable ? config.btnText : '',
    btnType: config.id,
    appealable
  }
}

/**
 * 将API数据映射到工资项数据结构
 * @param {Object} apiData - API返回的数据对象
 * @returns {Array} salaryItems 数组
 */
export function mapApiDataToSalaryItems(apiData = {}) {
  return SALARY_ITEM_CONFIG.map(config => createSalaryItem(config, apiData))
}

/**
 * 默认的工资项数据结构(用于初始化或兜底)
 */
export const defaultSalaryItems = mapApiDataToSalaryItems({})