From 481d8165f26fc7e37f0e87f6444ac0340bb563a3 Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期二, 03 十一月 2026 18:53:22 +0800
Subject: [PATCH] fix: 移植长辉已验证的四类接口 500/体验问题(不含物料颜色字段)

---
 yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java |  145 +++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 111 insertions(+), 34 deletions(-)

diff --git a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java
index 1bbffa3..2d6a18d 100644
--- a/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java
+++ b/yudao-module-erp/src/main/java/cn/iocoder/yudao/module/erp/service/sale/ErpSaleOrderServiceImpl.java
@@ -1,6 +1,7 @@
 package cn.iocoder.yudao.module.erp.service.sale;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.extra.spring.SpringUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.exception.ServiceException;
@@ -9,6 +10,7 @@
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
 import cn.iocoder.yudao.module.crm.api.contract.CrmContractApi;
+import cn.iocoder.yudao.module.crm.api.contract.dto.CrmContractForSaleDTO;
 import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO;
 import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderImportExcelVO;
 import cn.iocoder.yudao.module.erp.controller.admin.sale.vo.order.ErpSaleOrderImportReqVO;
@@ -114,6 +116,9 @@
         if (createReqVO.getSaleUserId() != null) {
             adminUserApi.validateUser(createReqVO.getSaleUserId());
         }
