Ver código fonte

修正调班排班过程中产生多余逗号

(cherry picked from commit 08b98e6014296a21c1c90fcf0b3a15eac8df5dc1)
zhonghuizhen 1 ano atrás
pai
commit
d336e3100c

+ 12 - 6
src/views/business/​scheduleManage/adjust.vue

@@ -162,12 +162,18 @@ export default {
          */
         getPartysList (poList) {
             const self = this
-            const result = poList.map(item => ({
-                value: item.party,
-                status: item.status,
-                label: (self.userOption.filter(o => o.value === item.party))[0].label,
-                type: 'success'
-            }))
+            const result = poList.reduce((acc, currentItem) => {
+                const existing = acc.find(item => item.value === currentItem.party)
+                if (!existing) {
+                    acc.push({
+                        value: currentItem.party,
+                        status: currentItem.status,
+                        label: (self.userOption.filter(o => o.value === currentItem.party))[0].label,
+                        type: 'success'
+                    })
+                }
+                return acc
+            }, [])
             return result
         },
         /**

+ 75 - 51
src/views/business/​scheduleManage/adjustVerify.vue

@@ -242,12 +242,18 @@ export default {
          */
         getPartysList (poList) {
             const self = this
-            const result = poList.map(item => ({
-                value: item.party,
-                status: item.status,
-                label: (self.userOption.filter(o => o.value === item.party))[0].label,
-                type: 'success'
-            }))
+            const result = poList.reduce((acc, currentItem) => {
+                const existing = acc.find(item => item.value === currentItem.party)
+                if (!existing) {
+                    acc.push({
+                        value: currentItem.party,
+                        status: currentItem.status,
+                        label: (self.userOption.filter(o => o.value === currentItem.party))[0].label,
+                        type: 'success'
+                    })
+                }
+                return acc
+            }, [])
             return result
         },
         /**
@@ -593,65 +599,47 @@ export default {
         },
         /**
          * 处理已通过的申请单,修改排班数据
-         */
+         * */
         async handleAccess (data) {
             if (data.adjustmentDetailPoList.length > 0) {
                 const scheduleId = data.scheduleId
                 const response = await getStaffSchedule({ id: scheduleId })
                 const submitData = response.data
                 const { staffScheduleDetailPoList, startDate } = response.data
+
+                // 提取根据日期计算索引的逻辑到独立函数
+                const getIndexByDate = (date) => this.getDays(startDate, date)
+
                 data.adjustmentDetailPoList.forEach(async (el) => {
                     const userId = el.createBy
-                    const userResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === userId) // 获取调班人的排班详情
+                    const userResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === userId)
+
                     if (data.type !== 'paiban') {
                         const partyId = el.party
-                        const partyResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === partyId) // 获取目标人的排班详情
-                        // 修改调班人排班数据
-                        const index = this.getDays(startDate, el.afterDate) // 计算得出是d几天
-                        staffScheduleDetailPoList[userResIndex][`d${index + 1}`] = staffScheduleDetailPoList[userResIndex][`d${index + 1}`].concat(',' + el.afterAdjust) // 申请人目标日期改变
-                        const partyIndex = this.getDays(startDate, el.beforeDate) // 计算得出是d几天
-                        const beforeAdjustList = el.beforeAdjust.split(',')
-                        beforeAdjustList.forEach((item, i) => {
-                            if (i === (beforeAdjustList.length - 1)) {
-                                staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item, '') // 申请人申请日期除去原来的班次
-                            } else { // 当不在最后一项时,把附带的逗号也除去
-                                staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item + ',', '') // 申请人申请日期除去原来的班次和逗号
-                            }
-                        })
-                        // 修改目标人排班数据
-                        staffScheduleDetailPoList[partyResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[partyResIndex][`d${partyIndex + 1}`].concat(',' + el.beforeAdjust)// 调班人申请日期改变
-                        const afterAdjustList = el.afterAdjust.split(',')
-                        afterAdjustList.forEach((item, i) => {
-                            if (i === (afterAdjustList.length - 1)) {
-                                staffScheduleDetailPoList[partyResIndex][`d${index + 1}`] = staffScheduleDetailPoList[partyResIndex][`d${index + 1}`].replace(item, '')// 调班人目标日期除去原来班次
-                            } else {
-                                staffScheduleDetailPoList[partyResIndex][`d${index + 1}`] = staffScheduleDetailPoList[partyResIndex][`d${index + 1}`].replace(item + ',', '')// 调班人目标日期除去原来班次和逗号
-                            }
-                        })
+                        const partyResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === partyId)
+
+                        // 计算调班人和目标人相关日期的索引
+                        const index = getIndexByDate(el.afterDate)
+                        const partyIndex = getIndexByDate(el.beforeDate)
+
+                        // 处理调班人排班数据
+                        this.removeScheduleDataWithRegex(userResIndex, partyIndex, el.beforeAdjust, staffScheduleDetailPoList)
+                        this.updateScheduleDataWithRegex(userResIndex, index, el.afterAdjust, staffScheduleDetailPoList)
+
+                        // 处理目标人排班数据
+                        this.removeScheduleDataWithRegex(partyResIndex, index, el.afterAdjust, staffScheduleDetailPoList)
+                        this.updateScheduleDataWithRegex(partyResIndex, partyIndex, el.beforeAdjust, staffScheduleDetailPoList)
                     } else {
-                        // 排版变更
-                        const index = this.getDays(startDate, el.afterDate) // 计算得出是d几天
-                        const partyIndex = this.getDays(startDate, el.beforeDate) // 计算得出是d几天
-                        const afterAdjustArr = el.afterAdjust.split(',')
-                        afterAdjustArr.forEach((item) => {
-                            if (!staffScheduleDetailPoList[userResIndex][`d${index + 1}`].includes(item)) { // 排班原本不包含的班次才加进去 避免重复
-                                if (staffScheduleDetailPoList[userResIndex][`d${index + 1}`] !== '') {
-                                    staffScheduleDetailPoList[userResIndex][`d${index + 1}`] = staffScheduleDetailPoList[userResIndex][`d${index + 1}`].concat(',' + item) // 申请人目标日期改变
-                                } else {
-                                    staffScheduleDetailPoList[userResIndex][`d${index + 1}`] = item
-                                }
-                            }
-                        })
+                        // 排班变更情况
+                        const index = getIndexByDate(el.afterDate)
+                        const partyIndex = getIndexByDate(el.beforeDate)
                         const beforeAdjustList = el.beforeAdjust.split(',')
-                        beforeAdjustList.forEach((item, i) => {
-                            if (i === (beforeAdjustList.length - 1)) {
-                                staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item, '') // 申请人申请日期除去原来的班次
-                            } else { // 当不在最后一项时,把附带的逗号也除去
-                                staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item + ',', '') // 申请人申请日期除去原来的班次和逗号
-                            }
-                        })
+                        this.removeScheduleDataForPaibanWithRegex(userResIndex, partyIndex, beforeAdjustList, staffScheduleDetailPoList)
+                        const afterAdjustArr = el.afterAdjust.split(',')
+                        this.updateScheduleDataForPaibanWithRegex(userResIndex, index, afterAdjustArr, staffScheduleDetailPoList)
                     }
                 })
