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 |  152 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 136 insertions(+), 16 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 dd1a2e6..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,7 +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;
@@ -20,17 +26,23 @@
 /**
  * MES 鐗╂枡浜у搧鍒嗙被 Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * 鍏煎灞傚疄鐜帮細鏁版嵁瀛樺偍鍦� mes_md_item_type 琛紝浣嗕紭鍏堜粠 MDM 鍒嗙被 API 鑾峰彇
+ * 瀹為檯鍒嗙被鏁版嵁宸茶縼绉诲埌 mdm_item_category 琛�
+ *
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
 public class MesMdItemTypeServiceImpl implements MesMdItemTypeService {
 
     @Resource
+    private MdmItemCategoryApi mdmItemCategoryApi;
+
+    @Resource
     private MesMdItemTypeMapper itemTypeMapper;
 
     @Resource
-    @Lazy // 寤惰繜鍔犺浇锛岄伩鍏嶅惊鐜緷璧�
+    @Lazy
     private MesMdItemService itemService;
 
     @Override
@@ -38,7 +50,7 @@
         // 鏍¢獙鏁版嵁
         validateItemTypeSaveData(createReqVO);
 
-        // 鎻掑叆
+        // 鎻掑叆鍒版湰鍦拌〃锛堜繚鎸佸吋瀹癸級
         MesMdItemTypeDO itemType = BeanUtils.toBean(createReqVO, MesMdItemTypeDO.class);
         itemTypeMapper.insert(itemType);
         return itemType.getId();
@@ -51,7 +63,7 @@
         // 鏍¢獙鏁版嵁
         validateItemTypeSaveData(updateReqVO);
 
-        // 鏇存柊
+        // 鏇存柊鏈湴琛�
         MesMdItemTypeDO updateObj = BeanUtils.toBean(updateReqVO, MesMdItemTypeDO.class);
         itemTypeMapper.updateById(updateObj);
     }
@@ -83,6 +95,12 @@
     }
 
     private void validateItemTypeExists(Long id) {
+        // 浼樺厛浠� MDM 鑾峰彇
+        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id);
+        if (result.isSuccess() && result.getData() != null) {
+            return;
+        }
+        // 鍥為��鍒版湰鍦拌〃
         if (itemTypeMapper.selectById(id) == null) {
             throw exception(MD_ITEM_TYPE_NOT_EXISTS);
         }
@@ -96,26 +114,36 @@
         if (Objects.equals(id, parentId)) {
             throw exception(MD_ITEM_TYPE_PARENT_ERROR);
         }
-        // 2. 鐖跺垎绫讳笉瀛樺湪
-        MesMdItemTypeDO parentItemType = itemTypeMapper.selectById(parentId);
+        // 2. 鐖跺垎绫讳笉瀛樺湪 - 浼樺厛浠� MDM 鑾峰彇
+        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(parentId);
+        MesMdItemTypeDO parentItemType = null;
+        if (result.isSuccess() && result.getData() != null) {
+            parentItemType = convertToMesDO(result.getData());
+        } else {
+            parentItemType = itemTypeMapper.selectById(parentId);
+        }
         if (parentItemType == null) {
             throw exception(MD_ITEM_TYPE_PARENT_NOT_EXITS);
         }
         // 3. 閫掑綊鏍¢獙鐖跺垎绫伙紝濡傛灉鐖跺垎绫绘槸鑷繁鐨勫瓙鍒嗙被锛屽垯鎶ラ敊锛岄伩鍏嶅舰鎴愮幆璺�
-        if (id == null) { // id 涓虹┖锛岃鏄庢柊澧烇紝涓嶉渶瑕佽�冭檻鐜矾
+        if (id == null) {
             return;
         }
         for (int i = 0; i < Short.MAX_VALUE; i++) {
-            // 3.1 鏍¢獙鐜矾
             parentId = parentItemType.getParentId();
             if (Objects.equals(id, parentId)) {
                 throw exception(MD_ITEM_TYPE_PARENT_IS_CHILD);
             }
-            // 3.2 缁х画閫掑綊涓嬩竴绾х埗鍒嗙被
             if (parentId == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(parentId)) {
                 break;
             }
-            parentItemType = itemTypeMapper.selectById(parentId);
+            // 缁х画鏌ユ壘鐖剁骇
+            CommonResult<MdmItemCategoryRespDTO> parentResult = mdmItemCategoryApi.getItemCategory(parentId);
+            if (parentResult.isSuccess() && parentResult.getData() != null) {
+                parentItemType = convertToMesDO(parentResult.getData());
+            } else {
+                parentItemType = itemTypeMapper.selectById(parentId);
+            }
             if (parentItemType == null) {
                 break;
             }
@@ -144,16 +172,24 @@
 
     @Override
     public MesMdItemTypeDO getItemType(Long id) {
+        // 浼樺厛浠� MDM 鑾峰彇
+        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id);
+        if (result.isSuccess() && result.getData() != null) {
+            return convertToMesDO(result.getData());
+        }
+        // 鍥為��鍒版湰鍦拌〃
         return itemTypeMapper.selectById(id);
     }
 
     @Override
     public MesMdItemTypeDO getItemTypeByCode(String code) {
+        // 浠庢湰鍦拌〃鏌�
         return itemTypeMapper.selectByCode(code);
     }
 
     @Override
     public List<MesMdItemTypeDO> getItemTypeList(MesMdItemTypeListReqVO listReqVO) {
+        // 浠庢湰鍦拌〃鏌�
         return itemTypeMapper.selectList(listReqVO);
     }
 
@@ -167,16 +203,12 @@
 
     @Override
     public List<MesMdItemTypeDO> getItemTypeChildrenList(Long parentId) {
-        List<MesMdItemTypeDO> allList = itemTypeMapper.selectList(); // 鍒嗙被鎬婚噺灏忥紝鍏ㄩ噺鏌�
-        // 閫掑綊鏀堕泦鎵�鏈夊瓙鍒嗙被
+        List<MesMdItemTypeDO> allList = itemTypeMapper.selectList();
         List<MesMdItemTypeDO> result = new ArrayList<>();
         collectItemTypeChildren(result, parentId, allList);
         return result;
     }
 
-    /**
-     * 閫掑綊鏀堕泦鎵�鏈夊瓙鍒嗙被
-     */
     private void collectItemTypeChildren(List<MesMdItemTypeDO> result, Long parentId, List<MesMdItemTypeDO> allList) {
         for (MesMdItemTypeDO itemType : allList) {
             if (Objects.equals(itemType.getParentId(), parentId)) {
@@ -186,4 +218,92 @@
         }
     }
 
-}
+    /**
+     * 杞崲 MDM 鍒嗙被 DTO 涓� MES DO
+     */
+    private MesMdItemTypeDO convertToMesDO(MdmItemCategoryRespDTO dto) {
+        MesMdItemTypeDO mesDO = new MesMdItemTypeDO();
+        mesDO.setId(dto.getId());
+        mesDO.setCode(dto.getCode());
+        mesDO.setName(dto.getName());
+        mesDO.setParentId(dto.getParentId());
+        mesDO.setItemOrProduct(dto.getItemOrProduct());
+        mesDO.setSort(dto.getSort());
+        mesDO.setStatus(dto.getStatus());
+        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