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 |  345 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 336 insertions(+), 9 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 8fb237a..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
@@ -15,10 +15,7 @@
 import cn.lili.common.security.context.UserContext;
 import cn.lili.common.security.enums.UserEnums;
 import cn.lili.modules.goods.entity.dos.*;
-import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
-import cn.lili.modules.goods.entity.dto.GoodsParamsDTO;
-import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
-import cn.lili.modules.goods.entity.dto.GoodsSortDTO;
+import cn.lili.modules.goods.entity.dto.*;
 import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
 import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
 import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
@@ -50,6 +47,10 @@
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.rocketmq.spring.core.RocketMQTemplate;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -57,6 +58,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.net.URLEncoder;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -122,6 +126,9 @@
 
     @Autowired
     private Cache<GoodsVO> cache;
+
+    @Autowired
+    private GoodsService goodsService;
 
     @Autowired
     private COSUtil cosUtil;
@@ -311,9 +318,91 @@
         return this.page(PageUtil.initPage(goodsSearchParams), goodsSearchParams.queryWrapper());
     }
 
+    @Override
+    public void queryExportGoods(HttpServletResponse response, GoodsSearchParams searchParams) {
+        List<Goods> list = this.list(searchParams.queryWrapper());
+
+        XSSFWorkbook workbook = initGoodsExportData(list);
+        try {
+            // 璁剧疆鍝嶅簲澶�
+            String fileName = URLEncoder.encode("鍟嗗搧娓呭崟", "UTF-8");
+            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
+            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
+
+            ServletOutputStream out = response.getOutputStream();
+            workbook.write(out);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                workbook.close();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+    }
+    private XSSFWorkbook initGoodsExportData(List<Goods> list) {
+        List<GoodsExportDTO> goodsExportDTOs = new ArrayList<>();
+        for (Goods goods : list) {
+            GoodsExportDTO goodsExportDTO = new GoodsExportDTO();
+            BeanUtils.copyProperties(goods, goodsExportDTO);
+            goodsExportDTOs.add(goodsExportDTO);
+        }
+
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("鍟嗗搧娓呭崟");
+        Row header = sheet.createRow(0);
+        String[] headers = {"鍟嗗搧鍚嶇О", "鍟嗗搧缂栧彿", "浠锋牸", "閿�鍞ā寮�", "鍟嗗搧绫诲瀷", "鐘舵��", "瀹℃牳鐘舵��", "搴楅摵鍚嶇О"};
+        for (int i = 0; i < headers.length; i++) {
+            Cell cell = header.createCell(i);
+            cell.setCellValue(headers[i]);
+        }
+        for (int i = 0; i < goodsExportDTOs.size(); i++) {
+            GoodsExportDTO goodsExportDTO = goodsExportDTOs.get(i);
+            Row row = sheet.createRow(i + 1);
+            row.createCell(0).setCellValue(goodsExportDTO.getGoodsName());
+            row.createCell(1).setCellValue(goodsExportDTO.getId());
+            row.createCell(2).setCellValue(goodsExportDTO.getPrice());
+            if ("RETAIL".equals(goodsExportDTO.getSalesModel())) {
+                row.createCell(3).setCellValue("闆跺敭");
+            } else if ("WHOLESALE".equals(goodsExportDTO.getSalesModel())) {
+                row.createCell(3).setCellValue("鎵瑰彂");
+            } else if ("PRESALE".equals(goodsExportDTO.getSalesModel())) {
+                row.createCell(3).setCellValue("棰勫敭");
+            } else {
+                row.createCell(3).setCellValue("鍏朵粬绫诲瀷");
+            }
+
+            if ("PHYSICAL_GOODS".equals(goodsExportDTO.getGoodsType())) {
+                row.createCell(4).setCellValue("瀹炵墿鍟嗗搧");
+            } else if ("VIRTUAL_GOODS".equals(goodsExportDTO.getGoodsType())) {
+                row.createCell(4).setCellValue("铏氭嫙鍟嗗搧");
+            } else {
+                row.createCell(4).setCellValue("鐢靛瓙鍗″埜");
+            }
+
+            if ("DOWN".equals(goodsExportDTO.getMarketEnable())) {
+                row.createCell(5).setCellValue("涓嬫灦");
+            } else if ("UPPER".equals(goodsExportDTO.getMarketEnable())) {
+                row.createCell(5).setCellValue("涓婃灦");
+            }
+
+            if ("TOBEAUDITED".equals(goodsExportDTO.getAuthFlag())) {
+                row.createCell(6).setCellValue("寰呭鏍�");
+            } else if ("PASS".equals(goodsExportDTO.getAuthFlag())) {
+                row.createCell(6).setCellValue("閫氳繃");
+            } else if ("REFUSE".equals(goodsExportDTO.getAuthFlag())) {
+                row.createCell(6).setCellValue("鎷掔粷");
+            }
+            row.createCell(7).setCellValue(goodsExportDTO.getStoreName());
+
+        }
+        return workbook;
+    }
     /**
      * 鍟嗗搧鏌ヨ
-     *
+     *g
      * @param goodsSearchParams 鏌ヨ鍙傛暟
      * @return 鍟嗗搧淇℃伅
      */
@@ -428,14 +517,251 @@
         boolean result;
         //妫�娴嬬鐞嗗憳鏉冮檺
         this.checkManagerAuthority();
+        
+        // 鑾峰彇褰撳墠鍟嗗搧鐨勫師濮嬫帓搴忓��
+        Goods currentGoods = this.getById(req.getGoodsId());
+        Integer oldSort = currentGoods != null ? currentGoods.getGoodsSort() : null;
+        Integer newSort = req.getSort();
+        
+        // 鏀堕泦闇�瑕佹洿鏂癊S鐨勫晢鍝両D
+        List<String> goodsIdsToUpdateEs = new ArrayList<>();
+        goodsIdsToUpdateEs.add(req.getGoodsId());
+        
+        // 澶勭悊鏁版嵁搴撲腑鐨勯噸澶嶆帓搴忓�硷紝纭繚鍞竴鎬�
+        List<String> duplicateResolvedGoodsIds = resolveDuplicateSorts();
+        goodsIdsToUpdateEs.addAll(duplicateResolvedGoodsIds);
+        
+        // 閲嶆柊鑾峰彇褰撳墠鍟嗗搧鐨勬帓搴忓�硷紙鍙兘宸茶resolveDuplicateSorts淇敼锛�
+        currentGoods = this.getById(req.getGoodsId());
+        oldSort = currentGoods != null ? currentGoods.getGoodsSort() : null;
+        
+        // 鏍规嵁鎿嶄綔绫诲瀷鎵ц涓嶅悓鐨勬帓搴忛�昏緫
+        if (oldSort == null) {
+            // 鏂板鎺掑簭
+            List<String> affectedGoodsIds = adjustForInsert(newSort);
+            goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+            // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+            List<String> afterInsertResolvedGoodsIds = resolveDuplicateSorts();
+            goodsIdsToUpdateEs.addAll(afterInsertResolvedGoodsIds);
+        } else if (!oldSort.equals(newSort)) {
+            // 淇敼鎺掑簭
+            if (newSort > oldSort) {
+                // 鎺掑簭鍊煎澶э細灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧+1锛岀洿鍒伴亣鍒版柇灞�
+                List<String> affectedGoodsIds = adjustForIncrease(newSort);
+                goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+                // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+                List<String> afterIncreaseResolvedGoodsIds = resolveDuplicateSorts();
+                goodsIdsToUpdateEs.addAll(afterIncreaseResolvedGoodsIds);
+            } else {
+                // 鎺掑簭鍊煎噺灏忥細灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧+1锛岀洿鍒伴亣鍒版柇灞�
+                List<String> affectedGoodsIds = adjustForDecrease(oldSort, newSort);
+                goodsIdsToUpdateEs.addAll(affectedGoodsIds);
+                // 澶勭悊鍙兘浜х敓鐨勯噸澶嶆帓搴忓��
+                List<String> afterDecreaseResolvedGoodsIds = resolveDuplicateSorts();
+                goodsIdsToUpdateEs.addAll(afterDecreaseResolvedGoodsIds);
+            }
+        }
+        
+        // 鏇存柊褰撳墠鍟嗗搧鐨勬帓搴忓��
         LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
-        updateWrapper.set(Goods::getGoodsSort,req.getSort());
+        updateWrapper.set(Goods::getGoodsSort, newSort);
         updateWrapper.eq(Goods::getId, req.getGoodsId());
         result = this.update(updateWrapper);
-        List<String> goodsIds = new ArrayList<>();
-        goodsIds.add(req.getGoodsId());
-        this.updateEsGoods(goodsIds);
+
+        // 鏇存柊ES绱㈠紩
+        this.updateEsGoods(goodsIdsToUpdateEs);
+
         return result;
+    }
+
+    /**
+     * 澶勭悊鏁版嵁搴撲腑鐨勯噸澶嶆帓搴忓�硷紝纭繚鍞竴鎬�
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public List<String> resolveDuplicateSorts() {
+        // 鏌ヨ鎵�鏈夋湁鎺掑簭鍊肩殑鍟嗗搧
+        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.isNotNull(Goods::getGoodsSort);
+        queryWrapper.orderByAsc(Goods::getGoodsSort);
+        List<Goods> allGoods = this.list(queryWrapper);
+        
+        // 鎸夋帓搴忓�煎垎缁�
+        Map<Integer, List<Goods>> sortMap = allGoods.stream()
+                .collect(Collectors.groupingBy(Goods::getGoodsSort));
+        
+        // 澶勭悊閲嶅鐨勬帓搴忓�硷紝鍙繚鐣欑涓�涓紝鍏朵綑閲嶆柊鍒嗛厤
+        List<Goods> toUpdate = new ArrayList<>();
+        int nextSort = getMaxSortValue(allGoods) + 1; // 浠庡綋鍓嶆渶澶у��+1寮�濮嬪垎閰�
+        for (Map.Entry<Integer, List<Goods>> entry : sortMap.entrySet()) {
+            List<Goods> goodsList = entry.getValue();
+            if (goodsList.size() > 1) {
+                // 鏈夐噸澶嶅�硷紝鍙繚鐣欑涓�涓紝鍏朵綑閲嶆柊鍒嗛厤
+                for (int i = 1; i < goodsList.size(); i++) {
+                    Goods goods = goodsList.get(i);
+                    goods.setGoodsSort(nextSort++);
+                    toUpdate.add(goods);
+                }
+            }
+        }
+        
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
+        // 鎵归噺鏇存柊
+        if (!toUpdate.isEmpty()) {
+            this.updateBatchById(toUpdate);
+        }
+        
+        return affectedGoodsIds;
+    }
+    
+    /**
+     * 鑾峰彇褰撳墠鏈�澶х殑鎺掑簭鍊�
+     * @param goodsList 鍟嗗搧鍒楄〃
+     * @return 鏈�澶ф帓搴忓��
+     */
+    private int getMaxSortValue(List<Goods> goodsList) {
+        return goodsList.stream()
+                .mapToInt(Goods::getGoodsSort)
+                .max()
+                .orElse(0);
+    }
+
+    /**
+     * 鎻掑叆鏂版帓搴忓�肩殑澶勭悊閫昏緫
+     * 灏嗘墍鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧鎺掑簭鍊�+1锛岀洿鍒伴亣鍒版柇灞�
+     * @param newSort 鏂扮殑鎺掑簭鍊�
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public List<String> adjustForInsert(Integer newSort) {
+        if (newSort == null) return Collections.emptyList();
+        
+        // 鏌ヨ鎵�鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧锛屾寜鎺掑簭鍊煎崌搴忔帓鍒�
+        LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery();
+        query.ge(Goods::getGoodsSort, newSort);
+        query.isNotNull(Goods::getGoodsSort);
+        query.orderByAsc(Goods::getGoodsSort);
+        List<Goods> goodsList = this.list(query);
+        
+        // 妫�鏌ヨ繛缁�э紝閬囧埌鏂眰鍒欏仠姝�
+        List<Goods> toUpdate = new ArrayList<>();
+        int expectedSort = newSort;
+        for (Goods goods : goodsList) {
+            if (goods.getGoodsSort().equals(expectedSort)) {
+                goods.setGoodsSort(goods.getGoodsSort() + 1);
+                toUpdate.add(goods);
+                expectedSort++;
+            } else {
+                // 閬囧埌鏂眰锛屽仠姝㈣皟鏁�
+                break;
+            }
+        }
+        
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧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 List<String> adjustForIncrease(Integer newSort) {
+        if (newSort == null) return Collections.emptyList();
+        
+        // 鏌ヨ鎵�鏈夊ぇ浜庣瓑浜庢柊鎺掑簭鍊肩殑鍟嗗搧锛屾寜鎺掑簭鍊煎崌搴忔帓鍒�
+        LambdaQueryWrapper<Goods> query = Wrappers.<Goods>lambdaQuery();
+        query.ge(Goods::getGoodsSort, newSort);
+        query.isNotNull(Goods::getGoodsSort);
+        query.orderByAsc(Goods::getGoodsSort);
+        List<Goods> goodsList = this.list(query);
+        
+        // 妫�鏌ヨ繛缁�э紝閬囧埌鏂眰鍒欏仠姝�
+        List<Goods> toUpdate = new ArrayList<>();
+        int expectedSort = newSort;
+        for (Goods goods : goodsList) {
+            if (goods.getGoodsSort().equals(expectedSort)) {
+                goods.setGoodsSort(goods.getGoodsSort() + 1);
+                toUpdate.add(goods);
+                expectedSort++;
+            } else {
+                // 閬囧埌鏂眰锛屽仠姝㈣皟鏁�
+                break;
+            }
+        }
+        
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
+        // 鎵归噺鏇存柊
+        if (!toUpdate.isEmpty()) {
+            this.updateBatchById(toUpdate);
+        }
+        
+        return affectedGoodsIds;
+    }
+
+    /**
+     * 鎺掑簭鍊煎噺灏忕殑澶勭悊閫昏緫
+     * 灏嗗ぇ浜庣瓑浜庢柊鎺掑簭鍊间笖灏忎簬鍘熸帓搴忓�肩殑鍟嗗搧鎺掑簭鍊�+1锛岀洿鍒伴亣鍒版柇灞�
+     * @param oldSort 鍘熸帓搴忓��
+     * @param newSort 鏂版帓搴忓��
+     * @return 鍙楀奖鍝嶇殑鍟嗗搧ID鍒楄〃
+     */
+    @Transactional(rollbackFor = Exception.class)
+    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.orderByAsc(Goods::getGoodsSort);
+        List<Goods> goodsList = this.list(query);
+        
+        // 妫�鏌ヨ繛缁�э紝閬囧埌鏂眰鍒欏仠姝�
+        List<Goods> toUpdate = new ArrayList<>();
+        int expectedSort = newSort;
+        for (Goods goods : goodsList) {
+            if (goods.getGoodsSort().equals(expectedSort)) {
+                goods.setGoodsSort(goods.getGoodsSort() + 1);
+                toUpdate.add(goods);
+                expectedSort++;
+            } else {
+                // 閬囧埌鏂眰锛屽仠姝㈣皟鏁�
+                break;
+            }
+        }
+        
+        // 鏀堕泦鍙楀奖鍝嶇殑鍟嗗搧ID
+        List<String> affectedGoodsIds = toUpdate.stream()
+                .map(Goods::getId)
+                .collect(Collectors.toList());
+        
+        // 鎵归噺鏇存柊
+        if (!toUpdate.isEmpty()) {
+            this.updateBatchById(toUpdate);
+        }
+        
+        return affectedGoodsIds;
     }
 
     @Override
@@ -587,6 +913,7 @@
         this.baseMapper.addGoodsCommentNum(commentNum, goodsId);
     }
 
+
     /**
      * 鏇存柊鍟嗗搧鐘舵��
      *

--
Gitblit v1.8.0