zxl
2025-03-24 f8ce7aa161ef2ef316357ead8208bc60de938ead
business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
@@ -6,6 +6,7 @@
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.common.base.Result;
import com.ycl.common.constant.ProcessConstants;
@@ -24,6 +25,7 @@
import com.ycl.domain.dto.FlowTaskDto;
import com.ycl.domain.dto.FlowViewerDto;
import com.ycl.domain.entity.ProcessCoding;
import com.ycl.domain.entity.ProcessLog;
import com.ycl.domain.entity.ProjectProcess;
import com.ycl.domain.entity.SysForm;
import com.ycl.domain.json.RejectData;
@@ -71,7 +73,6 @@
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -329,8 +330,18 @@
        if (taskService.createTaskQuery().taskId(flowTaskVo.getTaskId()).singleResult().isSuspended()) {
            throw new CustomException("任务处于挂起状态!");
        }
        // 当前任务 task
        Task task = taskService.createTaskQuery().taskId(flowTaskVo.getTaskId()).singleResult();
        ProjectProcess projectProcess = new LambdaQueryChainWrapper<>(projectProcessMapper)
                .eq(ProjectProcess::getProcessInsId, task.getProcessInstanceId())
                .eq(ProjectProcess::getProcessDefId, task.getProcessDefinitionId())
                .one();
        if (Objects.isNull(projectProcess)) {
            throw new CustomException("项目流程未绑定");
        }
        // 获取流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
        // 获取所有节点信息
