From 62efa4e6c0977bf3b216c59e545a102f0af77d65 Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期四, 30 十月 2025 13:57:52 +0800
Subject: [PATCH] 调整排序

---
 framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java |   88 ++++++++++++++++++++++++++++++++++----------
 1 files changed, 68 insertions(+), 20 deletions(-)

diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java
index f859351..c476146 100644
--- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java
+++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java
@@ -523,8 +523,13 @@
         Integer oldSort = currentGoods != null ? currentGoods.getGoodsSort() : null;
         Integer newSort = req.getSort();
         
+        // 鏀堕泦闇�瑕佹洿鏂癊S鐨勫晢鍝両D
+        List<String> goodsIdsToUpdateEs = new ArrayList<>();
+        goodsIdsToUpdateEs.add(req.getGoodsId());
+        
         // 澶勭悊鏁版嵁搴撲腑鐨勯噸澶嶆帓搴忓�硷紝纭繚鍞竴鎬�
-        resolveDuplicateSorts();
+        List<String> duplicateResolvedGoodsIds = resolveDuplicateSorts();
+        goodsIdsToUpdateEs.addAll(duplicateResolvedGoodsIds);
         
         // 閲嶆柊鑾峰彇褰撳墠鍟嗗搧鐨勬帓搴忓�硷紙鍙兘宸茶resolveDuplicateSorts淇敼锛�
         currentGoods = this.getById(req.getGoodsId());
@@ -533,15 +538,27 @@
         // 鏍规嵁鎿嶄綔绫诲瀷鎵ц涓嶅悓鐨勬帓搴忛�昏緫
         if (oldSort == null) {
             // 鏂板鎺掑簭
-            adjustForInsert(newSort);
+            List<String> affectedGoodsIds = adjustForInsert(newSort);
+            goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+            // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+            List<String> afterInsertResolvedGoodsIds = resolveDuplicateSorts();
+            goodsIdsToUpdateEs.addAll(afterInsertResolvedGoodsIds);
         } else if (!oldSort.equals(newSort)) {
             // 淇敼鎺掑簭
             if (newSort > oldSort) {
                 // 鎺掑簭鍊煎澶э細灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧+1锛岀洿鍒伴亣鍒版柇灞�
-                adjustForIncrease(newSort);
+                List<String> affectedGoodsIds = adjustForIncrease(newSort);
+                goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+                // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+                List<String> afterIncreaseResolvedGoodsIds = resolveDuplicateSorts();
+                goodsIdsToUpdateEs.addAll(afterIncreaseResolvedGoodsIds);
             } else {
                 // 鎺掑簭鍊煎噺灏忥細灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧+1锛岀洿鍒伴亣鍒版柇灞�
-                adjustForDecrease(oldSort, newSort);
+                List<String> affectedGoodsIds = adjustForDecrease(oldSort, newSort);
+                goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+                // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+                List<String> afterDecreaseResolvedGoodsIds = resolveDuplicateSorts();
+                goodsIdsToUpdateEs.addAll(afterDecreaseResolvedGoodsIds);
             }
         }
         
@@ -550,18 +567,19 @@
         updateWrapper.set(Goods::getGoodsSort, newSort);
         updateWrapper.eq(Goods::getId, req.getGoodsId());
         result = this.update(updateWrapper);
-        
+
         // 鏇存柊ES绱㈠紩
-        this.updateEsGoods(Collections.singletonList(req.getGoodsId()));
-        
+        this.updateEsGoods(goodsIdsToUpdateEs);
+
         return result;
     }
 
     /**
      * 澶勭悊鏁版嵁搴撲腑鐨勯噸澶嶆帓搴忓�硷紝纭繚鍞竴鎬�
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
      */
     @Transactional(rollbackFor = Exception.class)
-    public void resolveDuplicateSorts() {
+    public List<String> resolveDuplicateSorts() {
         // 鏌ヨ鎵�鏈夋湁鎺掑簭鍊肩殑鍟嗗搧
         LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.isNotNull(Goods::getGoodsSort);
@@ -587,10 +605,17 @@
             }
         }
         
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
         // 鎵归噺鏇存柊
         if (!toUpdate.isEmpty()) {
             this.updateBatchById(toUpdate);
         }
