From e751661fc9deb08ce060772a1e35ef865ec252b5 Mon Sep 17 00:00:00 2001 From: cxx Date: Sun, 28 Apr 2024 16:29:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A1=E6=89=B9=E6=B5=81=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/task.js | 155 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/app/service/task.js b/app/service/task.js index 9e37e2f..0b56a7a 100644 --- a/app/service/task.js +++ b/app/service/task.js @@ -5,6 +5,7 @@ const _ = require("lodash"); const validateUtil = require('@jianghujs/jianghu/app/common/validateUtil'); const idGenerateUtil = require("@jianghujs/jianghu/app/common/idGenerateUtil"); const { BizError, errorInfoEnum } = require("../constant/error"); +const dayjs = require('dayjs'); class TaskService extends Service { @@ -40,6 +41,160 @@ class TaskService extends Service { if (insertData.length > 0) await jianghuKnex('file', this.ctx).insert(insertData); } } + + async submitNode() { + const { jianghuKnex } = this.app; + const { actionData } = this.ctx.request.body.appData; + const { updateItem, type } = actionData; + + const { updateTaskData, historyInsertData, noticeInsertData } = await this.buildNext({ taskData: updateItem, type }) + + await jianghuKnex.transaction(async trx => { + if (updateTaskData) await trx("task", this.ctx).where({ taskId: updateItem.taskId }).jhUpdate(updateTaskData); + if (noticeInsertData.length > 0) await trx("notice_push", this.ctx).jhInsert(noticeInsertData); + if (historyInsertData.length > 0) await trx("task_history", this.ctx).jhInsert(historyInsertData); + }) + } + + async buildNext({ taskData, type }) { + + const { userId, username } = this.ctx.userInfo; + + const { taskId, workflowConfig, taskConfigId, taskEditUserList, taskEditedUserList } = taskData; + + var updateTaskData = null + var historyInsertData = [] + var noticeInsertData = [] + var curNode = workflowConfig.nodeList.find(node => node.id == taskConfigId) + var taskStatus = "running" + + // 1、更新task状态 + if (type == '同意') { + var approvalType = curNode.properties.approvalType + var editUserList = taskEditUserList && taskEditUserList.split(',') || [] + var editedUserList = taskEditedUserList && taskEditedUserList.split(',') || [] + + // 判断该节点是否已完成 + if ((approvalType == "会签" || approvalType == "依次审批") && editUserList.length + 1 < editedUserList.length) { + // 未完成 + updateTaskData = { + taskEditUserList: editUserList.filter(u => u != userId), + taskEditedUserList: editedUserList.push(userId).join(','), + } + + historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) + + } else {// 流转搭配下一节点 + + // 找下一个节点 + var nextNode = await this.getNextNode(workflowConfig, curNode) + var nextLine = workflowConfig.lineList.find(line => line.sourceNodeId == nextNode.id) + + // 下一个节点是审批节点 + if (nextNode.properties.type === "approvalTask") { + updateTaskData = { + taskConfigId: nextNode.id, + taskLineType: nextLine?.properties.type, + taskEditUserList: nextNode.properties.userIdList.join(','), + taskEditedUserList: null + } + + historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) + } + // 下一个节点是抄送节点 + if (nextNode.properties.type === "noticeTask") { + noticeInsertData.push({ + noticeId: await idGenerateUtil.uuid(), + noticeName: "通知-审批-" + taskId, + noticeContent: '{}', + pushContent: '{}', + pushStatus: "success", + pushAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + }) + historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) + nextNode = await this.getNextNode(workflowConfig, curNode) + taskData = { + ...taskData, + taskConfigId: nextNode.id, + taskEditUserList: null, + taskEditedUserList: null, + taskComment: null, + operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") + } + + const result = await this.buildNext({ taskData, type }) + updateTaskData = result.updateTaskData + historyInsertData.push(...result.historyInsertData) + noticeInsertData.push(...result.noticeInsertData) + } + + // 下一个节点是结束节点 + if (nextNode.properties.type === "end") { + taskStatus = "end" + updateTaskData = { + taskStatus: taskStatus, + 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 })) + } + } + } else { + // 直接结束任务 + taskStatus = "end" + updateTaskData = { + taskStatus: taskStatus, + 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 })) + } + + return { + updateTaskData, + noticeInsertData, + historyInsertData + } + } + + async getNextNode(workflow, curNode) { + var line = workflow.lineList.find(l => l.sourceNodeId == curNode.id) + var nextNode = workflow.nodeList.find(n => n.id == line.targetNodeId) + + if (nextNode.properties.type === "conditionTask") { + nextNode = this.getNextNode(workflow, nextNode) + } + + return nextNode + } + + async createTaskHistory({ taskData, taskStatus, type }) { + return { + taskId: taskData.taskId, + taskStatus: taskStatus, + taskTitle: taskData.taskTitle, + taskFormInput: JSON.stringify(taskData.taskFormInput), + taskConfigId: taskData.taskConfigId, + taskNextConfigList: taskData.taskNextConfigList, + taskLineTypeList: taskData.taskLineType, + taskInitUser: taskData.taskInitUser, + taskEditUserList: taskData.taskEditUserList, + taskViewUserList: taskData.taskViewUserList, + taskComment: taskData.taskComment, + workflowConfig: JSON.stringify(taskData.taskWorkflow), + taskExplain: type, + taskHandleDesc: type, + taskCostDuration: Math.round(dayjs().diff(dayjs(taskData.operationAt)) / 1000) + } + } + } module.exports = TaskService;