From 4c3c380fd04118d9507986002e3b563eb42dbdc4 Mon Sep 17 00:00:00 2001 From: dangsn <dangsn@chicecm.com> Date: 星期三, 16 八月 2023 16:09:01 +0800 Subject: [PATCH] 已发起事件获取流程开始时间和流程变量 --- Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java | 144 +++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 130 insertions(+), 14 deletions(-) diff --git a/Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java b/Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java index a84b86b..6aca608 100644 --- a/Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java +++ b/Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java @@ -24,14 +24,24 @@ import com.vci.ubcs.flow.engine.constant.FlowEngineConstant; import com.vci.ubcs.flow.engine.entity.FlowProcess; import com.vci.ubcs.flow.engine.utils.FlowCache; +import com.vci.ubcs.flow.engine.utils.FlowableUtils; +import com.vci.ubcs.starter.web.util.VciDateUtil; +import com.vci.ubcs.system.user.cache.UserCache; +import com.vci.ubcs.system.user.entity.User; import lombok.AllArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.flowable.engine.HistoryService; +import org.flowable.engine.RuntimeService; import org.flowable.engine.TaskService; +import org.flowable.engine.history.HistoricActivityInstance; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.history.HistoricProcessInstanceQuery; +import org.flowable.engine.runtime.Execution; +import org.flowable.engine.task.Comment; import org.flowable.task.api.TaskQuery; import org.flowable.task.api.history.HistoricTaskInstance; import org.flowable.task.api.history.HistoricTaskInstanceQuery; +import org.flowable.variable.api.history.HistoricVariableInstance; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.support.Kv; import org.springblade.core.tool.utils.Func; @@ -39,6 +49,7 @@ import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -54,6 +65,7 @@ private final TaskService taskService; private final HistoryService historyService; + private final RuntimeService runtimeService; @Override public IPage<BladeFlow> selectClaimPage(IPage<BladeFlow> page, BladeFlow bladeFlow) { @@ -72,9 +84,9 @@ .includeProcessVariables().active().orderByTaskCreateTime().desc(); // 鏋勫缓鍒楄〃鏁版嵁 - buildFlowTaskList(bladeFlow, flowList, claimUserQuery, FlowEngineConstant.STATUS_CLAIM); - buildFlowTaskList(bladeFlow, flowList, claimRoleWithTenantIdQuery, FlowEngineConstant.STATUS_CLAIM); - buildFlowTaskList(bladeFlow, flowList, claimRoleWithoutTenantIdQuery, FlowEngineConstant.STATUS_CLAIM); + buildFlowTaskList(bladeFlow, flowList, claimUserQuery, FlowEngineConstant.STATUS_CLAIM, page); + buildFlowTaskList(bladeFlow, flowList, claimRoleWithTenantIdQuery, FlowEngineConstant.STATUS_CLAIM, page); + buildFlowTaskList(bladeFlow, flowList, claimRoleWithoutTenantIdQuery, FlowEngineConstant.STATUS_CLAIM, page); // 璁$畻鎬绘暟 long count = claimUserQuery.count() + claimRoleWithTenantIdQuery.count() + claimRoleWithoutTenantIdQuery.count(); @@ -97,7 +109,7 @@ .includeProcessVariables().orderByTaskCreateTime().desc(); // 鏋勫缓鍒楄〃鏁版嵁 - buildFlowTaskList(bladeFlow, flowList, todoQuery, FlowEngineConstant.STATUS_TODO); + buildFlowTaskList(bladeFlow, flowList, todoQuery, FlowEngineConstant.STATUS_TODO,page); // 璁$畻鎬绘暟 long count = todoQuery.count(); @@ -117,7 +129,7 @@ HistoricProcessInstanceQuery historyQuery = historyService.createHistoricProcessInstanceQuery().startedBy(taskUser).orderByProcessInstanceStartTime().desc(); - if (bladeFlow.getCategory() != null) { + if (StringUtils.isNotEmpty(bladeFlow.getCategory())) { historyQuery.processDefinitionCategory(bladeFlow.getCategory()); } if (bladeFlow.getProcessDefinitionName() != null) { @@ -142,7 +154,7 @@ String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey()); if (businessKey.length > 1) { flow.setBusinessTable(businessKey[0]); - flow.setBusinessId(businessKey[1]); + flow.setBusinessId(businessKey.length>1?businessKey[1]:"1"); } flow.setHistoryActivityName(historicProcessInstance.getName()); flow.setProcessInstanceId(historicProcessInstance.getId()); @@ -171,6 +183,21 @@ flow.setProcessIsFinished(FlowEngineConstant.STATUS_UNFINISHED); } flow.setStatus(FlowEngineConstant.STATUS_FINISH); + + //娴佺▼鍙戣捣鏃堕棿 + flow.setProcessDefinitionStartTime(VciDateUtil.date2Str(historicProcessInstance.getStartTime(),VciDateUtil.DateTimeFormat)); + + //娴佺▼鍙橀噺 + + List<HistoricVariableInstance> variableInstances = historyService.createHistoricVariableInstanceQuery().processInstanceId(historicProcessInstance.getId()).list(); + Map<String,Object> variables = FlowableUtils.switchVariable(variableInstances); + flow.setVariables(variables); + + //businessKey + if (Func.isNotEmpty(historicProcessInstance)) { + flow.setBusinessId(businessKey.length>1 && StringUtils.isNotEmpty(businessKey[1])?businessKey[1]:"1"); + } + flowList.add(flow); }); @@ -190,7 +217,7 @@ HistoricTaskInstanceQuery doneQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(taskUser).finished() .includeProcessVariables().orderByHistoricTaskInstanceEndTime().desc(); - if (bladeFlow.getCategory() != null) { + if (StringUtils.isNotEmpty(bladeFlow.getCategory())) { doneQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory())); } if (bladeFlow.getProcessDefinitionName() != null) { @@ -230,7 +257,7 @@ if (Func.isNotEmpty(historicProcessInstance)) { String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey()); flow.setBusinessTable(businessKey[0]); - flow.setBusinessId(businessKey[1]); + flow.setBusinessId(businessKey.length>1?businessKey[1]:"1"); if (historicProcessInstance.getEndActivityId() != null) { flow.setProcessIsFinished(FlowEngineConstant.STATUS_FINISHED); } else { @@ -263,9 +290,22 @@ variables = Kv.create(); } variables.put(ProcessConstant.PASS_KEY, flow.isPass()); - variables.put("yn","y"); + // 瀹屾垚浠诲姟 taskService.complete(taskId, variables); + +// //璁剧疆涓嬩竴姝ュ鏍镐汉 +// Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();//涓嬩竴姝ヨ妭鐐� +// String taskKey = task.getTaskDefinitionKey();//灏辨槸娴佺▼閰嶇疆鐨勮妭鐐筰d +// Object to = getVar(processInstanceId,taskKey); +// if(to==null){ +// throw new ServiceException(taskKey+"娌℃湁璁剧疆瀹℃牳浜�"); +// } +// String assignee = to.toString(); +// +// if(StringUtils.isNotEmpty(assignee)) { +// task.setAssignee(assignee); +// } return true; } @@ -276,9 +316,11 @@ * @param flowList 娴佺▼鍒楄〃 * @param taskQuery 浠诲姟鏌ヨ绫� * @param status 鐘舵�� + * @param page */ - private void buildFlowTaskList(BladeFlow bladeFlow, List<BladeFlow> flowList, TaskQuery taskQuery, String status) { - if (bladeFlow.getCategory() != null) { + private void buildFlowTaskList(BladeFlow bladeFlow, List<BladeFlow> flowList, TaskQuery taskQuery, String status, IPage<BladeFlow> page) { +// if (bladeFlow.getCategory() != null) { + if (StringUtils.isNotEmpty(bladeFlow.getCategory())) { taskQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory())); } if (bladeFlow.getProcessDefinitionName() != null) { @@ -290,7 +332,9 @@ if (bladeFlow.getEndDate() != null) { taskQuery.taskCreatedBefore(bladeFlow.getEndDate()); } - taskQuery.list().forEach(task -> { + int firstResult = (int) ((page.getCurrent()-1)* page.getSize()); + int maxResult = (int) page.getSize(); + taskQuery.listPage(firstResult,maxResult).forEach(task -> { BladeFlow flow = new BladeFlow(); flow.setTaskId(task.getId()); flow.setTaskDefinitionKey(task.getTaskDefinitionKey()); @@ -302,10 +346,47 @@ flow.setVariables(task.getProcessVariables()); HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(task.getProcessInstanceId()); + + //鑾峰彇涓婁竴浠诲姟淇℃伅 + HistoricActivityInstance historicActivityInstance = getPrevHistoricActivityInstance(task.getProcessInstanceId(),task.getId()); + flow.setHistoryActivityId(historicActivityInstance.getTaskId()); + flow.setHistoryActivityName(historicActivityInstance.getActivityName()); + //鑾峰彇鍘嗗彶浠诲姟鎵ц浜� + if(historicActivityInstance.getActivityType().equals(FlowEngineConstant.USR_TASK)){ + User user = UserCache.getUser(TaskUtil.getUserId(historicActivityInstance.getAssignee())); + flow.setHistoryActivityAssigne(historicActivityInstance.getAssignee()); + flow.setHistoryActivityAssigneName(user.getName()); + List<Comment> commentList = taskService.getTaskComments(historicActivityInstance.getTaskId()); + if (commentList.size() > 0) { + flow.setHistoryActivityIdea(commentList.get(0).getFullMessage()); + } + } + List<HistoricProcessInstance> processInstanceList = historyService.createHistoricProcessInstanceQuery() + .processInstanceId(task.getProcessInstanceId()).orderByProcessInstanceStartTime().asc().list(); + flow.setProcessDefinitionStartTime(VciDateUtil.date2Str(processInstanceList.get(0).getStartTime(),VciDateUtil.DateTimeFormat)); + // 鑾峰彇娴佺▼鍙戣捣浜哄悕绉� + if (FlowEngineConstant.START_EVENT.equals(historicActivityInstance.getActivityType())) { + if (processInstanceList.size() > 0) { + if (StringUtil.isNotBlank(processInstanceList.get(0).getStartUserId())) { + String taskUser = processInstanceList.get(0).getStartUserId(); + User user = UserCache.getUser(TaskUtil.getUserId(taskUser)); + if (user != null) { + flow.setHistoryActivityAssigne(historicActivityInstance.getAssignee()); + flow.setHistoryActivityAssigneName(user.getName()); + } + } + } + } + if (Func.isNotEmpty(historicProcessInstance)) { - String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey()); + String[] businessKey = new String[2]; + if(historicProcessInstance.getBusinessKey().contains(StringPool.COLON)){ + businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey()); + }else{ + businessKey[0] = historicProcessInstance.getBusinessKey(); + } flow.setBusinessTable(businessKey[0]); - flow.setBusinessId(businessKey[1]); + flow.setBusinessId(businessKey.length>1 && StringUtils.isNotEmpty(businessKey[1])?businessKey[1]:"1"); } FlowProcess processDefinition = FlowCache.getProcessDefinition(task.getProcessDefinitionId()); @@ -321,6 +402,29 @@ }); } + private HistoricActivityInstance getPrevHistoricActivityInstance(String processInstanceId, String taskDefinitionId) { + List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId) + .orderByHistoricActivityInstanceStartTime().asc() + .orderByHistoricActivityInstanceEndTime().asc().list(); + List<HistoricActivityInstance> tempList = new ArrayList<>(); + int index = 0; + for(HistoricActivityInstance historicActivityInstance : historicActivityInstanceList){ + if (StringUtils.equals(FlowEngineConstant.USR_TASK, historicActivityInstance.getActivityType()) + || FlowEngineConstant.START_EVENT.equals(historicActivityInstance.getActivityType()) + || FlowEngineConstant.END_EVENT.equals(historicActivityInstance.getActivityType())) { + tempList.add(historicActivityInstance); + } + if(StringUtils.isNotEmpty(historicActivityInstance.getTaskId()) && historicActivityInstance.getTaskId().equals(taskDefinitionId)){ + index = tempList.indexOf(historicActivityInstance); + break; + } + } + if(index == 0){ + return tempList.get(0); + } + return tempList.get(index-1); + } + /** * 鑾峰彇鍘嗗彶娴佺▼ * @@ -331,4 +435,16 @@ return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); } + public Object getVar(String processInstanceId,String key){ + Object t = null; + List<Execution> list = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list(); + for (Execution execution : list) { + Object va = runtimeService.getVariable(execution.getId(),key); + if(va!=null){ + t=va; + } + } + return t; + } + } -- Gitblit v1.9.3