+        
+        return affectedGoodsIds;
     }
     
     /**
@@ -609,10 +634,11 @@
      * 鎻掑叆鏂版帓搴忓�肩殑澶勭悊閫昏緫
      * 灏嗘墍鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧鎺掑簭鍊�+1锛岀洿鍒伴亣鍒版柇灞�
      * @param newSort 鏂扮殑鎺掑簭鍊�
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
      */
     @Transactional(rollbackFor = Exception.class)
-    public void adjustForInsert(Integer newSort) {
-        if (newSort == null) return;
+    public List<String> adjustForInsert(Integer newSort) {
+        if (newSort == null) return Collections.emptyList();
         
         // 鏌ヨ鎵�鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧锛屾寜鎺掑簭鍊煎崌搴忔帓鍒�
         LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery();
@@ -635,20 +661,28 @@
             }
         }
         
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
         // 鎵归噺鏇存柊
         if (!toUpdate.isEmpty()) {
             this.updateBatchById(toUpdate);
         }
+        
+        return affectedGoodsIds;
     }
 
     /**
      * 鎺掑簭鍊煎澶х殑澶勭悊閫昏緫
      * 灏嗘墍鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧鎺掑簭鍊�+1锛岀洿鍒伴亣鍒版柇灞�
      * @param newSort 鏂版帓搴忓��
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
      */
     @Transactional(rollbackFor = Exception.class)
-    public  void adjustForIncrease(Integer newSort) {
-        if (newSort == null) return;
+    public List<String> adjustForIncrease(Integer newSort) {
+        if (newSort == null) return Collections.emptyList();
         
         // 鏌ヨ鎵�鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧锛屾寜鎺掑簭鍊煎崌搴忔帓鍒�
         LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery();
@@ -671,10 +705,17 @@
             }
         }
         
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
         // 鎵归噺鏇存柊
         if (!toUpdate.isEmpty()) {
             this.updateBatchById(toUpdate);
         }
+        
+        return affectedGoodsIds;
     }
 
     /**
@@ -682,38 +723,45 @@
      * 灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧鎺掑簭鍊�+1锛岀洿鍒伴亣鍒版柇灞�
      * @param oldSort 鍘熸帓搴忓��
      * @param newSort 鏂版帓搴忓��
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
      */
     @Transactional(rollbackFor = Exception.class)
-    public  void adjustForDecrease(Integer oldSort, Integer newSort) {
-        if (oldSort == null || newSort == null || oldSort.equals(newSort)) return;
+    public List<String> adjustForDecrease(Integer oldSort, Integer newSort) {
+        if (oldSort == null || newSort == null || oldSort.equals(newSort)) return Collections.emptyList();
         
-        // 鏌ヨ澶т簬绛変簬鏂版帓搴忓�间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧锛屾寜鎺掑簭鍊奸檷搴忔帓鍒�
+        // 鏌ヨ澶т簬绛変簬鏂版帓搴忓�间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧锛屾寜鎺掑簭鍊煎崌搴忔帓鍒�
         LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery();
         query.ge(Goods::getGoodsSort, newSort);
         query.lt(Goods::getGoodsSort, oldSort);
         query.isNotNull(Goods::getGoodsSort);
-        query.orderByDesc(Goods::getGoodsSort);
+        query.orderByAsc(Goods::getGoodsSort);
         List<Goods> goodsList = this.list(query);
         
         // 妫�鏌ヨ繛缁�э紝閬囧埌鏂眰鍒欏仠姝�
         List<Goods> toUpdate = new ArrayList<>();
-        int expectedSort = oldSort - 1;
-        for (int i = goodsList.size() - 1; i >= 0; i--) {
-            Goods goods = goodsList.get(i);
+        int expectedSort = newSort;
+        for (Goods goods : goodsList) {
             if (goods.getGoodsSort().equals(expectedSort)) {
                 goods.setGoodsSort(goods.getGoodsSort() + 1);
                 toUpdate.add(goods);
-                expectedSort--;
+                expectedSort++;
             } else {
                 // 閬囧埌鏂眰锛屽仠姝㈣皟鏁�
                 break;
             }
         }
         
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
         // 鎵归噺鏇存柊
         if (!toUpdate.isEmpty()) {
             this.updateBatchById(toUpdate);
         }
+        
+        return affectedGoodsIds;
     }
 
     @Override

--
Gitblit v1.8.0