Просмотр исходного кода

update 优化流程实例接口全部按照业务id操作

gssong 2 лет назад
Родитель
Сommit
9175b7a4c1

+ 26 - 26
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/controller/ActProcessInstanceController.java

@@ -58,33 +58,33 @@ public class ActProcessInstanceController extends BaseController {
     }
 
     /**
-     * 通过流程实例id获取历史流程图
+     * 通过业务id获取历史流程图
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
-    @GetMapping("/getHistoryImage/{processInstanceId}")
-    public R<String> getHistoryImage(@NotBlank(message = "流程实例id不能为空") @PathVariable String processInstanceId) {
-        return R.ok("操作成功", actProcessInstanceService.getHistoryImage(processInstanceId));
+    @GetMapping("/getHistoryImage/{businessKey}")
+    public R<String> getHistoryImage(@NotBlank(message = "业务id不能为空") @PathVariable String businessKey) {
+        return R.ok("操作成功", actProcessInstanceService.getHistoryImage(businessKey));
     }
 
     /**
-     * 通过流程实例id获取历史流程图运行中,历史等节点
+     * 通过业务id获取历史流程图运行中,历史等节点
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
-    @GetMapping("/getHistoryList/{processInstanceId}")
-    public R<Map<String, Object>> getHistoryList(@NotBlank(message = "流程实例id不能为空") @PathVariable String processInstanceId) {
-        return R.ok("操作成功", actProcessInstanceService.getHistoryList(processInstanceId));
+    @GetMapping("/getHistoryList/{businessKey}")
+    public R<Map<String, Object>> getHistoryList(@NotBlank(message = "业务id不能为空") @PathVariable String businessKey) {
+        return R.ok("操作成功", actProcessInstanceService.getHistoryList(businessKey));
     }
 
     /**
      * 获取审批记录
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
-    @GetMapping("/getHistoryRecord/{processInstanceId}")
-    public R<List<ActHistoryInfoVo>> getHistoryRecord(@NotBlank(message = "流程实例id不能为空") @PathVariable String processInstanceId) {
-        return R.ok(actProcessInstanceService.getHistoryRecord(processInstanceId));
+    @GetMapping("/getHistoryRecord/{businessKey}")
+    public R<List<ActHistoryInfoVo>> getHistoryRecord(@NotBlank(message = "业务id不能为空") @PathVariable String businessKey) {
+        return R.ok(actProcessInstanceService.getHistoryRecord(businessKey));
     }
 
     /**
@@ -102,37 +102,37 @@ public class ActProcessInstanceController extends BaseController {
     /**
      * 运行中的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
+     * @param businessKeys 业务id
      */
     @Log(title = "流程实例管理", businessType = BusinessType.DELETE)
     @RepeatSubmit()
