From 278bc72697e323e1a162f72c8572163033bd12ca Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 22 六月 2026 21:57:56 +0800
Subject: [PATCH] feat: 审批完成自动发货

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 1781 +++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 1,497 insertions(+), 284 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 1bd4a41..faaae71 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -27,6 +27,7 @@
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.*;
+import com.ruoyi.common.utils.excel.ExcelUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.security.LoginUser;
 import com.ruoyi.other.mapper.TempFileMapper;
@@ -53,6 +54,7 @@
 import com.ruoyi.sales.mapper.*;
 import com.ruoyi.sales.pojo.*;
 import com.ruoyi.sales.service.ISalesLedgerProcessRouteService;
+import com.ruoyi.sales.service.ISalesLedgerProcessRouteRecordService;
 import com.ruoyi.sales.service.ISalesLedgerProductProcessBindService;
 import com.ruoyi.sales.service.ISalesLedgerProductProcessService;
 import com.ruoyi.sales.service.ISalesLedgerService;
@@ -74,15 +76,21 @@
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionTemplate;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.net.URLEncoder;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -93,6 +101,7 @@
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -150,6 +159,7 @@
     private final ISalesLedgerProductProcessBindService salesLedgerProductProcessBindService;
 
     private final ISalesLedgerProcessRouteService salesLedgerProcessRouteService;
+    private final ISalesLedgerProcessRouteRecordService salesLedgerProcessRouteRecordService;
 
     private final StockInventoryService stockInventoryService;
     private final StockInRecordMapper stockInRecordMapper;
@@ -158,6 +168,8 @@
     private final StockInRecordService stockInRecordService;
     private final StockOutRecordService stockOutRecordService;
     private final StockUtils stockUtils;
+    private final ShipmentApprovalMapper shipmentApprovalMapper;
+
     @Autowired
     private IApproveProcessService approveProcessService;
 
@@ -181,6 +193,8 @@
     private SysUserMapper sysUserMapper;
 
     private final ICustomerRegionsService customerRegionsService;
+    @Autowired
+    private TransactionTemplate transactionTemplate;
 
     @Override
     public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
@@ -477,7 +491,7 @@
             salesLedger.setIsFh(isFh);
 
             salesLedger.setIsEdit(!isFh);