+        if (createReqVO.getContractId() != null && saleOrderMapper.selectByContractId(createReqVO.getContractId()) != null) {
+            throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "璇ュ悎鍚屽凡鍏宠仈閿�鍞鍗�");
+        }
         // 1.5 鐢熸垚璁㈠崟鍙凤紝骞舵牎楠屽敮涓�鎬�
         String no = noRedisDAO.generate(ErpNoRedisDAO.SALE_ORDER_NO_PREFIX);
         if (saleOrderMapper.selectByNo(no) != null) {
@@ -141,7 +146,7 @@
     public void updateSaleOrder(ErpSaleOrderSaveReqVO updateReqVO) {
         // 1.1 鏍¢獙瀛樺湪
         ErpSaleOrderDO saleOrder = validateSaleOrderExists(updateReqVO.getId());
-        if (ErpAuditStatus.APPROVE.getStatus().equals(saleOrder.getStatus())) {
+        if (!ErpAuditStatus.PROCESS.getStatus().equals(saleOrder.getStatus())) {
             throw exception(SALE_ORDER_UPDATE_FAIL_APPROVE, saleOrder.getNo());
         }
         // 1.2 鏍¢獙瀹㈡埛锛堜娇鐢� CRM 瀹㈡埛 API锛�
@@ -185,29 +190,21 @@
     @Transactional(rollbackFor = Exception.class)
     public void updateSaleOrderStatus(Long id, Integer status) {
         boolean approve = ErpAuditStatus.APPROVE.getStatus().equals(status);
-        // 1.1 鏍¢獙瀛樺湪
-        ErpSaleOrderDO saleOrder = validateSaleOrderExists(id);
-        // 1.2 鏍¢獙鐘舵��
-        if (saleOrder.getStatus().equals(status)) {
+        ErpSaleOrderDO order = validateSaleOrderExists(id);
+        if (approve && !ErpAuditStatus.PROCESS.getStatus().equals(order.getStatus())
+                || !approve && (!ErpAuditStatus.PROCESS.getStatus().equals(status)
+                || !ErpAuditStatus.APPROVE.getStatus().equals(order.getStatus()))) {
             throw exception(approve ? SALE_ORDER_APPROVE_FAIL : SALE_ORDER_PROCESS_FAIL);
         }
-        // 1.3 瀛樺湪閿�鍞嚭搴撳崟锛屾棤娉曞弽瀹℃牳
-        BigDecimal outCount = saleOrder.getOutCount() != null ? saleOrder.getOutCount() : BigDecimal.ZERO;
-        if (!approve && outCount.compareTo(BigDecimal.ZERO) > 0) {
-            throw exception(SALE_ORDER_PROCESS_FAIL_EXISTS_OUT);
+        if (!approve) {
+            BigDecimal outCount = order.getOutCount() == null ? BigDecimal.ZERO : order.getOutCount();
+            if (outCount.signum() > 0) throw exception(SALE_ORDER_PROCESS_FAIL_EXISTS_OUT);
+            validateNoActiveMps(id);
         }
-
-        // 2. 鏇存柊鐘舵��
-        int updateCount = saleOrderMapper.updateByIdAndStatus(id, saleOrder.getStatus(),
-                new ErpSaleOrderDO().setStatus(status));
-        if (updateCount == 0) {
+        if (saleOrderMapper.updateByIdAndStatus(id, order.getStatus(), new ErpSaleOrderDO().setStatus(status)) != 1) {
             throw exception(approve ? SALE_ORDER_APPROVE_FAIL : SALE_ORDER_PROCESS_FAIL);
         }
-
-        // 3. 瀹℃牳閫氳繃鏃讹紝涓洪渶瑕佺敓浜х殑鏄庣粏鍒涘缓 MPS
-        if (approve) {
-            createMpsForSaleOrder(id, saleOrder);
-        }
+        if (approve) createMpsForSaleOrder(id, order);
     }
 
     /**
@@ -317,6 +314,8 @@
             throw exception(SALE_ORDER_CANCEL_FAIL_EXISTS_OUT);
         }
 
+        validateNoActiveMps(id);
+
         // 2. 鏇存柊鐘舵�佷负宸蹭綔搴�
         int updateCount = saleOrderMapper.updateByIdAndStatus(id, saleOrder.getStatus(),
                 new ErpSaleOrderDO().setStatus(ErpAuditStatus.CANCEL.getStatus()));
@@ -324,10 +323,7 @@
             throw exception(SALE_ORDER_CANCEL_FAIL);
         }
 
-        // 3. 鍚屾鏇存柊鍏宠仈鍚堝悓鐘舵�佷负宸蹭綔搴�
-        if (saleOrder.getContractId() != null) {
-            contractApi.updateContractStatus(saleOrder.getContractId(), 50).getCheckedData(); // 50 = 宸蹭綔搴�
-        }
+        // 鍚堝悓瀹℃壒鐘舵�佺敱鍚堝悓鍙婂叾 BPM 娴佺▼缁存姢锛岃鍗曚綔搴熶笉鏀瑰彉鍚堝悓瀹℃壒缁撴灉銆�
     }
 
     @Override
@@ -347,12 +343,17 @@
             throw exception(SALE_ORDER_ENABLE_FAIL);
         }
 
-        // 3. 鍚屾鏇存柊鍏宠仈鍚堝悓鐘舵�佷负瀹℃牳閫氳繃
-        if (saleOrder.getContractId() != null) {
-            contractApi.updateContractStatus(saleOrder.getContractId(), 20).getCheckedData(); // 20 = 瀹℃牳閫氳繃
-        }
+        // 璁㈠崟鍚敤涓嶆敼鍙樺叧鑱斿悎鍚岀殑瀹℃壒缁撴灉銆�
     }
 
+    private void validateNoActiveMps(Long orderId) {
+        for (ErpSaleOrderItemDO item : saleOrderItemMapper.selectListByOrderId(orderId)) {
+            MesProMpsRespDTO mps = mpsApi.getMpsBySaleOrderItemId(item.getId());
+            if (mps != null && ObjectUtil.notEqual(mps.getStatus(), MPS_STATUS_CANCELED)) {
+                throw exception(SALE_ORDER_HAS_ACTIVE_MPS);
+            }
+        }
+    }
     private List<ErpSaleOrderItemDO> validateSaleOrderItems(List<ErpSaleOrderSaveReqVO.Item> list) {
         // 杞寲涓� ErpSaleOrderItemDO 鍒楄〃锛堜骇鍝侀獙璇佸凡绉婚櫎锛�
         return convertList(list, o -> BeanUtils.toBean(o, ErpSaleOrderItemDO.class, item -> {
@@ -414,7 +415,7 @@
             return;
         }
         saleOrders.forEach(saleOrder -> {
-            if (ErpAuditStatus.APPROVE.getStatus().equals(saleOrder.getStatus())) {
+            if (!ErpAuditStatus.PROCESS.getStatus().equals(saleOrder.getStatus())) {
                 throw exception(SALE_ORDER_DELETE_FAIL_APPROVE, saleOrder.getNo());
             }
         });
@@ -523,18 +524,23 @@
     @Override
     public ErpSaleOrderImportRespVO importSaleOrderList(List<ErpSaleOrderImportExcelVO> importOrders,
                                                         ErpSaleOrderImportReqVO importReqVO) {
+        if (Boolean.TRUE.equals(importReqVO.getUpdateSupport())) {
+            throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "閿�鍞鍗曞鍏ヤ粎鏀寔鏂板锛屼笉鏀寔瑕嗙洊");
+        }
         // 1. 鏍¢獙闈炵┖锛堣繃婊ゅ畬鍏ㄧ┖鐧界殑琛岋級
-        importOrders = filterList(importOrders, item -> StrUtil.isNotBlank(item.getProductCode()));
+        importOrders = filterList(importOrders, item -> item != null && (StrUtil.isNotBlank(item.getNo()) || StrUtil.isNotBlank(item.getCustomerName()) || StrUtil.isNotBlank(item.getProductCode()) || item.getCount() != null || item.getProductPrice() != null));
         if (CollUtil.isEmpty(importOrders)) {
             throw exception(SALE_ORDER_IMPORT_LIST_IS_EMPTY);
         }
         // 2. 鎸夈�屽崟鍙枫�嶅垎缁勶細鐩稿悓鍗曞彿鐨勫琛屽悎骞舵垚涓�寮犻攢鍞鍗曪紱鍗曞彿涓虹┖鏃舵瘡琛屽悇鑷垚涓�寮犺鍗�
         List<List<ErpSaleOrderImportExcelVO>> groups = new ArrayList<>();
         Map<String, Integer> groupIndexMap = new LinkedHashMap<>();
-        int rowIndex = 0;
         for (ErpSaleOrderImportExcelVO row : importOrders) {
-            rowIndex++;
-            String groupKey = StrUtil.isNotBlank(row.getNo()) ? row.getNo() : ("__row_" + rowIndex);
+            if (StrUtil.isBlank(row.getNo())) {
+                groups.add(new ArrayList<>(Collections.singletonList(row)));
+                continue;
+            }
+            String groupKey = row.getNo().trim();
             Integer index = groupIndexMap.get(groupKey);
             if (index == null) {
                 index = groups.size();
@@ -552,8 +558,7 @@
             String groupKey = StrUtil.isNotBlank(group.get(0).getNo()) ? group.get(0).getNo()
                     : "绗�" + (importOrders.indexOf(group.get(0)) + 1) + "琛�";
             try {
-                ErpSaleOrderSaveReqVO createReqVO = buildSaleOrderImportSaveReqVO(group);
-                Long orderId = createSaleOrder(createReqVO);
+                Long orderId = SpringUtil.getBean(ErpSaleOrderServiceImpl.class).createImportedOrder(group);
                 respVO.getCreateSaleOrderNos().add(saleOrderMapper.selectById(orderId).getNo());
             } catch (ServiceException ex) {
                 respVO.getFailureSaleOrderNos().put(groupKey, ex.getMessage());
@@ -562,11 +567,24 @@
         return respVO;
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public Long createImportedOrder(List<ErpSaleOrderImportExcelVO> group) {
+        return createSaleOrder(buildSaleOrderImportSaveReqVO(group));
+    }
     /**
      * 灏嗗悓涓�寮犻攢鍞鍗曠殑澶氳鏄庣粏杞崲鎴愬垱寤� Request VO
      */
     private ErpSaleOrderSaveReqVO buildSaleOrderImportSaveReqVO(List<ErpSaleOrderImportExcelVO> group) {
         ErpSaleOrderImportExcelVO first = group.get(0);
+        for (ErpSaleOrderImportExcelVO row : group) {
+            if (StrUtil.isNotBlank(row.getCustomerName()) && !StrUtil.equals(first.getCustomerName(), row.getCustomerName())
+                    || row.getOrderTime() != null && !row.getOrderTime().equals(first.getOrderTime())
+                    || StrUtil.isNotBlank(row.getSaleUserName()) && !StrUtil.equals(first.getSaleUserName(), row.getSaleUserName())
+                    || StrUtil.isNotBlank(row.getAccountName()) && !StrUtil.equals(first.getAccountName(), row.getAccountName())
+                    || StrUtil.isNotBlank(row.getContractNo()) && !StrUtil.equals(first.getContractNo(), row.getContractNo())) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鍚屼竴璁㈠崟鐨勫鎴枫�佷笅鍗曟椂闂淬�侀攢鍞憳鍜岀粨绠楄处鎴峰繀椤讳竴鑷�");
+            }
+        }
         // 1. 鏍¢獙骞惰В鏋愬鎴�
         if (StrUtil.isBlank(first.getCustomerName())) {
             throw exception(SALE_ORDER_IMPORT_CUSTOMER_NOT_EXISTS, "");
@@ -577,6 +595,12 @@
         }
         if (first.getOrderTime() == null) {
             throw exception(SALE_ORDER_IMPORT_ORDER_TIME_REQUIRED);
+        }
+        if (first.getDiscountPercent() != null && (first.getDiscountPercent().signum() < 0 || first.getDiscountPercent().compareTo(new BigDecimal("100")) > 0)) {
+            throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "浼樻儬鐜囧繀椤诲湪 0 鍒� 100 涔嬮棿");
+        }
+        if (first.getDepositPrice() != null && first.getDepositPrice().signum() < 0) {
+            throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "瀹氶噾涓嶈兘涓鸿礋鏁�");
         }
         // 2. 瑙f瀽缁撶畻璐︽埛锛堝彲閫夛級
         Long accountId = null;
@@ -599,6 +623,21 @@
         // 4. 缁勮鏄庣粏
         List<ErpSaleOrderSaveReqVO.Item> items = new ArrayList<>();
         for (ErpSaleOrderImportExcelVO row : group) {
+            if (StrUtil.isBlank(row.getProductCode()) || StrUtil.isBlank(row.getProductUnitCode())) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鐗╂枡缂栫爜鍜屽崟浣嶇紪鐮佷笉鑳戒负绌�");
+            }
+            if (row.getCount() == null || row.getCount().compareTo(BigDecimal.ZERO) <= 0) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鐗╂枡 " + row.getProductCode() + " 鐨勬暟閲忓繀椤诲ぇ浜庨浂");
+            }
+            if (row.getProductPrice() == null || row.getProductPrice().compareTo(BigDecimal.ZERO) < 0) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鐗╂枡 " + row.getProductCode() + " 鐨勫崟浠蜂笉鑳戒负绌烘垨涓鸿礋鏁�");
+            }
+            if (row.getNeedProduction() != null && row.getNeedProduction() != 0 && row.getNeedProduction() != 1) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鏄惁闇�瑕佺敓浜у彧鑳藉~鍐� 0 鎴� 1");
+            }
+            if (row.getTaxPercent() != null && (row.getTaxPercent().signum() < 0 || row.getTaxPercent().compareTo(new BigDecimal("100")) > 0)) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "绋庣巼蹇呴』鍦� 0 鍒� 100 涔嬮棿");
+            }
             MdmItemRespDTO product = mdmItemApi.getItemByCode(row.getProductCode()).getCheckedData();
             if (product == null) {
                 throw exception(SALE_ORDER_IMPORT_PRODUCT_NOT_EXISTS, row.getProductCode());
@@ -617,9 +656,47 @@
                     .setRemark(row.getItemRemark());
             items.add(item);
         }
