xiangpei
2025-03-03 ddb766143ae7d04eb193d6f93719582e9f72296c
business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
@@ -23,9 +23,11 @@
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;
import com.ycl.domain.vo.DoFormDetailVO;
import com.ycl.domain.vo.FlowQueryVo;
import com.ycl.domain.vo.FlowTaskVo;
import com.ycl.domain.vo.FormDetailVO;
@@ -68,12 +70,14 @@
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -150,21 +154,19 @@
        if (Objects.isNull(projectProcess)) {
            return AjaxResult.error("项目流程未绑定");
        }
        Map<String, Object> processVariables = new HashMap<>();
        //查出字典中需要注入的字段信息
        // 查出字典中需要注入的字段信息
        List<String> dictList = sysDictDService.selectDictDataByType("flow_variables").stream().map(SysDictData::getDictValue).collect(Collectors.toList());
        Map<String, Object> newV = new HashMap<>(2);
        if (!org.springframework.util.CollectionUtils.isEmpty(variables)) {
            for (String key : variables.keySet()) {
                newV.put(task.getTaskDefinitionKey() + "&" + key, variables.get(key));
                //字典里有就放入流程变量中
                // 字典里有就不做处理
                if (!CollectionUtils.isEmpty(dictList) && dictList.contains(key)) {
                    processVariables.put(key,variables.get(key));
                    newV.put(key,variables.get(key));
                }
            }
        }
        //添加流程变量
        if(!processVariables.isEmpty()) taskService.setVariables(taskId,processVariables);
        taskService.addComment(taskId, task.getProcessInstanceId(), FlowComment.SUBMIT.getType(), "完成提交");
        if (DelegationState.PENDING.equals(task.getDelegationState())) {
            taskService.resolveTask(taskId, newV);
@@ -179,6 +181,7 @@
                projectProcess.getProjectId(),
                projectProcess.getProcessInsId(),
                taskId,
                task.getTaskDefinitionKey(),
                task.getName(),
                ProcessLogEventTypeEnum.FINISHED,
                null));
@@ -320,6 +323,7 @@
                projectProcess.getProjectId(),
                projectProcess.getProcessInsId(),
                flowTaskVo.getTaskId(),
                task.getTaskDefinitionKey(),
                task.getName(),
                ProcessLogEventTypeEnum.REJECT,
                new RejectData(flowTaskVo.getComment())));
@@ -1182,8 +1186,9 @@
                    .list();
            //扩展 获取这个流程实例的监控信息 key:TaskId value:实体类
            Map<String, ProcessCoding> processCodingMap = processCodingMapper
                    .selectList(new QueryWrapper<ProcessCoding>().eq("process_ins_id", procInsId))
            Map<String, ProcessCoding> processCodingMap = new LambdaQueryChainWrapper<>(processCodingMapper)
                    .eq(ProcessCoding::getProcessInsId, procInsId)
                    .list()
                    .stream()
                    .collect(Collectors.toMap(ProcessCoding::getTaskId, Function.identity()));
            // 保存已经完成的流程节点编号
@@ -1248,22 +1253,70 @@
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        // 流程变量
        Map<String, Object> parameters = new HashMap<>();
        List<FormDetailVO> beforeNodes = new ArrayList<>();
        String processInsId = "";
        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().includeProcessVariables().finished().taskId(taskId).singleResult();
        if (Objects.nonNull(historicTaskInstance)) {
            parameters = historicTaskInstance.getProcessVariables();
            processInsId = historicTaskInstance.getProcessInstanceId();
            beforeNodes = this.getBeforeNodeForm(parameters, historicTaskInstance.getFormKey(), historicTaskInstance.getName(), historicTaskInstance.getProcessDefinitionId(), historicTaskInstance.getTaskDefinitionKey(), Boolean.FALSE);
        } else {
            parameters = taskService.getVariables(taskId);
            processInsId = task.getProcessInstanceId();
            beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.FALSE);
        }
        List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.FALSE);
        // 判断前置任务是不是和当前任务为同一个executeId
        // 判断当前任务是否被挂起中
        String finalProcessInsId = processInsId;
        List<FormDetailVO> dataList = new ArrayList<>(2);
        Map<String, List<FormDetailVO>> map = new HashMap<>(2);
        beforeNodes.stream().forEach(node -> {
            if (node.getCurrent()) {
                if (processLogService.taskIsHangup(taskId, task.getProcessInstanceId())) {
                    node.setTaskStatus(TaskStatusEnum.HANGUP);
                dataList.add(node);
            } else {
                List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
                        .processInstanceId(finalProcessInsId)
                        .finished()
                        .taskDefinitionKey(node.getBeforeNodeDefId())
                        .orderByTaskCreateTime()
                        .desc()
                        .list();
                if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                    List<FormDetailVO> l = map.get(beforeTasks.get(0));
                    if (CollectionUtils.isEmpty(l)) {
                        map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
                    } else {
                        l.add(node);
                    }
                }
            }
        });
        return AjaxResult.success(beforeNodes);
        for (String key : map.keySet()) {
            if (StringUtils.isNotBlank(key)) {
                // 同一执行器上只取最近的一个
                dataList.add(map.get(key).get(0));
            }
        }
        List<DoFormDetailVO> vos = dataList.stream().map(node -> {
            if (node.getCurrent()) {
                if (processLogService.taskIsHangup(taskId, finalProcessInsId)) {
                    node.setTaskStatus(TaskStatusEnum.HANGUP);
                }
            }
            // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
            List<ProcessLog> logList = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                    .eq(ProcessLog::getTaskDefKey, node.getBeforeNodeDefId())
                    .eq(ProcessLog::getProcessInsId, finalProcessInsId)
                    .orderByDesc(ProcessLog::getGmtCreate)
                    .list();
            DoFormDetailVO vo = new DoFormDetailVO();
            BeanUtils.copyProperties(node, vo);
            if (CollectionUtils.isNotEmpty(logList)) {
                vo.setEvents(logList);
            }
            return vo;
        }).collect(Collectors.toList());
        return AjaxResult.success(vos);
    }
    @Override
