import { EntityManager, getEntityManager, Query } from "relaen"; import { NfNode } from "../entity/nfnode"; import { NFAssignmentType, NFNodeType } from "../types"; import { NFUserManager } from "../usermanager"; import { NFTaskUtil } from "../taskutil"; import { NFTaskNode } from "./tasknode"; /** * 人工任务:按办理人 / 候选人 / 候选组落库待办,并挂起等待 `next`。 */ export class NFUserTaskNode extends NFTaskNode{ private assignment:NFAssignmentType = NFAssignmentType.ASSIGNEE; assignee?:string|number; candidateUsers?:string|number; candidateGroups?:string|number; /** * 创建人工任务节点。 * * @param cfg - 可含 `assignee` / `candidateUsers` / `candidateGroups`(均为 id) * @param process - 所属流程 */ constructor(cfg,process){ super(cfg,process); this.type = NFNodeType.USERTASK; this.assignee = cfg.assignee; this.candidateUsers = cfg.candidateUsers; this.candidateGroups = cfg.candidateGroups; } /** * 落库待办并登记到流程当前任务列表,然后挂起等待办理。 */ async run() { this.nfNode = await this.save(); await super.run(); await this.process.addTaskNode(this); } /** * 创建任务实例行:按候选人 / 候选组 / 办理人写入指派字段。 * * @returns 已保存的任务节点实体 */ async save(){ const em:EntityManager = await getEntityManager(); const query:Query = em.createQuery(NfNode.name); const prev = await query.select("*") .where({"nfProcess":this.process.entity.processId,defId:this.id}) .orderBy({nodeId:"desc"}) .getResult(); const variables = prev && !prev.endTime ? prev.variables : JSON.stringify(this.process.getParam()); const node = new NfNode(); node.nfProcess = this.process.entity; node.startTime = new Date().getTime(); node.defId = this.id; node.nodeName = this.name; node.variables = variables; if(this.candidateUsers != null && this.candidateUsers !== ''){ this.assignment = NFAssignmentType.CANDIDATE; const ids = NFTaskUtil.parseIdList(this.candidateUsers); node.assignee = null; node.candidateUsers = NFTaskUtil.formatIdList(ids); node.candidateGroups = null; }else if(this.candidateGroups != null && this.candidateGroups !== ''){ this.assignment = NFAssignmentType.CANDIDATE_GROUP; const groupIds = NFTaskUtil.parseIdList(this.candidateGroups); const ids = await NFUserManager.getUserIdsByGroupIds(groupIds); node.assignee = null; node.candidateUsers = NFTaskUtil.formatIdList(ids); node.candidateGroups = NFTaskUtil.formatIdList(groupIds); }else{ this.assignment = NFAssignmentType.ASSIGNEE; let assigneeId = this.process.entity.nfUser?.userId; if(this.assignee != null && this.assignee !== ''){ const ids = NFTaskUtil.parseIdList(this.assignee); if(ids.length > 0){ assigneeId = ids[0]; } } node.assignee = assigneeId; node.candidateUsers = NFTaskUtil.formatIdList(assigneeId != null ? [assigneeId] : []); node.candidateGroups = null; } await node.save(); await em.close(); return node; } }