peng
8 小时以前 d7a0b3b0935a582aa24de76b2901f3f4ee97d1a0
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;
@@ -73,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;
@@ -109,6 +111,9 @@
    private final ProcessLogService processLogService;
    private final ApplicationEventPublisher publisher;
    private final ProjectProcessMapper projectProcessMapper;
    @Value("${targetIp}")
    private String targetIp;
    /**
     * 完成审核任务
@@ -283,11 +288,11 @@
                    // 其它类型的表单数据:如图片、文件单独存表的。历史、运行中的流程需各存一份
                    v.setVAR_TYPE_("serializable");
                    v.setBYTEARRAY_ID_(new StrongUuidGenerator().getNextId());
                    projectProcessMapper.insertByteArray(v.getBYTEARRAY_ID_(), 1, "hist.var-" + key, newV.get(key));
                    projectProcessMapper.insertByteArray(v.getBYTEARRAY_ID_(), 1, "hist.var-" + key, objectToInputStream(newV.get(key)));
                    run.setVAR_TYPE_("serializable");
                    run.setBYTEARRAY_ID_(new StrongUuidGenerator().getNextId());
                    projectProcessMapper.insertByteArray(run.getBYTEARRAY_ID_(), 1, "var-" + key, newV.get(key));
                    projectProcessMapper.insertByteArray(run.getBYTEARRAY_ID_(), 1, "var-" + key, objectToInputStream(newV.get(key)));
                }
                projectProcessMapper.insertHisFlowableVar(v);
@@ -319,6 +324,12 @@
            oos.flush();
            return bos.toByteArray(); // 返回字节数组
        }
    }
    // 将对象转换为 InputStream,适配 BLOB 写入
    public static InputStream objectToInputStream(Object obj) throws IOException {
        byte[] bytes = objectToBytes(obj);
        return Objects.isNull(bytes) ? null : new ByteArrayInputStream(bytes);
    }
    /**
@@ -1445,7 +1456,6 @@
    public AjaxResult currentFlowTaskForm(String taskId) {
        // 流程变量
        List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
                .includeProcessVariables()
                .finished()
                .taskId(taskId)
                .orderByHistoricTaskInstanceStartTime()
@@ -1455,7 +1465,19 @@
            return AjaxResult.error("未找到该任务信息");
        }
        HistoricTaskInstance historicTaskInstance = hisTaskList.get(0);
        Map<String, Object> parameters = historicTaskInstance.getProcessVariables();
        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(),
@@ -1504,36 +1526,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 = "";
        List<HistoricTaskInstance> hisTaskList = historyService.createHistoricTaskInstanceQuery()
                .includeProcessVariables()
                .finished()
                .taskId(taskId)
                .orderByHistoricTaskInstanceStartTime()
                .desc()
                .list();
        if (CollectionUtils.isNotEmpty(hisTaskList) && Objects.nonNull(hisTaskList.get(0))) {
            parameters = hisTaskList.get(0).getProcessVariables();
            processInsId = hisTaskList.get(0).getProcessInstanceId();
            beforeNodes = this.getBeforeNodeForm(parameters,
                    hisTaskList.get(0).getFormKey(),
                    hisTaskList.get(0).getName(),
                    hisTaskList.get(0).getProcessDefinitionId(),
                    hisTaskList.get(0).getTaskDefinitionKey(),
                    Boolean.FALSE, Boolean.TRUE);
        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, Boolean.TRUE);
            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;
@@ -1782,6 +1823,7 @@
        // 处理每个表单的数据
        for (FormDetailVO formDetailVO : beforeNodes) {
            if (formDetailVO.getCurrent() && !currentNeedData) {
                // 当前节点的表单也要处理ip问题
                continue;  // 跳过当前节点,因为当前节点在获取前置节点时已经设置过了(但表单数据没有给)
            }
@@ -1807,41 +1849,81 @@
                if(CollectionUtils.isNotEmpty(oldFields)) {
                    // 设置已填写的表单为禁用状态
                    if (disableInput) {
                        for (JSONObject oldField : oldFields) {
                            JSONObject options = oldField.getJSONObject("options");
                    for (JSONObject oldField : oldFields) {
                        JSONObject options = oldField.getJSONObject("options");
                        if (disableInput) {
                            options.put("disabled", true);
                        }
                    }
                    formJson.put(ProcessConstants.WIDGET_LIST, oldFields);
                    normalizeUploadUrl(formJson);
                    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")) {
                            Object o = newP.get(s);
                            log.info("输出object------------》{}",JSON.toJSONString(o));
                            if (Objects.isNull(o)) {
                                continue;
                            }
                            List files = (List) o;
                            for (Object file : files) {
                                LinkedHashMap<String, String> fileMap = (LinkedHashMap<String, String>) file;
                                String url = fileMap.get("url");
                                if (StringUtils.isBlank(url)) {
                                        continue;
                                }
                                fileMap.put("url", url.replace("42.193.1.25", this.targetIp));
                                if (url.contains("119.6.246.90")){
                                fileMap.put("url", url.replace("119.6.246.90", 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);
//                }
            } else {
                // 当前节点一般使用模板初始化数据,统一在这里改写上传地址,避免旧IP残留
                Map<String, Object> formObj = formDetailVO.getFormJsonObj();
                if (Objects.nonNull(formObj) && Objects.nonNull(formObj.get(ProcessConstants.TASK_FORM_KEY))) {
                    JSONObject formJson = JSONObject.parseObject(JSON.toJSONString(formObj.get(ProcessConstants.TASK_FORM_KEY)));
                    normalizeUploadUrl(formJson);
                    formObj.put(ProcessConstants.TASK_FORM_KEY, formJson);
                    formDetailVO.setFormJsonObj(formObj);
                }
            }
        }
        return beforeNodes;
    }
    private void normalizeUploadUrl(JSONObject formJson) {
        if (Objects.isNull(formJson)) {
            return;
        }
        List<JSONObject> fields = JSON.parseObject(JSON.toJSONString(formJson.get(ProcessConstants.WIDGET_LIST)), new TypeReference<List<JSONObject>>() {
        });
        if (CollectionUtils.isEmpty(fields)) {
            return;
        }
        for (JSONObject field : fields) {
            if (!"file-upload".equals(field.get("type"))) {
                continue;
            }
            JSONObject options = field.getJSONObject("options");
            if (Objects.isNull(options)) {
                continue;
            }
            options.put("uploadURL", String.format("http://%s:10076/common/upload", this.targetIp));
        }
        formJson.put(ProcessConstants.WIDGET_LIST, fields);
    }
    /**
     * 流程节点信息