'use strict'; const Service = require('egg').Service; 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 { async appendFileAfterHook() { const { jianghuKnex } = this.app; const { taskFormInput, taskId } = this.ctx.request.body.appData.actionData; const fileInput = JSON.parse(taskFormInput).filter(item => item.type == 'file') if (fileInput.length > 0) { const insertData = [] const idSequence = await idGenerateUtil.idPlus({ knex: jianghuKnex, startValue: 1000, tableName: 'jianghujs_workflow.file', columnName: 'idSequence', plusValue: 1 }); fileInput.forEach(async element => { element.answer.forEach(async file => { const { filename, downloadPath, binarySize } = file insertData.push({ fileId: "F" + (idSequence), idSequence: idSequence, fileBizId: taskId, filename, downloadPath, binarySize }) idSequence++ }); }); 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); }) } // TODO:逻辑待优化 async buildNext({ taskData, type }) { const { userId, username } = this.ctx.userInfo; const { taskId, taskWorkflow, taskConfigId, taskEditUserList, taskEditedUserList } = taskData; var updateTaskData = null var historyInsertData = [] var noticeInsertData = [] var curNode = taskWorkflow.nodeList.find(node => node.id == taskConfigId) var taskStatus = "running" taskData.createHistoryBy = userId // 1、更新task状态 if (type == '同意') { var approvalType = curNode.properties.approvalType var editUserList = taskEditUserList && taskEditUserList.split(',') || [] var editedUserList = taskEditedUserList && taskEditedUserList.split(',') || [] // 判断该节点是否已完成 if (curNode.id.startsWith('approvalTask') && (approvalType == "会签" || approvalType == "依次审批") && editUserList.length > 1) { // 未完成 editedUserList.push(userId) updateTaskData = { taskEditUserList: editUserList.filter(u => u != userId).join(','), taskEditedUserList: editedUserList.join(','), } historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) } else {// 流转搭配下一节点 // 找下一个节点 var nextNode = await this.getNextNode(taskWorkflow, curNode) var nextLine = taskWorkflow.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") { historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) taskData = { ...taskData, taskConfigId: nextNode.id, taskEditUserList: nextNode.properties.userIdList.join(','), taskEditedUserList: null, taskComment: null, taskExplain: '抄送节点', taskHandleDesc: '抄送', taskLineTypeList: '抄送', operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") } noticeInsertData.push({ noticeId: await idGenerateUtil.uuid(), noticeName: "通知-审批-" + taskId, noticeContent: nextNode.properties.userIdList.join(','), pushContent: '{}', pushStatus: "success", pushAt: dayjs().format("YYYY-MM-DD HH:mm:ss") }) var noticeUser = nextNode.properties.userIdList for (let index = 0; index < noticeUser.length; index++) { const element = noticeUser[index]; taskData.createHistoryBy = element historyInsertData.push(await this.createTaskHistory({ taskData, taskStatus, type })) } taskData = { ...taskData, taskEditUserList: null, taskEditedUserList: null, taskComment: null, taskExplain: null, taskHandleDesc: null, taskLineTypeList: null, operationAt: dayjs().format("YYYY-MM-DD HH:mm:ss") } const result = await this.buildNext({ taskData, type: 'end' }) 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) { const { lineList, nodeList } = workflow var line = lineList.find(l => l.sourceNodeId == curNode.id), nextNode = nodeList.find(n => n.id == line.targetNodeId); if (nextNode.properties.type === "conditionTask") { nextNode = this.getNextNode(workflow, nextNode) } return nextNode } async createTaskHistory({ taskData, taskStatus, type }) { const { userId, username } = this.ctx.userInfo; // 解构 taskData 对象以简化访问 const { taskId, taskFormInput, taskConfigId, taskNextConfigList, taskLineType, taskEditUserList, taskViewUserList, taskComment, taskWorkflow, operationAt, createHistoryBy, taskHandleDesc, taskExplain, taskLineTypeList } = taskData; // 计算任务持续时间 const taskCostDuration = Math.round(dayjs().diff(dayjs(operationAt)) / 1000); return { taskId, taskStatus, taskFormInput: JSON.stringify(taskFormInput), taskConfigId, taskNextConfigList, taskLineTypeList: taskLineTypeList || type, taskEditUserList, taskViewUserList, taskComment, taskWorkflow: JSON.stringify(taskWorkflow), taskExplain: taskExplain || type, taskHandleDesc: taskHandleDesc || type, taskCostDuration, createHistoryBy: createHistoryBy, createHistoryAt: dayjs().format('YYYY-MM-DD HH:mm:ss') } } async getTaskWorkflow() { const { jianghuKnex } = this.app; const { userId } = this.ctx.userInfo; const { taskId } = this.ctx.request.body.appData.actionData; // 获取当前审批 const task = await jianghuKnex('task').where({ taskId }).first(); const { taskWorkflow, taskConfigId } = task // 获取审批流历史 const taskHistory = await jianghuKnex('task_history').where({ taskId }).select(); // 获取人员 const userList = await jianghuKnex('enterprise_user').select() var workflowHistory = JSON.parse(taskWorkflow).nodeList.filter(n => !['start', 'end', 'conditionTask'].includes(n.properties.type)) .map(node => { const { id, label, userIdList, approvalType } = node.properties const userConfig = userIdList?.map(appUserId => { var username = userList.find(u => u.userId == appUserId)?.username, historyRecord = taskHistory.find(h => h.taskConfigId == node.id && h.createHistoryBy == appUserId); if (historyRecord) { const { taskComment, taskHandleDesc, createHistoryAt, taskStatus } = historyRecord return { userId: userId, username: username, comment: taskComment, handleDesc: taskHandleDesc, createHistoryAt: dayjs(createHistoryAt || '').format('YYYY-MM-DD HH:mm:ss'), taskStatus: taskStatus, } } else { return { userId: userId, username: username, } } }) var nodeStatus = '审批中' if (id.startsWith('approvalTask')) { if (approvalType == "或签") { nodeStatus = _.some(userConfig, ['handleDesc', '拒绝']) ? '已拒绝' : _.some(userConfig, ['handleDesc', '同意']) ? '已同意' : '审批中' } else { var nodeStatus = _.some(userConfig, ['handleDesc', '拒绝']) ? '已拒绝' : _.every(userConfig, ['handleDesc', '同意']) ? '已同意' : '审批中' } } else if (id.startsWith('noticeTask')) { nodeStatus = _.every(userConfig, u => u.createHistoryAt) ? '已抄送' : '抄送中' } return { id, label, approvalType: approvalType, // 是审批节点就取审批形式 userList: userConfig, nodeStatus: nodeStatus } }) return workflowHistory } async getTaskList() { const { jianghuKnex } = this.app; const { userId } = this.ctx.userInfo; const task = await jianghuKnex('task').where({ taskStatus: 'running' }).where('taskEditUserList', 'like', '%' + userId + '%').select(); // 判断是否到该人审批 const result = task.filter(item => { const { taskWorkflow, taskConfigId, taskEditUserList } = item // 查看当前节点是否是"依次审批" var isApproval = true, type = JSON.parse(taskWorkflow).nodeList.find(node => node.id === taskConfigId)?.properties.approvalType if (type === '依次审批' && !taskEditUserList.startsWith(userId)) { isApproval = false } return isApproval }) return result } } module.exports = TaskService;