Răsfoiți Sursa

20230727-新增编辑资源时删除旧value

iaun 3 ani în urmă
părinte
comite
c97c79451e

+ 4 - 0
src/main/java/cn/iaun/it/controller/ItResourceController.java

@@ -282,6 +282,10 @@ public class ItResourceController {
             return new Result<>(ResultCode.FAILED_PARAM_INVALID, null);
             return new Result<>(ResultCode.FAILED_PARAM_INVALID, null);
         }
         }
         int result = itResourceService.setResource(itResource);
         int result = itResourceService.setResource(itResource);
+        if (result == 1) {
+            // 清除非此groupId的数据
+            itResourceService.deleteValueByResourceIdAndNEGroupId(itResource.getResourceId(), itResource.getGroupId());
+        }
         return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
         return new Result<>(result == 1 ? ResultCode.SUCCESS : ResultCode.FAILED, null);
     }
     }
 
 

+ 3 - 0
src/main/java/cn/iaun/it/mapper/ItResourceValueMapper.java

@@ -19,4 +19,7 @@ public interface ItResourceValueMapper extends BaseMapper<ItResourceValue> {
 
 
     // 通过resourceId获取resource表存在的条目数量
     // 通过resourceId获取resource表存在的条目数量
     int countByResourceIds(Set<Long> resourceIds);
     int countByResourceIds(Set<Long> resourceIds);
+
+    // 通过resourceId进行删除,并需要该value对应的groupId不等于传参的groupId(更新没有修改groupId则不需要删除)
+    int deleteValueByResourceIdAndNEGroupId(Long resourceId, Long groupId);
 }
 }

+ 4 - 2
src/main/java/cn/iaun/it/service/ItResourceService.java

@@ -57,9 +57,11 @@ public interface ItResourceService {
 
 
     int countResourceValueByResourceIds(Set<Long> resourceIds);
     int countResourceValueByResourceIds(Set<Long> resourceIds);
 
 
-    HashMap<Long,Long> getGroupIdByAttributeId(Set<Long> attributeIds);
+    HashMap<Long, Long> getGroupIdByAttributeId(Set<Long> attributeIds);
 
 
-    HashMap<Long,Long> getGroupIdByResourceId(Set<Long> resourceIds);
+    HashMap<Long, Long> getGroupIdByResourceId(Set<Long> resourceIds);
 
 
     int deleteAttributeByGroupId(Long groupId);
     int deleteAttributeByGroupId(Long groupId);
+
+    int deleteValueByResourceIdAndNEGroupId(Long resourceId, Long groupId);
 }
 }

+ 6 - 0
src/main/java/cn/iaun/it/service/impl/ItResourceServiceImpl.java

@@ -186,4 +186,10 @@ public class ItResourceServiceImpl implements ItResourceService {
     public int deleteAttributeByGroupId(Long groupId) {
     public int deleteAttributeByGroupId(Long groupId) {
         return itResourceAttributeMapper.delAttributeByGroupId(groupId);
         return itResourceAttributeMapper.delAttributeByGroupId(groupId);
     }
     }
+
+    @Override
+    public int deleteValueByResourceIdAndNEGroupId(Long resourceId, Long groupId) {
+        itResourceValueMapper.deleteValueByResourceIdAndNEGroupId(resourceId, groupId);
+        return 1;
+    }
 }
 }

+ 11 - 0
src/main/resources/mapper/ItResourceValueMapper.xml

@@ -15,6 +15,17 @@
         where attribute_id = #{attributeId}
         where attribute_id = #{attributeId}
           and resource_id = #{resourceId}
           and resource_id = #{resourceId}
     </update>
     </update>
+    <delete id="deleteValueByResourceIdAndNEGroupId">
+        DELETE
+        FROM it_resource_value
+        WHERE resource_id IN
+              (SELECT tmp.resource_id
+               from (SELECT rv.resource_id
+                     FROM it_resource_value rv
+                              LEFT join it_resource_attribute ira on rv.attribute_id = ira.attribute_id
+                     WHERE rv.resource_id = #{resourceId}
+                       AND ira.group_id != #{groupId}) tmp);
+    </delete>
     <select id="countByAttributeIds" resultType="java.lang.Integer">
     <select id="countByAttributeIds" resultType="java.lang.Integer">
         select count(1) as count
         select count(1) as count
         from it_resource_attribute ra
         from it_resource_attribute ra