-    @DeleteMapping("/deleteRunAndHisInstance/{processInstanceIds}")
-    public R<Void> deleteRunAndHisInstance(@NotNull(message = "流程实例id不能为空") @PathVariable String[] processInstanceIds) {
-        return toAjax(actProcessInstanceService.deleteRunAndHisInstance(Arrays.asList(processInstanceIds)));
+    @DeleteMapping("/deleteRunAndHisInstance/{businessKeys}")
+    public R<Void> deleteRunAndHisInstance(@NotNull(message = "业务id不能为空") @PathVariable String[] businessKeys) {
+        return toAjax(actProcessInstanceService.deleteRunAndHisInstance(Arrays.asList(businessKeys)));
     }
 
     /**
      * 已完成的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
+     * @param businessKeys 业务id
      */
     @Log(title = "流程实例管理", businessType = BusinessType.DELETE)
     @RepeatSubmit()
-    @DeleteMapping("/deleteFinishAndHisInstance/{processInstanceIds}")
-    public R<Void> deleteFinishAndHisInstance(@NotNull(message = "流程实例id不能为空") @PathVariable String[] processInstanceIds) {
-        return toAjax(actProcessInstanceService.deleteFinishAndHisInstance(Arrays.asList(processInstanceIds)));
+    @DeleteMapping("/deleteFinishAndHisInstance/{businessKeys}")
+    public R<Void> deleteFinishAndHisInstance(@NotNull(message = "业务id不能为空") @PathVariable String[] businessKeys) {
+        return toAjax(actProcessInstanceService.deleteFinishAndHisInstance(Arrays.asList(businessKeys)));
     }
 
     /**
      * 撤销流程申请
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
     @Log(title = "流程实例管理", businessType = BusinessType.INSERT)
     @RepeatSubmit()
-    @PostMapping("/cancelProcessApply/{processInstanceId}")
-    public R<Void> cancelProcessApply(@NotBlank(message = "流程实例id不能为空") @PathVariable String processInstanceId) {
-        return toAjax(actProcessInstanceService.cancelProcessApply(processInstanceId));
+    @PostMapping("/cancelProcessApply/{businessKey}")
+    public R<Void> cancelProcessApply(@NotBlank(message = "业务id不能为空") @PathVariable String businessKey) {
+        return toAjax(actProcessInstanceService.cancelProcessApply(businessKey));
     }
 
     /**

+ 3 - 3
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/domain/bo/ProcessInvalidBo.java

@@ -19,10 +19,10 @@ public class ProcessInvalidBo implements Serializable {
     private static final long serialVersionUID = 1L;
 
     /**
-     * 流程实例id
+     * 业务id
      */
-    @NotBlank(message = "流程实例id不能为空", groups = {AddGroup.class})
-    private String processInstanceId;
+    @NotBlank(message = "业务id不能为空", groups = {AddGroup.class})
+    private String businessKey;
 
     /**
      * 作废原因

+ 12 - 20
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/IActProcessInstanceService.java

@@ -20,18 +20,18 @@ public interface IActProcessInstanceService {
     /**
      * 通过流程实例id获取历史流程图
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 流程实例id
      * @return 结果
      */
-    String getHistoryImage(String processInstanceId);
+    String getHistoryImage(String businessKey);
 
     /**
-     * 通过流程实例id获取历史流程图运行中,历史等节点
+     * 通过业务id获取历史流程图运行中,历史等节点
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      * @return 结果
      */
-    Map<String, Object> getHistoryList(String processInstanceId);
+    Map<String, Object> getHistoryList(String businessKey);
 
     /**
      * 分页查询正在运行的流程实例
@@ -54,10 +54,10 @@ public interface IActProcessInstanceService {
     /**
      * 获取审批记录
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      * @return 结果
      */
-    List<ActHistoryInfoVo> getHistoryRecord(String processInstanceId);
+    List<ActHistoryInfoVo> getHistoryRecord(String businessKey);
 
     /**
      * 作废流程实例,不会删除历史记录(删除运行中的实例)
@@ -70,34 +70,26 @@ public interface IActProcessInstanceService {
     /**
      * 运行中的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
-     * @return 结果
-     */
-    boolean deleteRunAndHisInstance(List<String> processInstanceIds);
-
-    /**
-     * 按照业务id删除 运行中的实例 删除程实例,删除历史记录,删除业务与流程关联信息
-     *
      * @param businessKeys 业务id
      * @return 结果
      */
-    boolean deleteRunAndHisInstanceByBusinessKeys(List<String> businessKeys);
+    boolean deleteRunAndHisInstance(List<String> businessKeys);
 
     /**
      * 已完成的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
+     * @param businessKeys 业务id
      * @return 结果
      */
-    boolean deleteFinishAndHisInstance(List<String> processInstanceIds);
+    boolean deleteFinishAndHisInstance(List<String> businessKeys);
 
     /**
      * 撤销流程申请
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      * @return 结果
      */
-    boolean cancelProcessApply(String processInstanceId);
+    boolean cancelProcessApply(String businessKey);
 
     /**
      * 分页查询当前登录人单据

+ 35 - 53
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/ActProcessInstanceServiceImpl.java

@@ -184,28 +184,28 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     }
 
     /**
-     * 通过流程实例id获取历史流程图
+     * 通过业务id获取历史流程图
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
     @SneakyThrows
     @Override
-    public String getHistoryImage(String processInstanceId) {
+    public String getHistoryImage(String businessKey) {
         String processDefinitionId;
         // 获取当前的流程实例
-        ProcessInstance processInstance = QueryUtils.instanceQuery(processInstanceId).singleResult();
+        ProcessInstance processInstance = QueryUtils.businessKeyQuery(businessKey).singleResult();
         // 如果流程已经结束,则得到结束节点
         if (Objects.isNull(processInstance)) {
-            HistoricProcessInstance pi = QueryUtils.hisInstanceQuery(processInstanceId).singleResult();
+            HistoricProcessInstance pi = QueryUtils.hisInstanceQuery().processInstanceBusinessKey(businessKey).singleResult();
             processDefinitionId = pi.getProcessDefinitionId();
         } else {
             // 根据流程实例ID获得当前处于活动状态的ActivityId合集
-            ProcessInstance pi = QueryUtils.instanceQuery(processInstanceId).singleResult();
+            ProcessInstance pi = QueryUtils.instanceQuery(processInstance.getProcessInstanceId()).singleResult();
             processDefinitionId = pi.getProcessDefinitionId();
         }
 
         // 获得活动的节点
-        List<HistoricActivityInstance> highLightedFlowList = QueryUtils.hisActivityInstanceQuery(processInstanceId).orderByHistoricActivityInstanceStartTime().asc().list();
+        List<HistoricActivityInstance> highLightedFlowList = QueryUtils.hisActivityInstanceQuery(processInstance.getProcessInstanceId()).orderByHistoricActivityInstanceStartTime().asc().list();
 
         List<String> highLightedFlows = new ArrayList<>();
         List<String> highLightedNodes = new ArrayList<>();
@@ -240,15 +240,16 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     }
 
     /**
-     * 通过流程实例id获取历史流程图运行中,历史等节点
+     * 通过业务id获取历史流程图运行中,历史等节点
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
     @Override
-    public Map<String, Object> getHistoryList(String processInstanceId) {
+    public Map<String, Object> getHistoryList(String businessKey) {
         Map<String, Object> map = new HashMap<>();
         List<Map<String, Object>> taskList = new ArrayList<>();
-        HistoricProcessInstance historicProcessInstance = QueryUtils.hisInstanceQuery(processInstanceId).singleResult();
+        HistoricProcessInstance historicProcessInstance = QueryUtils.hisBusinessKeyQuery(businessKey).singleResult();
+        String processInstanceId = historicProcessInstance.getId();
         StringBuilder xml = new StringBuilder();
         ProcessDefinition processDefinition = repositoryService.getProcessDefinition(historicProcessInstance.getProcessDefinitionId());
         // 获取节点
@@ -333,13 +334,15 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     /**
      * 获取审批记录
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
     @Override
-    public List<ActHistoryInfoVo> getHistoryRecord(String processInstanceId) {
+    public List<ActHistoryInfoVo> getHistoryRecord(String businessKey) {
         // 查询任务办理记录
-        List<HistoricTaskInstance> list = QueryUtils.hisTaskInstanceQuery(processInstanceId).orderByHistoricTaskInstanceEndTime().desc().list();
+        List<HistoricTaskInstance> list = QueryUtils.hisTaskBusinessKeyQuery(businessKey).orderByHistoricTaskInstanceEndTime().desc().list();
         list = StreamUtils.sorted(list, Comparator.comparing(HistoricTaskInstance::getEndTime, Comparator.nullsFirst(Date::compareTo)).reversed());
+        HistoricProcessInstance historicProcessInstance = QueryUtils.hisBusinessKeyQuery(businessKey).singleResult();
+        String processInstanceId = historicProcessInstance.getId();
         List<ActHistoryInfoVo> actHistoryInfoVoList = new ArrayList<>();
         List<Comment> processInstanceComments = taskService.getProcessInstanceComments(processInstanceId);
         //附件
@@ -440,7 +443,8 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     @Transactional(rollbackFor = Exception.class)
     public boolean deleteRunInstance(ProcessInvalidBo processInvalidBo) {
         try {
-            List<Task> list = QueryUtils.taskQuery(processInvalidBo.getProcessInstanceId()).list();
+            List<Task> list = QueryUtils.taskQuery().processInstanceBusinessKey(processInvalidBo.getBusinessKey()).list();
+            String processInstanceId = list.get(0).getProcessInstanceId();
             List<Task> subTasks = StreamUtils.filter(list, e -> StringUtils.isNotBlank(e.getParentTaskId()));
             if (CollUtil.isNotEmpty(subTasks)) {
                 subTasks.forEach(e -> taskService.deleteTask(e.getId()));
@@ -452,10 +456,10 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
             for (Task task : StreamUtils.filter(list, e -> StringUtils.isBlank(e.getParentTaskId()))) {
                 taskService.addComment(task.getId(), task.getProcessInstanceId(), TaskStatusEnum.INVALID.getStatus(), deleteReason);
             }
-            HistoricProcessInstance historicProcessInstance = QueryUtils.hisInstanceQuery(processInvalidBo.getProcessInstanceId()).singleResult();
+            HistoricProcessInstance historicProcessInstance = QueryUtils.hisInstanceQuery(processInstanceId).singleResult();
             BusinessStatusEnum.checkInvalidStatus(historicProcessInstance.getBusinessStatus());
-            runtimeService.updateBusinessStatus(processInvalidBo.getProcessInstanceId(), BusinessStatusEnum.INVALID.getStatus());
-            runtimeService.deleteProcessInstance(processInvalidBo.getProcessInstanceId(), deleteReason);
+            runtimeService.updateBusinessStatus(processInstanceId, BusinessStatusEnum.INVALID.getStatus());
+            runtimeService.deleteProcessInstance(processInstanceId, deleteReason);
             FlowProcessEventHandler processHandler = flowEventStrategy.getProcessHandler(historicProcessInstance.getProcessDefinitionKey());
             if (processHandler != null) {
                 processHandler.handleProcess(historicProcessInstance.getBusinessKey(), BusinessStatusEnum.INVALID.getStatus(), false);
@@ -470,40 +474,11 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     /**
      * 运行中的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
-     */
-    @Override
-    @Transactional(rollbackFor = Exception.class)
-    public boolean deleteRunAndHisInstance(List<String> processInstanceIds) {
-        try {
-            // 1.删除运行中流程实例
-            List<Task> list = QueryUtils.taskQuery(processInstanceIds).list();
-            List<Task> subTasks = StreamUtils.filter(list, e -> StringUtils.isNotBlank(e.getParentTaskId()));
-            if (CollUtil.isNotEmpty(subTasks)) {
-                subTasks.forEach(e -> taskService.deleteTask(e.getId()));
-            }
-            runtimeService.bulkDeleteProcessInstances(processInstanceIds, LoginHelper.getUserId() + "删除了当前流程申请");
-            // 2.删除历史记录
-            List<HistoricProcessInstance> historicProcessInstanceList = QueryUtils.hisInstanceQuery(new HashSet<>(processInstanceIds)).list();
-            if (ObjectUtil.isNotEmpty(historicProcessInstanceList)) {
-                historyService.bulkDeleteHistoricProcessInstances(processInstanceIds);
-            }
-            wfTaskBackNodeService.deleteByInstanceIds(processInstanceIds);
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new ServiceException(e.getMessage());
-        }
-    }
-
-    /**
-     * 按照业务id删除 运行中的实例 删除程实例,删除历史记录,删除业务与流程关联信息
-     *
      * @param businessKeys 业务id
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean deleteRunAndHisInstanceByBusinessKeys(List<String> businessKeys) {
+    public boolean deleteRunAndHisInstance(List<String> businessKeys) {
         try {
             // 1.删除运行中流程实例
             List<ActHiProcinst> actHiProcinsts = actHiProcinstService.selectByBusinessKeyIn(businessKeys);
@@ -534,12 +509,18 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     /**
      * 已完成的实例 删除程实例,删除历史记录,删除业务与流程关联信息
      *
-     * @param processInstanceIds 流程实例id
+     * @param businessKeys 业务id
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean deleteFinishAndHisInstance(List<String> processInstanceIds) {
+    public boolean deleteFinishAndHisInstance(List<String> businessKeys) {
         try {
+            List<ActHiProcinst> actHiProcinsts = actHiProcinstService.selectByBusinessKeyIn(businessKeys);
+            if (CollUtil.isEmpty(actHiProcinsts)) {
+                log.warn("当前业务ID:{}查询到流程实例为空!", businessKeys);
+                return false;
+            }
+            List<String> processInstanceIds = StreamUtils.toList(actHiProcinsts, ActHiProcinst::getId);
             historyService.bulkDeleteHistoricProcessInstances(processInstanceIds);
             wfTaskBackNodeService.deleteByInstanceIds(processInstanceIds);
             return true;
@@ -552,13 +533,13 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
     /**
      * 撤销流程申请
      *
-     * @param processInstanceId 流程实例id
+     * @param businessKey 业务id
      */
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean cancelProcessApply(String processInstanceId) {
+    public boolean cancelProcessApply(String businessKey) {
         try {
-            ProcessInstance processInstance = QueryUtils.instanceQuery(processInstanceId)
+            ProcessInstance processInstance = QueryUtils.businessKeyQuery(businessKey)
                 .startedBy(String.valueOf(LoginHelper.getUserId())).singleResult();
             if (ObjectUtil.isNull(processInstance)) {
                 throw new ServiceException("您不是流程发起人,撤销失败!");
@@ -566,6 +547,7 @@ public class ActProcessInstanceServiceImpl implements IActProcessInstanceService
             if (processInstance.isSuspended()) {
                 throw new ServiceException(FlowConstant.MESSAGE_SUSPENDED);
             }
+            String processInstanceId = processInstance.getId();
             BusinessStatusEnum.checkCancelStatus(processInstance.getBusinessStatus());
             List<Task> taskList = QueryUtils.taskQuery(processInstanceId).list();
             for (Task task : taskList) {

+ 1 - 1
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/TestLeaveServiceImpl.java

@@ -115,7 +115,7 @@ public class TestLeaveServiceImpl implements ITestLeaveService {
     @Transactional(rollbackFor = Exception.class)
     public Boolean deleteWithValidByIds(Collection<Long> ids) {
         List<String> idList = StreamUtils.toList(ids, String::valueOf);
-        actProcessInstanceService.deleteRunAndHisInstanceByBusinessKeys(idList);
+        actProcessInstanceService.deleteRunAndHisInstance(idList);
         return baseMapper.deleteBatchIds(ids) > 0;
     }
 }

+ 12 - 0
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/utils/QueryUtils.java

@@ -75,6 +75,10 @@ public class QueryUtils {
         return hisTaskInstanceQuery().processInstanceId(processInstanceId);
     }
 
+    public static HistoricTaskInstanceQuery hisTaskBusinessKeyQuery(String businessKey) {
+        return hisTaskInstanceQuery().processInstanceBusinessKey(businessKey);
+    }
+
     public static ProcessInstanceQuery instanceQuery() {
         ProcessInstanceQuery query = PROCESS_ENGINE.getRuntimeService().createProcessInstanceQuery();
         if (TenantHelper.isEnable()) {
@@ -87,6 +91,10 @@ public class QueryUtils {
         return instanceQuery().processInstanceId(processInstanceId);
     }
 
+    public static ProcessInstanceQuery businessKeyQuery(String businessKey) {
+        return instanceQuery().processInstanceBusinessKey(businessKey);
+    }
+
     public static ProcessInstanceQuery instanceQuery(Set<String> processInstanceIds) {
         return instanceQuery().processInstanceIds(processInstanceIds);
     }
@@ -103,6 +111,10 @@ public class QueryUtils {
         return hisInstanceQuery().processInstanceId(processInstanceId);
     }
 
+    public static HistoricProcessInstanceQuery hisBusinessKeyQuery(String businessKey) {
+        return hisInstanceQuery().processInstanceBusinessKey(businessKey);
+    }
+
     public static HistoricProcessInstanceQuery hisInstanceQuery(Set<String> processInstanceIds) {
         return hisInstanceQuery().processInstanceIds(processInstanceIds);
     }