xiangpei
3 天以前 8d98a3403bd8ba92ef6c29c46d810a085a3dd1be
business/src/main/java/com/ycl/service/impl/FlowTaskServiceImpl.java
@@ -20,6 +20,7 @@
import com.ycl.common.enums.business.TaskStatusEnum;
import com.ycl.common.exception.CustomException;
import com.ycl.common.utils.SecurityUtils;
import com.ycl.constant.ProjectConstant;
import com.ycl.domain.dto.FlowCommentDto;
import com.ycl.domain.dto.FlowNextDto;
import com.ycl.domain.dto.FlowTaskDto;
@@ -28,6 +29,7 @@
import com.ycl.domain.entity.ProcessLog;
import com.ycl.domain.entity.ProjectProcess;
import com.ycl.domain.entity.SysForm;
import com.ycl.domain.form.EditFinishedTaskForm;
import com.ycl.domain.json.RejectData;
import com.ycl.domain.query.ProcessLogQuery;
import com.ycl.domain.vo.*;
@@ -72,6 +74,7 @@
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -108,6 +111,9 @@
    private final ProcessLogService processLogService;
    private final ApplicationEventPublisher publisher;
    private final ProjectProcessMapper projectProcessMapper;
    @Value("${targetIp}")
    private String targetIp;
    /**
     * 完成审核任务
@@ -190,6 +196,7 @@
        }
        return AjaxResult.success("提交成功");
    }
    /**
     * 容缺补交
@@ -1348,6 +1355,7 @@
                    .processInstanceId(procInsId)
                    .finished()
                    .list();
            listFinished = this.distinctHisActivity(listFinished);
            //扩展 获取这个流程实例的监控信息 key:TaskId value:实体类
            Map<String, ProcessCoding> processCodingMap = new LambdaQueryChainWrapper<>(processCodingMapper)
@@ -1411,6 +1419,99 @@
    }
    /**
     * 根据任务key去重历史任务,相同情况下取最新的一条
     *
     * @param hisTaskList
     * @return
     */
    private List<HistoricActivityInstance> distinctHisActivity(List<HistoricActivityInstance> hisTaskList) {
        Map<String, HistoricActivityInstance> uniqueTasks = new HashMap<>();
        for (HistoricActivityInstance task : hisTaskList) {
            String taskDefinitionKey = task.getActivityId();
            HistoricActivityInstance existingTask = uniqueTasks.get(taskDefinitionKey);
            // 如果任务key重复(可能被驳回过,重新提交导致key重复),取最近的一条
            if (existingTask == null || task.getStartTime().after(existingTask.getStartTime())) {
                uniqueTasks.put(taskDefinitionKey, task);
            }
        }
        // 最终去重后的任务列表
        return new ArrayList<>(uniqueTasks.values());
    }
    /**
     * 查询当前任务的表单数据
     *
     * @param taskId
     * @return
     * @throws Exception
     */
    @Override
    public AjaxResult currentFlowTaskForm(String taskId) {
        // 流程变量
        List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
                .finished()
                .taskId(taskId)
                .orderByHistoricTaskInstanceStartTime()
                .desc()
                .list();
        if (CollectionUtils.isEmpty(hisTaskList) || Objects.isNull(hisTaskList.get(0))) {
            return AjaxResult.error("未找到该任务信息");
        }
        HistoricTaskInstance historicTaskInstance = hisTaskList.get(0);
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables().processInstanceId(historicTaskInstance.getProcessInstanceId()).singleResult();
        Map<String, Object> parameters = new HashMap<>();
        if (Objects.nonNull(processInstance)) {
            parameters = processInstance.getProcessVariables();
        } else {
            // 如果为空,表明流程已经结束
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().processInstanceId(historicTaskInstance.getProcessInstanceId()).singleResult();
            if (Objects.isNull(historicProcessInstance)) {
                throw new RuntimeException("流程不存在");
            }
            parameters = historicProcessInstance.getProcessVariables();
        }
        String processInsId = historicTaskInstance.getProcessInstanceId();
        List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters,
                historicTaskInstance.getFormKey(),
                historicTaskInstance.getName(),
                historicTaskInstance.getProcessDefinitionId(),
                historicTaskInstance.getTaskDefinitionKey(),
                Boolean.TRUE, Boolean.FALSE);
        String finalProcessInsId = processInsId;
        List<DoFormDetailVO> vos = beforeNodes.stream()
                .filter(FormDetailVO::getCurrent)
                .map(node -> {
                    // 判断任务是否存在特殊操作(如跳过、转办等),需要在前端展示出来
                    ProcessLogQuery query = new ProcessLogQuery();
                    query.setTaskId(taskId);
                    query.setProcessInsId(finalProcessInsId);
                    Result result = processLogService.projectProcessLogList(query);
                    List<ProcessLogVO> logList = (List<ProcessLogVO>) result.get("data");
                    DoFormDetailVO vo = new DoFormDetailVO();
                    BeanUtils.copyProperties(node, vo);
                    if (CollectionUtils.isNotEmpty(logList)) {
                        vo.setEvents(logList);
                    }
                    // 根据日志判断当前任务的状态
                    if (logList.stream().anyMatch(log -> ProcessLogEventTypeEnum.FINISHED.equals(log.getEventType()))) {
                        vo.setTaskStatus(TaskStatusEnum.FINISHED);
                    } else if (logList.stream().anyMatch(log -> ProcessLogEventTypeEnum.JUMP.equals(log.getEventType()))) {
                        vo.setTaskStatus(TaskStatusEnum.JUMP);
                    } else if (processLogService.taskIsWait(taskId, processInsId)) {
                        vo.setTaskStatus(TaskStatusEnum.WAIT);
                    } else if (processLogService.taskIsHangup(taskId, processInsId)) {
                        vo.setTaskStatus(TaskStatusEnum.HANGUP);
                    }
                    return vo;
        }).collect(Collectors.toList());
        return AjaxResult.success(vos.get(0));
    }
    /**
     * 流程节点表单
     *
     * @param taskId 流程任务编号
@@ -1419,20 +1520,55 @@
    @Override
    public AjaxResult flowTaskForm(String taskId) throws Exception {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        // 流程变量
        Map<String, Object> parameters = new HashMap<>();
        List<FormDetailVO> beforeNodes = new ArrayList<>();
        // 1. 获取到变量信息,因为任务可能是运行中的也可能是历史任务
        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);
        String processDefId = "";
        String formKey = "";
        String taskName = "";
        String taskDefKey = "";
        if (Objects.isNull(task)) {
            List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
                    .finished()
                    .taskId(taskId)
                    .orderByHistoricTaskInstanceStartTime()
                    .desc()
                    .list();
            if (CollectionUtils.isEmpty(hisTaskList) || Objects.isNull(hisTaskList.get(0))) {
                return AjaxResult.error("任务不存在");
            }
            HistoricTaskInstance hisTask = hisTaskList.get(0);
            processInsId = hisTask.getProcessInstanceId();
            processDefId = hisTask.getProcessDefinitionId();
            formKey = hisTask.getFormKey();
            taskName = hisTask.getName();
            taskDefKey = hisTask.getTaskDefinitionKey();
        } else {
            parameters = taskService.getVariables(taskId);
            processInsId = task.getProcessInstanceId();
            beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.FALSE);
            processDefId = task.getProcessDefinitionId();
            formKey = task.getFormKey();
            taskName = task.getName();
            taskDefKey = task.getTaskDefinitionKey();
        }
        // 2. 获取流程变量
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables().processInstanceId(processInsId).singleResult();
        Map<String, Object> parameters = new HashMap<>();
        if (Objects.nonNull(processInstance)) {
            parameters = processInstance.getProcessVariables();
        } else {
            // 如果为空,表明流程已经结束
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().processInstanceId(processInsId).singleResult();
            if (Objects.isNull(historicProcessInstance)) {
                throw new RuntimeException("流程不存在");
            }
            parameters = historicProcessInstance.getProcessVariables();
        }
        List<FormDetailVO> beforeNodes = new ArrayList<>();
        beforeNodes = this.getBeforeNodeForm(parameters,
                formKey,
                taskName,
                processDefId,
                taskDefKey,
                Boolean.TRUE, Boolean.FALSE);
        // 判断前置任务是不是和当前任务为同一个executeId
        // 判断当前任务是否被挂起中
        String finalProcessInsId = processInsId;
@@ -1514,7 +1650,12 @@
            }
            HistoricTaskInstance hisTask = hisTasks.get(0);
            parameters = historicProcessInstance.getProcessVariables();
            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters, hisTask.getFormKey(), hisTask.getName(), hisTask.getProcessDefinitionId(), hisTask.getTaskDefinitionKey(), Boolean.TRUE);
            List<FormDetailVO> beforeNodes = this.getBeforeNodeForm(parameters,
                    hisTask.getFormKey(),
                    hisTask.getName(),
                    hisTask.getProcessDefinitionId(),
                    hisTask.getTaskDefinitionKey(),
                    Boolean.TRUE, Boolean.TRUE);
            List<FormDetailVO> dataList = new ArrayList<>(2);
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
            beforeNodes.stream().forEach(node -> {
@@ -1571,7 +1712,12 @@
            Task task = taskService.createTaskQuery().taskId(taskId).processInstanceId(processInsId).singleResult();
            List<FormDetailVO> beforeNodes = new ArrayList<>();
            if (Objects.nonNull(task)) {
                beforeNodes = this.getBeforeNodeForm(parameters, task.getFormKey(), task.getName(), task.getProcessDefinitionId(), task.getTaskDefinitionKey(), Boolean.TRUE);
                beforeNodes = this.getBeforeNodeForm(parameters,
                        task.getFormKey(),
                        task.getName(),
                        task.getProcessDefinitionId(),
                        task.getTaskDefinitionKey(),
                        Boolean.TRUE, Boolean.TRUE);
            } else {
                List<HistoricTaskInstance> hisTasks = historyService.createHistoricTaskInstanceQuery()
                        .taskId(taskId)
@@ -1584,7 +1730,12 @@
                    throw new RuntimeException("该任务不存在");
                }
                HistoricTaskInstance hisTask = hisTasks.get(0);
                beforeNodes = this.getBeforeNodeForm(parameters, hisTask.getFormKey(), hisTask.getName(), hisTask.getProcessDefinitionId(), hisTask.getTaskDefinitionKey(), Boolean.TRUE);
                beforeNodes = this.getBeforeNodeForm(parameters,
                        hisTask.getFormKey(),
                        hisTask.getName(),
                        hisTask.getProcessDefinitionId(),
                        hisTask.getTaskDefinitionKey(),
                        Boolean.TRUE, Boolean.TRUE);
            }
            List<FormDetailVO> dataList = new ArrayList<>(2);
            Map<String, List<FormDetailVO>> map = new HashMap<>(2);
@@ -1648,9 +1799,16 @@
     * @param taskName      任务
     * @param processDefId  流程定义id
     * @param processDefKey 流程实例id
     * @param disableInput 是否禁用已经有值的输入项
     * @return
     */
    private List<FormDetailVO> getBeforeNodeForm(Map<String, Object> parameters, String formKey, String taskName, String processDefId, String processDefKey, Boolean currentNeedData) {
    private List<FormDetailVO> getBeforeNodeForm(Map<String, Object> parameters,
                                                 String formKey,
                                                 String taskName,
                                                 String processDefId,
                                                 String processDefKey,
                                                 Boolean currentNeedData,
                                                 Boolean disableInput) {
        // 这里只需要查自身以及上一个节点(如果并行的有多个)的表单数据
        List<FormDetailVO> beforeNodes = taskCommonService.getBeforeNodeDefInfo(processDefId, processDefKey, sysFormService, Boolean.TRUE);
@@ -1659,6 +1817,7 @@
        // 处理每个表单的数据
        for (FormDetailVO formDetailVO : beforeNodes) {
            if (formDetailVO.getCurrent() && !currentNeedData) {
                // 当前节点的表单也要处理ip问题
                continue;  // 跳过当前节点,因为当前节点在获取前置节点时已经设置过了(但表单数据没有给)
            }
@@ -1686,37 +1845,41 @@
                    // 设置已填写的表单为禁用状态
                    for (JSONObject oldField : oldFields) {
                        JSONObject options = oldField.getJSONObject("options");
                        options.put("disabled", true);
                        if (disableInput) {
                            options.put("disabled", true);
                        }
                        // 处理文件上传ip问题
                        if ("file-upload".equals(oldField.get("type"))) {
                            options.put("uploadURL", String.format("http://%s:10076/common/upload", this.targetIp));
                        }
                    }
                    formJson.put(ProcessConstants.WIDGET_LIST, oldFields);
                    newP.put(ProcessConstants.TASK_FORM_KEY, formJson);
                    newP.remove(formDetailVO.getBeforeNodeDefId() + "&" + ProcessConstants.TASK_FORM_KEY);
                    // 处理已经上传的文件的ip地址
                    for (String s : newP.keySet()) {
                        if (ProcessConstants.TASK_FORM_KEY.equals(s)) {
                            continue;
                        }
                        if (s.startsWith("fileupload")) {
                            List files = (List) newP.get(s);
                            for (Object file : files) {
                                LinkedHashMap<String, String> fileMap = (LinkedHashMap<String, String>) file;
                                String url = fileMap.get("url");
                                fileMap.put("url", url.replace("42.193.1.25", this.targetIp));
                            }
                        }
                    }
                    formDetailVO.setFormJsonObj(newP);
                }
                // TODO 暂时只处理用户任务上的表单
//                if (StringUtils.isNotBlank(task.getFormKey())) {
//                    SysForm sysForm = sysFormService.selectSysFormById(Long.parseLong(task.getFormKey()));
//                    JSONObject data = JSONObject.parseObject(sysForm.getFormContent());
//                    List<JSONObject> newFields = JSON.parseObject(JSON.toJSONString(data.get(ProcessConstants.WIDGET_LIST)), new TypeReference<List<JSONObject>>() {
//                    });
//                    // 表单回显时 加入子表单信息到流程变量中
//                    for (JSONObject newField : newFields) {
//                        String key = newField.getString("id");
//                        // 处理图片上传组件回显问题
//                        if ("picture-upload".equals(newField.getString("type"))) {
//                            parameters.put(key, new ArrayList<>());
//                        } else {
//                            parameters.put(key, null);
//                        }
//                    }
//                    oldFields.addAll(newFields);
//                }
            }
        }
        return beforeNodes;
    }
    /**
     * 流程节点信息
     *