From 9b8343c270ff142f2d20e5d5961a667f4eb6ef53 Mon Sep 17 00:00:00 2001
From: xueqingkun <xueqingkun@126.com>
Date: Fri, 5 Jul 2024 15:01:19 +0800
Subject: [PATCH] fix bugs update-flow

---
 .../service/impl/FlowManageServiceImpl.java   | 32 ++++++---
 .../knowsub/dto/flow/SystemFlowTypeDTO.java   | 68 +++++++++++++++++++
 2 files changed, 92 insertions(+), 8 deletions(-)
 create mode 100644 know_sub_model/src/main/java/com/supervision/knowsub/dto/flow/SystemFlowTypeDTO.java

diff --git a/know_sub_business/src/main/java/com/supervision/knowsub/service/impl/FlowManageServiceImpl.java b/know_sub_business/src/main/java/com/supervision/knowsub/service/impl/FlowManageServiceImpl.java
index 3af2ace..b1b36d9 100644
--- a/know_sub_business/src/main/java/com/supervision/knowsub/service/impl/FlowManageServiceImpl.java
+++ b/know_sub_business/src/main/java/com/supervision/knowsub/service/impl/FlowManageServiceImpl.java
@@ -2,7 +2,9 @@ package com.supervision.knowsub.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.lang.Assert;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.supervision.knowsub.dto.flow.SystemFlowTypeDTO;
 import com.supervision.knowsub.exception.BusinessException;
 import com.supervision.knowsub.model.SystemBase;
 import com.supervision.knowsub.model.SystemFlow;
