From 83dea93d02925e162ad88954b7d0ed97dd8a442a Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@tuqin.cn>
Date: 星期二, 15 九月 2026 13:20:24 +0800
Subject: [PATCH] fix(mdm): 修复物料导入/下载模板报系统异常

---
 yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
index d71bea1..c3bc416 100644
--- a/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
+++ b/yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
@@ -15,7 +15,6 @@
 import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO;
 import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO;
 import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO;
-import cn.iocoder.yudao.module.mdm.dal.mysql.category.MdmItemCategoryMapper;
 import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemBatchConfigMapper;
 import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
 import cn.iocoder.yudao.module.mdm.dal.mysql.unit.MdmUnitMeasureMapper;
@@ -65,9 +64,6 @@
     private MesMdItemApi mesMdItemApi;
     @Resource
     private MdmItemBatchConfigMapper itemBatchConfigMapper;
-
-    @Resource
-    private MdmItemCategoryMapper itemCategoryMapper;
 
     @Resource
     private MdmUnitMeasureMapper unitMeasureMapper;
@@ -443,11 +439,13 @@
                 .createList(new ArrayList<>()).updateList(new ArrayList<>())
                 .failureList(new LinkedHashMap<>()).build();
 
-        // 1. 棰勭儹锛氬垎绫荤紪鐮� -> ID銆佸崟浣嶇紪鐮� -> ID
+        // 1. 棰勭儹锛氬垎绫荤紪鐮� -> ID锛圡ES 鐗╂枡鍒嗙被锛屼笌鐗╂枡琛ㄥ崟涓嬫媺鏁版嵁婧愪竴鑷达級銆佸崟浣嶇紪鐮� -> ID
         Map<String, Long> categoryCodeIdMap = new HashMap<>();
-        for (MdmItemCategoryDO category : itemCategoryMapper.selectList()) {
-            if (category.getCode() != null) {
-                categoryCodeIdMap.put(category.getCode(), category.getId());
+        for (String categoryCode : importList.stream()
+                .map(MdmItemImportExcelVO::getCategoryCode).filter(StrUtil::isNotBlank).distinct().toList()) {
+            CommonResult<MesMdItemTypeRespDTO> categoryResult = mesMdItemTypeApi.getItemTypeByCode(categoryCode);
+            if (categoryResult != null && categoryResult.getData() != null) {
+                categoryCodeIdMap.put(categoryCode, categoryResult.getData().getId());
             }
         }
         Map<String, Long> unitCodeIdMap = new HashMap<>();
@@ -459,7 +457,9 @@
         for (MdmItemImportExcelVO importVO : importList) {
             // 2.1 鍩虹鏍¢獙
             if (StrUtil.isBlank(importVO.getCode()) || StrUtil.isBlank(importVO.getName())) {
-                respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "鐗╂枡缂栫爜鍜岀墿鏂欏悕绉颁笉鑳戒负绌�");
+                String failKey = StrUtil.isNotBlank(importVO.getName()) ? importVO.getName()
+                        : (StrUtil.isNotBlank(importVO.getCode()) ? importVO.getCode() : "锛堝悕绉板拰缂栫爜涓虹┖鐨勮锛�");
+                respVO.getFailureList().put(failKey, "鐗╂枡缂栫爜鍜岀墿鏂欏悕绉颁笉鑳戒负绌�");
                 continue;
             }
             // 2.2 瑙f瀽鍒嗙被
@@ -467,7 +467,7 @@
             if (StrUtil.isNotBlank(importVO.getCategoryCode())) {
                 categoryId = categoryCodeIdMap.get(importVO.getCategoryCode());
                 if (categoryId == null) {
-                    respVO.getFailureList().put(importVO.getName(), "鐗╂枡鍒嗙被缂栫爜涓嶅瓨鍦細" + importVO.getCategoryCode());
+                    respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "鐗╂枡鍒嗙被缂栫爜涓嶅瓨鍦細" + importVO.getCategoryCode());
                     continue;
                 }
             }
@@ -476,7 +476,7 @@
             if (StrUtil.isNotBlank(importVO.getUnitMeasureCode())) {
                 unitMeasureId = unitCodeIdMap.get(importVO.getUnitMeasureCode());
                 if (unitMeasureId == null) {
-                    respVO.getFailureList().put(importVO.getName(), "璁¢噺鍗曚綅缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode());
+                    respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "璁¢噺鍗曚綅缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode());
                     continue;
                 }
             }
@@ -485,7 +485,7 @@
             if (StrUtil.isNotBlank(importVO.getUnitMeasureCode2())) {
                 unitMeasureId2 = unitCodeIdMap.get(importVO.getUnitMeasureCode2());
                 if (unitMeasureId2 == null) {
-                    respVO.getFailureList().put(importVO.getName(), "杈呭崟浣�1缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode2());
+                    respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "杈呭崟浣�1缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode2());
                     continue;
                 }
             }
@@ -494,7 +494,7 @@
             if (StrUtil.isNotBlank(importVO.getUnitMeasureCode3())) {
                 unitMeasureId3 = unitCodeIdMap.get(importVO.getUnitMeasureCode3());
                 if (unitMeasureId3 == null) {
-                    respVO.getFailureList().put(importVO.getName(), "杈呭崟浣�2缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode3());
+                    respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "杈呭崟浣�2缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode3());
                     continue;
                 }
             }
@@ -529,7 +529,7 @@
             try {
                 if (existByCode != null) {
                     if (!Boolean.TRUE.equals(isUpdateSupport)) {
-                        respVO.getFailureList().put(importVO.getName(), "鐗╂枡缂栫爜宸插瓨鍦�");
+                        respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "鐗╂枡缂栫爜宸插瓨鍦�");
                         continue;
                     }
                     saveReqVO.setId(existByCode.getId());
@@ -544,7 +544,7 @@
                 String reason = ex instanceof cn.iocoder.yudao.framework.common.exception.ServiceException
                         ? ((cn.iocoder.yudao.framework.common.exception.ServiceException) ex).getMessage()
                         : ex.getMessage();
-                respVO.getFailureList().put(importVO.getName(), reason == null ? "瀵煎叆澶辫触" : reason);
+                respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), reason == null ? "瀵煎叆澶辫触" : reason);
             }
         }
         return respVO;

--
Gitblit v1.9.3