Browse Source

bugfix:导入模板类修改,

lyx 2 weeks ago
parent
commit
95fbf5a9dd

+ 5 - 0
alien-entity/src/main/java/shop/alien/entity/store/StoreDictionary.java

@@ -84,6 +84,11 @@ public class StoreDictionary extends Model<StoreDictionary> {
     @TableField("hidden")
     private Integer hidden;
 
+    @ApiModelProperty(value = "排序ID")
+    @TableField("sort_id")
+    private Integer sortId;
+
+
     @Override
     protected Serializable pkVal() {
         return this.id;

+ 95 - 76
alien-store/src/main/java/shop/alien/store/service/impl/PlatformBusinessSectionServiceImpl.java

@@ -52,9 +52,9 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
     public List<StoreDictionary> queryBusinessSectionTree() {
         // 查询所有经营种类数据
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
-        queryWrapper.orderByAsc(StoreDictionary::getDictId);
+        queryWrapper.orderByAsc(StoreDictionary::getSortId);
         List<StoreDictionary> storeDictionaryList = storeDictionaryMapper.selectList(queryWrapper);
 
         // 构建三级树形结构
@@ -122,7 +122,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         }
 
         // 设置固定字段
-        storeDictionary.setTypeName("business_section");
+//        storeDictionary.setTypeName("business_section");
 //        storeDictionary.setTypeDetail("经营板块");
         storeDictionary.setDeleteFlag(0);
         storeDictionary.setCreatedTime(new Date());
@@ -137,7 +137,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         // 如果是二级或三级,验证父节点是否存在
         if (parentId != 0) {
             StoreDictionary parent = storeDictionaryMapper.selectById(parentId);
-            if (parent == null || !"business_section".equals(parent.getTypeName()) || parent.getDeleteFlag() == 1) {
+            if (parent == null || parent.getDeleteFlag() == 1) {
                 throw new IllegalArgumentException("父节点不存在或已删除");
             }
         }
@@ -147,6 +147,10 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         int nextDictId = (maxDictId == null ? 0 : Integer.parseInt(maxDictId)) + 1;
         storeDictionary.setDictId(String.valueOf(nextDictId));
 
+        // 生成 sort_id
+        Integer maxSortId = getMaxSortIdByParentId(parentId);
+        storeDictionary.setSortId(maxSortId == null ? 1 : maxSortId + 1);
+
         // 保存
         return this.save(storeDictionary);
     }
@@ -202,8 +206,8 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         // 标准化parentId用于比较(null转为0)
         Integer normalizedParentId = (parentId == null) ? 0 : parentId;
         Integer normalizedExistingParentId = (existingParentId == null) ? 0 : existingParentId;
-        String oldDictId = existing.getDictId();
-        String newDictId = storeDictionary.getDictId();
+        Integer oldSortId = existing.getSortId();
+        Integer newSortId = storeDictionary.getSortId();
         boolean parentIdChanged = !normalizedParentId.equals(normalizedExistingParentId);
         
         // 判断是否修改了关键字段(parent_id)
@@ -221,41 +225,41 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         if (parentIdChanged) {
             if (parentId != 0) {
                 StoreDictionary parent = storeDictionaryMapper.selectById(parentId);
-                if (parent == null || !"business_section".equals(parent.getTypeName()) || parent.getDeleteFlag() == 1) {
+                if (parent == null || parent.getDeleteFlag() == 1) {
                     throw new IllegalArgumentException("父节点不存在或已删除");
                 }
             }
             
-            // 如果用户指定了新位置(newDictId不为空),使用用户指定的位置
+            // 如果用户指定了新位置(newSortId不为空),使用用户指定的位置
             // 否则,使用新节点下的最大+1
-            if (StringUtils.isBlank(newDictId)) {
-                String maxDictId = getMaxDictIdByParentId(parentId);
-                int nextDictId = (maxDictId == null ? 0 : Integer.parseInt(maxDictId)) + 1;
-                newDictId = String.valueOf(nextDictId);
-                storeDictionary.setDictId(newDictId);
+            if (newSortId == null) {
+                Integer maxSortId = getMaxSortIdByParentId(parentId);
+                int nextSortId = (maxSortId == null ? 1 : maxSortId) + 1;
+                newSortId = nextSortId;
+                storeDictionary.setSortId(newSortId);
             }
             
             // 原节点下,原位置之后的记录需要前移(dictId - 1)
-            adjustSortOrderForMoveOut(existing.getId(), normalizedExistingParentId, oldDictId);
+            adjustSortOrderForMoveOut(existing.getId(), normalizedExistingParentId, oldSortId);
             
             // 新节点下,如果指定了新位置,需要调整新节点下的排序
-            if (StringUtils.isNotBlank(newDictId)) {
+            if (newSortId != null) {
                 try {
-                    int newOrder = Integer.parseInt(newDictId);
-                    String maxDictId = getMaxDictIdByParentId(parentId);
-                    int maxOrder = (maxDictId == null ? 0 : Integer.parseInt(maxDictId));
+                    int newOrder = newSortId;
+                    Integer maxSortId = getMaxSortIdByParentId(parentId);
+                    int maxOrder = (maxSortId == null ? 0 : maxSortId);
                     // 如果新位置不是最大+1,需要调整新节点下的排序
                     if (newOrder <= maxOrder) {
-                        adjustSortOrderForMoveIn(existing.getId(), normalizedParentId, newDictId);
+                        adjustSortOrderForMoveIn(existing.getId(), normalizedParentId, newSortId);
                     }
                 } catch (NumberFormatException e) {
-                    log.warn("无法解析新dictId: {}", newDictId, e);
+                    log.warn("无法解析新sortId: {}", newSortId, e);
                 }
             }
         }
 
         // 设置固定字段
-        storeDictionary.setTypeName("business_section");
+//        storeDictionary.setTypeName("business_section");
         // 如果没有指定删除标记,保持原有值;如果指定了,使用新值
         Integer oldHidden = existing.getHidden();
         Integer newHidden = storeDictionary.getHidden() == null ? oldHidden : storeDictionary.getHidden();
@@ -268,11 +272,11 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         // 处理排序逻辑:如果 parentId 没有改变,但 dictId 改变了,需要调整同级其他记录的排序
         if (!parentIdChanged) {
             // 如果 dictId 为空,保持原有值
-            if (StringUtils.isBlank(newDictId)) {
-                storeDictionary.setDictId(oldDictId);
-            } else if (!newDictId.equals(oldDictId)) {
+            if (newSortId == null) {
+                storeDictionary.setSortId(oldSortId);
+            } else if (!newSortId.equals(oldSortId)) {
                 // 调整同级其他记录的排序
-                adjustSortOrder(existing.getId(), normalizedParentId, oldDictId, newDictId);
+                adjustSortOrder(existing.getId(), normalizedParentId, oldSortId, newSortId);
             }
         }
 
@@ -297,7 +301,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
      */
     private boolean hasUndeletedChildren(Integer parentId) {
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
         queryWrapper.eq(StoreDictionary::getParentId, parentId);
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
         queryWrapper.last("LIMIT 1");
@@ -320,7 +324,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         
         // 查询所有未删除的子节点
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");
         queryWrapper.eq(StoreDictionary::getParentId, parentId);
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
         
@@ -350,7 +354,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
      */
     private String getMaxDictIdByParentId(Integer parentId) {
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
         
         if (parentId == null || parentId == 0) {
@@ -368,19 +372,44 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
     }
 
     /**
+     * 根据parentId获取最大的sortId
+     *
+     * @param parentId 父节点ID
+     * @return 最大的sortId,如果不存在则返回null
+     */
+    private Integer getMaxSortIdByParentId(Integer parentId) {
+        LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
+        queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
+        
+        if (parentId == null || parentId == 0) {
+            queryWrapper.and(wrapper -> wrapper.isNull(StoreDictionary::getParentId)
+                    .or().eq(StoreDictionary::getParentId, 0));
+        } else {
+            queryWrapper.eq(StoreDictionary::getParentId, parentId);
+        }
+        
+        queryWrapper.orderByDesc(StoreDictionary::getSortId);
+        queryWrapper.last("LIMIT 1");
+        
+        StoreDictionary maxDict = storeDictionaryMapper.selectOne(queryWrapper);
+        return maxDict != null && maxDict.getSortId() != null ? maxDict.getSortId() : null;
+    }
+
+    /**
      * 调整排序:当记录的dictId改变时,调整同级其他记录的排序
      * 上升则顺推:如果新dictId < 原dictId,将原dictId到新dictId之间的记录的dictId都+1
      * 下降则顺升:如果新dictId > 原dictId,将原dictId到新dictId之间的记录的dictId都-1
      *
      * @param currentId 当前记录ID
      * @param parentId 父节点ID
-     * @param oldDictId 原dictId
-     * @param newDictId 新dictId
+     * @param oldSortId 原sortId
+     * @param newSortId 新sortId
      */
-    private void adjustSortOrder(Integer currentId, Integer parentId, String oldDictId, String newDictId) {
+    private void adjustSortOrder(Integer currentId, Integer parentId, Integer oldSortId, Integer newSortId) {
         try {
-            int oldOrder = Integer.parseInt(oldDictId);
-            int newOrder = Integer.parseInt(newDictId);
+            int oldOrder = oldSortId;
+            int newOrder = newSortId;
             
             // 如果排序没有改变,直接返回
             if (oldOrder == newOrder) {
@@ -389,7 +418,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
             
             // 查询同级所有记录(排除当前记录)
             LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-            queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+            queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
             queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
             queryWrapper.ne(StoreDictionary::getId, currentId);
             
@@ -411,14 +440,14 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
                 // 将新dictId到原dictId之间的记录的dictId都+1
                 for (StoreDictionary sibling : siblings) {
                     try {
-                        int siblingOrder = Integer.parseInt(sibling.getDictId());
+                        int siblingOrder = sibling.getSortId();
                         if (siblingOrder >= newOrder && siblingOrder < oldOrder) {
-                            sibling.setDictId(String.valueOf(siblingOrder + 1));
+                            sibling.setSortId(siblingOrder + 1);
                             sibling.setUpdatedTime(new Date());
                             storeDictionaryMapper.updateById(sibling);
                         }
                     } catch (NumberFormatException e) {
-                        log.warn("无法解析dictId: {}", sibling.getDictId(), e);
+                        log.warn("无法解析sortId: {}", sibling.getSortId(), e);
                     }
                 }
             } 
@@ -427,19 +456,19 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
                 // 将原dictId到新dictId之间的记录的dictId都-1
                 for (StoreDictionary sibling : siblings) {
                     try {
-                        int siblingOrder = Integer.parseInt(sibling.getDictId());
+                        int siblingOrder = sibling.getSortId();
                         if (siblingOrder > oldOrder && siblingOrder <= newOrder) {
-                            sibling.setDictId(String.valueOf(siblingOrder - 1));
+                            sibling.setSortId(siblingOrder - 1);
                             sibling.setUpdatedTime(new Date());
                             storeDictionaryMapper.updateById(sibling);
                         }
                     } catch (NumberFormatException e) {
-                        log.warn("无法解析dictId: {}", sibling.getDictId(), e);
+                        log.warn("无法解析sortId: {}", sibling.getSortId(), e);
                     }
                 }
             }
         } catch (NumberFormatException e) {
-            log.error("调整排序失败,dictId格式错误: oldDictId={}, newDictId={}", oldDictId, newDictId, e);
+            log.error("调整排序失败,sortId格式错误: oldSortId={}, newSortId={}", oldSortId, newSortId, e);
         } catch (Exception e) {
             log.error("调整排序失败", e);
         }
@@ -450,15 +479,15 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
      *
      * @param currentId 当前记录ID
      * @param oldParentId 原父节点ID
-     * @param oldDictId 原dictId
+     * @param oldSortId 原sortId
      */
-    private void adjustSortOrderForMoveOut(Integer currentId, Integer oldParentId, String oldDictId) {
+    private void adjustSortOrderForMoveOut(Integer currentId, Integer oldParentId, Integer oldSortId) {
         try {
-            int oldOrder = Integer.parseInt(oldDictId);
+            int oldOrder = oldSortId;
             
             // 查询原节点同级所有记录(排除当前记录)
             LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-            queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+            queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
             queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
             queryWrapper.ne(StoreDictionary::getId, currentId);
             
@@ -478,18 +507,18 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
             // 原位置之后的记录需要前移(dictId - 1)
             for (StoreDictionary sibling : siblings) {
                 try {
-                    int siblingOrder = Integer.parseInt(sibling.getDictId());
+                    int siblingOrder = sibling.getSortId();
                     if (siblingOrder > oldOrder) {
-                        sibling.setDictId(String.valueOf(siblingOrder - 1));
+                        sibling.setSortId(siblingOrder - 1);
                         sibling.setUpdatedTime(new Date());
                         storeDictionaryMapper.updateById(sibling);
                     }
                 } catch (NumberFormatException e) {
-                    log.warn("无法解析dictId: {}", sibling.getDictId(), e);
+                    log.warn("无法解析sortId: {}", sibling.getSortId(), e);
                 }
             }
         } catch (NumberFormatException e) {
-            log.error("移出节点排序调整失败,dictId格式错误: oldDictId={}", oldDictId, e);
+            log.error("移出节点排序调整失败,sortId格式错误: oldSortId={}", oldSortId, e);
         } catch (Exception e) {
             log.error("移出节点排序调整失败", e);
         }
@@ -500,15 +529,15 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
      *
      * @param currentId 当前记录ID
      * @param newParentId 新父节点ID
-     * @param newDictId 新dictId
+     * @param newSortId 新sortId
      */
-    private void adjustSortOrderForMoveIn(Integer currentId, Integer newParentId, String newDictId) {
+    private void adjustSortOrderForMoveIn(Integer currentId, Integer newParentId, Integer newSortId) {
         try {
-            int newOrder = Integer.parseInt(newDictId);
+            int newOrder = newSortId;
             
             // 查询新节点同级所有记录(排除当前记录)
             LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-            queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+            queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
             queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
             queryWrapper.ne(StoreDictionary::getId, currentId);
             
@@ -528,9 +557,9 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
             // 新位置之后的记录需要后移(dictId + 1)
             for (StoreDictionary sibling : siblings) {
                 try {
-                    int siblingOrder = Integer.parseInt(sibling.getDictId());
+                    int siblingOrder = sibling.getSortId();
                     if (siblingOrder >= newOrder) {
-                        sibling.setDictId(String.valueOf(siblingOrder + 1));
+                        sibling.setSortId(siblingOrder + 1);
                         sibling.setUpdatedTime(new Date());
                         storeDictionaryMapper.updateById(sibling);
                     }
@@ -539,7 +568,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
                 }
             }
         } catch (NumberFormatException e) {
-            log.error("移入节点排序调整失败,dictId格式错误: newDictId={}", newDictId, e);
+            log.error("移入节点排序调整失败,sortId格式错误: newSortId={}", newSortId, e);
         } catch (Exception e) {
             log.error("移入节点排序调整失败", e);
         }
@@ -769,7 +798,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
     private StoreDictionary findOrCreateLevel(String dictDetail, Integer parentId, int expectedLevel, String hidden) {
         // 查找是否存在
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
         queryWrapper.eq(StoreDictionary::getDictDetail, dictDetail);
 
@@ -788,15 +817,17 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
 
         // 创建新分类
         StoreDictionary newDict = new StoreDictionary();
-        newDict.setTypeName("business_section");
         newDict.setDictDetail(dictDetail);
         // 一级为经营版块 ,二级为经营种类,三级为分类
         if (expectedLevel == 0) {
             newDict.setTypeDetail("经营板块");
+            newDict.setTypeName("business_section");
         } else if (expectedLevel == 1) {
             newDict.setTypeDetail("经营种类");
+            newDict.setTypeName("business_type");
         } else if (expectedLevel == 2) {
             newDict.setTypeDetail("分类");
+            newDict.setTypeName("business_classify");
         }
         newDict.setParentId(parentId == null ? null : parentId);
         newDict.setHidden(StringUtils.isNotBlank(hidden) && hidden.equals("隐藏") ? 1 : 0);
@@ -805,23 +836,11 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
 
         // 生成 dict_id
         String maxDictId = getMaxDictIdByParentId(newDict.getParentId());
-        /*int nextDictId;
-        if (StringUtils.isNotBlank(sortOrder) && expectedLevel == 1) {
-            try {
-                nextDictId = Integer.parseInt(sortOrder);
-                // 如果指定的排序已存在,需要调整
-                String existingDictId = getDictIdByParentIdAndOrder(newDict.getParentId(), nextDictId);
-                if (existingDictId != null) {
-                    // 使用最大+1
-                    nextDictId = (maxDictId == null ? 0 : Integer.parseInt(maxDictId)) + 1;
-                }
-            } catch (NumberFormatException e) {
-                nextDictId = (maxDictId == null ? 0 : Integer.parseInt(maxDictId)) + 1;
-            }
-        } else {
-            nextDictId = (maxDictId == null ? 0 : Integer.parseInt(maxDictId)) + 1;
-        }*/
         newDict.setDictId(String.valueOf(maxDictId == null ? 1 : Integer.parseInt(maxDictId) + 1));
+        
+        // 生成 sort_id
+        Integer maxSortId = getMaxSortIdByParentId(newDict.getParentId());
+        newDict.setSortId(maxSortId == null ? 1 : maxSortId + 1);
 
         boolean saved = this.save(newDict);
         return saved ? newDict : null;
@@ -836,7 +855,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
      */
     private String getDictIdByParentIdAndOrder(Integer parentId, int order) {
         LambdaQueryWrapper<StoreDictionary> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(StoreDictionary::getTypeName, "business_section");
+        queryWrapper.in(StoreDictionary::getTypeName, "business_section","business_type","business_classify");;
         queryWrapper.eq(StoreDictionary::getDeleteFlag, 0);
         queryWrapper.eq(StoreDictionary::getDictId, String.valueOf(order));
         
@@ -848,7 +867,7 @@ public class PlatformBusinessSectionServiceImpl extends ServiceImpl<StoreDiction
         }
         
         StoreDictionary dict = storeDictionaryMapper.selectOne(queryWrapper);
-        return dict != null ? dict.getDictId() : null;
+        return dict != null ? dict.getSortId().toString() : null;
     }
 
     /**