From 581aae987e17d99f9e62275b22502ad08d748696 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期五, 21 八月 2026 09:31:00 +0800
Subject: [PATCH] 物料管理/物料分类/计量单位增加Excel导入功能

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java |   76 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java
index ab5b7a2..9d51f7c 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java
@@ -2,10 +2,13 @@
 
 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.CommonResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.mdm.api.category.MdmItemCategoryApi;
 import cn.iocoder.yudao.module.mdm.api.category.dto.MdmItemCategoryRespDTO;
+import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportExcelVO;
+import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportRespVO;
 import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeListReqVO;
 import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeSaveReqVO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO;
@@ -230,4 +233,77 @@
         return mesDO;
     }
 
+    @Override
+    @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
+    public MesMdItemTypeImportRespVO importItemTypeList(List<MesMdItemTypeImportExcelVO> importList, Boolean isUpdateSupport) {
+        if (CollUtil.isEmpty(importList)) {
+            throw exception(MD_ITEM_TYPE_IMPORT_LIST_IS_EMPTY);
+        }
+        MesMdItemTypeImportRespVO respVO = MesMdItemTypeImportRespVO.builder()
+                .createList(new ArrayList<>()).updateList(new ArrayList<>())
+                .failureList(new LinkedHashMap<>()).build();
+
+        // 1. 寤虹珛缂栫爜 -> ID 鏄犲皠锛堢幇鏈夊垎绫� + 鏈鏂板缓鍒嗙被锛夛紝鐢ㄤ簬瑙f瀽"鐖跺垎绫荤紪鐮�"鍒�
+        Map<String, Long> codeIdMap = new HashMap<>();
+        for (MesMdItemTypeDO itemType : itemTypeMapper.selectList()) {
+            codeIdMap.put(itemType.getCode(), itemType.getId());
+        }
+        Map<String, Long> importedCodeIdMap = new HashMap<>();
+
+        // 2. 閬嶅巻瀵煎叆
+        for (MesMdItemTypeImportExcelVO importVO : importList) {
+            // 2.1 鍩虹鏍¢獙
+            if (StrUtil.isBlank(importVO.getCode()) || StrUtil.isBlank(importVO.getName())) {
+                respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "鍒嗙被缂栫爜鍜屽垎绫诲悕绉颁笉鑳戒负绌�");
+                continue;
+            }
+            // 2.2 瑙f瀽鐖跺垎绫� ID
+            Long parentId = MesMdItemTypeDO.PARENT_ID_ROOT;
+            if (StrUtil.isNotBlank(importVO.getParentCode())) {
+                Long pid = importedCodeIdMap.get(importVO.getParentCode()) != null
+                        ? importedCodeIdMap.get(importVO.getParentCode())
+                        : codeIdMap.get(importVO.getParentCode());
+                if (pid == null) {
+                    respVO.getFailureList().put(importVO.getName(), "鐖跺垎绫荤紪鐮佷笉瀛樺湪锛�" + importVO.getParentCode());
+                    continue;
+                }
+                parentId = pid;
+            }
+
+            // 2.3 宸插瓨鍦紙鎸夌紪鐮佸垽鏂級
+            MesMdItemTypeDO existByCode = itemTypeMapper.selectByCode(importVO.getCode());
+            if (existByCode != null) {
+                if (!Boolean.TRUE.equals(isUpdateSupport)) {
+                    respVO.getFailureList().put(importVO.getName(), "鍒嗙被缂栫爜宸插瓨鍦�");
+                    continue;
+                }
+                MesMdItemTypeDO updateObj = BeanUtils.toBean(importVO, MesMdItemTypeDO.class);
+                updateObj.setId(existByCode.getId());
+                updateObj.setParentId(parentId);
+                itemTypeMapper.updateById(updateObj);
+                respVO.getUpdateList().add(importVO.getName());
+            } else {
+                // 2.4 鏍¢獙鍚屼竴鐖跺垎绫讳笅鍚嶇О鍞竴
+                MesMdItemTypeDO existByName = itemTypeMapper.selectByParentIdAndName(parentId, importVO.getName());
+                if (existByName != null) {
+                    respVO.getFailureList().put(importVO.getName(), "鍚屼竴鐖跺垎绫讳笅宸插瓨鍦ㄨ鍚嶇О鐨勫垎绫�");
+                    continue;
+                }
+                // 2.5 鏍¢獙鍚屼竴鐖跺垎绫讳笅缂栫爜鍞竴
+                MesMdItemTypeDO existByParentCode = itemTypeMapper.selectByParentIdAndCode(parentId, importVO.getCode());
+                if (existByParentCode != null) {
+                    respVO.getFailureList().put(importVO.getName(), "鍚屼竴鐖跺垎绫讳笅宸插瓨鍦ㄨ缂栫爜鐨勫垎绫�");
+                    continue;
+                }
+                // 2.6 鏂板
+                MesMdItemTypeDO newObj = BeanUtils.toBean(importVO, MesMdItemTypeDO.class);
+                newObj.setParentId(parentId);
+                itemTypeMapper.insert(newObj);
+                respVO.getCreateList().add(importVO.getName());
+                importedCodeIdMap.put(importVO.getCode(), newObj.getId());
+            }
+        }
+        return respVO;
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3