Commit 62b271fbf8baefce4bbdc63c3fd802a02414d335

Authored by Lesan
1 parent 1784aab1

feat: 模型的流程数据清理功能

yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/BpmModelController.java
... ... @@ -161,6 +161,15 @@ public class BpmModelController {
161 161 return success(true);
162 162 }
163 163  
  164 + @DeleteMapping("/clean")
  165 + @Operation(summary = "清理模型")
  166 + @Parameter(name = "id", description = "编号", required = true, example = "1024")
  167 + @PreAuthorize("@ss.hasPermission('bpm:model:delete')")
  168 + public CommonResult<Boolean> cleanModel(@RequestParam("id") String id) {
  169 + modelService.cleanModel(getLoginUserId(), id);
  170 + return success(true);
  171 + }
  172 +
164 173 // ========== 仿钉钉/飞书的精简模型 =========
165 174  
166 175 @GetMapping("/simple/get")
... ...
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelService.java
... ... @@ -89,6 +89,14 @@ public interface BpmModelService {
89 89 void deleteModel(Long userId, String id);
90 90  
91 91 /**
  92 + * 清理模型
  93 + *
  94 + * @param userId 用户编号
  95 + * @param id 编号
  96 + */
  97 + void cleanModel(Long userId, String id);
  98 +
  99 + /**
92 100 * 修改模型的状态,实际更新的部署的流程定义的状态
93 101 *
94 102 * @param userId 用户编号
... ...
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelServiceImpl.java
... ... @@ -14,6 +14,7 @@ import cn.iocoder.yudao.module.bpm.convert.definition.BpmModelConvert;
14 14 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
15 15 import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelFormTypeEnum;
16 16 import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelTypeEnum;
  17 +import cn.iocoder.yudao.module.bpm.enums.task.BpmReasonEnum;
17 18 import cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker;
18 19 import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils;
19 20 import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.FlowableUtils;
... ... @@ -25,10 +26,14 @@ import org.flowable.bpmn.model.BpmnModel;
25 26 import org.flowable.bpmn.model.StartEvent;
26 27 import org.flowable.bpmn.model.UserTask;
27 28 import org.flowable.common.engine.impl.db.SuspensionState;
  29 +import org.flowable.engine.HistoryService;
28 30 import org.flowable.engine.RepositoryService;
  31 +import org.flowable.engine.RuntimeService;
  32 +import org.flowable.engine.history.HistoricProcessInstance;
29 33 import org.flowable.engine.repository.Model;
30 34 import org.flowable.engine.repository.ModelQuery;
31 35 import org.flowable.engine.repository.ProcessDefinition;
  36 +import org.flowable.engine.runtime.ProcessInstance;
32 37 import org.springframework.stereotype.Service;
33 38 import org.springframework.transaction.annotation.Transactional;
34 39 import org.springframework.validation.annotation.Validated;
... ... @@ -63,6 +68,11 @@ public class BpmModelServiceImpl implements BpmModelService {
63 68 @Resource
64 69 private BpmTaskCandidateInvoker taskCandidateInvoker;
65 70  
  71 + @Resource
  72 + private HistoryService historyService;
  73 + @Resource
  74 + private RuntimeService runtimeService;
  75 +
66 76 @Override
67 77 public List<Model> getModelList(String name) {
68 78 ModelQuery modelQuery = repositoryService.createModelQuery();
... ... @@ -247,6 +257,28 @@ public class BpmModelServiceImpl implements BpmModelService {
247 257 }
248 258  
249 259 @Override
  260 + public void cleanModel(Long userId, String id) {
  261 + // 1. 校验流程模型存在
  262 + Model model = validateModelManager(id, userId);
  263 +
  264 + // 2. 清理所有流程数据
  265 + // TODO @芋艿:这里没有找到批量操作的方法,会不会有性能问题~
  266 + // 2.1 先取消所有正在运行的流程
  267 + List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery()
  268 + .processDefinitionKey(model.getKey()).list();
  269 + processInstances.forEach(processInstance -> {
  270 + runtimeService.deleteProcessInstance(processInstance.getId(),
  271 + BpmReasonEnum.CANCEL_BY_SYSTEM.getReason());
  272 + });
  273 + // 2.2 再从历史中删除所有相关的流程数据
  274 + List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery()
  275 + .processDefinitionKey(model.getKey()).list();
  276 + historicProcessInstances.forEach(historicProcessInstance -> {
  277 + historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
  278 + });
  279 + }
  280 +
  281 + @Override
250 282 public void updateModelState(Long userId, String id, Integer state) {
251 283 // 1.1 校验流程模型存在
252 284 Model model = validateModelManager(id, userId);
... ...