batchOperation.js 10.6 KB
// packageWorkspace/pages/performanceRating/batchOperation/batchOperation.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/regional-assistant/'
import { levelList } from '~/packageWorkspace/pages/performanceRating/constant'
import api from '~/packageWorkspace/pages/performanceRating/api'
import NP from 'number-precision'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    selectedStaffList: [],
    staffList: [],
    batchConfigType: 1,
    batchInfo: {
      manualGradeCode: ''
    },
    levelList: levelList
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      pageQuery: options
    }, () => {
      // 获取员工列表
      this.getStaffList()
      // 获取当前月份的绩效配置规则
      this.getRuleConfig()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  getRuleConfig() {
    let { pageQuery } = this.data
    api.getPerformanceRule({queryMonth: pageQuery.queryMonth}).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        this.setData({
          ruleConfigInfo: data
        })
      } else {
        wx.$util.showToast(message)
      }
    })
  },

  getStaffList() {
    let { pageQuery } = this.data
    wx.$util.showLoading()
    api.getStaffListAPI({queryMonth: pageQuery.queryMonth}).then(res => {
      wx.$util.hideLoading()
      let { code, data, message } = res
      if (code == 0) {
        data?.forEach(val => {
          val.levelCode = val.adoptType == 1 ? val.autoGradeCode : val.adoptType == 2 ? val.manualGradeCode : ''
        })
        this.setData({
          staffList: data || []
        })
      } else {
        wx.$util.showToast(message)
      }
    })
  },

  // 批量选择员工
  handleSelectStaff() {
    let {
      selectedStaffList,
      staffList
    } = this.data
    this.setData({
      showSelectPopup: true,
      selectOptions: staffList,
      selectTitle: '选择员工',
      selectMultiple: true,
      selectValues: selectedStaffList?.map(val => { return val.mobilePhone }),
      selectType: 'staff'
    })
  },

  handleMaxTips() {
    wx.$util.showToast('最多支持选择20位员工')
  },

  handlePopupConfirm(e) {
    let { selectType, singleSelectIndex, batchSingleList } = this.data
    if (selectType == 'staff') {
      let { items } = e.detail
      this.setData({
        selectedStaffList: items
      }, () => {
        // 获取批量员工的绩效数据
        this.getBatchStaffPerformance()
      })
    } else {
      let { values } = e.detail
      let { ruleConfigInfo, batchInfo, batchFlag } = this.data
      const obj = ruleConfigInfo?.performanceLevelDetailDTOS?.find(val => val.levelCode == values[0])
      if (batchFlag) { // 统一设置等级
        this.setData({
          'batchInfo.manualGradeCode': values[0],
          'batchInfo.manualScoreSymbolValue': obj.scoreSymbolValue,
          'batchInfo.manualRewardCoefficient': obj.rewardCoefficient
        })
      } else {
        this.setData({
          [`batchSingleList[${singleSelectIndex}].manualGradeCode`]: values[0],
          [`batchSingleList[${singleSelectIndex}].manualScoreSymbolValue`]: obj.scoreSymbolValue,
          [`batchSingleList[${singleSelectIndex}].manualRewardCoefficient`]: obj.rewardCoefficient
        })
        // 未输入手动维护绩效金额 根据评分等级系数计算绩效工资
        if (((batchSingleList[singleSelectIndex].manualFinalPerformanceAmount ?? '') === '')) {
          const performanceSalary = NP.round(NP.times(NP.times(batchSingleList[singleSelectIndex].afterAttendanceTotalBasicSalary, 0.3), obj.rewardCoefficient), 2)
          this.setData({
            [`batchSingleList[${singleSelectIndex}].performanceSalary`]: performanceSalary,
            [`batchSingleList[${singleSelectIndex}].basicSalary`]: NP.round(NP.plus(performanceSalary, batchSingleList[singleSelectIndex].baseSalary), 2)
          })
        }
      }
    }
    this.hideSelectPopup()
  },

  hideSelectPopup() {
    this.setData({showSelectPopup: false})
  },

  getBatchStaffPerformance() {
    let { selectedStaffList, pageQuery } = this.data
    if (!selectedStaffList.length) return
    api.getBatchPerformanceInfo({queryMonth: pageQuery.queryMonth, mobilePhoneList: selectedStaffList?.map(val => { return val.mobilePhone })}).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        data?.forEach(val => {
          val.gradeCode = val.adoptType == 1 ? val.autoGradeCode : val.adoptType == 2 ? val.manualGradeCode : '',
          val.performanceSalary = !((val.manualFinalPerformanceAmount ?? '') === '') ? val.manualFinalPerformanceAmount : val.performanceSalary // 最终绩效工资
          val.rewardCoefficient = val.adoptType == 1 ? val.autoRewardCoefficient : val.adoptType == 2 ? val.manualRewardCoefficient : '' // 最终奖励系数
          val.scoreSymbolValue = val.adoptType == 1 ? val.autoScoreSymbolValue : val.adoptType == 2 ? val.manualScoreSymbolValue : '' // 最终分数范围
        })
        this.setData({
          batchSingleList: data || []
        })
      } else {
        wx.$util.showToast(message)
      }
    })
  },

  changeRadio(e) {
    let { value } = e.currentTarget.dataset
    let { selectedStaffList } = this.data
    if (value == 2 && !selectedStaffList.length) return wx.$util.showToast('请选择员工')
    this.setData({
      batchConfigType: value
    })
  },

  // 手动评分选择绩效等级
  selectLevelCode(e) {
    let { batch, index } = e.currentTarget.dataset
    let {
      batchInfo,
      levelList,
      batchSingleList
    } = this.data
    this.setData({
      showSelectPopup: true,
      selectOptions: levelList,
      selectTitle: '绩效',
      selectMultiple: false,
      selectValue: batch ? [batchInfo.manualGradeCode] : [batchSingleList[index].manualGradeCode],
      batchFlag: batch,
      selectType: 'level',
      singleSelectIndex: !batch ? index : null
    })
  },

  priceInput(e) {
    let { value } = e.detail
    let { index } = e.currentTarget.dataset
    let { batchConfigType, batchSingleList } = this.data
    let cleanValue = value.replace(/[^\d.]/g, '')
    const firstDotIndex = cleanValue.indexOf('.')
    if (firstDotIndex != -1) {
      cleanValue = cleanValue.substring(0, firstDotIndex + 1) + cleanValue.substring(firstDotIndex + 1).replace(/\./g, '')
    }
    if (cleanValue == '.') {
      cleanValue = '0.'
    }
    if (cleanValue.length > 1 && cleanValue.startsWith('0') && !cleanValue.startsWith('0.')) {
      cleanValue = cleanValue.replace(/^0+/, '')
      if (cleanValue == '') {
          cleanValue = '0'
      }
    }
    const numericValue = parseFloat(cleanValue)
    if (numericValue > 100000) {
      cleanValue = '100000'
    } else if (numericValue == 100000 && cleanValue.includes('.')) {
      cleanValue = '100000'
    }
    const dotIndexAfterCheck = cleanValue.indexOf('.');
    if (dotIndexAfterCheck != -1 && cleanValue.length > dotIndexAfterCheck + 3) {
      cleanValue = cleanValue.substring(0, dotIndexAfterCheck + 3)
    }
    value = cleanValue
    if (batchConfigType == 1) {
      this.setData({
        'batchInfo.manualFinalPerformanceAmount': value
      })
    } else {
      // 计算绩效工资,没有手动维护金额按绩效等级系数进行计算
      const computedValue = value || (batchSingleList[index]?.manualGradeCode ? NP.round(NP.times(NP.times(batchSingleList[index]?.afterAttendanceTotalBasicSalary, 0.3), batchSingleList[index].manualRewardCoefficient), 2) : 0)
      this.setData({
        [`batchSingleList[${index}].manualFinalPerformanceAmount`]: value,
        [`batchSingleList[${index}].performanceSalary`]: computedValue,
        [`batchSingleList[${index}].basicSalary`]: NP.round(NP.plus(computedValue, batchSingleList[index]?.baseSalary), 2)
      })
    }
  },

  handleHelpPopup() {
    this.setData({
      showHelpPopup: true
    })
  },

  handleConfirm() {
    const { batchConfigType, batchInfo, batchSingleList, selectedStaffList, pageQuery } = this.data
    if (!selectedStaffList.length) {
      return wx.$util.showToast('请选择员工')
    }
    const mapStaffToSubmitItem = (staff, source) => ({
      mobilePhone: staff.mobilePhone,
      evaluationMonth: pageQuery.queryMonth,
      manualGradeCode: source.manualGradeCode,
      manualRewardCoefficient: source.manualRewardCoefficient,
      manualScoreSymbolValue: source.manualScoreSymbolValue,
      manualFinalPerformanceAmount: source.manualFinalPerformanceAmount,
      adoptType: 2,
      amountCalculationType: !((source.manualFinalPerformanceAmount ?? '') === '') ? 2 : 1
    })
    let perfAppraisalSubmitReqDTO = []
    if (batchConfigType == 1) {
      // 批量统一设置
      if (!batchInfo.manualGradeCode && !batchInfo.manualFinalPerformanceAmount) {
        return wx.$util.showToast('请配置绩效评分')
      }
      perfAppraisalSubmitReqDTO = selectedStaffList.map(staff =>
        mapStaffToSubmitItem(staff, batchInfo)
      )
    } else {
      for (const staff of batchSingleList) {
        const hasGrade = (staff.manualGradeCode ?? '') !== '';
        const hasAmount = (staff.manualFinalPerformanceAmount ?? '') !== ''
        if (!hasGrade && !hasAmount) {
          wx.$util.showToast(`请配置【${staff.name}】的绩效评分`)
        }
      }
      perfAppraisalSubmitReqDTO = batchSingleList.map(staff =>
        mapStaffToSubmitItem(staff, staff)
      )
    }
    if (this.data.confirmFlag) return
    this.setData({ confirmFlag: true })
    wx.$util.showLoading()
    const params = {
      evaluationMonth: pageQuery.queryMonth,
      perfAppraisalSubmitReqDTO
    }
    api.batchSubmit(params)
      .then(res => {
        wx.$util.hideLoading();
        this.setData({ confirmFlag: false })
        const { code, message } = res
        if (code == 0) {
          wx.navigateBack()
        } else {
          wx.showModal({
            title: '提示',
            content: message,
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#222222'
          })
        }
      })
      .catch(() => {
        wx.$util.hideLoading()
        this.setData({ confirmFlag: false })
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})