+
                 // 保存修改后的排班
                 submitData.staffScheduleDetailPoList = staffScheduleDetailPoList
                 saveStaffSchedule(submitData).then(() => {
@@ -661,6 +649,42 @@ export default {
                 })
             }
         },
+        // 使用正则表达式处理更新排班数据(添加班次),确保无多余逗号
+        updateScheduleDataWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const currentData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const cleanedData = currentData.replace(/,$/, '') // 去除末尾可能存在的逗号
+            const newData = [cleanedData, adjustData].filter(Boolean).join(',') // 拼接数据,中间用逗号隔开,去除空字符串
+            scheduleList[index][`d${targetIndex + 1}`] = newData
+        },
+
+        // 使用正则表达式处理移除排班数据(去除原有班次),确保无多余逗号
+        removeScheduleDataWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const targetData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const dataList = adjustData.split(',')
+            const updatedData = dataList.reduce((acc, item) => {
+                const pattern = new RegExp(`,?${item}(,|$)`, 'g') // 匹配逗号+要移除的项或者要移除的项+结尾逗号或结尾(全局匹配)
+                return acc.replace(pattern, '')
+            }, targetData)
+            scheduleList[index][`d${targetIndex + 1}`] = updatedData.replace(/,$/, '') // 去除末尾可能出现的多余逗号
+        },
+
+        // 用于排班变更时使用正则表达式更新排班数据的函数(避免重复添加班次),确保无多余逗号
+        updateScheduleDataForPaibanWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const currentData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const dataToAdd = adjustData.filter(item => !currentData.includes(item))
+            const newData = [currentData, dataToAdd.join(',')].filter(Boolean).join(',')
+            scheduleList[index][`d${targetIndex + 1}`] = newData.replace(/,$/, '')
+        },
+
+        // 用于排班变更时使用正则表达式移除排班数据的函数,确保无多余逗号
+        removeScheduleDataForPaibanWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const targetData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const updatedData = adjustData.reduce((acc, item) => {
+                const pattern = new RegExp(`,?${item}(,|$)`, 'g') // 匹配逗号+要移除的项或者要移除的项+结尾逗号或结尾(全局匹配)
+                return acc.replace(pattern, '')
+            }, targetData)
+            scheduleList[index][`d${targetIndex + 1}`] = updatedData.replace(/,$/, '') // 去除末尾可能出现的多余逗号
+        },
         getDays (start, end) { // 根据日期获取排序 d1、d2、d3...
             if (!start || !end) {
                 return 0

+ 27 - 28
src/views/business/​scheduleManage/components/adjust-edit.vue

@@ -982,41 +982,40 @@ export default {
         },
         /**
          * 处理已通过的申请单,修改排班数据
-         */
+         * */
         async handleAccess (self) {
             const response = await getStaffSchedule({ id: self.formData.scheduleId })
             const submitData = response.data
             const { staffScheduleDetailPoList, startDate } = response.data
+            const getIndexByDate = (date) => this.getDays(startDate, date)
             self.formData.adjustList.forEach(async (el) => {
                 const userId = this.$store.getters.userId || ''
-                const userResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === userId) // 获取调班人的排班详情
-                // 排版变更
-                const index = this.getDays(startDate, el.afterDate) // 计算得出是d几天
-                const partyIndex = this.getDays(startDate, el.beforeDate) // 计算得出是d几天
-                el.afterAdjust.forEach((item) => {
-                    if (!staffScheduleDetailPoList[userResIndex][`d${index + 1}`].includes(item)) { // 排班原本不包含的班次才加进去 避免重复
-                        if (staffScheduleDetailPoList[userResIndex][`d${index + 1}`] !== '') {
-                            staffScheduleDetailPoList[userResIndex][`d${index + 1}`] = staffScheduleDetailPoList[userResIndex][`d${index + 1}`].concat(',' + item) // 申请人目标日期改变
-                        } else {
-                            staffScheduleDetailPoList[userResIndex][`d${index + 1}`] = item
-                        }
-                    }
-                })
-                el.beforeAdjust.forEach((item, i) => {
-                    if (i === (el.beforeAdjust.length - 1)) {
-                        staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item, '') // 申请人申请日期除去原来的班次
-                    } else { // 当不在最后一项时,把附带的逗号也除去
-                        staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`] = staffScheduleDetailPoList[userResIndex][`d${partyIndex + 1}`].replace(item + ',', '') // 申请人申请日期除去原来的班次和逗号
-                    }
-                })
-            })
-            // 保存修改后的排班
-            // submitData.staffScheduleDetailPoList = staffScheduleDetailPoList
-            saveStaffSchedule(submitData).then(() => {
-                console.log('排班已更新')
-            }).catch((err) => {
-                console.log(err)
+                const userResIndex = staffScheduleDetailPoList.findIndex(item => item.userId === userId)
+                const index = getIndexByDate(el.afterDate)
+                const partyIndex = getIndexByDate(el.beforeDate)
+                this.removeScheduleDataForPaibanWithRegex(userResIndex, partyIndex, el.beforeAdjust, staffScheduleDetailPoList)
+                this.updateScheduleDataForPaibanWithRegex(userResIndex, index, el.afterAdjust, staffScheduleDetailPoList)
             })
+            submitData.staffScheduleDetailPoList = staffScheduleDetailPoList
+            saveStaffSchedule(submitData).then(() => console.log('排班已更新')).catch(err => console.log(err))
+        },
+
+        // 用于排班变更时使用正则表达式更新排班数据的函数(避免重复添加班次),确保无多余逗号
+        updateScheduleDataForPaibanWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const currentData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const dataToAdd = adjustData.filter(item => !currentData.includes(item))
+            const newData = [currentData, dataToAdd.join(',')].filter(Boolean).join(',')
+            scheduleList[index][`d${targetIndex + 1}`] = newData.replace(/,$/, '')
+        },
+
+        // 用于排班变更时使用正则表达式移除排班数据的函数,确保无多余逗号
+        removeScheduleDataForPaibanWithRegex (index, targetIndex, adjustData, scheduleList) {
+            const targetData = scheduleList[index][`d${targetIndex + 1}`] || ''
+            const updatedData = adjustData.reduce((acc, item) => {
+                const pattern = new RegExp(`,?${item}(,|$)`, 'g') // 匹配逗号+要移除的项或者要移除的项+结尾逗号或结尾(全局匹配)
+                return acc.replace(pattern, '')
+            }, targetData)
+            scheduleList[index][`d${targetIndex + 1}`] = updatedData.replace(/,$/, '') // 去除末尾可能出现的多余逗号
         },
         handleAddParam () {
             // 把前面填写的行加入班次日期锁定