import { HttpRequest, Instance } from "noomi"; import {NFUserManager,NFEngine,NFProcess } from "noomiflow"; import { __Util } from "./__util"; import { EntityManager, getEntityManager, Query } from "relaen"; import { __AmDataConfig } from "../__data/__amdataconfig"; @Instance() export class AmFlowService{ /** * 获取用户未处理 * @param userId * @param pageNo * @param pageSize */ async getUnhandle(request:HttpRequest,model){ const logon = await __Util.getLogonUser(request); if(!logon){ throw "用户未登录"; } return await NFUserManager.getUnHandleNodes(logon.userId,model.pageNo,model.pageSize); } /** * 获取用户未处理 * @param userId * @param pageNo * @param pageSize */ async getHandled(request:HttpRequest,model){ const logon = await __Util.getLogonUser(request); if(!logon){ throw "用户未登录"; } return await NFUserManager.getHandledNodes(logon.userId,model.pageNo,model.pageSize); } /** * 获取用户未处理 * @param userId * @param pageNo * @param pageSize */ async getCreated(request:HttpRequest,model){ const logon = await __Util.getLogonUser(request); if(!logon){ throw "用户未登录"; } return await NFUserManager.getCreatedProcess(logon.userId,model.pageNo,model.pageSize); } /** * 创建流程 * @param request http request * @param flowName 流程定义名 * @param param 初始参数 * @returns 流程实例id */ async createFlow(request,flowName,param?):Promise{ const userInfo = await __Util.getLogonUser(request); if(!userInfo){ throw "用户未登录"; } const proc:NFProcess = await NFEngine.createProcess(flowName,flowName + '-' + userInfo.userName + '-' + new Date().toUTCString(),userInfo.userId); if(param){ for(let k of Object.keys(param)){ proc.setParam(k,param[k]); } } await proc.start(); return proc.entity.processId; } /** * 执行模块 */ doModule(process:NFProcess,mdl:Function){ //从param获取data数据集,并调用模块进行处理 process.setParam('data',mdl(process.getParam('data'))); process.next(); } /** * 执行请求 * @param request * @param model */ async doAsk(request,model){ const userInfo = await __Util.getLogonUser(request); if(!userInfo){ throw "用户未登录"; } const flowName = model.processName; const proc = await NFEngine.createProcess( flowName, flowName + '-' + userInfo.userName + '-' + new Date().toUTCString(), userInfo.userId ); //传递参数 proc.setParam('condId',model.condId); //开始流程 await proc.start(); // 进入下个节点 await proc.next({userId:userInfo.userId}); } /** * 执行审核 * @param model */ async doCheck(request,model){ const logon = await __Util.getLogonUser(request); if(!logon){ throw '用户未登录'; } const userId = logon.userId const agree = Number(model?.isAgree??0); const proc:NFProcess = await NFEngine.getInstance(model.processId); const vars = { userId, agree } if(model.reason){ vars['reason'] = model.reason; } await proc.setParams(vars); await proc.next({userId,agree}); //流程已结束,执行对应操作 if(proc.entity.endTime){ switch(proc.entity.processName.substring(0,4)){ case "数据导入": await this.doImport(model,agree); break; case "数据删除": await this.doDelete(model,agree); break; } } } /** * delete check * @param model */ async doDelete(model,agree){ if(agree){ const em:EntityManager = await getEntityManager(); const query:Query = em.createQuery(__AmDataConfig.cond.entity); const param = {}; param[__AmDataConfig.cond.id] = {rel:'in',value:model.condId.split(',')} await query.delete().where(param).getResult(); await em.close(); } } /** * import check * @param model * @param agree */ async doImport(model,agree){ const em:any = await getEntityManager(); const query:Query = em.createQuery(__AmDataConfig.cond.entity); const param = {}; param[__AmDataConfig.cond.id] = model.condId; const cond = await query.select('*').where(param).getResult() as any; const di = await cond.getAmDataImport(); if(agree){ //通过则保存 di.checkTime = new Date().getTime(); di.enabled = 1; await di.save(); }else{ //不通过则删除 await di.delete(); } await em.close(); } }