@@ -1273,17 +1326,120 @@
        Map<String, Object> parameters = new HashMap<>();
        if (Objects.isNull(task)) {
            // 如果为空,可能是任务已经结束
            HistoricTaskInstance hisTask = historyService.createHistoricTaskInstanceQuery().taskId(taskId).includeProcessVariables().singleResult();
            if (Objects.isNull(hisTask)) {
            List<HistoricTaskInstance> hisTasks = historyService.createHistoricTaskInstanceQuery()
                    .taskId(taskId)
                    .finished()
                    .includeProcessVariables()
                    .orderByTaskCreateTime()
                    .desc()
                    .list();
            if (CollectionUtils.isNotEmpty(hisTasks) && Objects.isNull(hisTasks.get(0))) {
                throw new RuntimeException("该任务不存在");
            }
            HistoricTaskInstance hisTask = hisTasks.get(0);
            parameters = hisTask.getProcessVariables();
            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, hisTask.getFormKey(), hisTask.getName(), hisTask.getProcessDefinitionId(), hisTask.getTaskDefinitionKey(), Boolean.TRUE);
            return AjaxResult.success(beforeNodes);
            List<FormDetailVO> dataList = new ArrayList<>(2);
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
            beforeNodes.stream().forEach(node -> {
                if (node.getCurrent()) {
                    dataList.add(node);
                } else {
                    List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
                            .processInstanceId(hisTask.getProcessInstanceId())
                            .finished()
                            .taskDefinitionKey(node.getBeforeNodeDefId())
                            .orderByTaskCreateTime()
                            .desc()
                            .list();
                    if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                        List<FormDetailVO> l = map.get(beforeTasks.get(0));
                        if (CollectionUtils.isEmpty(l)) {
                            map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
                        } else {
                            l.add(node);
                        }
                    }
                }
            });
            for (String key : map.keySet()) {
                if (StringUtils.isNotBlank(key)) {
                    // 同一执行器上只取最近的一个
                    dataList.add(map.get(key).get(0));
                }
            }
            List<DoFormDetailVO> vos = dataList.stream().map(node -> {
                if (node.getCurrent()) {
                    if (processLogService.taskIsHangup(taskId, hisTask.getProcessInstanceId())) {
                        node.setTaskStatus(TaskStatusEnum.HANGUP);
                    }
                }
                // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
                List<ProcessLog> logList = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                        .eq(ProcessLog::getTaskId, hisTask.getId())
                        .eq(ProcessLog::getProcessInsId, hisTask.getProcessInstanceId())
                        .orderByDesc(ProcessLog::getGmtCreate)
                        .list();
                DoFormDetailVO vo = new DoFormDetailVO();
                BeanUtils.copyProperties(node, vo);
                if (CollectionUtils.isNotEmpty(logList)) {
                    vo.setEvents(logList);
                }
                return vo;
            }).collect(Collectors.toList());
            return AjaxResult.success(vos);
        } else {
            parameters = taskService.getVariables(taskId);
            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.TRUE);
            return AjaxResult.success(beforeNodes);
            List<FormDetailVO> dataList = new ArrayList<>(2);
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
            beforeNodes.stream().forEach(node -> {
                if (node.getCurrent()) {
                    dataList.add(node);
                } else {
                    List<HistoricTaskInstance> beforeTasks = historyService.createHistoricTaskInstanceQuery()
                            .processInstanceId(task.getProcessInstanceId())
                            .finished()
                            .taskDefinitionKey(node.getBeforeNodeDefId())
                            .orderByTaskCreateTime()
                            .desc()
                            .list();
                    if (CollectionUtils.isNotEmpty(beforeTasks) && Objects.nonNull(beforeTasks.get(0))) {
                        List<FormDetailVO> l = map.get(beforeTasks.get(0));
                        if (CollectionUtils.isEmpty(l)) {
                            map.put(beforeTasks.get(0).getExecutionId(), Arrays.asList(node));
                        } else {
                            l.add(node);
                        }
                    }
                }
            });
            for (String key : map.keySet()) {
                if (StringUtils.isNotBlank(key)) {
                    // 同一执行器上只取最近的一个
                    dataList.add(map.get(key).get(0));
                }
            }
            List<DoFormDetailVO> vos = dataList.stream().map(node -> {
                if (node.getCurrent()) {
                    if (processLogService.taskIsHangup(taskId, task.getProcessInstanceId())) {
                        node.setTaskStatus(TaskStatusEnum.HANGUP);
                    }
                }
                // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
                List<ProcessLog> logList = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                        .eq(ProcessLog::getTaskId, task.getId())
                        .eq(ProcessLog::getProcessInsId, task.getProcessInstanceId())
                        .orderByDesc(ProcessLog::getGmtCreate)
                        .list();
                DoFormDetailVO vo = new DoFormDetailVO();
                BeanUtils.copyProperties(node, vo);
                if (CollectionUtils.isNotEmpty(logList)) {
                    vo.setEvents(logList);
                }
                return vo;
            }).collect(Collectors.toList());
            return AjaxResult.success(vos);
        }
    }