@@ -207,21 +209,35 @@ public class FlowManageServiceImpl implements FlowManageService {
      */
     private void updateFlowTypeRelation(String flowId, List<Integer> flowTypeList,List<String> baseIdList){
         List<SystemFlowTypeRelation> flowTypeRelationList = systemFlowTypeRelationService.lambdaQuery().eq(SystemFlowTypeRelation::getFlowId, flowId).list();
-        List<Integer> dbFlowTypeList = flowTypeRelationList.stream().map(SystemFlowTypeRelation::getFlowType).toList();
 
-        // 数据库中不存在,入参中存在,新增
-        List<Integer> needInsert = flowTypeList.stream().filter(flowType -> !dbFlowTypeList.contains(flowType)).toList();
+        // 从数据库中查询出来的数据
+        List<SystemFlowTypeDTO> dbFlowTypeDTOList = flowTypeRelationList.stream().map(SystemFlowTypeDTO::buildWithFlowTypeRelation).toList();
+
+        // 入参中的数据
+        List<SystemFlowTypeDTO> flowTypeDTOList = flowTypeList.stream().map(flowType ->
+                baseIdList.stream().map(baseId ->
+                        SystemFlowTypeDTO.builder().baseId(baseId).flowId(flowId).flowType(flowType).build()).toList()
+        ).flatMap(Collection::stream).toList();
+
+        // 数据库中不存在,入参中存在,新增 流程类型与子库id组合成唯一数据
+        List<SystemFlowTypeDTO> needInsert = flowTypeDTOList.stream().filter(systemFlowTypeDTO ->
+                dbFlowTypeDTOList.stream().noneMatch(dbSystemFlowTypeDTO ->
+                        dbSystemFlowTypeDTO.contentEquals(systemFlowTypeDTO))
+        ).toList();
+
+        // 数据库中存在,入参中不存在,删除 流程类型与子库id组合成唯一数据
+        List<SystemFlowTypeDTO> needDelete = dbFlowTypeDTOList.stream().filter(dbSystemFlowTypeDTO ->
+                flowTypeDTOList.stream().noneMatch(dbSystemFlowTypeDTO::contentEquals)).toList();
 
-        // 数据库中存在,入参中不存在,删除
-        List<Integer> needDelete = dbFlowTypeList.stream().filter(flowType -> !flowTypeList.contains(flowType)).distinct().toList();
 
         if (CollUtil.isNotEmpty(needInsert)){
-            doBatchSaveFlowTypeRelation(flowId, needInsert, baseIdList);
+            List<SystemFlowTypeRelation> flowTypeRelations = needInsert.stream().map(SystemFlowTypeDTO::toSystemFlowTypeRelation).toList();
+            systemFlowTypeRelationService.saveBatch(flowTypeRelations);
         }
 
         if (CollUtil.isNotEmpty(needDelete)){
-            systemFlowTypeRelationService.lambdaUpdate().in(SystemFlowTypeRelation::getFlowType, needDelete)
-                    .eq(SystemFlowTypeRelation::getFlowId, flowId).remove();
+            List<String> needDeleteIds = needDelete.stream().map(SystemFlowTypeDTO::getId).filter(StrUtil::isNotEmpty).toList();
+            systemFlowTypeRelationService.removeBatchByIds(needDeleteIds);
         }
     }
 
diff --git a/know_sub_model/src/main/java/com/supervision/knowsub/dto/flow/SystemFlowTypeDTO.java b/know_sub_model/src/main/java/com/supervision/knowsub/dto/flow/SystemFlowTypeDTO.java
new file mode 100644
index 0000000..909f168
--- /dev/null
+++ b/know_sub_model/src/main/java/com/supervision/knowsub/dto/flow/SystemFlowTypeDTO.java
@@ -0,0 +1,68 @@
+package com.supervision.knowsub.dto.flow;
+
+import cn.hutool.core.util.NumberUtil;
+import cn.hutool.core.util.StrUtil;
+import com.supervision.knowsub.model.SystemFlowTypeRelation;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Builder;
+import lombok.Data;
+import lombok.experimental.Tolerate;
+
+import java.util.Objects;
+
+/**
+ * 流程类型关联表
+ */
+@Data
+@Builder
+public class SystemFlowTypeDTO {
+
+    @Schema(description = "数据id")
+    private String id;
+
+    @Schema(description = "流程类型id")
+    private String flowId;
+
+    private Integer flowType;
+
+    @Schema(description = "子库id")
+    private String baseId;
+
+    @Tolerate
+    public SystemFlowTypeDTO() {
+    }
+
+
+    public static SystemFlowTypeDTO buildWithFlowTypeRelation(SystemFlowTypeRelation systemFlowTypeRelation){
+        if (Objects.isNull(systemFlowTypeRelation)){
+            return new SystemFlowTypeDTO();
+        }
+        return SystemFlowTypeDTO.builder()
+                .id(systemFlowTypeRelation.getId())
+                .flowId(systemFlowTypeRelation.getFlowId())
+                .flowType(systemFlowTypeRelation.getFlowType())
+                .baseId(systemFlowTypeRelation.getBaseId())
+                .build();
+    }
+
+    public SystemFlowTypeRelation toSystemFlowTypeRelation(){
+        SystemFlowTypeRelation flowTypeRelation = new SystemFlowTypeRelation();
+        flowTypeRelation.setId(this.getId());
+        flowTypeRelation.setFlowType(this.getFlowType());
+        flowTypeRelation.setFlowId(this.getFlowId());
+        flowTypeRelation.setBaseId(this.getBaseId());
+        return flowTypeRelation;
+    }
+
+    public boolean contentEquals(SystemFlowTypeDTO systemFlowTypeDTO) {
+
+        if (StrUtil.isAllNotEmpty(this.getId(), systemFlowTypeDTO.getId())){
+            return StrUtil.equals(this.getId(), systemFlowTypeDTO.getId());
+        }
+
+        return StrUtil.equals(this.getBaseId(), systemFlowTypeDTO.getBaseId())
+                && NumberUtil.equals(this.getFlowType(), systemFlowTypeDTO.getFlowType())
+                && StrUtil.equals(this.getFlowId(), systemFlowTypeDTO.getFlowId());
+
+    }
+}