From da3c9c404134948d4c0d760945f9aa419caf405e Mon Sep 17 00:00:00 2001 From: cxx Date: Wed, 15 May 2024 17:21:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E7=A6=81=E7=94=A8=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/task.js | 398 ++++++++++++++++++++++++------ app/view/page/taskManagement.html | 8 +- 2 files changed, 328 insertions(+), 78 deletions(-) diff --git a/app/service/task.js b/app/service/task.js index 3c42c47..5d38209 100644 --- a/app/service/task.js +++ b/app/service/task.js @@ -71,10 +71,12 @@ class TaskService extends Service { async submitNode(taskData, type, times, action) { const { jianghuKnex } = this.app; const { updateTaskData, historyInsertData, noticeInsertData } = await this.buildNext1({ taskData: taskData, type: type, times: times, action: action }) + const userList = jianghuKnex('enterprise_user').select() console.log('updateTaskData------', updateTaskData); console.log('historyInsertData------', historyInsertData); console.log('noticeInsertData------', noticeInsertData); + await jianghuKnex.transaction(async trx => { if (updateTaskData) await trx("task", this.ctx).where({ taskId: taskData.taskId }).jhUpdate(updateTaskData); if (noticeInsertData.length > 0) await trx("notice_push", this.ctx).jhInsert(noticeInsertData); @@ -87,9 +89,16 @@ class TaskService extends Service { const { userId, username } = this.ctx.userInfo; const { taskId, taskInfo, taskWorkflow, taskConfigId, taskEditUserList, taskEditedUserList } = taskData; - const taskHistory = await jianghuKnex('task_history').where({ taskId }).where('taskConfigId', 'like', 'approvalTask-%').select(); + const taskHistory = await jianghuKnex('task_history') + .where({ taskId }) + .where('taskConfigId', 'like', 'approvalTask-%') + .whereNot('taskHandleDesc', '=', '系统自动转交') + .select(); + const userList = await jianghuKnex('enterprise_user').select(); - var updateTaskData = null, + console.log('taskHistory+++_', taskHistory); + + var updateTaskData = {}, historyInsertData = [], noticeInsertData = [], curNode = taskWorkflow.nodeList.find(node => node.id == taskConfigId), @@ -97,7 +106,7 @@ class TaskService extends Service { taskStatus = "running", taskHistoryUser = [...taskHistory.map(h => h.createHistoryBy)]; - if (action == 'update') taskHistoryUser.push(userId) + if (action == 'updateTask') taskHistoryUser.push(userId) // 插入上一次节点历史 if (!taskData?.createHistoryBy) taskData.createHistoryBy = [userId] taskStatus = curNodeType == 'end' ? 'end' : 'running' @@ -131,10 +140,11 @@ class TaskService extends Service { switch (curNodeType) { case 'approvalTask': var curApprovalType = curNode?.properties.approvalType, - autoApprovel = taskInfo.autoApprovel, editUserList = taskEditUserList && taskEditUserList.split(',') || [], editedUserList = taskEditedUserList && taskEditedUserList.split(',') || []; + const { autoApprovel, abnormalType, abnormalUser } = taskInfo + if (times == 0) { editUserList = editUserList.filter(u => u != userId) editedUserList.splice(-1, 0, userId) @@ -142,6 +152,79 @@ class TaskService extends Service { } if (['会签'].includes(curApprovalType)) { + // 检查是否需要异常处理 + // 先检查即将审批的人是否存在账号禁用情况 + // 1、abnormalType == -1 自动通过 + // 2、abnormalType == 1 替换成abnormalUser的人 再检查是否被禁用,否则就自动通过 + + // 检查是否需要替换人 abnormalType, abnormalUser + var bannedUser = [] + editUserList.forEach(editUser => { + var isBanned = userList.find(u => u.userId == editUser) == undefined ? true : false + if (isBanned) bannedUser.push(editUser) + + return isBanned + }) + if (bannedUser.length > 0) { + // 自动转交给指定人 + if (abnormalType == 1 && userList.find(u => u.userId == abnormalUser) !== undefined) { + // 从后往前遍历,以避免索引变化的影响 + for (let i = editUserList.length - 1; i >= 0; i--) { + if (bannedUser.includes(editUserList[i])) { + // 在当前索引之后插入 0 + editUserList.splice(i + 1, 0, abnormalUser); + } + } + var abnormalUserName = userList.find(u => u.userId == abnormalUser)?.username || abnormalUser + // 插入历史记录 + taskData = { + ...taskData, + taskComment: "账号禁用,系统自动转交" + abnormalUserName, + taskExplain: "系统自动转交", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: bannedUser, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + // 修改审批流 + var newUserIdList = curNode.properties.userIdList + + // 从后往前遍历,以避免索引变化的影响 + for (let i = newUserIdList.length - 1; i >= 0; i--) { + if (bannedUser.includes(newUserIdList[i])) { + // 在当前索引之后插入 0 + newUserIdList.splice(i + 1, 0, abnormalUser); + } + } + const foundNode = taskWorkflow.nodeList.find(node => node.id == curNode.id); + if (foundNode) { + foundNode.properties.userIdList = newUserIdList; + } + updateTaskData.taskWorkflow = JSON.stringify(_.cloneDeep(taskWorkflow)) + } else { + // 自动同意 + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + taskData = { + ...taskData, + taskComment: "账号禁用,自动同意", + taskExplain: "系统自动转交", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: bannedUser, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + } + } + // 把可以自动审批的人给审批了 if (autoApprovel == 1 && times == 1) { var autoApprovelUser = _.intersection(taskHistoryUser, editUserList) @@ -183,15 +266,15 @@ class TaskService extends Service { } const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData + _.assign(updateTaskData, result.updateTaskData) historyInsertData.push(...result.historyInsertData) noticeInsertData.push(...result.noticeInsertData) } else { - updateTaskData = { + _.assign(updateTaskData, { taskConfigId: curNode.id, - taskEditUserList: editUserList.join(','), - taskEditedUserList: editedUserList.join(','), - } + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + }) } } else if (['或签'].includes(curApprovalType)) { if (times == 0) { @@ -215,63 +298,162 @@ class TaskService extends Service { } const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData + _.assign(updateTaskData, result.updateTaskData) historyInsertData.push(...result.historyInsertData) noticeInsertData.push(...result.noticeInsertData) } else { - // 自动流转,识别是否需要自动审批 - if (autoApprovel == 1) { - var autoApprovelUser = _.intersection(taskHistoryUser, editUserList) + var bannedUser = [] + editUserList.forEach(editUser => { + var isBanned = userList.find(u => u.userId == editUser) == undefined ? true : false + if (isBanned) bannedUser.push(editUser) - if (autoApprovelUser.length > 0) { + return isBanned + }) + + if (bannedUser.length > 0) { + // 自动转交给指定人 + if (abnormalType == 1 && userList.find(u => u.userId == abnormalUser) !== undefined) { + // 从后往前遍历,以避免索引变化的影响 + for (let i = editUserList.length - 1; i >= 0; i--) { + if (bannedUser.includes(editUserList[i])) { + // 在当前索引之后插入 0 + editUserList.splice(i + 1, 0, abnormalUser); + } + } + + // 插入历史记录 + var abnormalUserName = userList.find(u => u.userId == abnormalUser)?.username || abnormalUser taskData = { ...taskData, - taskComment: null, - taskExplain: "自动审批", + taskComment: "账号禁用,系统自动转交" + abnormalUserName, + taskExplain: "系统自动转交", taskHandleDesc: "同意", taskLineTypeList: "同意", - createHistoryBy: [autoApprovelUser[0]], + createHistoryBy: bannedUser, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + // 修改审批流 + var newUserIdList = curNode.properties.userIdList + + // 从后往前遍历,以避免索引变化的影响 + for (let i = newUserIdList.length - 1; i >= 0; i--) { + if (bannedUser.includes(newUserIdList[i])) { + // 在当前索引之后插入 0 + newUserIdList.splice(i + 1, 0, abnormalUser); + } + } + const foundNode = taskWorkflow.nodeList.find(node => node.id == curNode.id); + if (foundNode) { + foundNode.properties.userIdList = newUserIdList; + } + updateTaskData.taskWorkflow = JSON.stringify(_.cloneDeep(taskWorkflow)) + } else { + // 自动同意 + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + taskData = { + ...taskData, + taskComment: "账号禁用,自动同意", + taskExplain: "系统自动转交", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: bannedUser, operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") } historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) - - var nextNode = await this.getNextNode(taskWorkflow, curNode) - - editUserList = nextNode.properties.userIdList; - editedUserList = null; - - taskData = { - ...taskData, - taskConfigId: nextNode.id, - taskEditUserList: editUserList?.join(','), - taskEditedUserList: editedUserList?.join(','), - taskComment: null, - taskExplain: null, - taskHandleDesc: null, - taskLineTypeList: null, - createHistoryBy: null, - operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") - } - - const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData - historyInsertData.push(...result.historyInsertData) - noticeInsertData.push(...result.noticeInsertData) - } else { - updateTaskData = { - taskConfigId: curNode.id, - taskEditUserList: editUserList.join(','), - taskEditedUserList: editedUserList.join(','), - } - } - } else { - updateTaskData = { - taskConfigId: curNode.id, - taskEditUserList: editUserList.join(','), - taskEditedUserList: editedUserList.join(','), } } + + + if (abnormalType == -1 && editedUserList.length > 0 + || abnormalType == 1 && editedUserList.filter(editUser => !bannedUser.includes(editUser)).length > 0 + || editUserList.length == 0) { + var nextNode = await this.getNextNode(taskWorkflow, curNode) + + editUserList = nextNode.properties.userIdList; + editedUserList = null; + + taskData = { + ...taskData, + taskConfigId: nextNode.id, + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + taskComment: null, + taskExplain: null, + taskHandleDesc: null, + taskLineTypeList: null, + createHistoryBy: null, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) + _.assign(updateTaskData, result.updateTaskData) + historyInsertData.push(...result.historyInsertData) + noticeInsertData.push(...result.noticeInsertData) + + } else { + // 自动流转,识别是否需要自动审批 + if (editUserList.length > 0 && autoApprovel == 1) { + var autoApprovelUser = _.intersection(taskHistoryUser, editUserList) + + if (autoApprovelUser.length > 0) { + taskData = { + ...taskData, + taskComment: null, + taskExplain: "自动审批", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: [autoApprovelUser[0]], + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + + var nextNode = await this.getNextNode(taskWorkflow, curNode) + + editUserList = nextNode.properties.userIdList; + editedUserList = null; + + taskData = { + ...taskData, + taskConfigId: nextNode.id, + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + taskComment: null, + taskExplain: null, + taskHandleDesc: null, + taskLineTypeList: null, + createHistoryBy: null, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) + _.assign(updateTaskData, result.updateTaskData) + historyInsertData.push(...result.historyInsertData) + noticeInsertData.push(...result.noticeInsertData) + } else { + _.assign(updateTaskData, { + taskConfigId: curNode.id, + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + }) + } + } else { + _.assign(updateTaskData, { + taskConfigId: curNode.id, + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + }) + } + } + } } else if (['依次审批'].includes(curApprovalType)) { @@ -296,11 +478,80 @@ class TaskService extends Service { } const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData + _.assign(updateTaskData, result.updateTaskData) historyInsertData.push(...result.historyInsertData) noticeInsertData.push(...result.noticeInsertData) } else { - // 判断下一个是否可以自动审批 + var bannedUser = [] + editUserList.forEach(editUser => { + var isBanned = userList.find(u => u.userId == editUser) == undefined ? true : false + if (isBanned) bannedUser.push(editUser) + + return isBanned + }) + + if (bannedUser.length > 0) { + // 自动转交给指定人 + if (abnormalType == 1 && userList.find(u => u.userId == abnormalUser) !== undefined) { + // 从后往前遍历,以避免索引变化的影响 + for (let i = editUserList.length - 1; i >= 0; i--) { + if (bannedUser.includes(editUserList[i])) { + // 在当前索引之后插入 0 + editUserList.splice(i + 1, 0, abnormalUser); + } + } + + // 插入历史记录 + var abnormalUserName = userList.find(u => u.userId == abnormalUser)?.username || abnormalUser + taskData = { + ...taskData, + taskComment: "账号禁用,系统自动转交" + abnormalUserName, + taskExplain: "系统自动转交", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: bannedUser, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + // 修改审批流 + var newUserIdList = curNode.properties.userIdList + + // 从后往前遍历,以避免索引变化的影响 + for (let i = newUserIdList.length - 1; i >= 0; i--) { + if (bannedUser.includes(newUserIdList[i])) { + // 在当前索引之后插入 0 + newUserIdList.splice(i + 1, 0, abnormalUser); + } + } + const foundNode = taskWorkflow.nodeList.find(node => node.id == curNode.id); + if (foundNode) { + foundNode.properties.userIdList = newUserIdList; + } + updateTaskData.taskWorkflow = JSON.stringify(_.cloneDeep(taskWorkflow)) + } else { + // 自动同意 + editUserList = editUserList.filter(editUser => !bannedUser.includes(editUser)) + editedUserList = editedUserList ? editedUserList.concat(bannedUser) : bannedUser + + taskData = { + ...taskData, + taskComment: "账号禁用,自动同意", + taskExplain: "系统自动转交", + taskHandleDesc: "同意", + taskLineTypeList: "同意", + createHistoryBy: bannedUser, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type })) + } + } + + // 判断下一个是否可以自动审批 或是 禁用 if (autoApprovel == 1) { while (editUserList.length > 0 && taskHistoryUser.includes(editUserList[0])) { // 插入历史 @@ -340,28 +591,27 @@ class TaskService extends Service { } const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData + _.assign(updateTaskData, result.updateTaskData) historyInsertData.push(...result.historyInsertData) noticeInsertData.push(...result.noticeInsertData) } else { - updateTaskData = { + _.assign(updateTaskData, { taskConfigId: curNode.id, - taskEditUserList: editUserList.join(','), - taskEditedUserList: editedUserList.join(','), - } + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + }) } } else { - updateTaskData = { + _.assign(updateTaskData, { taskConfigId: curNode.id, - taskEditUserList: editUserList.join(','), - taskEditedUserList: editedUserList.join(','), - } + taskEditUserList: editUserList?.join(','), + taskEditedUserList: editedUserList?.join(','), + }) } } } - break; case 'noticeTask': noticeInsertData.push({ @@ -389,20 +639,19 @@ class TaskService extends Service { } const result = await this.buildNext1({ taskData, type: '同意', times: 1, action }) - updateTaskData = result.updateTaskData + _.assign(updateTaskData, result.updateTaskData) historyInsertData.push(...result.historyInsertData) noticeInsertData.push(...result.noticeInsertData) - break; case 'end': - updateTaskData = { + _.assign(updateTaskData, { taskStatus: "success", taskConfigId: null, taskLineType: null, taskEditUserList: null, taskEditedUserList: null, taskFinishAt: dayjs().format("YYYY-MM-DD HH:mm:ss") - } + }) break; default: break; @@ -412,15 +661,14 @@ class TaskService extends Service { // 插入该条审批记录 historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus: 'end', type })) // 直接结束任务 - updateTaskData = { + _.assign(updateTaskData, { taskStatus: 'reject', taskConfigId: null, taskLineType: null, taskEditUserList: null, taskEditedUserList: null, taskFinishAt: dayjs().format("YYYY-MM-DD HH:mm:ss") - } - // historyInsertData.push(...await this.createTaskHistory({ taskData, taskStatus, type: '拒绝' })) + }) } @@ -705,8 +953,8 @@ class TaskService extends Service { const { taskComment, taskHandleDesc, createHistoryAt, taskStatus } = historyRecord return { - userId: userId, - username: username, + userId: appUserId, + username: username || appUserId + '(账号被禁用)', comment: taskComment, handleDesc: taskHandleDesc, createHistoryAt: dayjs(createHistoryAt || '').format('YYYY-MM-DD HH:mm:ss'), @@ -714,8 +962,8 @@ class TaskService extends Service { } } else { return { - userId: userId, - username: username, + userId: appUserId, + username: username || appUserId + '(账号被禁用)', } } }) diff --git a/app/view/page/taskManagement.html b/app/view/page/taskManagement.html index d6515db..7e11504 100644 --- a/app/view/page/taskManagement.html +++ b/app/view/page/taskManagement.html @@ -195,7 +195,8 @@ taskStatusColor:{ '已通过': 'primary', '已拒绝': 'red', - '审核中': 'blue' + '审核中': 'blue', + "已撤回": 'grey' } }, // 计算 @@ -248,7 +249,8 @@ const statusMap = { 'success': '已通过', 'reject': '已拒绝', - 'running': '审核中' + 'running': '审核中', + 'revoke': '已撤回', }; rows.forEach(row => { row.taskFormInput = JSON.parse(row.taskFormInput); @@ -282,7 +284,7 @@ pageId: 'taskManagement', actionId: 'updateItem', actionData: { - taskStatus: 'end', + taskStatus: 'revoke', taskLineType: null, taskEditUserList: null, taskEditedUserList: null,