@@ -423,6 +434,21 @@
        try {
            // 如果父级任务多于 1 个,说明当前节点不是并行节点,原因为不考虑多对多情况
            if (targetIds.size() > 1) {
                // 删除被驳回任务的所有日志
                for (String targetId : targetIds) {
                    List<HistoricTaskInstance> rejectHisTaskList = historyService.createHistoricTaskInstanceQuery()
                            .taskDefinitionKey(targetId)
                            .processInstanceId(projectProcess.getProcessInsId())
                            .orderByHistoricTaskInstanceStartTime()
                            .desc()
                            .list();
                    if (CollectionUtils.isNotEmpty(rejectHisTaskList)) {
                        new LambdaUpdateChainWrapper<>(processLogService.getBaseMapper())
                                .eq(ProcessLog::getTaskId, rejectHisTaskList.get(0).getId())
                                .eq(ProcessLog::getProcessInsId, projectProcess.getProcessInsId())
                                .remove();
                    }
                }
                // 1 对 多任务跳转,currentIds 当前节点(1),targetIds 跳转到的节点(多)
                runtimeService.createChangeActivityStateBuilder()
                        .processInstanceId(task.getProcessInstanceId()).
@@ -430,22 +456,30 @@
            }
            // 如果父级任务只有一个,因此当前任务可能为网关中的任务
            if (targetIds.size() == 1) {
                // 删除被驳回任务的所有日志
                List<HistoricTaskInstance> rejectHisTaskList = historyService.createHistoricTaskInstanceQuery()
                        .taskDefinitionKey(targetIds.get(0))
                        .processInstanceId(projectProcess.getProcessInsId())
                        .orderByHistoricTaskInstanceStartTime()
                        .desc()
                        .list();
                if (CollectionUtils.isNotEmpty(rejectHisTaskList)) {
                    new LambdaUpdateChainWrapper<>(processLogService.getBaseMapper())
                            .eq(ProcessLog::getTaskId, rejectHisTaskList.get(0).getId())
                            .eq(ProcessLog::getProcessInsId, projectProcess.getProcessInsId())
                            .remove();
                }
                // 1 对 1 或 多 对 1 情况,currentIds 当前要跳转的节点列表(1或多),targetIds.get(0) 跳转到的节点(1)
                runtimeService.createChangeActivityStateBuilder()
                        .processInstanceId(task.getProcessInstanceId())
                        .moveActivityIdsToSingleActivityId(currentIds, targetIds.get(0)).changeState();
            }
            historyService.deleteHistoricTaskInstance(flowTaskVo.getTaskId());
        } catch (FlowableObjectNotFoundException e) {
            throw new CustomException("未找到流程实例,流程可能已发生变化");
        } catch (FlowableException e) {
            throw new CustomException("无法取消或开始活动");
        }
        ProjectProcess projectProcess = new LambdaQueryChainWrapper<>(projectProcessMapper)
                .eq(ProjectProcess::getProcessInsId, task.getProcessInstanceId())
                .eq(ProjectProcess::getProcessDefId, task.getProcessDefinitionId())
                .one();
        if (Objects.isNull(projectProcess)) {
            throw new CustomException("项目流程未绑定");
        }
        // 保存日志
        publisher.publishEvent(new TaskLogEvent(this, null,
@@ -978,17 +1012,17 @@
    public AjaxResult flowRecord(String procInsId) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(procInsId)) {
            List<HistoricActivityInstance> list = historyService
                    .createHistoricActivityInstanceQuery()
            List<HistoricTaskInstance> list = historyService
                    .createHistoricTaskInstanceQuery()
                    .processInstanceId(procInsId)
                    .orderByHistoricActivityInstanceStartTime()
                    .orderByHistoricTaskInstanceStartTime()
                    .desc().list();
            //扩展 获取这个流程实例的监控信息 key:TaskId value:实体类
            Map<String, ProcessCoding> processCodingMap = processCodingMapper.selectList(new QueryWrapper<ProcessCoding>().eq("process_ins_id", procInsId))
                    .stream()
                    .collect(Collectors.toMap(ProcessCoding::getTaskId, Function.identity()));
            List<FlowTaskDto> hisFlowList = new ArrayList<>();
            for (HistoricActivityInstance histIns : list) {
            for (HistoricTaskInstance histIns : list) {
                // 展示开始节点
//                if ("startEvent".equals(histIns.getActivityType())) {
//                    FlowTaskDto flowTask = new FlowTaskDto();
@@ -1006,10 +1040,10 @@
//                    flowTask.setFinishTime(histIns.getEndTime());
//                    hisFlowList.add(flowTask);
//                } else
                if (StringUtils.isNotBlank(histIns.getTaskId())) {
                if (StringUtils.isNotBlank(histIns.getId())) {
                    FlowTaskDto flowTask = new FlowTaskDto();
                    flowTask.setTaskId(histIns.getTaskId());
                    flowTask.setTaskName(histIns.getActivityName());
                    flowTask.setTaskId(histIns.getId());
                    flowTask.setTaskName(histIns.getName());
                    flowTask.setCreateTime(histIns.getStartTime());
                    flowTask.setFinishTime(histIns.getEndTime());
                    if (StringUtils.isNotBlank(histIns.getAssignee())) {
@@ -1019,7 +1053,7 @@
                        flowTask.setDeptName(Objects.nonNull(sysUser.getDept()) ? sysUser.getDept().getDeptName() : "");
                    }
                    // 展示审批人员
                    List<HistoricIdentityLink> linksForTask = historyService.getHistoricIdentityLinksForTask(histIns.getTaskId());
                    List<HistoricIdentityLink> linksForTask = historyService.getHistoricIdentityLinksForTask(histIns.getId());
                    StringBuilder stringBuilder = new StringBuilder();
                    for (HistoricIdentityLink identityLink : linksForTask) {
                        // 获选人,候选组/角色(多个)
@@ -1046,7 +1080,7 @@
                    flowTask.setDuration(histIns.getDurationInMillis() == null || histIns.getDurationInMillis() == 0 ? null : getDate(histIns.getDurationInMillis()));
                    //扩展 判断是否超时
                    ProcessCoding processCoding = processCodingMap.get(histIns.getTaskId());
                    ProcessCoding processCoding = processCodingMap.get(histIns.getId());
                    if (processCoding != null) {
                        //通过耗时判断是否是代办节点
                        //如果任务是代办节点
@@ -1065,7 +1099,7 @@
                    // 获取意见评论内容
                    List<Comment> commentList = taskService.getProcessInstanceComments(histIns.getProcessInstanceId());
                    commentList.forEach(comment -> {
                        if (histIns.getTaskId().equals(comment.getTaskId())) {
                        if (histIns.getId().equals(comment.getTaskId())) {
                            flowTask.setComment(FlowCommentDto.builder().type(comment.getType()).comment(comment.getFullMessage()).build());
                        }
                    });
@@ -1406,6 +1440,7 @@
        Map<String, List<FormDetailVO>> map = new HashMap<>(2);
        beforeNodes.stream().forEach(node -> {
            if (node.getCurrent()) {
                node.setTaskId(taskId);
                dataList.add(node);
            } else {
                List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
@@ -1416,6 +1451,7 @@
                        .desc()
                        .list();
                if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                    node.setTaskId(beforeTasks.get(0).getId());
                    List<FormDetailVO> l = map.get(beforeTasks.get(0));
                    if (CollectionUtils.isEmpty(l)) {
                        map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
@@ -1439,9 +1475,9 @@
            }
            // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
            ProcessLogQuery query = new ProcessLogQuery();
            query.setTaskDefKey(node.getUserTask().getId());
            query.setTaskId(node.getTaskId());
            query.setProcessInsId(finalProcessInsId);
            Result result = processLogService.projectProcessLogPage(query);
            Result result = processLogService.projectProcessLogList(query);
            List<ProcessLogVO> logList = (List<ProcessLogVO>) result.get("data");
            DoFormDetailVO vo = new DoFormDetailVO();
            BeanUtils.copyProperties(node, vo);
@@ -1483,6 +1519,7 @@
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
            beforeNodes.stream().forEach(node -> {
                if (node.getCurrent()) {
                    node.setTaskId(taskId);
                    dataList.add(node);
                } else {
                    List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
@@ -1493,6 +1530,7 @@
                            .desc()
                            .list();
                    if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                        node.setTaskId(beforeTasks.get(0).getId());
                        List<FormDetailVO> l = map.get(beforeTasks.get(0));
                        if (CollectionUtils.isEmpty(l)) {
                            map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
@@ -1516,9 +1554,9 @@
                }
                // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
                ProcessLogQuery query = new ProcessLogQuery();
                query.setTaskDefKey(node.getUserTask().getId());
                query.setTaskId(node.getTaskId());
                query.setProcessInsId(hisTask.getProcessInstanceId());
                Result result = processLogService.projectProcessLogPage(query);
                Result result = processLogService.projectProcessLogList(query);
                List<ProcessLogVO> logList = (List<ProcessLogVO>) result.get("data");
                DoFormDetailVO vo = new DoFormDetailVO();
                BeanUtils.copyProperties(node, vo);
@@ -1552,6 +1590,7 @@
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
            beforeNodes.stream().forEach(node -> {
                if (node.getCurrent()) {
                    node.setTaskId(taskId);
                    dataList.add(node);
                } else {
                    List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
@@ -1562,6 +1601,7 @@
                            .desc()
                            .list();
                    if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                        node.setTaskId(beforeTasks.get(0).getId());
                        List<FormDetailVO> l = map.get(beforeTasks.get(0));
                        if (CollectionUtils.isEmpty(l)) {
                            map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
@@ -1585,9 +1625,9 @@
                }
                // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
                ProcessLogQuery query = new ProcessLogQuery();
                query.setTaskDefKey(node.getUserTask().getId());
                query.setTaskId(node.getTaskId());
                query.setProcessInsId(processInsId);
                Result result = processLogService.projectProcessLogPage(query);
                Result result = processLogService.projectProcessLogList(query);
                List<ProcessLogVO> logList = (List<ProcessLogVO>) result.get("data");
                DoFormDetailVO vo = new DoFormDetailVO();
                BeanUtils.copyProperties(node, vo);