Browse Source

fix:【BPM 工作流】流程模型分类删除时,额外校验是否被流程模型所使用。

YunaiV 1 year ago
parent
commit
3aac40a51e

+ 1 - 0
yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java

@@ -74,6 +74,7 @@ public interface ErrorCodeConstants {
     ErrorCode CATEGORY_NOT_EXISTS = new ErrorCode(1_009_012_000, "流程分类不存在");
     ErrorCode CATEGORY_NAME_DUPLICATE = new ErrorCode(1_009_012_001, "流程分类名字【{}】重复");
     ErrorCode CATEGORY_CODE_DUPLICATE = new ErrorCode(1_009_012_002, "流程分类编码【{}】重复");
+    ErrorCode CATEGORY_DELETE_FAIL_MODEL_USED = new ErrorCode(1_009_012_003, "删除失败,流程分类【{}】已被流程模型使用,请先删除对应的流程模型");
 
     // ========== BPM 流程监听器 1-009-013-000 ==========
     ErrorCode PROCESS_LISTENER_NOT_EXISTS = new ErrorCode(1_009_013_000, "流程监听器不存在");

+ 13 - 3
yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmCategoryServiceImpl.java

@@ -34,6 +34,9 @@ public class BpmCategoryServiceImpl implements BpmCategoryService {
     @Resource
     private BpmCategoryMapper bpmCategoryMapper;
 
+    @Resource
+    private BpmModelService modelService;
+
     @Override
     public Long createCategory(BpmCategorySaveReqVO createReqVO) {
         // 校验唯一
@@ -77,15 +80,22 @@ public class BpmCategoryServiceImpl implements BpmCategoryService {
     @Override
     public void deleteCategory(Long id) {
         // 校验存在
-        validateCategoryExists(id);
+        BpmCategoryDO category = validateCategoryExists(id);
+        // 校验是否被流程模型使用
+        Long count = modelService.getModelCountByCategory(category.getCode());
+        if (count > 0) {
+            throw exception(CATEGORY_DELETE_FAIL_MODEL_USED, category.getName());
+        }
         // 删除
         bpmCategoryMapper.deleteById(id);
     }
 
-    private void validateCategoryExists(Long id) {
-        if (bpmCategoryMapper.selectById(id) == null) {
+    private BpmCategoryDO validateCategoryExists(Long id) {
+        BpmCategoryDO category = bpmCategoryMapper.selectById(id);
+        if (category == null) {
             throw exception(CATEGORY_NOT_EXISTS);
         }
+        return category;
     }
 
     @Override

+ 8 - 0
yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelService.java

@@ -24,6 +24,14 @@ public interface BpmModelService {
      */
     List<Model> getModelList(String name);
 
+    /**
+     * 根据分类编码获得流程模型数量
+     *
+     * @param category 分类编码
+     * @return 流程模型数量
+     */
+    Long getModelCountByCategory(String category);
+
     /**
      * 创建流程模型
      *

+ 8 - 0
yudao-module-bpm/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelServiceImpl.java

@@ -88,6 +88,14 @@ public class BpmModelServiceImpl implements BpmModelService {
         return modelQuery.list();
     }
 
+    @Override
+    public Long getModelCountByCategory(String category) {
+        return repositoryService.createModelQuery()
+                .modelCategory(category)
+                .modelTenantId(FlowableUtils.getTenantId())
+                .count();
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public String createModel(@Valid BpmModelSaveReqVO createReqVO) {