From 9075785a9e14f6220783d052d231f915c6930b87 Mon Sep 17 00:00:00 2001
From: xiaoyi <908147524@qq.com>
Date: 星期三, 04 十一月 2026 10:21:45 +0800
Subject: [PATCH] revert(collect): 撤销 collect 数采模块(新高设备管理已有自己的采集方式)

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemTypeServiceImpl.java |  270 ++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 180 insertions(+), 90 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..0ab3f9d 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,20 +2,26 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
 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.mdm.api.category.dto.MdmItemCategorySaveReqDTO;
+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;
-import cn.iocoder.yudao.module.mes.dal.mysql.md.item.MesMdItemTypeMapper;
 import jakarta.annotation.Resource;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@@ -23,8 +29,8 @@
 /**
  * MES 鐗╂枡浜у搧鍒嗙被 Service 瀹炵幇绫�
  *
- * 鍏煎灞傚疄鐜帮細鏁版嵁瀛樺偍鍦� mes_md_item_type 琛紝浣嗕紭鍏堜粠 MDM 鍒嗙被 API 鑾峰彇
- * 瀹為檯鍒嗙被鏁版嵁宸茶縼绉诲埌 mdm_item_category 琛�
+ * 鐗╂枡鍒嗙被宸茬粺涓�鐢� MDM 绠$悊锛屾暟鎹瓨鍌ㄥ湪 mdm_item_category 琛ㄣ��
+ * 鏈湇鍔′繚鐣� MES 鐨勫垎绫绘帴鍙o紝搴曞眰鍏ㄩ儴閫氳繃 {@link MdmItemCategoryApi} 璇诲啓 MDM 鍒嗙被銆�
  *
  * @author 瓒呯骇绠$悊鍛�
  */
