From b5e1bceabb36fe1d1e5e88397280d12e7044c6dd Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期五, 06 十一月 2026 15:32:31 +0800
Subject: [PATCH] 完善设备采集、安全管理及采购业务

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesServiceImpl.java |  150 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 150 insertions(+), 0 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesServiceImpl.java
index 6593aa3..7e23186 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesServiceImpl.java
@@ -2,12 +2,19 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
 import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesPageReqVO;
+import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesImportExcelVO;
 import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesSaveReqVO;
+import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLineSaveReqVO;
+import cn.iocoder.yudao.module.mes.controller.admin.wm.vo.MesWmDocImportRespVO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
+import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
+import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO;
 import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.MesWmProductSalesShippingReqVO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeDO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO;
@@ -38,7 +45,13 @@
 import org.springframework.validation.annotation.Validated;
 
 import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
@@ -79,6 +92,10 @@
 
     @Resource
     private ErpSaleOrderApi saleOrderApi;
+
+    @Resource
+    @Lazy
+    private MesMdItemService mdItemService;
 
     @Override
     public Long createProductSales(MesWmProductSalesSaveReqVO createReqVO) {
@@ -440,4 +457,137 @@
         productSalesMapper.updateById(new MesWmProductSalesDO().setId(id).setStatus(status));
     }
 
