xiangpei
2025-04-07 80662b34fe93b4ede00c7fc03fbd9f01355c94e2
business/src/main/java/com/ycl/service/impl/ProjectProcessServiceImpl.java
@@ -8,6 +8,7 @@
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.ycl.common.constant.ProcessConstants;
import com.ycl.common.constant.ProcessOverTimeConstants;
import com.ycl.common.core.domain.AjaxResult;
import com.ycl.common.core.domain.entity.SysDept;
import com.ycl.common.core.domain.entity.SysDictData;
import com.ycl.common.core.domain.entity.SysRole;
@@ -40,6 +41,7 @@
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.persistence.StrongUuidGenerator;
import org.flowable.common.engine.impl.util.CollectionUtil;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
@@ -53,6 +55,7 @@
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 lombok.RequiredArgsConstructor;
@@ -2929,4 +2932,207 @@
    private List<Task> getCurrentNodeTaskList(String processInstanceId) {
        return taskService.createTaskQuery().processDefinitionId(processInstanceId).list();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result editFinishedTask(EditFinishedTaskForm form) {
        List<HistoricTaskInstance> hisTasks = historyService.createHistoricTaskInstanceQuery().taskId(form.getTaskId()).orderByHistoricTaskInstanceStartTime().desc().list();
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(hisTasks) || Objects.isNull(hisTasks.get(0))) {
            return Result.error("任务不存在");
        }
        HistoricTaskInstance task = hisTasks.get(0);
        ProjectProcess projectProcess = new LambdaQueryChainWrapper<>(projectProcessMapper)
                .eq(ProjectProcess::getProcessInsId, task.getProcessInstanceId())
                .eq(ProjectProcess::getProcessDefId, task.getProcessDefinitionId())
                .one();
        if (Objects.isNull(projectProcess)) {
            return Result.error("项目流程未绑定");
        }
        // 查出字典中需要注入的字段信息
        List<String> dictList = dictTypeService.selectDictDataByType("flow_variables").stream().map(SysDictData::getDictValue).collect(Collectors.toList());
        Map<String, Object> newV = new HashMap<>(2);
        if (!org.springframework.util.CollectionUtils.isEmpty(form.getVariables())) {
            for (String key : form.getVariables().keySet()) {
                newV.put(task.getTaskDefinitionKey() + "&" + key, form.getVariables().get(key));
                // 字典里有就不做处理
                if (!org.apache.commons.collections4.CollectionUtils.isEmpty(dictList) && dictList.contains(key)) {
                    newV.put(key,form.getVariables().get(key));
                }
            }
        }
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
        Date now = new Date();
        // 1. 表单变量替换
        for (String key : newV.keySet()) {
            FlowableVarVO var = null;
            if (Objects.isNull(processInstance)) {
                // 查询历史流程变量
                var = projectProcessMapper.getHisByteId(projectProcess.getProcessInsId(), key);
            } else {
                // 查询运行时流程变量
                var = projectProcessMapper.getRuByteId(projectProcess.getProcessInsId(), key);
            }
            if (Objects.nonNull(var)) {
                // 先删除原来的变量,再把当前的保存进去
                projectProcessMapper.deleteHisFlowableVar(var.getID_());
                projectProcessMapper.deleteRunFlowableVar(var.getID_());
                if (StringUtils.isNotBlank(var.getBYTEARRAY_ID_())) {
                    projectProcessMapper.deleteByteArray(var.getID_());
                }
            }
            // 保存当前的表单数据
            FlowableVarVO run = new FlowableVarVO();
            FlowableVarVO v = new FlowableVarVO();
            v.setNAME_(key);
            v.setPROC_INST_ID_(task.getProcessInstanceId());
            v.setEXECUTION_ID_(task.getProcessInstanceId());
            v.setCREATE_TIME_(now);
            v.setLAST_UPDATED_TIME_(now);
            v.setID_(new StrongUuidGenerator().getNextId());
            BeanUtils.copyProperties(v, run);
            run.setID_(new StrongUuidGenerator().getNextId());
            if (newV.get(key) instanceof String) {
                v.setVAR_TYPE_("string");
                v.setTEXT_((String) newV.get(key));
                run.setVAR_TYPE_("string");
                run.setTEXT_((String) newV.get(key));
            } else if (newV.get(key) instanceof Integer) {
                v.setVAR_TYPE_("integer");
                v.setLONG_((Long) newV.get(key));
                v.setTEXT_((String) newV.get(key));
                run.setVAR_TYPE_("integer");
                run.setLONG_((Long) newV.get(key));
                run.setTEXT_((String) newV.get(key));
            } else if (newV.get(key) instanceof Long) {
                v.setVAR_TYPE_("long");
                v.setLONG_((Long) newV.get(key));
                v.setTEXT_((String) newV.get(key));
                run.setVAR_TYPE_("long");
                run.setLONG_((Long) newV.get(key));
                run.setTEXT_((String) newV.get(key));
            } else if (newV.get(key) instanceof Boolean) {
                v.setVAR_TYPE_("boolean");
                v.setLONG_((Long) newV.get(key));
                v.setTEXT_((String) newV.get(key));
                run.setVAR_TYPE_("boolean");
                run.setLONG_((Long) newV.get(key));
                run.setTEXT_((String) newV.get(key));
            } else {
                // 其它类型的表单数据:如图片、文件单独存表的。历史、运行中的流程需各存一份
                v.setVAR_TYPE_("serializable");
                v.setBYTEARRAY_ID_(new StrongUuidGenerator().getNextId());
                projectProcessMapper.insertByteArray(v.getBYTEARRAY_ID_(), 1, "hist.var-" + key, 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.insertHisFlowableVar(v);
            projectProcessMapper.insertRunFlowableVar(run);
        }
        // 2. 任务状态调整:仅支持 已完成==》容缺/跳过   容缺《===》跳过
        if (Objects.nonNull(form.getNowStatus()) && ! Objects.equals(form.getOriginalStatus(), form.getNowStatus())) {
            if (TaskStatusEnum.FINISHED.equals(form.getOriginalStatus())) {
                ProcessLog finishedLog = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                        .eq(ProcessLog::getTaskId, form.getTaskId())
                        .eq(ProcessLog::getEventType, ProcessLogEventTypeEnum.FINISHED)
                        .one();
                if (Objects.nonNull(finishedLog)) {
                    if (TaskStatusEnum.JUMP.equals(form.getNowStatus())) {
                        // 添加跳过日志
                        publisher.publishEvent(new TaskLogEvent(this, null,
                                finishedLog.getUserId(),
                                projectProcess.getProjectId(),
                                projectProcess.getProcessInsId(),
                                form.getTaskId(),
                                task.getTaskDefinitionKey(),
                                task.getName(),
                                ProcessLogEventTypeEnum.JUMP,
                                new JumpData(form.getTaskJumpForm().getDesc())));
                        // 删除原来的完成日志
                        processLogService.removeById(finishedLog.getId());
                    } else if (TaskStatusEnum.WAIT.equals(form.getNowStatus())) {
                        // 添加容缺日志
                        publisher.publishEvent(new TaskLogEvent(this, null,
                                finishedLog.getUserId(),
                                projectProcess.getProjectId(),
                                projectProcess.getProcessInsId(),
                                form.getTaskId(),
                                task.getTaskDefinitionKey(),
                                task.getName(),
                                ProcessLogEventTypeEnum.WAIT,
                                new WaitData(form.getTaskWaitForm().getDesc())));
                        // 删除原来的完成日志
                        processLogService.removeById(finishedLog.getId());
                    } else {
                        return Result.error("已完成任务只能调整为容缺或跳过状态");
                    }
                }
            }
            else if (TaskStatusEnum.WAIT.equals(form.getOriginalStatus())) {
                ProcessLog waitLog = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                        .eq(ProcessLog::getTaskId, form.getTaskId())
                        .eq(ProcessLog::getEventType, ProcessLogEventTypeEnum.WAIT)
                        .one();
                if (Objects.nonNull(waitLog)) {
                    if (TaskStatusEnum.JUMP.equals(form.getNowStatus())) {
                        // 添加跳过日志
                        publisher.publishEvent(new TaskLogEvent(this, null,
                                waitLog.getUserId(),
                                projectProcess.getProjectId(),
                                projectProcess.getProcessInsId(),
                                form.getTaskId(),
                                task.getTaskDefinitionKey(),
                                task.getName(),
                                ProcessLogEventTypeEnum.JUMP,
                                new JumpData(form.getTaskJumpForm().getDesc())));
                        // 删除原来的容缺日志
                        processLogService.removeById(waitLog.getId());
                    } else {
                        return Result.error("容缺任务只能调整为跳过状态");
                    }
                }
            } else if (TaskStatusEnum.JUMP.equals(form.getOriginalStatus())) {
                ProcessLog jumpLog = new LambdaQueryChainWrapper<>(processLogService.getBaseMapper())
                        .eq(ProcessLog::getTaskId, form.getTaskId())
                        .eq(ProcessLog::getEventType, ProcessLogEventTypeEnum.JUMP)
                        .one();
                if (Objects.nonNull(jumpLog)) {
                    if (TaskStatusEnum.WAIT.equals(form.getNowStatus())) {
                        // 添加容缺日志
                        publisher.publishEvent(new TaskLogEvent(this, null,
                                jumpLog.getUserId(),
                                projectProcess.getProjectId(),
                                projectProcess.getProcessInsId(),
                                form.getTaskId(),
                                task.getTaskDefinitionKey(),
                                task.getName(),
                                ProcessLogEventTypeEnum.WAIT,
                                new WaitData(form.getTaskWaitForm().getDesc())));
                        // 删除原来的跳过日志
                        processLogService.removeById(jumpLog.getId());
                    } else {
                        return Result.error("跳过任务只能调整为容缺任务");
                    }
                }
            }
        }
        // 3. 保存修改日志
        publisher.publishEvent(new TaskLogEvent(this, null,
                SecurityUtils.getUserId(),
                projectProcess.getProjectId(),
                projectProcess.getProcessInsId(),
                form.getTaskId(),
                task.getTaskDefinitionKey(),
                task.getName(),
                ProcessLogEventTypeEnum.EDIT,
                null));
        return Result.ok("修改成功");
    }
}