-            
+
             // 鏍规嵁 reviewStatus 鎺у埗鍙嶅鐩稿叧瀛楁鐨勬樉绀�
             if (salesLedger.getReviewStatus() != null && salesLedger.getReviewStatus() != 2) {
                 // 褰� reviewStatus 涓嶄负 2 鏃讹紝闅愯棌鍙嶅鏃堕棿銆佸弽瀹′汉鍜屽弽瀹′汉ID
@@ -750,21 +764,22 @@
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void saleProcessBind(SalesLedgerProcessRoute salesLedgerProcessRoute) {
-        if (salesLedgerProcessRoute == null) {
+    public void saleProcessBind(SalesLedgerProcessRouteDto salesLedgerProcessRouteDto) {
+        if (salesLedgerProcessRouteDto == null) {
             throw new ServiceException("缁戝畾澶辫触,鏁版嵁涓嶈兘涓虹┖");
         }
 
-        SalesLedger salesLedger = baseMapper.selectById(salesLedgerProcessRoute.getSalesLedgerId());
+        SalesLedger salesLedger = baseMapper.selectById(salesLedgerProcessRouteDto.getSalesLedgerId());
         if (salesLedger == null) {
             throw new ServiceException("缁戝畾澶辫触,閿�鍞鍗曚笉瀛樺湪");
         }
-        ProcessRoute processRoute = processRouteMapper.selectById(salesLedgerProcessRoute.getProcessRouteId());
+        ProcessRoute processRoute = processRouteMapper.selectById(salesLedgerProcessRouteDto.getProcessRouteId());
         if (processRoute == null) {
             throw new ServiceException("缁戝畾澶辫触,宸ヨ壓璺嚎涓嶅瓨鍦�");
         }
         //  娓呴櫎宸茬粦瀹氱殑鏁版嵁
         salesLedgerProcessRouteService.remove(new LambdaQueryWrapper<SalesLedgerProcessRoute>().eq(SalesLedgerProcessRoute::getSalesLedgerId, salesLedger.getId()));
+        salesLedgerProcessRouteRecordService.remove(new LambdaQueryWrapper<SalesLedgerProcessRouteRecord>().eq(SalesLedgerProcessRouteRecord::getSalesLedgerId, salesLedger.getId()));
 
         //  灏嗘暟鎹縼绉诲埌sales_ledger_process_route
         List<ProcessRouteItem> routeItems = processRouteItemMapper.selectList(new LambdaQueryWrapper<ProcessRouteItem>().eq(ProcessRouteItem::getRouteId, processRoute.getId()));
@@ -779,6 +794,43 @@
             salesLedgerProcessRouteList.add(ledgerProcessRoute);
         }
         salesLedgerProcessRouteService.saveBatch(salesLedgerProcessRouteList);
+
+        List<SalesLedgerProcessRoute> savedRoutes = salesLedgerProcessRouteService.list(new LambdaQueryWrapper<SalesLedgerProcessRoute>()
+                .eq(SalesLedgerProcessRoute::getSalesLedgerId, salesLedger.getId())
+                .eq(SalesLedgerProcessRoute::getProcessRouteId, processRoute.getId()));
+        Map<Long, SalesLedgerProcessRoute> routeMap = savedRoutes.stream()
+                .filter(item -> item.getProcessRouteItemId() != null)
+                .collect(Collectors.toMap(SalesLedgerProcessRoute::getProcessRouteItemId, item -> item, (a, b) -> a));
+
+        Map<Long, SalesLedgerProcessRouteRecord> inputRecordMap = new HashMap<>();
+        if (CollectionUtils.isNotEmpty(salesLedgerProcessRouteDto.getRecordList())) {
+            for (SalesLedgerProcessRouteRecord record : salesLedgerProcessRouteDto.getRecordList()) {
+                if (record != null && record.getProcessRouteItemId() != null) {
+                    inputRecordMap.put(record.getProcessRouteItemId(), record);
+                }
+            }
+        }
+
+        List<SalesLedgerProcessRouteRecord> routeRecordList = new ArrayList<>();
+        for (ProcessRouteItem routeItem : routeItems) {
+            SalesLedgerProcessRoute route = routeMap.get(routeItem.getId());
+            if (route == null || route.getId() == null) {
+                continue;
+            }
+            SalesLedgerProcessRouteRecord inputRecord = inputRecordMap.get(routeItem.getId());
+            SalesLedgerProcessRouteRecord record = new SalesLedgerProcessRouteRecord();
+            record.setSalesLedgerId(salesLedger.getId());
+            record.setSalesLedgerProcessRouteId(route.getId());
+            Integer isCompleted = inputRecord != null && inputRecord.getIsCompleted() != null ? inputRecord.getIsCompleted() : 0;
+            record.setIsCompleted(isCompleted);
+            if (Objects.equals(isCompleted, 1)) {
+                record.setCompletedTime(inputRecord != null && inputRecord.getCompletedTime() != null ? inputRecord.getCompletedTime() : LocalDateTime.now());
+            } else {
+                record.setCompletedTime(null);
+            }
+            routeRecordList.add(record);
+        }
+        salesLedgerProcessRouteRecordService.saveBatch(routeRecordList);
     }
 
     /**
@@ -809,6 +861,13 @@
         List<Long> idList = Arrays.stream(ids).filter(Objects::nonNull).collect(Collectors.toList());
         if (CollectionUtils.isEmpty(idList)) {
             return 0;
+        }
+        // 鏍¢獙锛氬凡瀹℃牳鐨勮鍗曚笉鑳藉垹闄�
+        List<SalesLedger> ledgers = salesLedgerMapper.selectBatchIds(idList);
+        for (SalesLedger ledger : ledgers) {
+            if (ledger.getReviewStatus() != null && ledger.getReviewStatus() == 1) {
+                throw new ServiceException("宸插鏍哥殑璁㈠崟涓嶈兘鍒犻櫎锛�" + ledger.getSalesContractNo());
+            }
         }
         // 鍒犻櫎閿�鍞鐞嗘暟鎹�
         LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
@@ -904,11 +963,25 @@
                 if (salesLedger.getDeliveryStatus() == 5) {
                     throw new ServiceException("璁㈠崟宸插彂璐�,绂佹缂栬緫");
                 }
+                // 鏌ヨ鏁版嵁搴撲腑鐨勫師濮嬭褰曠敤浜庢牎楠�
+                SalesLedger existingLedger = salesLedgerMapper.selectById(salesLedger.getId());
                 if (salesLedger.getReviewStatus() != null && salesLedger.getReviewStatus() == 1) {
+                    // 瀹℃牳鎿嶄綔锛氭牎楠屽鏍镐汉涓嶈兘鏄綍鍏ヤ汉锛堜粎瀵规湭瀹℃牳鈫掑凡瀹℃牳鐨勮浆鎹級
+                    if (existingLedger != null && existingLedger.getReviewStatus() != null && existingLedger.getReviewStatus() == 0) {
+                        Long currentUserId = SecurityUtils.getUserId();
+                        String entryPerson = existingLedger.getEntryPerson();
+                        if (entryPerson != null && entryPerson.equals(String.valueOf(currentUserId))) {
+                            throw new ServiceException("涓嶈兘瀹℃牳鏈汉褰曞叆鐨勮鍗�");
+                        }
+                    }
                     salesLedger.setReviewStatus(salesLedgerDto.getReviewStatus());
                 } else if (salesLedger.getReviewStatus() != null && salesLedger.getReviewStatus() == 2) {
                     handleCounterReview(salesLedger);
                 } else {
+                    // 鏈鏍哥姸鎬佺殑缂栬緫鎿嶄綔锛氭牎楠屽凡瀹℃牳鐨勮鍗曚笉鑳界紪杈�
+                    if (existingLedger != null && existingLedger.getReviewStatus() != null && existingLedger.getReviewStatus() == 1) {
+                        throw new ServiceException("宸插鏍哥殑璁㈠崟涓嶈兘缂栬緫");
+                    }
                     salesLedger.setReviewStatus(0);
                 }
                 salesLedgerMapper.updateById(salesLedger);
@@ -1099,7 +1172,7 @@
     @Transactional(readOnly = true)
     public String generateSalesContractNo() {
         LocalDate currentDate = LocalDate.now();
-        String datePart = currentDate.format(DateTimeFormatter.BASIC_ISO_DATE);
+        String datePart = currentDate.format(DateTimeFormatter.ofPattern("yyMMdd"));
         String lockKey = LOCK_PREFIX + datePart;
         String lockValue = Thread.currentThread().getId() + "-" + System.nanoTime(); // 鍞竴鏍囪瘑閿佹寔鏈夎��
 
@@ -1150,6 +1223,11 @@
     public SalesLedgerProcessRouteDto salesProcess(Long salesLedgerId) {
         SalesLedgerProcessRouteDto dto = new SalesLedgerProcessRouteDto();
         List<SalesLedgerProcessRoute> list = baseMapper.selectSalesProcess(salesLedgerId);
+        List<SalesLedgerProcessRouteRecord> recordList = salesLedgerProcessRouteRecordService.list(
+                new LambdaQueryWrapper<SalesLedgerProcessRouteRecord>()
+                        .eq(SalesLedgerProcessRouteRecord::getSalesLedgerId, salesLedgerId)
+                        .orderByAsc(SalesLedgerProcessRouteRecord::getId)
+        );
         if (CollectionUtils.isNotEmpty(list)) {
             Long processRouteId = list.get(0).getProcessRouteId();
             ProcessRoute processRoute = processRouteMapper.selectById(processRouteId);
@@ -1176,6 +1254,17 @@
             }
         }
         dto.setList(list);
+        if (CollectionUtils.isNotEmpty(list) && CollectionUtils.isNotEmpty(recordList)) {
+            Map<Long, Long> routeItemIdMap = list.stream()
+                    .filter(item -> item.getId() != null && item.getProcessRouteItemId() != null)
+                    .collect(Collectors.toMap(SalesLedgerProcessRoute::getId, SalesLedgerProcessRoute::getProcessRouteItemId, (a, b) -> a));
+            recordList.forEach(record -> {
+                if (record != null && record.getSalesLedgerProcessRouteId() != null) {
+                    record.setProcessRouteItemId(routeItemIdMap.get(record.getSalesLedgerProcessRouteId()));
+                }
+            });
+        }
+        dto.setRecordList(recordList);
         return dto;
     }
 
@@ -1744,7 +1833,7 @@
         LoginUser loginUser = SecurityUtils.getLoginUser();
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
-        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveDeptId(208L);
         approveProcessVO.setApproveReason("鍏ュ簱瀹℃壒:" + ledger.getSalesContractNo());
         approveProcessVO.setApproveRemark("salesStock:" + ledger.getId() + ":" + productIds);
         approveProcessVO.setApproveUserIds(approveUserIds);
@@ -1821,7 +1910,11 @@
             BigDecimal stockedQty = item.getStockedQuantity() == null ? BigDecimal.ZERO : item.getStockedQuantity();
             return orderQty.compareTo(BigDecimal.ZERO) <= 0 || stockedQty.compareTo(orderQty) >= 0;
         });
-        ledger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0));
+        int newStockStatus = allStocked ? 2 : (hasStocked ? 1 : 0);
+        if (newStockStatus > 0 && ledger.getDeliveryDate() == null) {
+            ledger.setDeliveryDate(LocalDate.now().plusDays(7));
+        }
+        ledger.setStockStatus(newStockStatus);
         baseMapper.updateById(ledger);
     }
 
@@ -1911,7 +2004,7 @@
         LoginUser loginUser = SecurityUtils.getLoginUser();
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
-        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveDeptId(208L);
         approveProcessVO.setApproveReason(reason);
         approveProcessVO.setApproveRemark(remark);
         approveProcessVO.setApproveUserIds(approveUserIds);
@@ -1986,7 +2079,11 @@
             return sq != null && sq.compareTo(BigDecimal.ZERO) > 0;
         });
         boolean allLinesFull = ledgerAllProducts.stream().allMatch(p -> Objects.equals(p.getProductStockStatus(), 2));
-        salesLedger.setStockStatus(allLinesFull ? 2 : (anyInbound ? 1 : 0));
+        int newStockStatus = allLinesFull ? 2 : (anyInbound ? 1 : 0);
+        if (newStockStatus > 0 && salesLedger.getDeliveryDate() == null) {
+            salesLedger.setDeliveryDate(LocalDate.now().plusDays(7));
+        }
+        salesLedger.setStockStatus(newStockStatus);
         baseMapper.updateById(salesLedger);
     }
 
@@ -2066,7 +2163,7 @@
         LoginUser loginUser = SecurityUtils.getLoginUser();
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
-        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveDeptId(208L);
         approveProcessVO.setApproveReason(reason);
         approveProcessVO.setApproveRemark(remark);
         approveProcessVO.setApproveUserIds(approveUserIds);
@@ -2359,7 +2456,6 @@
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
     public void shippingImport(MultipartFile file) {
         if (file == null || file.isEmpty()) {
             throw new ServiceException("瀵煎叆澶辫触,瀵煎叆鏂囦欢鏁版嵁涓嶈兘涓虹┖");
@@ -2377,105 +2473,246 @@
         }
         Map<String, List<SalesShippingImportDto>> groupedByOrderNo = list.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(SalesShippingImportDto::getOrderNo, LinkedHashMap::new, Collectors.toList()));
 
-        for (Map.Entry<String, List<SalesShippingImportDto>> entry : groupedByOrderNo.entrySet()) {
-            String orderNo = entry.getKey();
-            if (!StringUtils.hasText(orderNo)) {
-                throw new ServiceException("瀵煎叆澶辫触,瀛樺湪璁㈠崟缂栧彿涓虹┖鐨勬暟鎹�");
-            }
-            List<SalesShippingImportDto> rowList = entry.getValue();
-            if (CollectionUtils.isEmpty(rowList)) {
-                continue;
-            }
-            SalesLedger ledger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>().eq(SalesLedger::getSalesContractNo, orderNo).last("LIMIT 1"));
-            if (ledger == null) {
-                throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]涓嶅瓨鍦�,鏃犳硶琛ュ綍宸插彂璐ф暟鎹�");
-            }
-            List<SalesLedgerProduct> dbProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()).eq(SalesLedgerProduct::getType, SaleEnum.SALE.getCode()));
-            if (CollectionUtils.isEmpty(dbProducts)) {
-                throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]娌℃湁閿�鍞骇鍝�,鏃犳硶琛ュ綍鍙戣揣");
-            }
-            Map<String, List<SalesLedgerProduct>> productByCategory = dbProducts.stream().collect(Collectors.groupingBy(p -> StringUtils.hasText(p.getProductCategory()) ? p.getProductCategory().trim() : ""));
-            Set<String> importedRowKeys = new HashSet<>();
+//        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+//        List<String> errorMessages = Collections.synchronizedList(new ArrayList<>());
+        groupedByOrderNo.forEach((orderNo, rowList) -> {
+//            SecurityContext originalContext = SecurityContextHolder.getContext();
+//            SecurityContext ctx = SecurityContextHolder.createEmptyContext();
+//            ctx.setAuthentication(authentication);
+//            SecurityContextHolder.setContext(ctx);
+            try {
+                if (!StringUtils.hasText(orderNo)) {
+                    throw new ServiceException("瀵煎叆澶辫触,瀛樺湪璁㈠崟缂栧彿涓虹┖鐨勬暟鎹�");
+                }
+                if (CollectionUtils.isEmpty(rowList)) {
+                    return;
+                }
+                if (StringUtils.hasText(rowList.get(0).getCustomerName()) && rowList.get(0).getCustomerName().startsWith("琛ョ墖")) {
+                    return;
+                }
+                rowList.sort(Comparator.comparing(r -> buildCategoryProductName(r) + "||" + buildSpecificationModel(r)));
+                SalesLedger ledger = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>().eq(SalesLedger::getSalesContractNo, orderNo).last("LIMIT 1"));
+                if (ledger == null) {
+//                throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]涓嶅瓨鍦�,鏃犳硶琛ュ綍宸插彂璐ф暟鎹�");
+                    return;
+                }
+                List<SalesLedgerProduct> dbProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()).eq(SalesLedgerProduct::getType, SaleEnum.SALE.getCode()));
+                if (CollectionUtils.isEmpty(dbProducts)) {
+                    throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]娌℃湁閿�鍞骇鍝�,鏃犳硶琛ュ綍鍙戣揣");
+                }
+                dbProducts.sort(Comparator.comparing(SalesLedgerProduct::getProductModelId, Comparator.nullsLast(Comparator.naturalOrder())));
+                Map<String, List<SalesLedgerProduct>> productByCategory = dbProducts.stream().collect(Collectors.groupingBy(p -> StringUtils.hasText(p.getProductCategory()) ? p.getProductCategory().trim() : ""));
+                Set<String> importedRowKeys = new HashSet<>();
 
-            for (SalesShippingImportDto row : rowList) {
-                BigDecimal shipQty = defaultDecimal(row.getQuantity());
-                if (shipQty.compareTo(BigDecimal.ZERO) <= 0) {
-                    throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪鏁伴噺灏忎簬绛変簬0鐨勬暟鎹�");
-                }
-                String rowKey = buildShippingRowKey(ledger.getId(), row);
-                if (!importedRowKeys.add(rowKey)) {
-                    throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪閲嶅鍙戣揣鏄庣粏琛�");
-                }
-                Map<SalesLedgerProduct, BigDecimal> allocations = allocateShippingProductLines(orderNo, row, productByCategory, dbProducts);
-                for (Map.Entry<SalesLedgerProduct, BigDecimal> alloc : allocations.entrySet()) {
-                    SalesLedgerProduct dbProduct = alloc.getKey();
-                    BigDecimal allocQty = alloc.getValue();
-                    if (dbProduct.getProductModelId() == null) {
-                        throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]浜у搧瑙勬牸鏈淮鎶�,鏃犳硶琛ュ綍鍑哄簱");
+                for (SalesShippingImportDto row : rowList) {
+                    BigDecimal shipQty = defaultDecimal(row.getQuantity());
+                    if (shipQty.compareTo(BigDecimal.ZERO) <= 0) {
+                        throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪鏁伴噺灏忎簬绛変簬0鐨勬暟鎹�");
                     }
-                    // 鍘嗗彶宸插彂璐цˉ褰曪細鐩存帴鍐欏叆鍏ュ簱+鍑哄簱璁板綍
-                    stockUtils.addStock(
-                            ledger.getId(),
-                            dbProduct.getId(),
-                            dbProduct.getProductModelId(),
-                            allocQty,
-                            StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode(),
-                            dbProduct.getId()
-                    );
-                    stockUtils.substractStock(
-                            ledger.getId(),
-                            dbProduct.getId(),
-                            dbProduct.getProductModelId(),
-                            allocQty,
-                            StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
-                            dbProduct.getId()
-                    );
-                    BigDecimal oldShipped = defaultDecimal(dbProduct.getShippedQuantity());
-                    BigDecimal newShipped = oldShipped.add(allocQty);
-                    dbProduct.setStockedQuantity(defaultDecimal(dbProduct.getQuantity()));
-                    dbProduct.setShippedQuantity(newShipped);
-                    dbProduct.setApproveStatus(3);
-                    updateProductStockStatus(dbProduct);
-                    dbProduct.fillRemainingQuantity();
-                    updateProductShipStatus(dbProduct);
-                    salesLedgerProductMapper.updateById(dbProduct);
-
-                    ShippingInfo shippingInfo = new ShippingInfo();
-                    shippingInfo.setSalesLedgerId(ledger.getId());
-                    shippingInfo.setSalesLedgerProductId(dbProduct.getId());
-                    shippingInfo.setStatus("宸插彂璐�");
-                    shippingInfo.setShippingNo(row.getShippingNo());
-                    shippingInfo.setType("璐ц溅");
-                    shippingInfo.setShippingCarNumber("鏃�");
-                    shippingInfo.setShippingDate(row.getReportDate());
-                    long existedShippingCount = shippingInfoMapper.selectCount(new LambdaQueryWrapper<ShippingInfo>().eq(ShippingInfo::getSalesLedgerId, ledger.getId()).eq(ShippingInfo::getSalesLedgerProductId, dbProduct.getId()).eq(StringUtils.hasText(row.getShippingNo()), ShippingInfo::getShippingNo, row.getShippingNo()).eq(row.getReportDate() != null, ShippingInfo::getShippingDate, row.getReportDate()));
-                    if (existedShippingCount > 0) {
-                        throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪閲嶅鍙戣揣璁板綍,璇峰嬁閲嶅瀵煎叆");
+                    String rowKey = buildShippingRowKey(ledger.getId(), row);
+                    if (!importedRowKeys.add(rowKey)) {
+                        throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪閲嶅鍙戣揣鏄庣粏琛�");
                     }
-                    shippingInfoMapper.insert(shippingInfo);
-                    createShippingQualityInspect(ledger, dbProduct, row, allocQty);
-                }
-            }
+                    Map<SalesLedgerProduct, BigDecimal> allocations = allocateShippingProductLines(orderNo, row, productByCategory, dbProducts);
+                    for (Map.Entry<SalesLedgerProduct, BigDecimal> alloc : allocations.entrySet()) {
+                        SalesLedgerProduct dbProduct = alloc.getKey();
+                        BigDecimal allocQty = alloc.getValue();
+                        if (dbProduct.getProductModelId() == null) {
+                            throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]浜у搧瑙勬牸鏈淮鎶�,鏃犳硶琛ュ綍鍑哄簱");
+                        }
+                        Long inspectId = createShippingQualityInspect(ledger, dbProduct, row, allocQty);
+                        // 鍘嗗彶宸插彂璐цˉ褰曪細鐩存帴鍐欏叆鍏ュ簱+鍑哄簱璁板綍
+                        stockUtils.addStock(
+                                ledger.getId(),
+                                dbProduct.getId(),
+                                dbProduct.getProductModelId(),
+                                allocQty,
+                                StockInQualifiedRecordTypeEnum.SALE_QC_OK_IN.getCode(),
+                                inspectId != null ? inspectId : dbProduct.getId()
+                        );
+                        stockUtils.substractStock(
+                                ledger.getId(),
+                                dbProduct.getId(),
+                                dbProduct.getProductModelId(),
+                                allocQty,
+                                StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
+                                dbProduct.getId()
+                        );
 
-            List<SalesLedgerProduct> latestProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()).eq(SalesLedgerProduct::getType, SaleEnum.SALE.getCode()));
-            boolean allShipped = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
-                BigDecimal qty = defaultDecimal(p.getQuantity());
-                BigDecimal shipped = defaultDecimal(p.getShippedQuantity());
-                return shipped.compareTo(qty) >= 0;
-            });
-            boolean anyInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().anyMatch(p -> defaultDecimal(p.getStockedQuantity()).compareTo(BigDecimal.ZERO) > 0);
-            boolean allInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
-                BigDecimal qty = defaultDecimal(p.getQuantity());
-                BigDecimal stocked = defaultDecimal(p.getStockedQuantity());
-                return qty.compareTo(BigDecimal.ZERO) <= 0 || stocked.compareTo(qty) >= 0;
-            });
-            if (allShipped && rowList.get(0).getReportDate() != null) {
-                ledger.setDeliveryDate(DateUtils.toLocalDate(rowList.get(0).getReportDate()));
-            }
-            ledger.setStockStatus(allInbound ? 2 : (anyInbound ? 1 : 0));
-            ledger.setDeliveryStatus(allShipped ? 5 : 1);
-            salesLedgerMapper.updateById(ledger);
-        }
+                        StockInRecord inRecord = stockInRecordMapper.selectOne(new LambdaQueryWrapper<StockInRecord>()
+                                .eq(StockInRecord::getSalesLedgerProductId, dbProduct.getId())
+                                .orderByDesc(StockInRecord::getId).last("LIMIT 1"));
+                        if (inRecord != null) {
+                            String batch = "RK";
+                            if (row.getReportDate() != null) {
+                                String dateStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(row.getReportDate());
+                                batch += dateStr + "-" + row.getReportDate().getTime();
+                            } else {
+                                batch += System.currentTimeMillis() + "-" + inRecord.getId();
+                            }
+                            inRecord.setInboundBatches(batch);
+                            if (row.getReportDate() != null) {
+                                LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+                                inRecord.setCreateTime(reportDateTime);
+                                inRecord.setUpdateTime(reportDateTime);
+                            }
+                            stockInRecordMapper.updateById(inRecord);
+                        }
+
+                        StockOutRecord outRecord = stockOutRecordMapper.selectOne(new LambdaQueryWrapper<StockOutRecord>()
+                                .eq(StockOutRecord::getSalesLedgerProductId, dbProduct.getId())
+                                .orderByDesc(StockOutRecord::getId).last("LIMIT 1"));
+                        if (outRecord != null) {
+                            String batch = "CK";
+                            if (row.getReportDate() != null) {
+                                String dateStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(row.getReportDate());
+                                batch += dateStr + "-" + row.getReportDate().getTime();
+                            } else {
+                                batch += System.currentTimeMillis() + "-" + outRecord.getId();
+                            }
+                            outRecord.setOutboundBatches(batch);
+                            if (row.getReportDate() != null) {
+                                LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+                                outRecord.setCreateTime(reportDateTime);
+                                outRecord.setUpdateTime(reportDateTime);
+                            }
+                            stockOutRecordMapper.updateById(outRecord);
+                        }
+
+                        BigDecimal oldShipped = defaultDecimal(dbProduct.getShippedQuantity());
+                        BigDecimal newShipped = oldShipped.add(allocQty);
+                        dbProduct.setStockedQuantity(defaultDecimal(dbProduct.getQuantity()));
+                        dbProduct.setShippedQuantity(newShipped);
+                        dbProduct.setApproveStatus(3);
+                        updateProductStockStatus(dbProduct);
+                        dbProduct.fillRemainingQuantity();
+                        updateProductShipStatus(dbProduct);
+                        salesLedgerProductMapper.updateById(dbProduct);
+
+                        ShippingInfo shippingInfo = new ShippingInfo();
+                        shippingInfo.setSalesLedgerId(ledger.getId());
+                        shippingInfo.setSalesLedgerProductId(dbProduct.getId());
+                        shippingInfo.setStatus("宸插彂璐�");
+                        shippingInfo.setShippingNo(row.getShippingNo());
+                        shippingInfo.setType("璐ц溅");
+                        shippingInfo.setShippingCarNumber("鏃�");
+                        shippingInfo.setShippingDate(row.getReportDate());
+                        long existedShippingCount = shippingInfoMapper.selectCount(new LambdaQueryWrapper<ShippingInfo>()
+                                .eq(ShippingInfo::getSalesLedgerId, ledger.getId())
+                                .eq(ShippingInfo::getSalesLedgerProductId, dbProduct.getId())
+                                .eq(StringUtils.hasText(row.getShippingNo()), ShippingInfo::getShippingNo, row.getShippingNo())
+                                .eq(row.getReportDate() != null, ShippingInfo::getShippingDate, row.getReportDate()));
+                        if (existedShippingCount > 0) {
+//                        continue;
+                            List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
+                                    .eq(ShippingInfo::getSalesLedgerId, ledger.getId())
+                                    .eq(ShippingInfo::getSalesLedgerProductId, dbProduct.getId())
+                                    .eq(StringUtils.hasText(row.getShippingNo()), ShippingInfo::getShippingNo, row.getShippingNo())
+                                    .eq(row.getReportDate() != null, ShippingInfo::getShippingDate, row.getReportDate()));
+                            throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪閲嶅鍙戣揣璁板綍,璇峰嬁閲嶅瀵煎叆");
+                        }
+                        shippingInfoMapper.insert(shippingInfo);
+                        // createShippingQualityInspect moved up
+                    }
+                }
+
+                List<SalesLedgerProduct> latestProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()).eq(SalesLedgerProduct::getType, SaleEnum.SALE.getCode()));
+                boolean allShipped = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
+                    BigDecimal qty = defaultDecimal(p.getQuantity());
+                    BigDecimal shipped = defaultDecimal(p.getShippedQuantity());
+                    return shipped.compareTo(qty) >= 0;
+                });
+                boolean anyInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().anyMatch(p -> defaultDecimal(p.getStockedQuantity()).compareTo(BigDecimal.ZERO) > 0);
+                boolean allInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
+                    BigDecimal qty = defaultDecimal(p.getQuantity());
+                    BigDecimal stocked = defaultDecimal(p.getStockedQuantity());
+                    return qty.compareTo(BigDecimal.ZERO) <= 0 || stocked.compareTo(qty) >= 0;
+                });
+                if (allShipped && rowList.get(0).getReportDate() != null) {
+                    ledger.setDeliveryDate(DateUtils.toLocalDate(rowList.get(0).getReportDate()));
+                }
+                ledger.setStockStatus(allInbound ? 2 : (anyInbound ? 1 : 0));
+                ledger.setDeliveryStatus(allShipped ? 5 : 1);
+//            ledger.setReviewStatus(1);
+
+                Long entryUserId = StringUtils.hasText(ledger.getEntryPerson()) ? Long.parseLong(ledger.getEntryPerson()) : 1L;
+                Long entryDeptId = 1L;
+                if (StringUtils.hasText(ledger.getEntryPerson())) {
+                    try {
+                        SysUser entryUser = sysUserMapper.selectById(ledger.getEntryPerson());
+                        if (entryUser != null && entryUser.getDeptId() != null) {
+                            entryDeptId = entryUser.getDeptId();
+                        }
+                    } catch (Exception ignored) {
+                    }
+                }
+                String reportDateStr = rowList.get(0).getReportDate() != null ? new java.text.SimpleDateFormat("yyyy-MM-dd").format(rowList.get(0).getReportDate()) : java.time.LocalDate.now().toString();
+
+                try {
+                    String productIds = latestProducts.stream().map(p -> String.valueOf(p.getId())).collect(Collectors.joining(","));
+                    String inboundApproveUserIds = resolveApproveUserIds(null, ledger.getId(), INBOUND_BIZ_TYPE_WEB);
+                    if (StringUtils.isEmpty(inboundApproveUserIds)) {
+                        inboundApproveUserIds = "1";
+                    }
+
+                    ApproveProcessVO stockInVo = new ApproveProcessVO();
+                    stockInVo.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
+                    stockInVo.setApproveDeptId(208L);
+                    stockInVo.setApproveReason("鍏ュ簱瀹℃壒:" + ledger.getSalesContractNo());
+                    stockInVo.setApproveRemark("salesStock:" + ledger.getId() + ":" + productIds);
+                    stockInVo.setApproveUserIds(inboundApproveUserIds);
+                    stockInVo.setApproveUser(entryUserId);
+                    stockInVo.setApproveTime(reportDateStr);
+                    approveProcessService.addApprove(stockInVo);
+
+                    ApproveProcess savedStockIn = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                            .eq(ApproveProcess::getApproveReason, stockInVo.getApproveReason())
+                            .eq(ApproveProcess::getApproveType, stockInVo.getApproveType())
+                            .orderByDesc(ApproveProcess::getId).last("LIMIT 1"));
+                    if (savedStockIn != null) {
+                        savedStockIn.setApproveStatus(2);
+                        savedStockIn.setApproveOverTime(rowList.get(0).getReportDate());
+                        approveProcessService.updateById(savedStockIn);
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鐢熸垚鍏ュ簱瀹℃壒鐢宠澶辫触", e);
+                    throw new ServiceException("鐢熸垚鍏ュ簱鐢宠瀹℃壒澶辫触:{}" + e.getMessage());
+                }
+
+                try {
+                    ApproveProcessVO deliveryVo = new ApproveProcessVO();
+                    deliveryVo.setApproveType(ApproveTypeEnum.DELIVERY.getCode());
+                    deliveryVo.setApproveDeptId(208L);
+                    deliveryVo.setApproveReason("鍙戣揣瀹℃壒:" + ledger.getSalesContractNo());
+                    deliveryVo.setApproveUserIds("1");
+                    deliveryVo.setApproveUser(entryUserId);
+                    deliveryVo.setApproveTime(reportDateStr);
+                    approveProcessService.addApprove(deliveryVo);
+
+                    ApproveProcess savedDelivery = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                            .eq(ApproveProcess::getApproveReason, deliveryVo.getApproveReason())
+                            .eq(ApproveProcess::getApproveType, deliveryVo.getApproveType())
+                            .orderByDesc(ApproveProcess::getId).last("LIMIT 1"));
+                    if (savedDelivery != null) {
+                        savedDelivery.setApproveStatus(2);
+                        savedDelivery.setApproveOverTime(rowList.get(0).getReportDate());
+                        approveProcessService.updateById(savedDelivery);
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鐢熸垚鍙戣揣瀹℃壒鐢宠澶辫触", e);
+                    throw new ServiceException("鐢熸垚鍙戣揣鐢宠瀹℃壒澶辫触:{}" + e.getMessage());
+                }
+
+                    salesLedgerMapper.updateById(ledger);
+                } catch (Exception e) {
+                    log.error("璁㈠崟 {} 宸插彂璐ц褰曞鍏ュけ璐�", orderNo, e);
+//                    errorMessages.add("璁㈠崟 [" + orderNo + "] 瀵煎叆澶辫触: " + e.getMessage());
+                }
+//            SecurityContextHolder.setContext(originalContext);
+        });
+//        if (!errorMessages.isEmpty()) {
+//            throw new ServiceException("閮ㄥ垎鏁版嵁瀵煎叆澶辫触:\n" + String.join("\n", errorMessages));
+//        }
     }
 
     @Override
@@ -2496,10 +2733,10 @@
             throw new ServiceException("瀵煎叆澶辫触,鏂囦欢鏁版嵁涓虹┖");
         }
         List<SysUser> allUsers = sysUserMapper.selectList(null);
-        Map<String, SysUser> userByNickNameMap = allUsers.stream().filter(Objects::nonNull).filter(u -> StringUtils.hasText(u.getNickName())).collect(Collectors.toMap(SysUser::getNickName, Function.identity(), (a, b) -> a));
-        Map<String, SysUser> userByUserNameMap = allUsers.stream().filter(Objects::nonNull).filter(u -> StringUtils.hasText(u.getUserName())).collect(Collectors.toMap(SysUser::getUserName, Function.identity(), (a, b) -> a));
+        Map<String, SysUser> userByNickNameMap = new ConcurrentHashMap<>(allUsers.stream().filter(Objects::nonNull).filter(u -> StringUtils.hasText(u.getNickName())).collect(Collectors.toMap(SysUser::getNickName, Function.identity(), (a, b) -> a)));
+        Map<String, SysUser> userByUserNameMap = new ConcurrentHashMap<>(allUsers.stream().filter(Objects::nonNull).filter(u -> StringUtils.hasText(u.getUserName())).collect(Collectors.toMap(SysUser::getUserName, Function.identity(), (a, b) -> a)));
 
-        Map<String, Customer> customerNameMap = customerMapper.selectList(null).stream().filter(Objects::nonNull).filter(c -> StringUtils.hasText(c.getCustomerName())).collect(Collectors.toMap(Customer::getCustomerName, Function.identity(), (a, b) -> a));
+        Map<String, Customer> customerNameMap = new ConcurrentHashMap<>(customerMapper.selectList(null).stream().filter(Objects::nonNull).filter(c -> StringUtils.hasText(c.getCustomerName())).collect(Collectors.toMap(Customer::getCustomerName, Function.identity(), (a, b) -> a)));
         List<CustomerRegions> allRegions = customerRegionsService.list();
         CustomerRegions hebiRegion = allRegions.stream().filter(Objects::nonNull).filter(r -> "楣ゅ".equals(r.getRegionsName())).findFirst().orElseGet(() -> {
             CustomerRegions region = new CustomerRegions();
@@ -2508,7 +2745,7 @@
             customerRegionsService.save(region);
             return region;
         });
-        Map<String, Product> productNameMap = productMapper.selectList(null).stream().filter(Objects::nonNull).filter(p -> StringUtils.hasText(p.getProductName())).collect(Collectors.toMap(Product::getProductName, Function.identity(), (a, b) -> a));
+        Map<String, Product> productNameMap = new ConcurrentHashMap<>(productMapper.selectList(null).stream().filter(Objects::nonNull).filter(p -> StringUtils.hasText(p.getProductName())).collect(Collectors.toMap(Product::getProductName, Function.identity(), (a, b) -> a)));
         Product finishedGoodsParent = productNameMap.get("鎴愬搧");
         if (finishedGoodsParent == null || finishedGoodsParent.getId() == null) {
             finishedGoodsParent = new Product();
@@ -2517,120 +2754,317 @@
             productMapper.insert(finishedGoodsParent);
             productNameMap.put("鎴愬搧", finishedGoodsParent);
         }
-        Map<String, ProductModel> productModelKeyMap = productModelMapper.selectList(null).stream().filter(Objects::nonNull).filter(m -> m.getProductId() != null && StringUtils.hasText(m.getModel())).collect(Collectors.toMap(m -> buildProductModelKey(m.getProductId(), m.getModel()), Function.identity(), (a, b) -> a));
+        Map<String, ProductModel> productModelKeyMap = new ConcurrentHashMap<>(productModelMapper.selectList(null).stream().filter(Objects::nonNull).filter(m -> m.getProductId() != null && StringUtils.hasText(m.getModel())).collect(Collectors.toMap(m -> buildProductModelKey(m.getProductId(), m.getModel()), Function.identity(), (a, b) -> a)));
 
-        List<String> extraProcessNames = Arrays.asList("鎵撳瓟", "鎸栫己", "瀹夊叏瑙�", "纾ㄨ竟");
-        Map<String, SalesLedgerProductProcess> processMap = salesLedgerProductProcessService.list(new LambdaQueryWrapper<SalesLedgerProductProcess>().in(SalesLedgerProductProcess::getProcessName, extraProcessNames)).stream().filter(Objects::nonNull).filter(p -> StringUtils.hasText(p.getProcessName())).collect(Collectors.toMap(SalesLedgerProductProcess::getProcessName, Function.identity(), (a, b) -> a));
+        List<String> extraProcessNames = Arrays.asList("鎵撳瓟", "鎸栫己", "瀹夊叏瑙�", "纾ㄨ竟", "绮剧(杈�", "杩愯垂", "鍔犳�ヨ垂");
+        Map<String, SalesLedgerProductProcess> processMap = new ConcurrentHashMap<>(salesLedgerProductProcessService.list(new LambdaQueryWrapper<SalesLedgerProductProcess>().in(SalesLedgerProductProcess::getProcessName, extraProcessNames)).stream().filter(Objects::nonNull).filter(p -> StringUtils.hasText(p.getProcessName())).collect(Collectors.toMap(SalesLedgerProductProcess::getProcessName, Function.identity(), (a, b) -> a)));
         List<ProcessRoute> processRoutes = processRouteMapper.selectList(new LambdaQueryWrapper<ProcessRoute>().eq(ProcessRoute::getProductModelId, 0L));
-        Map<String, ProcessRoute> routeNameMap = processRoutes.stream().filter(Objects::nonNull).filter(r -> StringUtils.hasText(r.getProcessRouteName())).collect(Collectors.toMap(r -> normalizeRouteFlowKey(r.getProcessRouteName()), Function.identity(), this::chooseBetterRoute));
-        Map<Long, List<ProcessRouteItem>> routeItemMap = Collections.emptyMap();
+        Map<String, ProcessRoute> routeNameMap = new ConcurrentHashMap<>(processRoutes.stream().filter(Objects::nonNull).filter(r -> StringUtils.hasText(r.getProcessRouteName())).collect(Collectors.toMap(r -> normalizeRouteFlowKey(r.getProcessRouteName()), Function.identity(), this::chooseBetterRoute)));
+        Map<Long, List<ProcessRouteItem>> routeItemMap = new ConcurrentHashMap<>();
         List<Long> routeIds = processRoutes.stream().map(ProcessRoute::getId).filter(Objects::nonNull).collect(Collectors.toList());
         if (CollectionUtils.isNotEmpty(routeIds)) {
-            routeItemMap = processRouteItemMapper.selectList(new LambdaQueryWrapper<ProcessRouteItem>().in(ProcessRouteItem::getRouteId, routeIds).orderByAsc(ProcessRouteItem::getDragSort).orderByAsc(ProcessRouteItem::getId)).stream().filter(Objects::nonNull).collect(Collectors.groupingBy(ProcessRouteItem::getRouteId));
+            routeItemMap.putAll(processRouteItemMapper.selectList(new LambdaQueryWrapper<ProcessRouteItem>().in(ProcessRouteItem::getRouteId, routeIds).orderByAsc(ProcessRouteItem::getDragSort).orderByAsc(ProcessRouteItem::getId)).stream().filter(Objects::nonNull).collect(Collectors.groupingBy(ProcessRouteItem::getRouteId)));
         }
+
+        final Map<Long, List<ProcessRouteItem>> finalRouteItemMap = routeItemMap;
+        final Product finalFinishedGoodsParent = finishedGoodsParent;
 
         Map<String, List<SalesNotShippingImportDto>> groupedByOrderNo = list.stream().filter(Objects::nonNull).collect(Collectors.groupingBy(SalesNotShippingImportDto::getOrderNo, LinkedHashMap::new, Collectors.toList()));
 
-        for (Map.Entry<String, List<SalesNotShippingImportDto>> entry : groupedByOrderNo.entrySet()) {
-            String orderNo = entry.getKey();
-            if (!StringUtils.hasText(orderNo)) {
-                throw new ServiceException("瀵煎叆澶辫触,瀛樺湪璁㈠崟缂栧彿涓虹┖鐨勬暟鎹�");
-            }
-            List<SalesNotShippingImportDto> rowList = entry.getValue();
-            if (CollectionUtils.isEmpty(rowList)) {
-                continue;
-            }
-            SalesNotShippingImportDto first = rowList.get(0);
-            SalesLedger exists = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>().eq(SalesLedger::getSalesContractNo, orderNo).last("LIMIT 1"));
-            if (exists != null) {
-                throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]宸插瓨鍦�");
-            }
-
-            SalesLedger ledger = new SalesLedger();
-            SysUser creatorUser = resolveImportUser(first.getCreator(), userByNickNameMap, userByUserNameMap, "鍒跺崟鍛�", orderNo);
-            ledger.setSalesContractNo(orderNo);
-            ledger.setCustomerContractNo(first.getContractNo());
-            ledger.setProjectName(first.getProjectName());
-            ledger.setSalesman(first.getSalesman());
-            Customer customer = getOrCreateImportCustomer(first.getCustomerName(), customerNameMap, hebiRegion.getId());
-            ledger.setCustomerName(customer.getCustomerName());
-            ledger.setRemarks(first.getRemark());
-            ledger.setEntryPerson(String.valueOf(creatorUser.getUserId()));
-            ledger.setEntryDate(first.getReportDate());
-            if (first.getReportDate() != null) {
-                ledger.setExecutionDate(DateUtils.toLocalDate(first.getReportDate()));
-            }
-            ledger.setDeliveryDate(first.getDeliveryDeadline() == null ? (first.getReportDate() == null ? LocalDate.now().plusDays(7) : DateUtils.toLocalDate(first.getReportDate()).plusDays(7)) : DateUtils.toLocalDate(first.getDeliveryDeadline()));
-            ledger.setDeliveryStatus(1);
-            ledger.setStockStatus(0);
-
-            ledger.setCustomerId(customer.getId());
-            ledger.setCustomerContractNo(StringUtils.hasText(ledger.getCustomerContractNo()) ? ledger.getCustomerContractNo() : customer.getTaxpayerIdentificationNumber());
-
-            ledger.setContractAmount(BigDecimal.ZERO);
-            salesLedgerMapper.insert(ledger);
-            bindImportProcessRoute(ledger.getId(), rowList, routeNameMap, routeItemMap);
-
-            BigDecimal contractAmount = BigDecimal.ZERO;
-            for (SalesNotShippingImportDto row : rowList) {
-                SalesLedgerProduct product = new SalesLedgerProduct();
-                product.setSalesLedgerId(ledger.getId());
-                product.setType(SaleEnum.SALE.getCode());
-                String specificationModel = buildSpecificationModel(row);
-                Product importProduct = resolveOrCreateImportProduct(row, productNameMap, finishedGoodsParent, orderNo);
-                ProductModel importProductModel = resolveOrCreateImportProductModel(importProduct, specificationModel, row.getGlassThickness(), productModelKeyMap);
-                product.setProductCategory(row.getProductSubCategory());
-                product.setSpecificationModel(specificationModel);
-                product.setProductId(importProduct.getId());
-                product.setProductModelId(importProductModel.getId());
-                product.setFloorCode(row.getFloorNo());
-                product.setWidth(defaultDecimal(row.getWidth()));
-                product.setHeight(defaultDecimal(row.getHeight()));
-                product.setQuantity(defaultDecimal(row.getQuantity()));
-                product.setActualPieceArea(defaultDecimal(row.getActualSingleArea()));
-                product.setActualTotalArea(defaultDecimal(row.getActualTotalArea()));
-                product.setSettlePieceArea(defaultDecimal(row.getSettlementSingleArea()));
-                product.setSettleTotalArea(defaultDecimal(row.getSettlementTotalArea()));
-                product.setTaxInclusiveUnitPrice(defaultDecimal(row.getUnitPrice()));
-                product.setPerimeter(defaultDecimal(row.getPerimeter()));
-                product.setHeavyBox(defaultDecimal(row.getHeavyBox()));
-                product.setProcessRequirement(row.getProcessRequirement());
-                product.setRemark(StringUtils.hasText(row.getAuditRemark()) ? row.getAuditRemark() : row.getRemark());
-                product.setApproveStatus(0);
-                product.setProductStockStatus(0);
-                product.setRegister(creatorUser.getNickName());
-                product.setRegisterDate(row.getReportDate() == null ? LocalDateTime.now() : LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault()));
-
-                BigDecimal qty = defaultDecimal(product.getQuantity());
-                if (qty.compareTo(BigDecimal.ZERO) <= 0) {
-                    throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪鏁伴噺灏忎簬绛変簬0鐨勬暟鎹�");
+//        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+//        List<String> errorMessages = Collections.synchronizedList(new ArrayList<>());
+        groupedByOrderNo.forEach((orderNo, rowList) -> {
+//            SecurityContext originalContext = SecurityContextHolder.getContext();
+//            SecurityContext ctx = SecurityContextHolder.createEmptyContext();
+//            ctx.setAuthentication(authentication);
+//            SecurityContextHolder.setContext(ctx);
+            try {
+                if (!StringUtils.hasText(orderNo)) {
+                    throw new ServiceException("瀵煎叆澶辫触,瀛樺湪璁㈠崟缂栧彿涓虹┖鐨勬暟鎹�");
                 }
-
-                BigDecimal lineAmount = defaultDecimal(row.getGlassAmount()).add(defaultDecimal(row.getOtherProcessFee()));
-                if (lineAmount.compareTo(BigDecimal.ZERO) <= 0 && product.getTaxInclusiveUnitPrice().compareTo(BigDecimal.ZERO) > 0 && product.getSettleTotalArea().compareTo(BigDecimal.ZERO) > 0) {
-                    lineAmount = product.getTaxInclusiveUnitPrice().multiply(product.getSettleTotalArea()).setScale(2, RoundingMode.HALF_UP);
+                if (CollectionUtils.isEmpty(rowList)) {
+                    return;
                 }
-                product.setTaxRate(BigDecimal.ZERO);
-                product.setTaxInclusiveTotalPrice(lineAmount);
-                product.setTaxExclusiveTotalPrice(lineAmount);
-                product.setNoInvoiceNum(qty);
-                product.setNoInvoiceAmount(lineAmount);
-                product.setPendingInvoiceTotal(lineAmount);
-                product.fillRemainingQuantity();
-                salesLedgerProductMapper.insert(product);
+                    rowList.sort(Comparator.comparing(r -> buildCategoryProductName(r) + "||" + buildSpecificationModel(r)));
+                    SalesNotShippingImportDto first = rowList.get(0);
+                    SalesLedger exists = salesLedgerMapper.selectOne(new LambdaQueryWrapper<SalesLedger>().eq(SalesLedger::getSalesContractNo, orderNo).last("LIMIT 1"));
+                    if (exists != null) {
+                        throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]宸插瓨鍦�");
+                    }
 
-                List<SalesLedgerProductProcess> bindProcessList = buildImportProcessBinds(row, processMap);
-                if (CollectionUtils.isNotEmpty(bindProcessList)) {
-                    salesLedgerProductProcessBindService.updateProductProcessBind(bindProcessList, product.getId());
+                    SalesLedger ledger = new SalesLedger();
+                    SysUser creatorUser = resolveImportUser(first.getCreator(), userByNickNameMap, userByUserNameMap, "鍒跺崟鍛�", orderNo);
+                    ledger.setSalesContractNo(orderNo);
+                    ledger.setCustomerContractNo(first.getContractNo());
+                    ledger.setProjectName(first.getProjectName());
+                    ledger.setSalesman(first.getSalesman());
+                    Customer customer = getOrCreateImportCustomer(first.getCustomerName(), customerNameMap, hebiRegion.getId());
+                    ledger.setCustomerName(customer.getCustomerName());
+                    ledger.setRemarks(first.getRemark());
+                    ledger.setEntryPerson(String.valueOf(creatorUser.getUserId()));
+                    ledger.setEntryDate(first.getReportDate());
+                    if (first.getReportDate() != null) {
+                        ledger.setExecutionDate(DateUtils.toLocalDate(first.getReportDate()));
+                    }
+                    ledger.setDeliveryDate(first.getDeliveryDeadline() == null ? (first.getReportDate() == null ? LocalDate.now().plusDays(7) : DateUtils.toLocalDate(first.getReportDate()).plusDays(7)) : DateUtils.toLocalDate(first.getDeliveryDeadline()));
+                    ledger.setDeliveryStatus(1);
+                    ledger.setStockStatus(0);
+                    ledger.setReviewStatus(0);
+
+                    ledger.setCustomerId(customer.getId());
+                    ledger.setCustomerContractNo(StringUtils.hasText(ledger.getCustomerContractNo()) ? ledger.getCustomerContractNo() : customer.getTaxpayerIdentificationNumber());
+
+                    ledger.setContractAmount(BigDecimal.ZERO);
+                    salesLedgerMapper.insert(ledger);
+                    bindImportProcessRoute(ledger.getId(), rowList, routeNameMap, finalRouteItemMap);
+                    int reviewCount = 0;
+
+                    BigDecimal contractAmount = BigDecimal.ZERO;
+                    for (SalesNotShippingImportDto row : rowList) {
+                        SalesLedgerProduct product = new SalesLedgerProduct();
+                        product.setSalesLedgerId(ledger.getId());
+                        product.setType(SaleEnum.SALE.getCode());
+                        String specificationModel = buildSpecificationModel(row);
+                        Product importProduct = resolveOrCreateImportProduct(row, productNameMap, finalFinishedGoodsParent, orderNo);
+                        ProductModel importProductModel = resolveOrCreateImportProductModel(importProduct, specificationModel, row.getGlassThickness(), productModelKeyMap);
+                        product.setProductCategory(row.getProductSubCategory());
+                        product.setSpecificationModel(specificationModel);
+                        product.setProductId(importProduct.getId());
+                        product.setProductModelId(importProductModel.getId());
+                        product.setFloorCode(row.getFloorNo());
+                        product.setWidth(defaultDecimal(row.getWidth()));
+                        product.setHeight(defaultDecimal(row.getHeight()));
+                        product.setQuantity(defaultDecimal(row.getQuantity()));
+                        product.setActualPieceArea(defaultDecimal(row.getActualSingleArea()));
+                        product.setActualTotalArea(defaultDecimal(row.getActualTotalArea()));
+                        product.setSettlePieceArea(defaultDecimal(row.getSettlementSingleArea()));
+                        product.setSettleTotalArea(defaultDecimal(row.getSettlementTotalArea()));
+                        product.setTaxInclusiveUnitPrice(defaultDecimal(row.getUnitPrice()));
+                        product.setPerimeter(defaultDecimal(row.getPerimeter()));
+                        product.setHeavyBox(defaultDecimal(row.getHeavyBox()));
+                        product.setProcessRequirement(row.getProcessRequirement());
+                        product.setRemark(StringUtils.hasText(row.getAuditRemark()) ? row.getAuditRemark() : row.getRemark());
+                        product.setApproveStatus(0);
+                        product.setProductStockStatus(0);
+                        product.setRegister(creatorUser.getNickName());
+                        product.setRegisterDate(row.getReportDate() == null ? LocalDateTime.now() : LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault()));
+
+                        BigDecimal qty = defaultDecimal(product.getQuantity());
+                        if (qty.compareTo(BigDecimal.ZERO) <= 0) {
+                            throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]瀛樺湪鏁伴噺灏忎簬绛変簬0鐨勬暟鎹�");
+                        }
+
+                        BigDecimal lineAmount = defaultDecimal(row.getGlassAmount()).add(defaultDecimal(row.getOtherProcessFee()));
+                        if (lineAmount.compareTo(BigDecimal.ZERO) <= 0 && product.getTaxInclusiveUnitPrice().compareTo(BigDecimal.ZERO) > 0 && product.getSettleTotalArea().compareTo(BigDecimal.ZERO) > 0) {
+                            lineAmount = product.getTaxInclusiveUnitPrice().multiply(product.getSettleTotalArea()).setScale(2, RoundingMode.HALF_UP);
+                        }
+                        product.setTaxRate(BigDecimal.ZERO);
+                        product.setTaxInclusiveTotalPrice(lineAmount);
+                        product.setTaxExclusiveTotalPrice(lineAmount);
+                        product.setNoInvoiceNum(qty);
+                        product.setNoInvoiceAmount(lineAmount);
+                        product.setPendingInvoiceTotal(lineAmount);
+                        product.fillRemainingQuantity();
+                        salesLedgerProductMapper.insert(product);
+                        if (StringUtils.isNotEmpty(row.getAuditor())) {
+                            reviewCount++;
+                        }
+
+                        List<SalesLedgerProductProcess> bindProcessList = buildImportProcessBinds(row, processMap);
+                        if (CollectionUtils.isNotEmpty(bindProcessList)) {
+                            salesLedgerProductProcessBindService.updateProductProcessBind(bindProcessList, product.getId());
+                        }
+
+                        // 鏈」鐩棤鐢熶骇妯″潡锛屾棤闇�鍚� ProductOrder 琛ㄤ腑鏂板鏁版嵁
+                        // salesLedgerProductServiceImpl.addProductionData(product);
+                        contractAmount = contractAmount.add(lineAmount);
+
+                        if (customer.getCustomerName() != null && customer.getCustomerName().startsWith("琛ョ墖")) {
+                            Long inspectId = createNotShippingQualityInspect(ledger, product, row, qty);
+                            stockUtils.addStock(
+                                    ledger.getId(),
+                                    product.getId(),
+                                    product.getProductModelId(),
+                                    qty,
+                                    StockInQualifiedRecordTypeEnum.SALE_QC_OK_IN.getCode(),
+                                    inspectId != null ? inspectId : product.getId()
+                            );
+                            stockUtils.substractStock(
+                                    ledger.getId(),
+                                    product.getId(),
+                                    product.getProductModelId(),
+                                    qty,
+                                    StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
+                                    product.getId()
+                            );
+
+                            StockInRecord inRecord = stockInRecordMapper.selectOne(new LambdaQueryWrapper<StockInRecord>()
+                                    .eq(StockInRecord::getSalesLedgerProductId, product.getId())
+                                    .orderByDesc(StockInRecord::getId).last("LIMIT 1"));
+                            if (inRecord != null) {
+                                String batch = "RK";
+                                if (row.getReportDate() != null) {
+                                    String dateStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(row.getReportDate());
+                                    batch += dateStr + "-" + row.getReportDate().getTime();
+                                } else {
+                                    batch += System.currentTimeMillis() + "-" + inRecord.getId();
+                                }
+                                inRecord.setInboundBatches(batch);
+                                if (row.getReportDate() != null) {
+                                    LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+                                    inRecord.setCreateTime(reportDateTime);
+                                    inRecord.setUpdateTime(reportDateTime);
+                                }
+                                stockInRecordMapper.updateById(inRecord);
+                            }
+
+                            StockOutRecord outRecord = stockOutRecordMapper.selectOne(new LambdaQueryWrapper<StockOutRecord>()
+                                    .eq(StockOutRecord::getSalesLedgerProductId, product.getId())
+                                    .orderByDesc(StockOutRecord::getId).last("LIMIT 1"));
+                            if (outRecord != null) {
+                                String batch = "CK";
+                                if (row.getReportDate() != null) {
+                                    String dateStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(row.getReportDate());
+                                    batch += dateStr + "-" + row.getReportDate().getTime();
+                                } else {
+                                    batch += System.currentTimeMillis() + "-" + outRecord.getId();
+                                }
+                                outRecord.setOutboundBatches(batch);
+                                if (row.getReportDate() != null) {
+                                    LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+                                    outRecord.setCreateTime(reportDateTime);
+                                    outRecord.setUpdateTime(reportDateTime);
+                                }
+                                stockOutRecordMapper.updateById(outRecord);
+                            }
+
+                            product.setStockedQuantity(qty);
+                            product.setShippedQuantity(qty);
+                            product.setApproveStatus(3);
+                            updateProductStockStatus(product);
+                            product.fillRemainingQuantity();
+                            updateProductShipStatus(product);
+                            salesLedgerProductMapper.updateById(product);
+
+                            ShippingInfo shippingInfo = new ShippingInfo();
+                            shippingInfo.setSalesLedgerId(ledger.getId());
+                            shippingInfo.setSalesLedgerProductId(product.getId());
+                            shippingInfo.setStatus("宸插彂璐�");
+                            if (row.getReportDate() != null) {
+                                String dateStr = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(row.getReportDate());
+                                shippingInfo.setShippingNo("CK" + dateStr + "-" + row.getReportDate().getTime());
+                            } else {
+                                shippingInfo.setShippingNo("CK" + System.currentTimeMillis());
+                            }
+                            shippingInfo.setType("璐ц溅");
+                            shippingInfo.setShippingCarNumber("鏃�");
+                            shippingInfo.setShippingDate(row.getReportDate());
+                            shippingInfoMapper.insert(shippingInfo);
+                            // createNotShippingQualityInspect moved up
+                        }
+                    }
+
+                    ledger.setContractAmount(contractAmount);
+                    if (reviewCount == rowList.size()) {
+                        ledger.setReviewStatus(1);
+                    }
+
+                    if (customer.getCustomerName() != null && customer.getCustomerName().startsWith("琛ョ墖")) {
+                        List<SalesLedgerProduct> latestProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>().eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()).eq(SalesLedgerProduct::getType, SaleEnum.SALE.getCode()));
+                        boolean allShipped = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
+                            BigDecimal pQty = defaultDecimal(p.getQuantity());
+                            BigDecimal shipped = defaultDecimal(p.getShippedQuantity());
+                            return shipped.compareTo(pQty) >= 0;
+                        });
+                        boolean anyInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().anyMatch(p -> defaultDecimal(p.getStockedQuantity()).compareTo(BigDecimal.ZERO) > 0);
+                        boolean allInbound = CollectionUtils.isNotEmpty(latestProducts) && latestProducts.stream().allMatch(p -> {
+                            BigDecimal pQty = defaultDecimal(p.getQuantity());
+                            BigDecimal stocked = defaultDecimal(p.getStockedQuantity());
+                            return pQty.compareTo(BigDecimal.ZERO) <= 0 || stocked.compareTo(pQty) >= 0;
+                        });
+                        if (allShipped && rowList.get(0).getReportDate() != null) {
+                            ledger.setDeliveryDate(DateUtils.toLocalDate(rowList.get(0).getReportDate()));
+                        }
+                        ledger.setStockStatus(allInbound ? 2 : (anyInbound ? 1 : 0));
+                        ledger.setDeliveryStatus(allShipped ? 5 : 1);
+
+                        Long entryUserId = StringUtils.hasText(ledger.getEntryPerson()) ? Long.parseLong(ledger.getEntryPerson()) : 1L;
+                        Long entryDeptId = 1L;
+                        if (StringUtils.hasText(ledger.getEntryPerson())) {
+                            try {
+                                SysUser entryUser = sysUserMapper.selectById(ledger.getEntryPerson());
+                                if (entryUser != null && entryUser.getDeptId() != null) {
+                                    entryDeptId = entryUser.getDeptId();
+                                }
+                            } catch (Exception ignored) {
+                            }
+                        }
+                        String reportDateStr = rowList.get(0).getReportDate() != null ? new java.text.SimpleDateFormat("yyyy-MM-dd").format(rowList.get(0).getReportDate()) : LocalDate.now().toString();
+
+                        try {
+                            String productIds = latestProducts.stream().map(p -> String.valueOf(p.getId())).collect(Collectors.joining(","));
+                            String inboundApproveUserIds = resolveApproveUserIds(null, ledger.getId(), INBOUND_BIZ_TYPE_WEB);
+                            if (StringUtils.isEmpty(inboundApproveUserIds)) {
+                                inboundApproveUserIds = "1";
+                            }
+
+                            ApproveProcessVO stockInVo = new ApproveProcessVO();
+                            stockInVo.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
+                            stockInVo.setApproveDeptId(208L);
+                            stockInVo.setApproveReason("鍏ュ簱瀹℃壒:" + ledger.getSalesContractNo());
+                            stockInVo.setApproveRemark("salesStock:" + ledger.getId() + ":" + productIds);
+                            stockInVo.setApproveUserIds(inboundApproveUserIds);
+                            stockInVo.setApproveUser(entryUserId);
+                            stockInVo.setApproveTime(reportDateStr);
+                            approveProcessService.addApprove(stockInVo);
+
+                            ApproveProcess savedStockIn = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                                    .eq(ApproveProcess::getApproveReason, stockInVo.getApproveReason())
+                                    .eq(ApproveProcess::getApproveType, stockInVo.getApproveType())
+                                    .orderByDesc(ApproveProcess::getId).last("LIMIT 1"));
+                            if (savedStockIn != null) {
+                                savedStockIn.setApproveStatus(2);
+                                savedStockIn.setApproveOverTime(rowList.get(0).getReportDate());
+                                approveProcessService.updateById(savedStockIn);
+                            }
+                        } catch (Exception e) {
+                            log.error("鑷姩鐢熸垚鍏ュ簱瀹℃壒鐢宠澶辫触", e);
+                        }
+
+                        try {
+                            ApproveProcessVO deliveryVo = new ApproveProcessVO();
+                            deliveryVo.setApproveType(ApproveTypeEnum.DELIVERY.getCode());
+                            deliveryVo.setApproveDeptId(208L);
+                            deliveryVo.setApproveReason("鍙戣揣瀹℃壒:" + ledger.getSalesContractNo());
+                            deliveryVo.setApproveUserIds("1");
+                            deliveryVo.setApproveUser(entryUserId);
+                            deliveryVo.setApproveTime(reportDateStr);
+                            approveProcessService.addApprove(deliveryVo);
+
+                            ApproveProcess savedDelivery = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
+                                    .eq(ApproveProcess::getApproveReason, deliveryVo.getApproveReason())
+                                    .eq(ApproveProcess::getApproveType, deliveryVo.getApproveType())
+                                    .orderByDesc(ApproveProcess::getId).last("LIMIT 1"));
+                            if (savedDelivery != null) {
+                                savedDelivery.setApproveStatus(2);
+                                savedDelivery.setApproveOverTime(rowList.get(0).getReportDate());
+                                approveProcessService.updateById(savedDelivery);
+                            }
+                        } catch (Exception e) {
+                            log.error("鑷姩鐢熸垚鍙戣揣瀹℃壒鐢宠澶辫触", e);
+                        }
+                    }
+
+                    salesLedgerMapper.updateById(ledger);
+                } catch (Exception e) {
+                    log.error("璁㈠崟 {} 鏈彂璐ц褰曞鍏ュけ璐�", orderNo, e);
+//                    errorMessages.add("璁㈠崟 [" + orderNo + "] 瀵煎叆澶辫触: " + e.getMessage());
                 }
-
-                salesLedgerProductServiceImpl.addProductionData(product);
-                contractAmount = contractAmount.add(lineAmount);
-            }
-
-            ledger.setContractAmount(contractAmount);
-            salesLedgerMapper.updateById(ledger);
-        }
+//            SecurityContextHolder.setContext(originalContext);
+        });
+//        if (!errorMessages.isEmpty()) {
+//            throw new ServiceException("閮ㄥ垎鏁版嵁瀵煎叆澶辫触:\n" + String.join("\n", errorMessages));
+//        }
     }
 
     private List<SalesLedgerProductProcess> buildImportProcessBinds(SalesNotShippingImportDto row, Map<String, SalesLedgerProductProcess> processMap) {
@@ -2639,6 +3073,9 @@
         mergeProcessQuantity(processQuantityMap, "鎸栫己", toProcessQuantity(row.getNotching()));
         mergeProcessQuantity(processQuantityMap, "瀹夊叏瑙�", toProcessQuantity(row.getSafetyCorner()));
         mergeProcessQuantity(processQuantityMap, "纾ㄨ竟", toProcessQuantity(row.getGrindingIrregular()));
+        mergeProcessQuantity(processQuantityMap, "绮剧(杈�", toProcessQuantity(row.getFineGrinding()));
+        mergeProcessQuantity(processQuantityMap, "杩愯垂", toProcessQuantity(row.getFreightFee()));
+        mergeProcessQuantity(processQuantityMap, "鍔犳�ヨ垂", toProcessQuantity(row.getUrgentFee()));
 
         List<SalesLedgerProductProcess> result = new ArrayList<>();
         for (Map.Entry<String, Integer> entry : processQuantityMap.entrySet()) {
@@ -2692,6 +3129,11 @@
         if (CollectionUtils.isEmpty(processNames)) {
             throw new ServiceException("瀵煎叆澶辫触,宸ヨ壓璺嚎[" + flowKey + "]瑙f瀽澶辫触");
         }
+        synchronized (("ROUTE_" + flowKey).intern()) {
+            ProcessRoute exists = routeNameMap.get(flowKey);
+            if (exists != null && exists.getId() != null) {
+                return exists;
+            }
         ProcessRoute route = new ProcessRoute();
         route.setProductModelId(0L);
         route.setProcessRouteName(flowKey);
@@ -2714,6 +3156,7 @@
         routeNameMap.put(flowKey, route);
         routeItemMap.put(route.getId(), routeItems);
         return route;
+        }
     }
 
     private void mergeProcessQuantity(Map<String, Integer> processQuantityMap, String processName, Integer quantity) {
@@ -2790,12 +3233,18 @@
         if (exists != null && exists.getId() != null) {
             return exists;
         }
+        synchronized (("CUSTOMER_" + key).intern()) {
+            exists = customerNameMap.get(key);
+            if (exists != null && exists.getId() != null) {
+                return exists;
+            }
         Customer customer = new Customer();
         customer.setCustomerName(key);
         customer.setRegionsId(hebiRegionId);
         customerMapper.insert(customer);
         customerNameMap.put(key, customer);
         return customer;
+        }
     }
 
     private Product resolveOrCreateImportProduct(SalesNotShippingImportDto row, Map<String, Product> productNameMap, Product finishedGoodsParent, String orderNo) {
@@ -2817,12 +3266,18 @@
         if (!StringUtils.hasText(newProductName)) {
             throw new ServiceException("瀵煎叆澶辫触,璁㈠崟缂栧彿[" + orderNo + "]浜у搧鍚嶇О鍜屼骇鍝佸垎绫诲潎涓虹┖");
         }
+        synchronized (("PRODUCT_" + newProductName).intern()) {
+            Product product = productNameMap.get(newProductName);
+            if (product != null && product.getId() != null) {
+                return product;
+            }
         Product created = new Product();
         created.setParentId(finishedGoodsParent == null ? null : finishedGoodsParent.getId());
         created.setProductName(newProductName);
         productMapper.insert(created);
         productNameMap.put(newProductName, created);
         return created;
+        }
     }
 
     private ProductModel resolveOrCreateImportProductModel(Product product, String modelName, BigDecimal thickness, Map<String, ProductModel> productModelKeyMap) {
@@ -2834,16 +3289,36 @@
         if (exists != null && exists.getId() != null) {
             return exists;
         }
+        synchronized (("MODEL_" + key).intern()) {
+            exists = productModelKeyMap.get(key);
+            if (exists != null && exists.getId() != null) {
+                return exists;
+            }
         ProductModel created = new ProductModel();
         created.setProductId(product.getId());
         created.setModel(modelName.trim());
         created.setThickness(thickness);
+        created.setUnit("mm");
         productModelMapper.insert(created);
         productModelKeyMap.put(key, created);
         return created;
+        }
     }
 
     private String buildSpecificationModel(SalesNotShippingImportDto row) {
+        if (StringUtils.hasText(row.getProductName())) {
+            return row.getProductName().trim();
+        }
+        if (StringUtils.hasText(row.getProductSubCategory())) {
+            return row.getProductSubCategory().trim();
+        }
+        if (StringUtils.hasText(row.getProductCategory())) {
+            return row.getProductCategory().trim();
+        }
+        return "";
+    }
+
+    private String buildSpecificationModel(SalesShippingImportDto row) {
         if (StringUtils.hasText(row.getProductName())) {
             return row.getProductName().trim();
         }
@@ -2955,16 +3430,21 @@
         String shippingNo = StringUtils.hasText(row.getShippingNo()) ? row.getShippingNo().trim() : "";
         String dateStr = row.getReportDate() == null ? "" : String.valueOf(row.getReportDate().getTime());
         String subCategory = StringUtils.hasText(row.getProductSubCategory()) ? row.getProductSubCategory().trim() : "";
-        return ledgerId + "|" + subCategory + "|" + shippingNo + "|" + dateStr + "|" + defaultDecimal(row.getQuantity());
+        Long sequence = row.getSequence();
+        if (sequence == null) {
+            throw new ServiceException("鏂板澶辫触,瀵煎叆鍑哄簱鍗曠紪鍙蜂负绌�");
+        }
+        return ledgerId + "|" + subCategory + "|" + shippingNo + "|" + dateStr + "|" + defaultDecimal(row.getQuantity()) + "|" + sequence;
     }
 
-    private void createShippingQualityInspect(SalesLedger ledger, SalesLedgerProduct dbProduct, SalesShippingImportDto row, BigDecimal inspectQty) {
+    private Long createShippingQualityInspect(SalesLedger ledger, SalesLedgerProduct dbProduct, SalesShippingImportDto row, BigDecimal inspectQty) {
         if (ledger == null || dbProduct == null || inspectQty == null || inspectQty.compareTo(BigDecimal.ZERO) <= 0) {
-            return;
+            return null;
         }
         Date checkDate = row.getReportDate() != null ? row.getReportDate() : new Date();
         QualityInspect qualityInspect = new QualityInspect();
         qualityInspect.setInspectType(2);
+        qualityInspect.setPurchaseLedgerId(dbProduct.getId());
         qualityInspect.setCheckTime(checkDate);
         qualityInspect.setCustomer(StringUtils.hasText(ledger.getCustomerName()) ? ledger.getCustomerName() : row.getCustomerName());
         qualityInspect.setCheckName(StringUtils.hasText(row.getCreator()) ? row.getCreator().trim() : null);
@@ -2991,12 +3471,19 @@
         }
         qualityInspectMapper.insert(qualityInspect);
 
+        if (row.getReportDate() != null) {
+            LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+            qualityInspect.setCreateTime(reportDateTime);
+            qualityInspect.setUpdateTime(reportDateTime);
+            qualityInspectMapper.updateById(qualityInspect);
+        }
+
         if (selectedStandard == null || selectedStandard.getId() == null) {
-            return;
+            return qualityInspect.getId();
         }
         List<QualityTestStandardParam> standardParams = qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery().eq(QualityTestStandardParam::getTestStandardId, selectedStandard.getId()));
         if (CollectionUtils.isEmpty(standardParams)) {
-            return;
+            return qualityInspect.getId();
         }
         List<QualityInspectParam> inspectParams = standardParams.stream().map(item -> {
             QualityInspectParam param = new QualityInspectParam();
@@ -3009,6 +3496,69 @@
             return param;
         }).collect(Collectors.toList());
         inspectParams.forEach(qualityInspectParamMapper::insert);
+        return qualityInspect.getId();
+    }
+
+    private Long createNotShippingQualityInspect(SalesLedger ledger, SalesLedgerProduct dbProduct, SalesNotShippingImportDto row, BigDecimal inspectQty) {
+        if (ledger == null || dbProduct == null || inspectQty == null || inspectQty.compareTo(BigDecimal.ZERO) <= 0) {
+            return null;
+        }
+        Date checkDate = row.getReportDate() != null ? row.getReportDate() : new Date();
+        QualityInspect qualityInspect = new QualityInspect();
+        qualityInspect.setInspectType(2);
+        qualityInspect.setPurchaseLedgerId(dbProduct.getId());
+        qualityInspect.setCheckTime(checkDate);
+        qualityInspect.setCustomer(StringUtils.hasText(ledger.getCustomerName()) ? ledger.getCustomerName() : row.getCustomerName());
+        qualityInspect.setCheckName(StringUtils.hasText(row.getCreator()) ? row.getCreator().trim() : null);
+        qualityInspect.setProductId(dbProduct.getProductId());
+        qualityInspect.setProductName(dbProduct.getProductCategory());
+        qualityInspect.setModel(dbProduct.getSpecificationModel());
+        qualityInspect.setUnit(resolveInspectUnit(dbProduct));
+        qualityInspect.setQuantity(inspectQty);
+        qualityInspect.setQualifiedQuantity(inspectQty);
+        qualityInspect.setUnqualifiedQuantity(BigDecimal.ZERO);
+        qualityInspect.setPassRate(BigDecimal.valueOf(100));
+        qualityInspect.setCheckResult("鍚堟牸");
+        qualityInspect.setInspectState(1);
+        qualityInspect.setApprovalStatus(1);
+        qualityInspect.setProductModelId(dbProduct.getProductModelId());
+
+        QualityTestStandard selectedStandard = null;
+        if (dbProduct.getProductId() != null) {
+            List<QualityTestStandard> standards = qualityTestStandardMapper.getQualityTestStandardByProductId(dbProduct.getProductId(), 2, null);
+            if (CollectionUtils.isNotEmpty(standards)) {
+                selectedStandard = standards.get(0);
+                qualityInspect.setTestStandardId(selectedStandard.getId());
+            }
+        }
+        qualityInspectMapper.insert(qualityInspect);
+
+        if (row.getReportDate() != null) {
+            LocalDateTime reportDateTime = LocalDateTime.ofInstant(row.getReportDate().toInstant(), ZoneId.systemDefault());
+            qualityInspect.setCreateTime(reportDateTime);
+            qualityInspect.setUpdateTime(reportDateTime);
+            qualityInspectMapper.updateById(qualityInspect);
+        }
+
+        if (selectedStandard == null || selectedStandard.getId() == null) {
+            return qualityInspect.getId();
+        }
+        List<QualityTestStandardParam> standardParams = qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery().eq(QualityTestStandardParam::getTestStandardId, selectedStandard.getId()));
+        if (CollectionUtils.isEmpty(standardParams)) {
+            return qualityInspect.getId();
+        }
+        List<QualityInspectParam> inspectParams = standardParams.stream().map(item -> {
+            QualityInspectParam param = new QualityInspectParam();
+            param.setInspectId(qualityInspect.getId());
+            param.setParameterItem(item.getParameterItem());
+            param.setUnit(item.getUnit());
+            param.setStandardValue(item.getStandardValue());
+            param.setControlValue(item.getControlValue());
+            param.setTestValue("鏃犵憰鐤�");
+            return param;
+        }).collect(Collectors.toList());
+        inspectParams.forEach(qualityInspectParamMapper::insert);
+        return qualityInspect.getId();
     }
 
     private String resolveInspectUnit(SalesLedgerProduct dbProduct) {
@@ -3145,7 +3695,7 @@
         LoginUser loginUser = SecurityUtils.getLoginUser();
         ApproveProcessVO approveProcessVO = new ApproveProcessVO();
         approveProcessVO.setApproveType(7);
-        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveDeptId(208L);
         approveProcessVO.setApproveReason("鍙戣揣瀹℃壒:" + salesLedger.getSalesContractNo());
         approveProcessVO.setApproveRemark(remarkJson);
         approveProcessVO.setApproveUserIds(dto.getApproveUserIds().trim());
@@ -3327,12 +3877,103 @@
         baseMapper.updateById(salesLedger);
     }
 
-    
+
     /**
      * 澶勭悊閿�鍞彴璐﹀弽瀹¢�昏緫
      * 1. 璁剧疆鍙嶅鐩稿叧淇℃伅锛堟椂闂淬�佷汉鍛橈級
      * 2. 澶嶅埗鍘熼攢鍞彴璐﹀強浜у搧鏁版嵁锛岀敓鎴愭柊鐨勫彴璐�
      * 3. 瀵瑰師鍙拌处鐨勫簱瀛樻暟鎹繘琛屽弽鍚戞搷浣�
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public List<Long> counterReview(CounterReviewDto dto) {
+        if (dto == null || CollectionUtils.isEmpty(dto.getIds())) {
+            throw new ServiceException("璇烽�夋嫨瑕佸弽瀹℃牳鐨勮鍗�");
+        }
+        if (dto.getCounterReviewType() == null || (dto.getCounterReviewType() != 1 && dto.getCounterReviewType() != 2)) {
+            throw new ServiceException("璇烽�夋嫨鍙嶅鏍哥被鍨嬶細浣滃簾鎴栭噸鏂扮敓鎴�");
+        }
+        if (dto.getCounterReviewDesc() == null || dto.getCounterReviewDesc().trim().isEmpty()) {
+            throw new ServiceException("璇疯緭鍏ュ弽瀹℃牳鎻忚堪");
+        }
+
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        List<Long> newLedgerIds = new ArrayList<>();
+
+        for (Long id : dto.getIds()) {
+            SalesLedger originalLedger = salesLedgerMapper.selectById(id);
+            if (originalLedger == null) {
+                throw new ServiceException("璁㈠崟涓嶅瓨鍦紝鏃犳硶鍙嶅鏍�");
+            }
+            if (originalLedger.getReviewStatus() == null || originalLedger.getReviewStatus() != 1) {
+                throw new ServiceException("璁㈠崟" + originalLedger.getSalesContractNo() + "涓嶆槸宸插鏍哥姸鎬侊紝鏃犳硶鍙嶅鏍�");
+            }
+
+            // 1. 鏍囪鍘熻鍗曚负宸插弽瀹�
+            originalLedger.setReviewStatus(2);
+            originalLedger.setCounterReviewTime(LocalDateTime.now());
+            originalLedger.setCounterReviewPerson(loginUser.getUser().getNickName());
+            originalLedger.setCounterReviewPersonId(loginUser.getUserId());
+            originalLedger.setCounterReviewType(dto.getCounterReviewType());
+            originalLedger.setCounterReviewDesc(dto.getCounterReviewDesc());
+            salesLedgerMapper.updateById(originalLedger);
+
+            // 2. 浣滃簾搴撳瓨锛氬叆搴撴墸鍑忋�佸嚭搴撳鍔犮�佸垹闄よ褰�
+            processOriginalOrderStock(id);
+
+            // 3. 娓呴櫎璐ㄦ璁板綍
+            clearQualityInspectRecords(id);
+
+            // 4. 娓呴櫎鍙戣揣淇℃伅鍜屽彂璐у鎵硅褰�
+            clearShippingAndApprovalRecords(id);
+
+            // 5. 鍙栨秷瀹℃壒娴佺▼
+            cancelApproveProcesses(id, originalLedger.getSalesContractNo());
+
+            // 6. 閲嶆柊鐢熸垚锛氬垱寤烘柊鍙拌处鍓湰
+            if (dto.getCounterReviewType() == 2) {
+                SalesLedger newLedger = new SalesLedger();
+                BeanUtils.copyProperties(originalLedger, newLedger);
+                newLedger.setId(null);
+                newLedger.setSalesContractNo(generateSalesContractNo());
+                newLedger.setDeliveryStatus(1);
+                newLedger.setStockStatus(0);
+                newLedger.setReviewStatus(0);
+                newLedger.setCounterReviewTime(null);
+                newLedger.setCounterReviewPerson(null);
+                newLedger.setCounterReviewPersonId(null);
+                newLedger.setCounterReviewType(null);
+                newLedger.setCounterReviewDesc(null);
+                salesLedgerMapper.insert(newLedger);
+
+                // 澶嶅埗浜у搧鍒版柊鍙拌处
+                List<SalesLedgerProduct> originalProducts = salesLedgerProductMapper.selectList(
+                        Wrappers.<SalesLedgerProduct>lambdaQuery()
+                                .eq(SalesLedgerProduct::getSalesLedgerId, id)
+                );
+                for (SalesLedgerProduct originalProduct : originalProducts) {
+                    SalesLedgerProduct newProduct = new SalesLedgerProduct();
+                    BeanUtils.copyProperties(originalProduct, newProduct);
+                    newProduct.setId(null);
+                    newProduct.setSalesLedgerId(newLedger.getId());
+                    newProduct.setStockedQuantity(BigDecimal.ZERO);
+                    newProduct.setShippedQuantity(BigDecimal.ZERO);
+                    newProduct.setUnqualifiedStockedQuantity(BigDecimal.ZERO);
+                    newProduct.setUnqualifiedShippedQuantity(BigDecimal.ZERO);
+                    newProduct.setReturnQuality(BigDecimal.ZERO);
+                    newProduct.setAvailableQuality(newProduct.getQuantity().subtract(newProduct.getReturnQuality()));
+                    newProduct.setProductStockStatus(0);
+                    newProduct.fillRemainingQuantity();
+                    salesLedgerProductMapper.insert(newProduct);
+                }
+                newLedgerIds.add(newLedger.getId());
+            }
+        }
+        return newLedgerIds;
+    }
+
+    /**
+     * 鏃х増鍙嶅澶勭悊锛堝吋瀹� addOrUpdateSalesLedger 涓� reviewStatus=2 鐨勮皟鐢級
      */
     private void handleCounterReview(SalesLedger salesLedger) {
         // 1. 璁剧疆鍙嶅鐩稿叧淇℃伅
@@ -3341,15 +3982,15 @@
         salesLedger.setCounterReviewPerson(loginUser.getUser().getNickName());
         salesLedger.setCounterReviewPersonId(loginUser.getUserId());
         salesLedger.setReviewStatus(2);
-        
+
         Long originalSalesLedgerId = salesLedger.getId();
-        
+
         // 2. 鏌ヨ鍘熼攢鍞彴璐︽暟鎹�
         SalesLedger originalLedger = salesLedgerMapper.selectById(originalSalesLedgerId);
         if (originalLedger == null) {
             throw new ServiceException("鍘熻鍗曚笉瀛樺湪锛屾棤娉曞弽瀹�");
         }
-        
+
         // 3. 鍒涘缓鏂扮殑閿�鍞彴璐︼紝澶嶅埗鍘熷彴璐︽暟鎹�
         SalesLedger newLedger = new SalesLedger();
         BeanUtils.copyProperties(originalLedger, newLedger);
@@ -3361,16 +4002,16 @@
         newLedger.setCounterReviewTime(null); // 娓呯┖鍙嶅鏃堕棿
         newLedger.setCounterReviewPerson(null); // 娓呯┖鍙嶅浜�
         newLedger.setCounterReviewPersonId(null); // 娓呯┖鍙嶅浜篒D
-        
+
         // 4. 鎻掑叆鏂扮殑閿�鍞彴璐�
         salesLedgerMapper.insert(newLedger);
-        
+
         // 5. 鏌ヨ骞跺鍒跺師鍙拌处鐨勬墍鏈変骇鍝佹暟鎹�
         List<SalesLedgerProduct> originalProducts = salesLedgerProductMapper.selectList(
-            Wrappers.<SalesLedgerProduct>lambdaQuery()
-                .eq(SalesLedgerProduct::getSalesLedgerId, originalSalesLedgerId)
+                Wrappers.<SalesLedgerProduct>lambdaQuery()
+                        .eq(SalesLedgerProduct::getSalesLedgerId, originalSalesLedgerId)
         );
-        
+
         for (SalesLedgerProduct originalProduct : originalProducts) {
             // 5.1 鍒涘缓鏂颁骇鍝佽褰曪紝澶嶅埗鍘熶骇鍝佹暟鎹�
             SalesLedgerProduct newProduct = new SalesLedgerProduct();
@@ -3385,80 +4026,652 @@
             newProduct.setAvailableQuality(newProduct.getQuantity().subtract(newProduct.getReturnQuality())); // 閲嶆柊璁$畻鍙敤鏁伴噺
             newProduct.setProductStockStatus(0); // 浜у搧搴撳瓨鐘舵�侀噸缃负0
             newProduct.fillRemainingQuantity(); // 閲嶆柊璁$畻鍓╀綑鏁伴噺
-            
+
             // 5.2 鎻掑叆鏂颁骇鍝佽褰�
             salesLedgerProductMapper.insert(newProduct);
         }
-        
+
         // 6. 澶勭悊鍘熻鍗曠殑搴撳瓨鏁版嵁锛堢敓鎴愬弽瀹″嚭鍏ュ簱璁板綍锛�
         processOriginalOrderStock(originalSalesLedgerId);
+
+        // 7. 娓呴櫎鍘熻鍗曠殑璐ㄦ璁板綍
+        clearQualityInspectRecords(originalSalesLedgerId);
+
+        // 8. 娓呴櫎鍘熻鍗曠殑鍙戣揣淇℃伅鍜屽彂璐у鎵硅褰�
+        clearShippingAndApprovalRecords(originalSalesLedgerId);
+
+        // 9. 鍙栨秷鍘熻鍗曠浉鍏崇殑瀹℃壒娴佺▼
+        cancelApproveProcesses(originalSalesLedgerId, originalLedger.getSalesContractNo());
+    }
+
+    /**
+     * 娓呴櫎鍘熻鍗曠殑璐ㄦ璁板綍
+     */
+    private void clearQualityInspectRecords(Long originalSalesLedgerId) {
+        // 鍒犻櫎涓庡師璁㈠崟鍏宠仈鐨勮川妫�璁板綍
+        qualityInspectMapper.delete(
+                Wrappers.<QualityInspect>lambdaQuery()
+                        .eq(QualityInspect::getPurchaseLedgerId, originalSalesLedgerId)
+        );
+    }
+
+    /**
+     * 娓呴櫎鍘熻鍗曠殑鍙戣揣淇℃伅鍜屽彂璐у鎵硅褰�
+     */
+    private void clearShippingAndApprovalRecords(Long originalSalesLedgerId) {
+        // 1. 鏌ヨ鍘熻鍗曠殑鎵�鏈夊彂璐т俊鎭�
+        List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(
+                Wrappers.<ShippingInfo>lambdaQuery()
+                        .eq(ShippingInfo::getSalesLedgerId, originalSalesLedgerId)
+        );
+
+        // 2. 鍒犻櫎鍙戣揣瀹℃壒璁板綍
+        if (!CollectionUtils.isEmpty(shippingInfos)) {
+            List<Long> shippingInfoIds = shippingInfos.stream()
+                    .map(ShippingInfo::getId)
+                    .collect(Collectors.toList());
+
+            shipmentApprovalMapper.delete(
+                    Wrappers.<ShipmentApproval>lambdaQuery()
+                            .eq(ShipmentApproval::getSalesLedgerId, originalSalesLedgerId)
+                            .or()
+                            .in(ShipmentApproval::getShippingInfoId, shippingInfoIds)
+            );
+
+            // 3. 鍒犻櫎鍙戣揣淇℃伅璁板綍
+            shippingInfoMapper.delete(
+                    Wrappers.<ShippingInfo>lambdaQuery()
+                            .eq(ShippingInfo::getSalesLedgerId, originalSalesLedgerId)
+            );
+        }
+    }
+
+    /**
+     * 鍙栨秷鍘熻鍗曠浉鍏崇殑瀹℃壒娴佺▼
+     */
+    private void cancelApproveProcesses(Long originalSalesLedgerId, String originalSalesContractNo) {
+        // 鍙栨秷鍏ュ簱瀹℃壒娴佺▼
+        List<ApproveProcess> stockInApproveProcesses = approveProcessService.list(
+                new LambdaQueryWrapper<ApproveProcess>()
+                        .eq(ApproveProcess::getApproveType, ApproveTypeEnum.STOCK_IN.getCode())
+                        .like(ApproveProcess::getApproveRemark, "salesStock:" + originalSalesLedgerId + ":")
+                        .eq(ApproveProcess::getApproveDelete, 0)
+        );
+
+        for (ApproveProcess process : stockInApproveProcesses) {
+            process.setApproveStatus(3); // 璁剧疆涓哄鎵瑰け璐ョ姸鎬�
+            process.setApproveDelete(1); // 鏍囪涓哄凡鍒犻櫎
+            approveProcessService.updateById(process);
+        }
+
+        // 鍙栨秷鍙戣揣瀹℃壒娴佺▼
+        List<ApproveProcess> deliveryApproveProcesses = approveProcessService.list(
+                new LambdaQueryWrapper<ApproveProcess>()
+                        .eq(ApproveProcess::getApproveType, 7) // 鍙戣揣瀹℃壒绫诲瀷
+                        .like(ApproveProcess::getApproveReason, "鍙戣揣瀹℃壒:" + originalSalesContractNo)
+                        .eq(ApproveProcess::getApproveDelete, 0)
+        );
+
+        for (ApproveProcess process : deliveryApproveProcesses) {
+            process.setApproveStatus(3); // 璁剧疆涓哄鎵瑰け璐ョ姸鎬�
+            process.setApproveDelete(1); // 鏍囪涓哄凡鍒犻櫎
+            approveProcessService.updateById(process);
+        }
     }
 
     /**
      * 澶勭悊鍘熻鍗曠殑搴撳瓨鏁版嵁
-     * 1. 瀵瑰師璁㈠崟鐨勫叆搴撴暟鎹敓鎴愬弽瀹″嚭搴撹褰曪紙閿�鍞�-鍙嶅鍑哄簱锛�
-     * 2. 瀵瑰師璁㈠崟鐨勫嚭搴撴暟鎹敓鎴愬弽瀹″叆搴撹褰曪紙閿�鍞�-鍙嶅鍏ュ簱锛�
-     * 3. 鏇存柊搴撳瓨琛ㄦ暟鎹�
+     * 1. 鍒犻櫎鍘熻鍗曠殑鎵�鏈夊叆搴撹褰曪紝骞舵墸鍑忓簱瀛�
+     * 2. 鍒犻櫎鍘熻鍗曠殑鎵�鏈夊嚭搴撹褰曪紝骞跺鍔犲簱瀛�
      */
     private void processOriginalOrderStock(Long originalSalesLedgerId) {
         // 1. 鏌ヨ鍘熻鍗曠殑鎵�鏈夊叆搴撹褰�
         List<StockInRecord> stockInRecords = stockInRecordMapper.selectList(
-            Wrappers.<StockInRecord>lambdaQuery()
-                .eq(StockInRecord::getSalesLedgerId, originalSalesLedgerId)
+                Wrappers.<StockInRecord>lambdaQuery()
+                        .eq(StockInRecord::getSalesLedgerId, originalSalesLedgerId)
         );
-        
-        // 2. 瀵规瘡鏉″叆搴撹褰曠敓鎴愬搴旂殑鍙嶅鍑哄簱璁板綍
+
+        // 2. 鍒犻櫎鍏ュ簱璁板綍骞舵墸鍑忓簱瀛�
         for (StockInRecord stockInRecord : stockInRecords) {
-            // 2.1 鍒涘缓鍙嶅鍑哄簱璁板綍
-            StockOutRecord stockOutRecord = new StockOutRecord();
-            stockOutRecord.setOutboundBatches(OrderUtils.countTodayByCreateTime(stockOutRecordMapper, "CK")); // 鐢熸垚鍑哄簱鎵规鍙�
-            stockOutRecord.setStockOutNum(stockInRecord.getStockInNum()); // 鍑哄簱鏁伴噺绛変簬鍘熷叆搴撴暟閲�
-            stockOutRecord.setRecordId(stockInRecord.getId()); // 璁板綍鍘熷叆搴撹褰旾D
-            stockOutRecord.setRecordType(StockOutQualifiedRecordTypeEnum.SALE_COUNTER_REVIEW_STOCK_OUT.getCode()); // 璁剧疆涓洪攢鍞弽瀹″嚭搴撶被鍨�
-            stockOutRecord.setProductModelId(stockInRecord.getProductModelId()); // 浜у搧瑙勬牸ID
-            stockOutRecord.setRemark("閿�鍞�-鍙嶅鍑哄簱"); // 澶囨敞
-            stockOutRecord.setType(stockInRecord.getType()); // 绫诲瀷锛堝悎鏍�/涓嶅悎鏍硷級
-            stockOutRecord.setSalesLedgerId(stockInRecord.getSalesLedgerId()); // 閿�鍞鍗旾D
-            stockOutRecord.setSalesLedgerProductId(stockInRecord.getSalesLedgerProductId()); // 閿�鍞鍗曚骇鍝両D
-            
-            // 2.2 鎻掑叆鍙嶅鍑哄簱璁板綍
-            stockOutRecordMapper.insert(stockOutRecord);
-            
-            // 2.3 浠庡簱瀛樿〃涓墸鍑忕浉搴旀暟閲�
-            StockInventoryDto stockInventoryDto = new StockInventoryDto();
-            stockInventoryDto.setProductModelId(stockOutRecord.getProductModelId());
-            stockInventoryDto.setQualitity(stockOutRecord.getStockOutNum());
-            stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
-        }
-        
-        // 3. 鏌ヨ鍘熻鍗曠殑鎵�鏈夊嚭搴撹褰�
-        List<StockOutRecord> stockOutRecords = stockOutRecordMapper.selectList(
-            Wrappers.<StockOutRecord>lambdaQuery()
-                .eq(StockOutRecord::getSalesLedgerId, originalSalesLedgerId)
-        );
-        
-        // 4. 瀵规瘡鏉″嚭搴撹褰曠敓鎴愬搴旂殑鍙嶅鍏ュ簱璁板綍
-        for (StockOutRecord stockOutRecord : stockOutRecords) {
-            // 4.1 鍒涘缓鍙嶅鍏ュ簱璁板綍
-            StockInRecord stockInRecord = new StockInRecord();
-            stockInRecord.setInboundBatches(OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK")); // 鐢熸垚鍏ュ簱鎵规鍙�
-            stockInRecord.setStockInNum(stockOutRecord.getStockOutNum()); // 鍏ュ簱鏁伴噺绛変簬鍘熷嚭搴撴暟閲�
-            stockInRecord.setRecordId(stockOutRecord.getId()); // 璁板綍鍘熷嚭搴撹褰旾D
-            stockInRecord.setRecordType(StockInQualifiedRecordTypeEnum.SALE_COUNTER_REVIEW_STOCK_IN.getCode()); // 璁剧疆涓洪攢鍞弽瀹″叆搴撶被鍨�
-            stockInRecord.setProductModelId(stockOutRecord.getProductModelId()); // 浜у搧瑙勬牸ID
-            stockInRecord.setRemark("閿�鍞�-鍙嶅鍏ュ簱"); // 澶囨敞
-            stockInRecord.setType(stockOutRecord.getType()); // 绫诲瀷锛堝悎鏍�/涓嶅悎鏍硷級
-            stockInRecord.setSalesLedgerId(stockOutRecord.getSalesLedgerId()); // 閿�鍞鍗旾D
-            stockInRecord.setSalesLedgerProductId(stockOutRecord.getSalesLedgerProductId()); // 閿�鍞鍗曚骇鍝両D
-            
-            // 4.2 鎻掑叆鍙嶅鍏ュ簱璁板綍
-            stockInRecordMapper.insert(stockInRecord);
-            
-            // 4.3 鍚戝簱瀛樿〃涓鍔犵浉搴旀暟閲�
+            // 浠庡簱瀛樿〃涓墸鍑忕浉搴旀暟閲�
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setProductModelId(stockInRecord.getProductModelId());
             stockInventoryDto.setQualitity(stockInRecord.getStockInNum());
+            stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+        }
+
+        // 3. 鍒犻櫎鎵�鏈夊叆搴撹褰�
+        stockInRecordMapper.delete(
+                Wrappers.<StockInRecord>lambdaQuery()
+                        .eq(StockInRecord::getSalesLedgerId, originalSalesLedgerId)
+        );
+
+        // 4. 鏌ヨ鍘熻鍗曠殑鎵�鏈夊嚭搴撹褰�
+        List<StockOutRecord> stockOutRecords = stockOutRecordMapper.selectList(
+                Wrappers.<StockOutRecord>lambdaQuery()
+                        .eq(StockOutRecord::getSalesLedgerId, originalSalesLedgerId)
+        );
+
+        // 5. 鍒犻櫎鍑哄簱璁板綍骞跺鍔犲簱瀛�
+        for (StockOutRecord stockOutRecord : stockOutRecords) {
+            // 鍚戝簱瀛樿〃涓鍔犵浉搴旀暟閲�
+            StockInventoryDto stockInventoryDto = new StockInventoryDto();
+            stockInventoryDto.setProductModelId(stockOutRecord.getProductModelId());
+            stockInventoryDto.setQualitity(stockOutRecord.getStockOutNum());
             stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
         }
+
+        // 6. 鍒犻櫎鎵�鏈夊嚭搴撹褰�
+        stockOutRecordMapper.delete(
+                Wrappers.<StockOutRecord>lambdaQuery()
+                        .eq(StockOutRecord::getSalesLedgerId, originalSalesLedgerId)
+        );
     }
-}
\ No newline at end of file
+
+
+    @Override
+    public void incrementPrintCount(Long id, String printType) {
+        if (id == null) {
+            throw new ServiceException("閿�鍞彴璐D涓嶈兘涓虹┖");
+        }
+        if (printType == null || (!"label".equals(printType) && !"document".equals(printType))) {
+            throw new ServiceException("鎵撳嵃绫诲瀷蹇呴』涓� label 鎴� document");
+        }
+        SalesLedger ledger = salesLedgerMapper.selectById(id);
+        if (ledger == null) {
+            throw new ServiceException("閿�鍞彴璐︿笉瀛樺湪");
+        }
+        if ("label".equals(printType)) {
+            int currentCount = ledger.getLabelPrintCount() == null ? 0 : ledger.getLabelPrintCount();
+            salesLedgerMapper.update(null,
+                    Wrappers.<SalesLedger>lambdaUpdate()
+                            .eq(SalesLedger::getId, id)
+                            .set(SalesLedger::getLabelPrintCount, currentCount + 1)
+            );
+        } else {
+            int currentCount = ledger.getDocumentPrintCount() == null ? 0 : ledger.getDocumentPrintCount();
+            salesLedgerMapper.update(null,
+                    Wrappers.<SalesLedger>lambdaUpdate()
+                            .eq(SalesLedger::getId, id)
+                            .set(SalesLedger::getDocumentPrintCount, currentCount + 1)
+            );
+        }
+    }
+
+    @Override
+    public void exportWithProducts(HttpServletResponse response, SalesLedgerDto salesLedgerDto) {
+        try {
+            // 1. 鏌ヨ閿�鍞彴璐﹀垪琛紙瀵煎嚭浣跨敤鍗囧簭鎺掑簭锛�
+            Page<SalesLedger> page = new Page<>(-1, -1);
+            // 浣跨敤 Wrappers 鏋勫缓鍗囧簭鏌ヨ
+            LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.<SalesLedger>lambdaQuery()
+                    .orderByAsc(SalesLedger::getEntryDate)
+                    .orderByAsc(SalesLedger::getId);
+
+            // 娣诲姞鏌ヨ鏉′欢
+            if (salesLedgerDto.getCustomerName() != null && !salesLedgerDto.getCustomerName().isEmpty()) {
+                queryWrapper.like(SalesLedger::getCustomerName, salesLedgerDto.getCustomerName());
+            }
+            if (salesLedgerDto.getSalesContractNo() != null && !salesLedgerDto.getSalesContractNo().isEmpty()) {
+                queryWrapper.like(SalesLedger::getSalesContractNo, salesLedgerDto.getSalesContractNo());
+            }
+            if (salesLedgerDto.getProjectName() != null && !salesLedgerDto.getProjectName().isEmpty()) {
+                queryWrapper.like(SalesLedger::getProjectName, salesLedgerDto.getProjectName());
+            }
+            if (salesLedgerDto.getEntryDateStart() != null && !salesLedgerDto.getEntryDateStart().isEmpty()) {
+                queryWrapper.ge(SalesLedger::getEntryDate, salesLedgerDto.getEntryDateStart());
+            }
+            if (salesLedgerDto.getEntryDateEnd() != null && !salesLedgerDto.getEntryDateEnd().isEmpty()) {
+                queryWrapper.le(SalesLedger::getEntryDate, salesLedgerDto.getEntryDateEnd());
+            }
+            if (salesLedgerDto.getDeliveryStatus() != null) {
+                queryWrapper.eq(SalesLedger::getDeliveryStatus, salesLedgerDto.getDeliveryStatus());
+            }
+            if (salesLedgerDto.getStockStatus() != null) {
+                queryWrapper.eq(SalesLedger::getStockStatus, salesLedgerDto.getStockStatus());
+            }
+            if (salesLedgerDto.getReviewStatus() != null) {
+                queryWrapper.eq(SalesLedger::getReviewStatus, salesLedgerDto.getReviewStatus());
+            }
+
+            if (salesLedgerDto.getReviewStatusList() != null && !salesLedgerDto.getReviewStatusList().isEmpty()) {
+                queryWrapper.and(w -> w.in(SalesLedger::getReviewStatus, salesLedgerDto.getReviewStatusList())
+                        .or().isNull(SalesLedger::getReviewStatus));
+            }
+
+            IPage<SalesLedger> ledgerPage = salesLedgerMapper.selectPage(page, queryWrapper);
+            List<SalesLedger> ledgerList = ledgerPage.getRecords();
+
+            // 2. 鏀堕泦鏁版嵁
+            List<SalesLedgerExportDto> ledgerExportList = new ArrayList<>();
+            List<SalesLedgerProductExportDto> productExportList = new ArrayList<>();
+
+            for (SalesLedger ledger : ledgerList) {
+                // 杞崲鍙拌处鏁版嵁
+                SalesLedgerExportDto ledgerDto = new SalesLedgerExportDto();
+                ledgerDto.setSalesContractNo(ledger.getSalesContractNo());
+                ledgerDto.setCustomerContractNo(ledger.getCustomerContractNo());
+                ledgerDto.setProjectName(ledger.getProjectName());
+                ledgerDto.setCustomerName(ledger.getCustomerName());
+                ledgerDto.setSalesman(ledger.getSalesman());
+                ledgerDto.setEntryPersonName(ledger.getEntryPersonName());
+                ledgerDto.setEntryDate(ledger.getEntryDate());
+                ledgerDto.setExecutionDate(ledger.getExecutionDate() != null ?
+                        java.sql.Date.valueOf(ledger.getExecutionDate()) : null);
+                ledgerDto.setDeliveryDate(ledger.getDeliveryDate() != null ?
+                        java.sql.Date.valueOf(ledger.getDeliveryDate()) : null);
+                ledgerDto.setContractAmount(ledger.getContractAmount());
+                ledgerDto.setRemarks(ledger.getRemarks());
+                ledgerDto.setCustomerRemarks(ledger.getCustomerRemarks());
+                ledgerDto.setDeliveryStatusText(getDeliveryStatusText(ledger.getDeliveryStatus()));
+                ledgerDto.setStockStatusText(getStockStatusText(ledger.getStockStatus()));
+                ledgerDto.setReviewStatusText(getReviewStatusText(ledger.getReviewStatus()));
+
+                ledgerExportList.add(ledgerDto);
+
+                // 鏌ヨ璇ュ彴璐︾殑浜у搧鍒楄〃
+                List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
+                        Wrappers.<SalesLedgerProduct>lambdaQuery()
+                                .eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId())
+                                .eq(SalesLedgerProduct::getType, 1)
+                );
+
+                for (SalesLedgerProduct product : products) {
+                    SalesLedgerProductExportDto productDto = new SalesLedgerProductExportDto();
+                    productDto.setSalesContractNo(ledger.getSalesContractNo());
+                    productDto.setProductCategory(product.getProductCategory());
+                    productDto.setSpecificationModel(product.getSpecificationModel());
+                    productDto.setThickness(product.getThickness());
+                    productDto.setFloorCode(product.getFloorCode());
+                    productDto.setWidth(product.getWidth());
+                    productDto.setHeight(product.getHeight());
+                    productDto.setQuantity(product.getQuantity());
+                    productDto.setSettlePieceArea(product.getSettlePieceArea());
+                    productDto.setSettleTotalArea(product.getSettleTotalArea());
+                    productDto.setTaxInclusiveUnitPrice(product.getTaxInclusiveUnitPrice());
+                    productDto.setTaxRate(product.getTaxRate());
+                    productDto.setTaxInclusiveTotalPrice(product.getTaxInclusiveTotalPrice());
+                    productDto.setTaxExclusiveTotalPrice(product.getTaxExclusiveTotalPrice());
+                    productDto.setInvoiceType(product.getInvoiceType());
+                    productDto.setProcessRequirement(product.getProcessRequirement());
+                    productDto.setRemark(product.getRemark());
+                    productExportList.add(productDto);
+                }
+            }
+
+            // 3. 浣跨敤ExcelUtil瀵煎嚭锛堝唴閮ㄤ細鍒涘缓Workbook骞跺啓鍏ュ搷搴旓級
+            // 鍏堝垱寤轰复鏃舵枃浠讹紝鍐嶅悎骞朵袱涓猻heet
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            String fileName = URLEncoder.encode("閿�鍞彴璐�.xlsx", "utf-8").replaceAll("\\+", "%20");
+            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
+
+            org.apache.poi.xssf.usermodel.XSSFWorkbook workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook();
+
+            // Sheet1: 閿�鍞彴璐� - 鎵嬪姩濉厖
+            fillSheetWithData(workbook, "閿�鍞彴璐�", SalesLedgerExportDto.class, ledgerExportList);
+
+            // Sheet2: 浜у搧鏄庣粏 - 鎵嬪姩濉厖
+            fillSheetWithData(workbook, "浜у搧鏄庣粏", SalesLedgerProductExportDto.class, productExportList);
+
+            workbook.write(response.getOutputStream());
+            workbook.close();
+
+        } catch (Exception e) {
+            log.error("瀵煎嚭閿�鍞彴璐﹀け璐�", e);
+            throw new ServiceException("瀵煎嚭澶辫触锛�" + e.getMessage());
+        }
+    }
+
+    @Override
+    public void exportProcessRoute(HttpServletResponse response, List<Long> salesLedgerIds, String completedTimeStart, String completedTimeEnd) {
+        try {
+            if (CollectionUtils.isEmpty(salesLedgerIds)) {
+                throw new ServiceException("璇烽�夋嫨瑕佸鍑虹殑閿�鍞彴璐�");
+            }
+            LocalDateTime startTime = parseCompletedTime(completedTimeStart);
+            LocalDateTime endTime = parseCompletedTime(completedTimeEnd);
+            if (startTime == null && endTime == null) {
+                startTime = LocalDate.now().atStartOfDay();
+                endTime = LocalDateTime.now();
+            } else {
+                if (startTime == null) {
+                    startTime = LocalDate.now().atStartOfDay();
+                }
+                if (endTime == null) {
+                    endTime = LocalDateTime.now();
+                }
+            }
+
+            LambdaQueryWrapper<SalesLedgerProcessRouteRecord> queryWrapper = Wrappers.<SalesLedgerProcessRouteRecord>lambdaQuery()
+                    .eq(SalesLedgerProcessRouteRecord::getIsCompleted, 1)
+                    .ge(SalesLedgerProcessRouteRecord::getCompletedTime, startTime)
+                    .le(SalesLedgerProcessRouteRecord::getCompletedTime, endTime)
+                    .orderByAsc(SalesLedgerProcessRouteRecord::getCompletedTime)
+                    .orderByAsc(SalesLedgerProcessRouteRecord::getId);
+
+            if (CollectionUtils.isNotEmpty(salesLedgerIds)) {
+                queryWrapper.in(SalesLedgerProcessRouteRecord::getSalesLedgerId, salesLedgerIds);
+            }
+
+            List<SalesLedgerProcessRouteRecord> completedRoutes = salesLedgerProcessRouteRecordService.list(queryWrapper);
+            Map<Long, SalesLedger> salesLedgerMap = Collections.emptyMap();
+            Map<Long, SalesLedgerProcessRoute> routeMap = Collections.emptyMap();
+            Map<Long, ProcessRouteItem> processRouteItemMap = Collections.emptyMap();
+            Map<Long, List<SalesLedgerProduct>> productMap = Collections.emptyMap();
+
+            if (CollectionUtils.isNotEmpty(completedRoutes)) {
+                List<Long> routeSalesLedgerIds = completedRoutes.stream()
+                        .map(SalesLedgerProcessRouteRecord::getSalesLedgerId)
+                        .filter(Objects::nonNull)
+                        .distinct()
+                        .collect(Collectors.toList());
+                List<Long> salesLedgerProcessRouteIds = completedRoutes.stream()
+                        .map(SalesLedgerProcessRouteRecord::getSalesLedgerProcessRouteId)
+                        .filter(Objects::nonNull)
+                        .distinct()
+                        .collect(Collectors.toList());
+
+                if (CollectionUtils.isNotEmpty(routeSalesLedgerIds)) {
+                    salesLedgerMap = salesLedgerMapper.selectBatchIds(routeSalesLedgerIds).stream()
+                            .filter(Objects::nonNull)
+                            .collect(Collectors.toMap(SalesLedger::getId, item -> item, (a, b) -> a));
+                    List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                            .in(SalesLedgerProduct::getSalesLedgerId, routeSalesLedgerIds)
+                            .eq(SalesLedgerProduct::getType, 1)
+                            .orderByAsc(SalesLedgerProduct::getSalesLedgerId)
+                            .orderByAsc(SalesLedgerProduct::getId));
+                    productMap = products.stream().collect(Collectors.groupingBy(
+                            SalesLedgerProduct::getSalesLedgerId,
+                            LinkedHashMap::new,
+                            Collectors.toList()
+                    ));
+                }
+
+                if (CollectionUtils.isNotEmpty(salesLedgerProcessRouteIds)) {
+                    routeMap = salesLedgerProcessRouteService.listByIds(salesLedgerProcessRouteIds).stream()
+                            .filter(Objects::nonNull)
+                            .collect(Collectors.toMap(SalesLedgerProcessRoute::getId, item -> item, (a, b) -> a));
+                    List<Long> processRouteItemIds = routeMap.values().stream()
+                            .map(SalesLedgerProcessRoute::getProcessRouteItemId)
+                            .filter(Objects::nonNull)
+                            .distinct()
+                            .collect(Collectors.toList());
+                    if (CollectionUtils.isNotEmpty(processRouteItemIds)) {
+                        processRouteItemMap = processRouteItemMapper.selectBatchIds(processRouteItemIds).stream()
+                                .filter(Objects::nonNull)
+                                .collect(Collectors.toMap(ProcessRouteItem::getId, item -> item, (a, b) -> a));
+                    }
+                }
+            }
+
+            Map<Long, List<SalesLedgerProcessRouteRecord>> routeGroupMap = new LinkedHashMap<>();
+            for (SalesLedgerProcessRouteRecord record : completedRoutes) {
+                SalesLedgerProcessRoute route = routeMap.get(record.getSalesLedgerProcessRouteId());
+                if (route == null || route.getProcessRouteItemId() == null) {
+                    continue;
+                }
+                routeGroupMap.computeIfAbsent(route.getProcessRouteItemId(), k -> new ArrayList<>()).add(record);
+            }
+
+            final Map<Long, ProcessRouteItem> finalProcessRouteItemMap = processRouteItemMap;
+            LinkedHashMap<String, List<List<Object>>> sheetMap = new LinkedHashMap<>();
+            List<Long> orderedProcessRouteItemIds = routeGroupMap.keySet().stream()
+                    .sorted((left, right) -> compareProcessRouteItem(left, right, finalProcessRouteItemMap))
+                    .collect(Collectors.toList());
+
+            for (Long processRouteItemId : orderedProcessRouteItemIds) {
+                ProcessRouteItem processRouteItem = finalProcessRouteItemMap.get(processRouteItemId);
+                String sheetName = buildUniqueSheetName(sheetMap, processRouteItem, processRouteItemId);
+                List<List<Object>> sheetData = new ArrayList<>();
+                sheetData.add(buildProcessRouteHeader());
+
+                for (SalesLedgerProcessRouteRecord route : routeGroupMap.getOrDefault(processRouteItemId, Collections.emptyList())) {
+                    SalesLedger salesLedger = salesLedgerMap.get(route.getSalesLedgerId());
+                    if (salesLedger == null) {
+                        continue;
+                    }
+                    List<SalesLedgerProduct> products = productMap.getOrDefault(salesLedger.getId(), Collections.emptyList());
+                    if (CollectionUtils.isEmpty(products)) {
+                        sheetData.add(buildProcessRouteRow(salesLedger, null, route));
+                        continue;
+                    }
+                    for (SalesLedgerProduct product : products) {
+                        sheetData.add(buildProcessRouteRow(salesLedger, product, route));
+                    }
+                }
+
+                if (sheetData.size() == 1) {
+                    sheetData.add(Arrays.asList("", "", "", "", "", "", "", "", ""));
+                }
+                sheetMap.put(sheetName, sheetData);
+            }
+
+            if (sheetMap.isEmpty()) {
+                List<List<Object>> sheetData = new ArrayList<>();
+                sheetData.add(buildProcessRouteHeader());
+                sheetData.add(Arrays.asList("", "", "", "", "", "", ""));
+                sheetMap.put("宸ヨ壓璺嚎", sheetData);
+            }
+
+            ExcelUtils.exportManySheet(response, "閿�鍞彴璐﹀伐鑹鸿矾绾垮鍑�", sheetMap);
+        } catch (Exception e) {
+            log.error("瀵煎嚭鍞悗鍙拌处宸ヨ壓璺嚎澶辫触", e);
+            throw new ServiceException("瀵煎嚭鍞悗鍙拌处宸ヨ壓璺嚎澶辫触锛�" + e.getMessage());
+        }
+    }
+
+    private LocalDateTime parseCompletedTime(String value) {
+        if (!StringUtils.hasText(value)) {
+            return null;
+        }
+        String text = value.trim();
+        try {
+            return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        } catch (Exception ex) {
+            try {
+                return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
+            } catch (Exception ignored) {
+                return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
+            }
+        }
+    }
+
+    private int compareProcessRouteItem(Long left, Long right, Map<Long, ProcessRouteItem> processRouteItemMap) {
+        ProcessRouteItem leftItem = processRouteItemMap.get(left);
+        ProcessRouteItem rightItem = processRouteItemMap.get(right);
+        int leftSort = leftItem != null && leftItem.getDragSort() != null ? leftItem.getDragSort() : Integer.MAX_VALUE;
+        int rightSort = rightItem != null && rightItem.getDragSort() != null ? rightItem.getDragSort() : Integer.MAX_VALUE;
+        if (leftSort != rightSort) {
+            return Integer.compare(leftSort, rightSort);
+        }
+        String leftName = leftItem != null && StringUtils.hasText(leftItem.getProcessName()) ? leftItem.getProcessName() : "";
+        String rightName = rightItem != null && StringUtils.hasText(rightItem.getProcessName()) ? rightItem.getProcessName() : "";
+        int nameCompare = leftName.compareTo(rightName);
+        if (nameCompare != 0) {
+            return nameCompare;
+        }
+        return Long.compare(left, right);
+    }
+
+    private List<Object> buildProcessRouteHeader() {
+        return Arrays.asList("鏃ユ湡", "璁㈠崟缂栧彿", "瀹㈡埛鍚嶇О", "瑙勬牸", "鏁伴噺", "闈㈢Н", "鏄惁鏄伐绋�");
+    }
+
+    private List<Object> buildProcessRouteRow(SalesLedger salesLedger, SalesLedgerProduct product, SalesLedgerProcessRouteRecord route) {
+        List<Object> row = new ArrayList<>();
+        row.add(route.getCompletedTime() == null ? "" : route.getCompletedTime().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+        row.add(salesLedger.getSalesContractNo());
+        row.add(salesLedger.getCustomerName());
+        row.add(product == null ? "" : product.getSpecificationModel());
+        row.add(product == null ? "" : product.getQuantity());
+        row.add(product == null ? "" : resolveExportArea(product));
+        row.add("");
+        return row;
+    }
+
+    private BigDecimal resolveExportArea(SalesLedgerProduct product) {
+        if (product == null) {
+            return BigDecimal.ZERO;
+        }
+        if (product.getSettleTotalArea() != null) {
+            return product.getSettleTotalArea();
+        }
+        if (product.getActualTotalArea() != null) {
+            return product.getActualTotalArea();
+        }
+        BigDecimal qty = product.getQuantity() == null ? BigDecimal.ONE : product.getQuantity();
+        if (product.getSettlePieceArea() != null) {
+            return product.getSettlePieceArea().multiply(qty).setScale(2, RoundingMode.HALF_UP);
+        }
+        if (product.getActualPieceArea() != null) {
+            return product.getActualPieceArea().multiply(qty).setScale(2, RoundingMode.HALF_UP);
+        }
+        if (product.getWidth() != null && product.getHeight() != null) {
+            BigDecimal area = product.getWidth().multiply(product.getHeight())
+                    .divide(new BigDecimal("1000000"), 2, RoundingMode.HALF_UP);
+            return area.multiply(qty).setScale(2, RoundingMode.HALF_UP);
+        }
+        return BigDecimal.ZERO;
+    }
+
+    private String buildUniqueSheetName(Map<String, List<List<Object>>> sheetMap, ProcessRouteItem processRouteItem, Long processRouteItemId) {
+        String baseName = processRouteItem != null && StringUtils.hasText(processRouteItem.getProcessName())
+                ? processRouteItem.getProcessName()
+                : "宸ュ簭" + processRouteItemId;
+        baseName = sanitizeSheetName(baseName);
+        String sheetName = baseName;
+        int suffix = 2;
+        while (sheetMap.containsKey(sheetName)) {
+            String suffixText = "_" + suffix++;
+            int maxBaseLength = 31 - suffixText.length();
+            String trimmedBase = baseName.length() > maxBaseLength ? baseName.substring(0, maxBaseLength) : baseName;
+            sheetName = trimmedBase + suffixText;
+        }
+        return sheetName;
+    }
+
+    private String sanitizeSheetName(String sheetName) {
+        if (!StringUtils.hasText(sheetName)) {
+            return "宸ヨ壓璺嚎";
+        }
+        String sanitized = sheetName.replaceAll("[\\\\/?*\\[\\]:]", "_").trim();
+        if (sanitized.isEmpty()) {
+            sanitized = "宸ヨ壓璺嚎";
+        }
+        return sanitized.length() > 31 ? sanitized.substring(0, 31) : sanitized;
+    }
+
+    /**
+     * 鎵嬪姩濉厖Sheet鏁版嵁
+     */
+    private <T> void fillSheetWithData(org.apache.poi.xssf.usermodel.XSSFWorkbook workbook, String sheetName, Class<T> clazz, List<T> dataList) throws Exception {
+        org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet(sheetName);
+
+        // 鑾峰彇瀛楁涓婄殑@Excel娉ㄨВ
+        java.lang.reflect.Field[] fields = clazz.getDeclaredFields();
+        List<java.lang.reflect.Field> excelFields = new ArrayList<>();
+        for (java.lang.reflect.Field field : fields) {
+            com.ruoyi.framework.aspectj.lang.annotation.Excel excel = field.getAnnotation(com.ruoyi.framework.aspectj.lang.annotation.Excel.class);
+            if (excel != null) {
+                excelFields.add(field);
+            }
+        }
+
+        // 鍒涘缓琛ㄥご
+        org.apache.poi.ss.usermodel.Row headerRow = sheet.createRow(0);
+        org.apache.poi.ss.usermodel.CellStyle headerStyle = workbook.createCellStyle();
+        headerStyle.setFillForegroundColor(org.apache.poi.ss.usermodel.IndexedColors.GREY_50_PERCENT.getIndex());
+        headerStyle.setFillPattern(org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND);
+        headerStyle.setAlignment(org.apache.poi.ss.usermodel.HorizontalAlignment.CENTER);
+        org.apache.poi.ss.usermodel.Font headerFont = workbook.createFont();
+        headerFont.setBold(true);
+        headerStyle.setFont(headerFont);
+
+        for (int i = 0; i < excelFields.size(); i++) {
+            java.lang.reflect.Field field = excelFields.get(i);
+            field.setAccessible(true);
+            com.ruoyi.framework.aspectj.lang.annotation.Excel excel = field.getAnnotation(com.ruoyi.framework.aspectj.lang.annotation.Excel.class);
+            org.apache.poi.ss.usermodel.Cell cell = headerRow.createCell(i);
+            cell.setCellValue(excel.name());
+            cell.setCellStyle(headerStyle);
+            sheet.setColumnWidth(i, 20 * 256);
+        }
+
+        // 鍒涘缓鏁版嵁琛�
+        org.apache.poi.ss.usermodel.CellStyle dataStyle = workbook.createCellStyle();
+        dataStyle.setAlignment(org.apache.poi.ss.usermodel.HorizontalAlignment.CENTER);
+
+        for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) {
+            T data = dataList.get(rowIndex);
+            org.apache.poi.ss.usermodel.Row row = sheet.createRow(rowIndex + 1);
+            for (int colIndex = 0; colIndex < excelFields.size(); colIndex++) {
+                java.lang.reflect.Field field = excelFields.get(colIndex);
+                field.setAccessible(true);
+                Object value = field.get(data);
+                org.apache.poi.ss.usermodel.Cell cell = row.createCell(colIndex);
+                if (value == null) {
+                    cell.setCellValue("");
+                } else if (value instanceof Number) {
+                    cell.setCellValue(((Number) value).doubleValue());
+                } else if (value instanceof Date) {
+                    cell.setCellValue(new java.text.SimpleDateFormat("yyyy-MM-dd").format((Date) value));
+                } else {
+                    cell.setCellValue(value.toString());
+                }
+                cell.setCellStyle(dataStyle);
+            }
+        }
+    }
+
+    private String getDeliveryStatusText(Integer status) {
+        if (status == null) return "鏈煡";
+        switch (status) {
+            case 1:
+                return "鏈彂璐�";
+            case 2:
+                return "瀹℃壒涓�";
+            case 3:
+                return "瀹℃壒涓嶉�氳繃";
+            case 4:
+                return "瀹℃壒閫氳繃";
+            case 5:
+                return "宸插彂璐�";
+            case 6:
+                return "閮ㄥ垎鍙戣揣";
+            default:
+                return "鏈煡";
+        }
+    }
+
+    private String getStockStatusText(Integer status) {
+        if (status == null) return "鏈煡";
+        switch (status) {
+            case 0:
+                return "鏈叆搴�";
+            case 1:
+                return "閮ㄥ垎鍏ュ簱";
+            case 2:
+                return "宸插叆搴�";
+            case 3:
+                return "瀹℃壒涓�";
+            default:
+                return "鏈煡";
+        }
+    }
+
+    private String getReviewStatusText(Integer status) {
+        if (status == null) return "寰呭鏍�";
+        switch (status) {
+            case 0:
+                return "寰呭鏍�";
+            case 1:
+                return "宸插鏍�";
+            case 2:
+                return "宸插弽瀹�";
+            default:
+                return "寰呭鏍�";
+        }
+    }
+
+}

--
Gitblit v1.9.3