+        CrmContractForSaleDTO contract = null;
+        if (StrUtil.isNotBlank(first.getContractNo())) {
+            contract = contractApi.getContractForSaleByNo(first.getContractNo().trim()).getCheckedData();
+            if (contract == null || !Integer.valueOf(20).equals(contract.getAuditStatus())) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鍚堝悓涓嶅瓨鍦ㄦ垨灏氭湭瀹℃壒閫氳繃");
+            }
+            if (!customer.getId().equals(contract.getCustomerId())) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鍚堝悓瀹㈡埛涓庤鍗曞鎴蜂笉涓�鑷�");
+            }
+            if (saleOrderMapper.selectByContractId(contract.getId()) != null) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "璇ュ悎鍚屽凡鍏宠仈閿�鍞鍗�");
+            }
+            Map<String, BigDecimal> expected = new LinkedHashMap<>();
+            if (contract.getItems() != null) {
+                for (CrmContractForSaleDTO.Item item : contract.getItems()) {
+                    if (item.getItemId() == null || item.getItemUnitId() == null || item.getContractPrice() == null || item.getCount() == null) {
+                        throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "鍚堝悓鏄庣粏涓嶅畬鏁达紝鏃犳硶鍏宠仈璁㈠崟");
+                    }
+                    String key = item.getItemId() + ":" + item.getItemUnitId() + ":" + item.getContractPrice().stripTrailingZeros().toPlainString();
+                    expected.merge(key, item.getCount(), BigDecimal::add);
+                }
+            }
+            Map<String, BigDecimal> actual = new LinkedHashMap<>();
+            for (ErpSaleOrderSaveReqVO.Item item : items) {
+                String key = item.getProductId() + ":" + item.getProductUnitId() + ":" + item.getProductPrice().stripTrailingZeros().toPlainString();
+                actual.merge(key, item.getCount(), BigDecimal::add);
+            }
+            if (expected.isEmpty() || expected.size() != actual.size() || expected.entrySet().stream()
+                    .anyMatch(entry -> !actual.containsKey(entry.getKey()) || entry.getValue().compareTo(actual.get(entry.getKey())) != 0)) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "璁㈠崟鐗╂枡銆佸崟浣嶃�佸崟浠峰強鏁伴噺椤讳笌鍚堝悓鏄庣粏涓�鑷�");
+            }
+            BigDecimal expectedDiscount = ObjectUtil.defaultIfNull(contract.getDiscountPercent(), BigDecimal.ZERO);
+            if (expectedDiscount.compareTo(ObjectUtil.defaultIfNull(first.getDiscountPercent(), BigDecimal.ZERO)) != 0) {
+                throw exception(SALE_ORDER_IMPORT_INVALID_ROW, "璁㈠崟浼樻儬鐜囬』涓庡悎鍚屼竴鑷�");
+            }
+        }
         // 5. 缁勮涓昏〃
         ErpSaleOrderSaveReqVO createReqVO = new ErpSaleOrderSaveReqVO()
                 .setCustomerId(customer.getId())
+                .setContractId(contract == null ? null : contract.getId())
+                .setContractNo(contract == null ? null : first.getContractNo().trim())
                 .setOrderTime(first.getOrderTime())
                 .setSaleUserId(saleUserId)
                 .setAccountId(accountId)

--
Gitblit v1.9.3