+    // ==================== 鎵归噺瀵煎叆 ====================
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public MesWmDocImportRespVO importProductSalesList(List<MesWmProductSalesImportExcelVO> importList) {
+        MesWmDocImportRespVO respVO = MesWmDocImportRespVO.empty();
+        if (CollUtil.isEmpty(importList)) {
+            return respVO;
+        }
+        // 1. 鎸夈�屽嚭搴撳崟鍙枫�嶅綊骞舵垚鍗曟嵁 + 鏄庣粏琛�
+        Map<String, List<MesWmProductSalesImportExcelVO>> grouped = new LinkedHashMap<>();
+        int index = 0;
+        for (MesWmProductSalesImportExcelVO row : importList) {
+            index++;
+            String code = StrUtil.trim(row.getSalesCode());
+            if (StrUtil.isBlank(code)) {
+                respVO.getFailureCodes().put("绗� " + index + " 琛�", "鍑哄簱鍗曞彿涓嶈兘涓虹┖");
+                continue;
+            }
+            grouped.computeIfAbsent(code, k -> new ArrayList<>()).add(row);
+        }
+
+        // 2. 棰勫姞杞藉鎴凤紝鎸夊悕绉板缓绱㈠紩
+        Map<String, Long> clientMap = new HashMap<>();
+        try {
+            List<CrmCustomerRespDTO> customers = customerApi.getCustomerSimpleList();
+            if (CollUtil.isNotEmpty(customers)) {
+                customers.forEach(c -> {
+                    if (StrUtil.isNotBlank(c.getName())) {
+                        clientMap.put(c.getName().trim(), c.getId());
+                    }
+                });
+            }
+        } catch (Exception e) {
+            log.warn("[importProductSalesList][鍔犺浇瀹㈡埛澶辫触]", e);
+        }
+
+        // 3. 閫愬紶鍗曟嵁澶勭悊
+        for (Map.Entry<String, List<MesWmProductSalesImportExcelVO>> entry : grouped.entrySet()) {
+            String code = entry.getKey();
+            List<MesWmProductSalesImportExcelVO> rows = entry.getValue();
+            MesWmProductSalesImportExcelVO head = rows.get(0);
+            try {
+                // 3.1 鏍¢獙瀹㈡埛
+                String clientName = StrUtil.trim(head.getClientName());
+                Long clientId = clientName == null ? null : clientMap.get(clientName);
+                if (clientId == null) {
+                    respVO.getFailureCodes().put(code, "瀹㈡埛[" + clientName + "]涓嶅瓨鍦�");
+                    continue;
+                }
+                // 3.2 鏍¢獙鏃ユ湡涓庣紪鐮�
+                LocalDateTime salesDate = parseImportDateTime(head.getSalesDate());
+                if (salesDate == null) {
+                    respVO.getFailureCodes().put(code, "鍑哄簱鏃ユ湡涓嶈兘涓虹┖鎴栨牸寮忎笉姝g‘锛堟敮鎸� yyyy-MM-dd / yyyy-MM-dd HH:mm:ss锛�");
+                    continue;
+                }
+                if (productSalesMapper.selectByCode(code) != null) {
+                    respVO.getFailureCodes().put(code, "鍑哄簱鍗曞彿宸插瓨鍦�");
+                    continue;
+                }
+                // 3.3 鍒涘缓鍗曟嵁澶�
+                MesWmProductSalesSaveReqVO saveReqVO = new MesWmProductSalesSaveReqVO();
+                saveReqVO.setCode(code);
+                saveReqVO.setName(StrUtil.blankToDefault(StrUtil.trim(head.getSalesName()), code));
+                saveReqVO.setClientId(clientId);
+                saveReqVO.setSalesDate(salesDate);
+                saveReqVO.setSalesOrderCode(StrUtil.trim(head.getSalesOrderCode()));
+                saveReqVO.setContactName(StrUtil.trim(head.getContactName()));
+                saveReqVO.setContactTelephone(StrUtil.trim(head.getContactTelephone()));
+                saveReqVO.setContactAddress(StrUtil.trim(head.getContactAddress()));
+                saveReqVO.setCarrier(StrUtil.trim(head.getCarrier()));
+                saveReqVO.setShippingNumber(StrUtil.trim(head.getShippingNumber()));
+                saveReqVO.setRemark(StrUtil.trim(head.getRemark()));
+                Long salesId = createProductSales(saveReqVO);
+                // 3.4 閫愯鍒涘缓鏄庣粏
+                for (MesWmProductSalesImportExcelVO row : rows) {
+                    createProductSalesLineByImport(salesId, row, respVO, code);
+                }
+                respVO.getCreateCodes().add(code);
+            } catch (Exception e) {
+                respVO.getFailureCodes().put(code, e.getMessage() == null ? "瀵煎叆澶辫触" : e.getMessage());
+            }
+        }
+        return respVO;
+    }
+
+    /**
+     * 瀵煎叆鏃跺垱寤哄崟琛屾槑缁嗭紱澶辫触鍙褰曚笉鎶涘嚭锛岄伩鍏嶅奖鍝嶆暣寮犲崟鎹殑鍏跺畠琛�
+     */
+    private void createProductSalesLineByImport(Long salesId, MesWmProductSalesImportExcelVO row,
+                                                MesWmDocImportRespVO respVO, String salesCode) {
+        String itemCode = StrUtil.trim(row.getItemCode());
+        MesMdItemDO item = itemCode == null ? null : mdItemService.getItemByCode(itemCode);
+        if (item == null) {
+            respVO.getFailureCodes().put(salesCode, "鐗╂枡缂栫爜[" + itemCode + "]涓嶅瓨鍦紝璇ヨ宸茶烦杩�");
+            return;
+        }
+        if (row.getQuantity() == null || row.getQuantity().signum() <= 0) {
+            respVO.getFailureCodes().put(salesCode, "鐗╂枡[" + itemCode + "]鍑哄簱鏁伴噺蹇呴』澶т簬 0锛岃琛屽凡璺宠繃");
+            return;
+        }
+        try {
+            MesWmProductSalesLineSaveReqVO lineReqVO = new MesWmProductSalesLineSaveReqVO();
+            lineReqVO.setSalesId(salesId);
+            lineReqVO.setItemId(item.getId());
+            lineReqVO.setQuantity(row.getQuantity());
+            lineReqVO.setBatchCode(StrUtil.trim(row.getBatchCode()));
+            lineReqVO.setRemark(StrUtil.trim(row.getRemark()));
+            productSalesLineService.createProductSalesLine(lineReqVO);
+        } catch (Exception e) {
+            respVO.getFailureCodes().put(salesCode, "鐗╂枡[" + itemCode + "]鏄庣粏鍒涘缓澶辫触锛�" + e.getMessage());
+        }
+    }
+
+    /**
+     * 瑙f瀽瀵煎叆鏃ユ湡锛屾敮鎸� yyyy-MM-dd 涓� yyyy-MM-dd HH:mm:ss锛屽け璐ヨ繑鍥� null
+     */
+    private LocalDateTime parseImportDateTime(String text) {
+        if (StrUtil.isBlank(text)) {
+            return null;
+        }
+        String value = text.trim().replace('/', '-');
+        try {
+            if (value.length() <= 10) {
+                return LocalDate.parse(value).atStartOfDay();
+            }
+            return LocalDateTime.parse(value.replace(' ', 'T'));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
 }
+

--
Gitblit v1.9.3