From a7eae503f2a30a75ff4d7215c68fd34574466565 Mon Sep 17 00:00:00 2001 From: dimengzhe Date: Tue, 11 Feb 2025 08:41:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=81=E7=A8=8B=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/flowtask/FlowTaskService.java | 128 +++++++++++++++++- .../anrui/portal/biz/flow/FlowableRest.java | 107 ++++++++++++++- 2 files changed, 233 insertions(+), 2 deletions(-) diff --git a/anrui-flowable/anrui-flowable-biz/src/main/java/com/yxt/anrui/flowable/biz/flowtask/FlowTaskService.java b/anrui-flowable/anrui-flowable-biz/src/main/java/com/yxt/anrui/flowable/biz/flowtask/FlowTaskService.java index 79f61f5c9e..cf22c64048 100644 --- a/anrui-flowable/anrui-flowable-biz/src/main/java/com/yxt/anrui/flowable/biz/flowtask/FlowTaskService.java +++ b/anrui-flowable/anrui-flowable-biz/src/main/java/com/yxt/anrui/flowable/biz/flowtask/FlowTaskService.java @@ -302,6 +302,131 @@ public class FlowTaskService extends MybatisBaseService businessFlowRecord(String procInsId) { + long startTimes = System.currentTimeMillis(); // 记录开始时间 + ResultBean rb = ResultBean.fireFail(); + FlowRecordVo flowRecordVo = new FlowRecordVo(); + if (StringUtils.isNotBlank(procInsId)) { + // 批量获取所有历史任务实例 + List list = historyService + .createHistoricActivityInstanceQuery() + .processInstanceId(procInsId) + .list(); + // 批量获取所有历史任务实例的变量 + List historicTasks = historyService.createHistoricTaskInstanceQuery() + .processInstanceId(procInsId) + .includeProcessVariables() + .list(); + Map taskInstanceMap = historicTasks.stream() + .collect(Collectors.toMap(HistoricTaskInstance::getId, task -> task)); + + // 获取流程实例信息 + Map processInstance = processService.getProcessInstanceById(procInsId); + + // 预收集所有审批人的 ID,避免多个网络请求 + Set assigneeIds = list.stream() + .map(HistoricActivityInstance::getAssignee) + .filter(StringUtils::isNotBlank) + .collect(Collectors.toSet()); + + // 将 Set 转换为以逗号分隔的字符串 + String assigneeIdsStr = String.join(",", assigneeIds); + + // 远程调用批量查询用户信息 + Map userMap = sysUserFeign.fetchBySids(assigneeIdsStr).getData().stream() + .collect(Collectors.toMap(SysUserVo::getSid, user -> user)); + + // 直接查询所有流程实例评论 + List commentList = taskService.getProcessInstanceComments(procInsId); + commentList.sort(Comparator.comparing(Comment::getTime)); + + // 预初始化 hisFlowList,确保它始终为非 null + List hisFlowList = new ArrayList<>(); + // 使用一个线程安全的集合来存储结果 + List finalHisFlowList = Collections.synchronizedList(hisFlowList); + + // 使用 parallelStream 前先进行 null 检查和过滤 + hisFlowList = list.parallelStream() + .filter(histIns -> histIns != null && histIns.getTaskId() != null) // 过滤掉 histIns 或 histIns.getTaskId() 为 null 的元素 + .map(histIns -> { + FlowTask flowTask = new FlowTask(); + flowTask.setTaskId(histIns.getTaskId()); + flowTask.setTaskName(histIns.getActivityName()); + flowTask.setTaskDefKey(histIns.getActivityId()); + flowTask.setProcDefId(histIns.getProcessDefinitionId()); + + // 获取任务变量 + HistoricTaskInstance historicTaskInstance = taskInstanceMap.get(histIns.getTaskId()); + flowTask.setProcVars(historicTaskInstance != null ? historicTaskInstance.getProcessVariables() : Collections.emptyMap()); + + // 设置开始和结束时间 + flowTask.setCreateTime(histIns.getStartTime()); + flowTask.setFinishTime(histIns.getEndTime()); + + // 判断流程是否结束 + flowTask.setProcessEndTime(processInstance.get("END_TIME_") == null ? "" : "end"); + + // 处理审批人员 + if (StringUtils.isNotBlank(histIns.getAssignee()) && userMap.containsKey(histIns.getAssignee())) { + SysUserVo user = userMap.get(histIns.getAssignee()); + if (user != null) { + TaskUserInfo taskUserInfo = new TaskUserInfo(); + taskUserInfo.setAssigneeName(user.getName()); + taskUserInfo.setAssigneeSid(user.getSid()); + taskUserInfo.setAssigneeHeadImage(fileUploadComponent.getUrlPrefix() + user.getHeadImage()); + flowTask.setTaskUserInfos(Collections.singletonList(taskUserInfo)); + } + } + // 处理意见评论内容 + if (commentList != null && !commentList.isEmpty()) { + commentList.stream() + .filter(comment -> histIns.getTaskId().equals(comment.getTaskId())) + .forEach(comment -> { + String type = comment.getType(); + FlowCommentDto build = flowableService.selectComment(type, histIns.getTaskId(), histIns.getProcessInstanceId(), comment.getId()); + if ("4".equals(type)) { // 加签审批意见 + FlowTask flowTask1 = new FlowTask(); + String userSid = comment.getUserId(); + SysUserVo user = userMap.get(userSid); + + TaskUserInfo taskUserInfo = new TaskUserInfo(); + taskUserInfo.setAssigneeName(user != null ? user.getName() : ""); + taskUserInfo.setAssigneeSid(userSid); + taskUserInfo.setAssigneeHeadImage(user != null ? fileUploadComponent.getUrlPrefix() + user.getHeadImage() : ""); + + flowTask1.setTaskName("加签审批意见"); + flowTask1.setFinishTime(comment.getTime()); + flowTask1.setCreateTime(comment.getTime()); + flowTask1.setTaskUserInfos(Collections.singletonList(taskUserInfo)); + flowTask1.setComment(build); + flowTask1.setProcDefId(flowTask.getProcDefId()); + flowTask1.setProcVars(flowTask.getProcVars()); + flowTask1.setTaskDefKey(histIns.getActivityId()); + finalHisFlowList.add(flowTask1); + } else { + flowTask.setComment(build); + } + }); + } + return flowTask; + + }).collect(Collectors.toList()); + + + flowRecordVo.setFlowList(hisFlowList); + } + log.info("flowRecordVo:{}", JSONObject.toJSONString(flowRecordVo)); + long endTime = System.currentTimeMillis(); + log.info("流程记录接口耗时 {} ms", endTime - startTimes); + return rb.success().setData(flowRecordVo); + } + + /** + * 流程历史流转记录 + * + * @param procInsId 流程实例Id + * @return + */ + public ResultBean businessFlowRecordOld(String procInsId) { ResultBean rb = ResultBean.fireFail(); FlowRecordVo flowRecordVo = new FlowRecordVo(); if (StringUtils.isNotBlank(procInsId)) { @@ -418,6 +543,7 @@ public class FlowTaskService extends MybatisBaseService> flowRecordAndComment(String procInsId, String deployId) { + // 并行调用外部服务 + CompletableFuture> flowRecordFuture = + CompletableFuture.supplyAsync(() -> flowableService.flowRecord(procInsId, deployId)); + + CompletableFuture>> commentListFuture = + CompletableFuture.supplyAsync(() -> getCommentList(procInsId, deployId)); + + // 并行执行 flowTaskFeign.yuyanTest 查询 + CompletableFuture>>> flowTaskFuture = + CompletableFuture.supplyAsync(() -> { + ResultBean>> listResultBean = ResultBean.fireFail(); + // 获取流转记录和查询条件 + ResultBean flowRecordVoResultBean = flowRecordFuture.join(); + List flowList1 = flowRecordVoResultBean.getData().getFlowList(); + + if (!flowList1.isEmpty()) { + com.yxt.anrui.flowable.api.flowtask.FlowTask appFlowableRecordVo = flowList1.get(flowList1.size() - 1); + if (!"Event_end".equals(appFlowableRecordVo.getTaskDefKey())) { + String proDefId = appFlowableRecordVo.getProcDefId(); + Map map = (Map) appFlowableRecordVo.getProcVars(); + + FlowableQuery flowableQuery = new FlowableQuery(); + flowableQuery.setModelId(proDefId); + flowableQuery.setTaskDefKey(appFlowableRecordVo.getTaskDefKey()); + flowableQuery.setMap(map); + + return flowTaskFeign.yuyanTest(flowableQuery); + } + } + return listResultBean.success().setData(new ArrayList<>()); + }); + + // 等待外部服务调用完成 + ResultBean flowRecordVoResultBean = flowRecordFuture.join(); + ResultBean> commentList = commentListFuture.join(); + ResultBean>> flowTaskResultBean = flowTaskFuture.join(); + + List flowList = new ArrayList<>(); + List flowList1 = flowRecordVoResultBean.getData().getFlowList(); + + // 处理流转记录 + flowList1.forEach(f -> { + PCHistTaskListAndCommentList a = new PCHistTaskListAndCommentList(); + a.setTime(f.getFinishTime() == null ? new Date() : f.getFinishTime()); + + Map stringObjectMap = BeanUtil.beanToMap(f); + stringObjectMap.put("finishTime", DateUtil.format(f.getFinishTime(), "yyyy-MM-dd HH:mm:ss")); + stringObjectMap.put("createTime", DateUtil.format(f.getCreateTime(), "yyyy-MM-dd HH:mm:ss")); + + a.setFlowableRecordVo(stringObjectMap); + a.setState("0"); + + List taskUserInfos = f.getTaskUserInfos(); + if (taskUserInfos != null) { + taskUserInfos.removeAll(Collections.singleton(null)); + if (!taskUserInfos.isEmpty()) { + if (taskUserInfos.size() == 1) { + com.yxt.anrui.flowable.api.flowtask.TaskUserInfo appUserVo = taskUserInfos.get(0); + if ("admin".equals(appUserVo.getAssigneeName())) { + return; // 如果是admin,跳过该条记录 + } + } + } + } + flowList.add(a); + }); + + // 处理评论记录 + commentList.getData().forEach(f -> { + PCHistTaskListAndCommentList a = new PCHistTaskListAndCommentList(); + a.setTime(f.getCreateTime()); + f.setTitle(f.getTaskUserInfo().getAssigneeName() + "添加了评论"); + + Map stringObjectMap = BeanUtil.beanToMap(f); + a.setProcessCommentVo(stringObjectMap); + a.setState("1"); + + flowList.add(a); + }); + + // 处理额外的流转记录 + flowTaskResultBean.getData().forEach(f -> { + PCHistTaskListAndCommentList a = new PCHistTaskListAndCommentList(); + Map stringObjectMap = BeanUtil.beanToMap(f); + stringObjectMap.put("taskName", f.get("nodeName").toString()); + stringObjectMap.put("finishTime", null); + stringObjectMap.put("createTime", ""); + stringObjectMap.put("taskUserInfos", new ArrayList<>()); + a.setFlowableRecordVo(stringObjectMap); + a.setState("3"); + + flowList.add(a); + }); + + // 合并排序:先按时间排序 + flowList.sort(Comparator.nullsLast(Comparator.comparing(PCHistTaskListAndCommentList::getTime, Comparator.nullsLast(Comparator.naturalOrder())))); + + // 返回结果 + ResultBean> resultBean = new ResultBean>().success(); + resultBean.setData(flowList); + return resultBean; + } + + public ResultBean> flowRecordAndCommentOld(String procInsId, String deployId) { ResultBean flowRecordVoResultBean = flowableService.flowRecord(procInsId, deployId); com.yxt.anrui.flowable.api.flowtask.FlowRecordVo flowRecordVo = flowRecordVoResultBean.getData(); List flowList = new ArrayList<>(); - //流转记录 + //流转记录 List flowList1 = flowRecordVo.getFlowList(); flowList1.forEach(f->{ PCHistTaskListAndCommentList a=new PCHistTaskListAndCommentList();