@@ -32,73 +38,82 @@
 @Validated
 public class MesMdItemTypeServiceImpl implements MesMdItemTypeService {
 
-    @Resource
-    private MdmItemCategoryApi mdmItemCategoryApi;
+    /** MDM 鐗╂枡绫诲瀷锛氱墿鏂� */
+    private static final Integer MDM_ITEM_TYPE_MATERIAL = 1;
+    /** MDM 鐗╂枡绫诲瀷锛氫骇鍝� */
+    private static final Integer MDM_ITEM_TYPE_PRODUCT = 2;
+    /** MES 鐗╂枡/浜у搧鏍囪瘑锛氫骇鍝� */
+    private static final String ITEM_OR_PRODUCT_PRODUCT = "PRODUCT";
 
     @Resource
-    private MesMdItemTypeMapper itemTypeMapper;
+    private MdmItemCategoryApi mdmItemCategoryApi;
 
     @Resource
     @Lazy
     private MesMdItemService itemService;
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Long createItemType(MesMdItemTypeSaveReqVO createReqVO) {
-        // 鏍¢獙鏁版嵁
-        validateItemTypeSaveData(createReqVO);
-
-        // 鎻掑叆鍒版湰鍦拌〃锛堜繚鎸佸吋瀹癸級
-        MesMdItemTypeDO itemType = BeanUtils.toBean(createReqVO, MesMdItemTypeDO.class);
-        itemTypeMapper.insert(itemType);
-        return itemType.getId();
+        // 鏍¢獙鐖跺垎绫�
+        validateParentItemType(createReqVO.getId(), createReqVO.getParentId());
+        // 鍐欏叆 MDM 鍒嗙被琛�
+        CommonResult<Long> result = mdmItemCategoryApi.createItemCategory(buildSaveDTO(createReqVO));
+        return result.getData();
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void updateItemType(MesMdItemTypeSaveReqVO updateReqVO) {
         // 鏍¢獙瀛樺湪
         validateItemTypeExists(updateReqVO.getId());
-        // 鏍¢獙鏁版嵁
-        validateItemTypeSaveData(updateReqVO);
-
-        // 鏇存柊鏈湴琛�
-        MesMdItemTypeDO updateObj = BeanUtils.toBean(updateReqVO, MesMdItemTypeDO.class);
-        itemTypeMapper.updateById(updateObj);
+        // 鏍¢獙鐖跺垎绫�
+        validateParentItemType(updateReqVO.getId(), updateReqVO.getParentId());
+        // 鏇存柊 MDM 鍒嗙被琛�
+        MdmItemCategorySaveReqDTO updateDTO = buildSaveDTO(updateReqVO);
+        updateDTO.setId(updateReqVO.getId());
+        mdmItemCategoryApi.updateItemCategory(updateDTO);
     }
 
-    private void validateItemTypeSaveData(MesMdItemTypeSaveReqVO reqVO) {
-        // 鏍¢獙鐖跺垎绫荤紪鍙风殑鏈夋晥鎬�
-        validateParentItemType(reqVO.getId(), reqVO.getParentId());
-        // 鏍¢獙鍒嗙被鍚嶇О鐨勫敮涓�鎬�
-        validateItemTypeNameUnique(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
-        // 鏍¢獙鍒嗙被缂栫爜鐨勫敮涓�鎬�
-        validateItemTypeCodeUnique(reqVO.getId(), reqVO.getParentId(), reqVO.getCode());
+    private MdmItemCategorySaveReqDTO buildSaveDTO(MesMdItemTypeSaveReqVO reqVO) {
+        MdmItemCategorySaveReqDTO dto = new MdmItemCategorySaveReqDTO();
+        dto.setParentId(ObjUtil.defaultIfNull(reqVO.getParentId(), MesMdItemTypeDO.PARENT_ID_ROOT));
+        dto.setCode(reqVO.getCode());
+        dto.setName(reqVO.getName());
+        dto.setItemOrProduct(reqVO.getItemOrProduct());
+        dto.setItemType(convertMdmItemType(reqVO.getItemOrProduct()));
+        dto.setSort(ObjUtil.defaultIfNull(reqVO.getSort(), 0));
+        dto.setStatus(ObjUtil.defaultIfNull(reqVO.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
+        dto.setRemark(reqVO.getRemark());
+        return dto;
+    }
+
+    /**
+     * MES 鐗╂枡/浜у搧鏍囪瘑 -> MDM 鐗╂枡绫诲瀷
+     */
+    private Integer convertMdmItemType(String itemOrProduct) {
+        if (StrUtil.isBlank(itemOrProduct)) {
+            return null;
+        }
+        return ITEM_OR_PRODUCT_PRODUCT.equalsIgnoreCase(itemOrProduct)
+                ? MDM_ITEM_TYPE_PRODUCT : MDM_ITEM_TYPE_MATERIAL;
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void deleteItemType(Long id) {
         // 1.1 鏍¢獙瀛樺湪
         validateItemTypeExists(id);
-        // 1.2 鏍¢獙鏄惁鏈夊瓙鍒嗙被
-        if (itemTypeMapper.selectCountByParentId(id) > 0) {
-            throw exception(MD_ITEM_TYPE_EXITS_CHILDREN);
-        }
-        // 1.3 鏍¢獙鏄惁鏈夌墿鏂�
+        // 1.2 鏍¢獙鏄惁鏈夌墿鏂�
         if (itemService.getItemCountByItemTypeId(id) > 0) {
             throw exception(MD_ITEM_TYPE_EXITS_ITEM);
         }
-
-        // 2. 鍒犻櫎
-        itemTypeMapper.deleteById(id);
+        // 2. 鍒犻櫎锛堝瓙鍒嗙被銆丮DM 鐗╂枡寮曠敤鐢� MDM 渚ф牎楠岋級
+        mdmItemCategoryApi.deleteItemCategory(id);
     }
 
     private void validateItemTypeExists(Long id) {
-        // 浼樺厛浠� MDM 鑾峰彇
-        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id);
-        if (result.isSuccess() && result.getData() != null) {
-            return;
-        }
-        // 鍥為��鍒版湰鍦拌〃
-        if (itemTypeMapper.selectById(id) == null) {
+        if (getItemType(id) == null) {
             throw exception(MD_ITEM_TYPE_NOT_EXISTS);
         }
     }
@@ -111,14 +126,8 @@
         if (Objects.equals(id, parentId)) {
             throw exception(MD_ITEM_TYPE_PARENT_ERROR);
         }
-        // 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);
-        }
+        // 2. 鐖跺垎绫讳笉瀛樺湪
+        MesMdItemTypeDO parentItemType = getItemType(parentId);
         if (parentItemType == null) {
             throw exception(MD_ITEM_TYPE_PARENT_NOT_EXITS);
         }
@@ -134,60 +143,35 @@
             if (parentId == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(parentId)) {
                 break;
             }
-            // 缁х画鏌ユ壘鐖剁骇
-            CommonResult<MdmItemCategoryRespDTO> parentResult = mdmItemCategoryApi.getItemCategory(parentId);
-            if (parentResult.isSuccess() && parentResult.getData() != null) {
-                parentItemType = convertToMesDO(parentResult.getData());
-            } else {
-                parentItemType = itemTypeMapper.selectById(parentId);
-            }
+            parentItemType = getItemType(parentId);
             if (parentItemType == null) {
                 break;
             }
         }
     }
 
-    private void validateItemTypeNameUnique(Long id, Long parentId, String name) {
-        MesMdItemTypeDO itemType = itemTypeMapper.selectByParentIdAndName(parentId, name);
-        if (itemType == null) {
-            return;
-        }
-        if (ObjUtil.notEqual(itemType.getId(), id)) {
-            throw exception(MD_ITEM_TYPE_NAME_DUPLICATE);
-        }
-    }
-
-    private void validateItemTypeCodeUnique(Long id, Long parentId, String code) {
-        MesMdItemTypeDO itemType = itemTypeMapper.selectByParentIdAndCode(parentId, code);
-        if (itemType == null) {
-            return;
-        }
-        if (ObjUtil.notEqual(itemType.getId(), id)) {
-            throw exception(MD_ITEM_TYPE_CODE_DUPLICATE);
-        }
-    }
-
     @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);
+        return result.isSuccess() ? convertToMesDO(result.getData()) : null;
     }
 
     @Override
     public MesMdItemTypeDO getItemTypeByCode(String code) {
-        // 浠庢湰鍦拌〃鏌�
-        return itemTypeMapper.selectByCode(code);
+        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategoryByCode(code);
+        return result.isSuccess() ? convertToMesDO(result.getData()) : null;
     }
 
     @Override
     public List<MesMdItemTypeDO> getItemTypeList(MesMdItemTypeListReqVO listReqVO) {
-        // 浠庢湰鍦拌〃鏌�
-        return itemTypeMapper.selectList(listReqVO);
+        List<MesMdItemTypeDO> list = getAllItemTypeList();
+        return list.stream()
+                .filter(itemType -> StrUtil.isBlank(listReqVO.getName())
+                        || StrUtil.contains(itemType.getName(), listReqVO.getName()))
+                .filter(itemType -> listReqVO.getStatus() == null
+                        || Objects.equals(listReqVO.getStatus(), itemType.getStatus()))
+                .sorted(Comparator.comparing(itemType -> ObjUtil.defaultIfNull(itemType.getSort(), 0)))
+                .toList();
     }
 
     @Override
@@ -195,12 +179,25 @@
         if (CollUtil.isEmpty(ids)) {
             return Collections.emptyList();
         }
-        return itemTypeMapper.selectByIds(ids);
+        CommonResult<List<MdmItemCategoryRespDTO>> result = mdmItemCategoryApi.getItemCategoryList(ids);
+        return convertToMesDOList(result.isSuccess() ? result.getData() : null);
+    }
+
+    private List<MesMdItemTypeDO> getAllItemTypeList() {
+        CommonResult<List<MdmItemCategoryRespDTO>> result = mdmItemCategoryApi.getItemCategoryAllList();
+        return convertToMesDOList(result.isSuccess() ? result.getData() : null);
+    }
+
+    private List<MesMdItemTypeDO> convertToMesDOList(List<MdmItemCategoryRespDTO> list) {
+        if (CollUtil.isEmpty(list)) {
+            return new ArrayList<>();
+        }
+        return new ArrayList<>(list.stream().map(this::convertToMesDO).toList());
     }
 
     @Override
     public List<MesMdItemTypeDO> getItemTypeChildrenList(Long parentId) {
-        List<MesMdItemTypeDO> allList = itemTypeMapper.selectList();
+        List<MesMdItemTypeDO> allList = getAllItemTypeList();
         List<MesMdItemTypeDO> result = new ArrayList<>();
         collectItemTypeChildren(result, parentId, allList);
         return result;
@@ -219,6 +216,9 @@
      * 杞崲 MDM 鍒嗙被 DTO 涓� MES DO
      */
     private MesMdItemTypeDO convertToMesDO(MdmItemCategoryRespDTO dto) {
+        if (dto == null) {
+            return null;
+        }
         MesMdItemTypeDO mesDO = new MesMdItemTypeDO();
         mesDO.setId(dto.getId());
         mesDO.setCode(dto.getCode());
@@ -227,7 +227,97 @@
         mesDO.setItemOrProduct(dto.getItemOrProduct());
         mesDO.setSort(dto.getSort());
         mesDO.setStatus(dto.getStatus());
+        mesDO.setRemark(dto.getRemark());
+        mesDO.setCreateTime(dto.getCreateTime());
         return mesDO;
     }
 
-}
\ No newline at end of file
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public MesMdItemTypeImportRespVO importItemTypeList(List<MesMdItemTypeImportExcelVO> importItemTypeList,
+                                                        boolean updateSupport) {
+        // 1. 鏍¢獙闈炵┖
+        if (CollUtil.isEmpty(importItemTypeList)) {
+            throw exception(MD_ITEM_TYPE_IMPORT_LIST_IS_EMPTY);
+        }
+
+        // 2. 閫愯澶勭悊
+        MesMdItemTypeImportRespVO respVO = MesMdItemTypeImportRespVO.builder()
+                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
+                .failureCodes(new LinkedHashMap<>()).build();
+        List<MesMdItemTypeDO> allList = getAllItemTypeList();
+        AtomicInteger index = new AtomicInteger(1);
+        importItemTypeList.forEach(importItem -> {
+            int currentIndex = index.getAndIncrement();
+            String key = StrUtil.blankToDefault(importItem.getCode(), "绗� " + currentIndex + " 琛�");
+            if (StrUtil.isBlank(importItem.getCode())) {
+                respVO.getFailureCodes().put(key, "鍒嗙被缂栫爜涓嶈兘涓虹┖");
+                return;
+            }
+            if (StrUtil.isBlank(importItem.getName())) {
+                respVO.getFailureCodes().put(key, "鍒嗙被鍚嶇О涓嶈兘涓虹┖");
+                return;
+            }
+            if (StrUtil.isBlank(importItem.getItemOrProduct())) {
+                respVO.getFailureCodes().put(key, "鐗╂枡/浜у搧鏍囪瘑涓嶈兘涓虹┖");
+                return;
+            }
+            // 2.1 瑙f瀽鐖跺垎绫伙紙涓虹┖鍒欓粯璁や负鏍瑰垎绫伙級
+            Long parentId = MesMdItemTypeDO.PARENT_ID_ROOT;
+            if (StrUtil.isNotBlank(importItem.getParentCode())) {
+                MesMdItemTypeDO parentItemType = allList.stream()
+                        .filter(itemType -> Objects.equals(importItem.getParentCode(), itemType.getCode()))
+                        .findFirst().orElse(null);
+                if (parentItemType == null) {
+                    respVO.getFailureCodes().put(key, "鐖跺垎绫荤紪鐮乕" + importItem.getParentCode() + "]涓嶅瓨鍦�");
+                    return;
+                }
+                parentId = parentItemType.getId();
+            }
+            // 2.2 鍒ゆ柇鏄惁瀛樺湪锛堝悓涓�鐖跺垎绫讳笅缂栫爜鍞竴锛�
+            Long finalParentId = parentId;
+            MesMdItemTypeDO existItemType = allList.stream()
+                    .filter(itemType -> Objects.equals(finalParentId, itemType.getParentId())
+                            && Objects.equals(importItem.getCode(), itemType.getCode()))
+                    .findFirst().orElse(null);
+            try {
+                MdmItemCategorySaveReqDTO dto = new MdmItemCategorySaveReqDTO();
+                dto.setParentId(parentId);
+                dto.setCode(importItem.getCode());
+                dto.setName(importItem.getName());
+                dto.setItemOrProduct(importItem.getItemOrProduct());
+                dto.setItemType(convertMdmItemType(importItem.getItemOrProduct()));
+                dto.setSort(ObjUtil.defaultIfNull(importItem.getSort(), 0));
+                dto.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
+                dto.setRemark(importItem.getRemark());
+                if (existItemType == null) {
+                    // 2.2.1 鏂板
+                    dto.setId(null);
+                    Long id = mdmItemCategoryApi.createItemCategory(dto).getData();
+                    MesMdItemTypeDO created = new MesMdItemTypeDO();
+                    created.setId(id);
+                    created.setParentId(parentId);
+                    created.setCode(importItem.getCode());
+                    created.setName(importItem.getName());
+                    created.setItemOrProduct(importItem.getItemOrProduct());
+                    created.setSort(dto.getSort());
+                    created.setStatus(dto.getStatus());
+                    created.setRemark(importItem.getRemark());
+                    allList.add(created);
+                    respVO.getCreateCodes().add(importItem.getCode());
+                } else if (updateSupport) {
+                    // 2.2.2 鏇存柊
+                    dto.setId(existItemType.getId());
+                    mdmItemCategoryApi.updateItemCategory(dto);
+                    respVO.getUpdateCodes().add(importItem.getCode());
+                } else {
+                    respVO.getFailureCodes().put(key, "鍒嗙被缂栫爜宸插瓨鍦�");
+                }
+            } catch (ServiceException e) {
+                respVO.getFailureCodes().put(key, e.getMessage());
+            }
+        });
+        return respVO;
+    }
+
+}

--
Gitblit v1.9.3