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-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java |  239 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 216 insertions(+), 23 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 e210590..d71bea1 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
@@ -2,21 +2,33 @@
 
 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.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportExcelVO;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportRespVO;
 import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemPageReqVO;
 import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemSaveReqVO;
 import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO;
+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;
 import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService;
 import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService;
 import cn.iocoder.yudao.module.mdm.service.unit.MdmUnitMeasureService;
 import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi;
+import cn.iocoder.yudao.module.mes.api.item.MesMdItemTypeApi;
 import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSyncReqDTO;
+import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemTypeRespDTO;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import jakarta.annotation.Resource;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -32,7 +44,7 @@
 /**
  * MDM 鐗╂枡涓绘暟鎹� Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -54,6 +66,16 @@
     @Resource
     private MdmItemBatchConfigMapper itemBatchConfigMapper;
 
+    @Resource
+    private MdmItemCategoryMapper itemCategoryMapper;
+
+    @Resource
+    private MdmUnitMeasureMapper unitMeasureMapper;
+
+    @Resource
+    private MesMdItemTypeApi mesMdItemTypeApi;
+
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Long createItem(MdmItemSaveReqVO createReqVO) {
@@ -67,8 +89,9 @@
         // 淇濆瓨鎵规閰嶇疆锛堝鏋滄湁锛�
         saveBatchConfig(item.getId(), createReqVO);
 
-        // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+        // 鍕鹃�夊悓姝ユ椂锛屽悓姝ュ埌 MES
         if (Boolean.TRUE.equals(createReqVO.getSyncMes())) {
+            createReqVO.setId(item.getId());
             syncItemToMes(createReqVO);
         }
         return item.getId();
@@ -89,9 +112,11 @@
         // 淇濆瓨鎵规閰嶇疆锛堝鏋滄湁锛�
         saveBatchConfig(updateReqVO.getId(), updateReqVO);
 
-        // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+        // 鏍规嵁鍚屾寮�鍏冲喅瀹氾細鎵撳紑鍒欏悓姝ュ埌 MES锛屽叧闂垯鍙栨秷鍚屾锛堜粠 MES 鍒犻櫎锛�
         if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) {
             syncItemToMes(updateReqVO);
+        } else {
+            mesMdItemApi.deleteItem(updateReqVO.getId());
         }
     }
 
@@ -120,25 +145,13 @@
         // 瑙f瀽鍒嗙被 id -> code锛堜娇鐢� MDM 鏈湴鍒嗙被鏈嶅姟锛�
         String itemTypeCode = null;
         if (reqVO.getCategoryId() != null) {
-            MdmItemCategoryDO category = itemCategoryService.getItemCategory(reqVO.getCategoryId());
-            if (category != null) {
-                itemTypeCode = category.getCode();
-            }
-        } else if (reqVO.getItemType() != null) {
-            // 鏍规嵁 itemType 鏌ユ壘瀵瑰簲鍒嗙被缂栫爜
-            // itemType: 1鍘熸枡, 2鍗婃垚鍝�, 3鎴愬搧, 4杈呮枡
-            String itemTypeName = convertItemTypeToName(reqVO.getItemType());
-            List<MdmItemCategoryDO> categories = itemCategoryService.getItemCategoryList(
-                    itemCategoryService.getItemCategoryChildrenList(null).stream().map(MdmItemCategoryDO::getId).collect(Collectors.toSet()));
-            itemTypeCode = categories.stream()
-                    .filter(c -> c.getName() != null && c.getName().equals(itemTypeName))
-                    .findFirst()
-                    .map(MdmItemCategoryDO::getCode)
-                    .orElse(null);
+            CommonResult<MesMdItemTypeRespDTO> itemType = mesMdItemTypeApi.getItemType(reqVO.getCategoryId());
+            itemTypeCode = itemType.getData().getCode();
         }
         // 缁勮鍚屾 DTO
         MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO();
         syncReqDTO.setCode(reqVO.getCode());
+        syncReqDTO.setMdmItemId(reqVO.getId());
         syncReqDTO.setName(reqVO.getName());
         syncReqDTO.setSpecification(reqVO.getSpecification());
         syncReqDTO.setUnitCode(unitCode);
@@ -148,6 +161,7 @@
         syncReqDTO.setMaxStock(reqVO.getMaxStock());
         syncReqDTO.setHighValue(reqVO.getIsHighValue());
         syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged());
+        syncReqDTO.setStatus(reqVO.getStatus());
         syncReqDTO.setRemark(reqVO.getRemark());
         // 鎵规閰嶇疆瀛楁
         syncReqDTO.setProduceDateFlag(reqVO.getProduceDateFlag());
@@ -166,11 +180,14 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void updateItemStatus(Long id, Integer status) {
         // 鏍¢獙瀛樺湪
         validateItemExists(id);
         // 鏇存柊鐘舵��
         itemMapper.updateById(new MdmItemDO().setId(id).setStatus(status));
+        // 鍚屾鐘舵�佸埌 MES
+        mesMdItemApi.updateItemStatus(id, status);
     }
 
     @Override
@@ -178,13 +195,66 @@
     public void deleteItem(Long id) {
         // 鏍¢獙瀛樺湪
         MdmItemDO item = validateItemExists(id);
-        // 瑙e喅杞垹闄ゅ敮涓�閿啿绐侊細濡傛灉宸插瓨鍦ㄧ浉鍚� code 涓斿凡杞垹闄ょ殑璁板綍锛屽厛鐗╃悊鍒犻櫎瀹�
-//        MdmItemDO existingDeleted = itemMapper.selectByCodeAndDeleted(item.getCode(), true);
-//        if (existingDeleted != null && !existingDeleted.getId().equals(id)) {
-//            itemMapper.physicalDeleteById(existingDeleted.getId());
-//        }
+        // 鏍¢獙鐗╂枡鏄惁琚笟鍔″紩鐢�
+        validateItemNoReference(id);
         // 杞垹闄ゅ綋鍓嶈褰�
         itemMapper.deleteById(id);
+        // 鍚屾鍒犻櫎鍒� MES
+        mesMdItemApi.deleteItem(id);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void updateItemSync(Long id, Boolean sync) {
+        // 鏍¢獙瀛樺湪
+        MdmItemDO item = validateItemExists(id);
+        if (Boolean.TRUE.equals(sync)) {
+            // 鍚屾鍒� MES锛氫粠 DB 璇诲彇瀹屾暣鐗╂枡鏁版嵁缁勮鍚庤蛋 syncItemToMes
+            MdmItemSaveReqVO reqVO = BeanUtils.toBean(item, MdmItemSaveReqVO.class);
+            // DO 瀛楁鍚嶄负 highValue锛屼笌 VO 鐨� isHighValue 涓嶈嚜鍔ㄥ尮閰嶏紝鎵嬪姩琛ラ綈
+            reqVO.setIsHighValue(item.getHighValue());
+            // 琛ラ綈鎵规灞炴�ч厤缃�
+            MdmItemBatchConfigDO config = itemBatchConfigMapper.selectOne(
+                    MdmItemBatchConfigDO::getItemId, id);
+            if (config != null) {
+                reqVO.setProduceDateFlag(config.getProduceDateFlag());
+                reqVO.setExpireDateFlag(config.getExpireDateFlag());
+                reqVO.setReceiptDateFlag(config.getReceiptDateFlag());
+                reqVO.setVendorFlag(config.getVendorFlag());
+                reqVO.setPurchaseOrderCodeFlag(config.getPurchaseOrderCodeFlag());
+                reqVO.setLotNumberFlag(config.getLotNumberFlag());
+                reqVO.setQualityStatusFlag(config.getQualityStatusFlag());
+            }
+            syncItemToMes(reqVO);
+        } else {
+            // 鍙栨秷鍚屾锛氫粠 MES 鍒犻櫎璇ョ墿鏂欙紙MES 鏃犳鐗╂枡鏃跺唴閮ㄤ负绌烘搷浣滐級
+            mesMdItemApi.deleteItem(id);
+        }
+    }
+
+    /**
+     * 鏍¢獙鐗╂枡鏈涓氬姟寮曠敤锛堥攢鍞�侀噰璐�佺敓浜с�佽川妫�銆佸簱瀛樼瓑锛�
+     *
+     * @param id 鐗╂枡缂栧彿
+     */
+    private void validateItemNoReference(Long id) {
+        List<Map<String, Object>> references = itemMapper.selectItemReferenceCount(id);
+        if (CollUtil.isNotEmpty(references)) {
+            String refDesc = references.stream()
+                    .map(r -> r.get("source") + "(" + r.get("cnt") + ")")
+                    .collect(Collectors.joining("銆�"));
+            throw exception(MDM_ITEM_EXISTS_REFERENCE, refDesc);
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void deleteItemList(Collection<Long> ids) {
+        if (CollUtil.isEmpty(ids)) {
+            return;
+        }
+        // 閫愪釜鍒犻櫎锛堝鐢ㄥ崟鏉″垹闄ら�昏緫锛屼繚鎸佹牎楠岋級
+        ids.forEach(this::deleteItem);
     }
 
     @Override
@@ -310,6 +380,12 @@
         return itemMapper.selectByCode(code);
     }
 
+    @Override
+    public List<MdmItemDO> getItemSimpleList() {
+        return itemMapper.selectList(MdmItemDO::getStatus,
+                cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
+    }
+
     /**
      * 淇濆瓨鎵规閰嶇疆锛堝埌 MDM 鏈湴琛級
      */
@@ -357,4 +433,121 @@
         }
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public MdmItemImportRespVO importItemList(List<MdmItemImportExcelVO> importList, Boolean isUpdateSupport) {
+        if (CollUtil.isEmpty(importList)) {
+            throw exception(MDM_ITEM_IMPORT_LIST_IS_EMPTY);
+        }
+        MdmItemImportRespVO respVO = MdmItemImportRespVO.builder()
+                .createList(new ArrayList<>()).updateList(new ArrayList<>())
+                .failureList(new LinkedHashMap<>()).build();
+
+        // 1. 棰勭儹锛氬垎绫荤紪鐮� -> ID銆佸崟浣嶇紪鐮� -> ID
+        Map<String, Long> categoryCodeIdMap = new HashMap<>();
+        for (MdmItemCategoryDO category : itemCategoryMapper.selectList()) {
+            if (category.getCode() != null) {
+                categoryCodeIdMap.put(category.getCode(), category.getId());
+            }
+        }
+        Map<String, Long> unitCodeIdMap = new HashMap<>();
+        for (MdmUnitMeasureDO unit : unitMeasureMapper.selectList()) {
+            unitCodeIdMap.put(unit.getCode(), unit.getId());
+        }
+
+        // 2. 閬嶅巻瀵煎叆
+        for (MdmItemImportExcelVO 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瀽鍒嗙被
+            Long categoryId = null;
+            if (StrUtil.isNotBlank(importVO.getCategoryCode())) {
+                categoryId = categoryCodeIdMap.get(importVO.getCategoryCode());
+                if (categoryId == null) {
+                    respVO.getFailureList().put(importVO.getName(), "鐗╂枡鍒嗙被缂栫爜涓嶅瓨鍦細" + importVO.getCategoryCode());
+                    continue;
+                }
+            }
+            // 2.3 瑙f瀽涓诲崟浣�
+            Long unitMeasureId = null;
+            if (StrUtil.isNotBlank(importVO.getUnitMeasureCode())) {
+                unitMeasureId = unitCodeIdMap.get(importVO.getUnitMeasureCode());
+                if (unitMeasureId == null) {
+                    respVO.getFailureList().put(importVO.getName(), "璁¢噺鍗曚綅缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode());
+                    continue;
+                }
+            }
+            // 2.4 瑙f瀽杈呭崟浣�1
+            Long unitMeasureId2 = null;
+            if (StrUtil.isNotBlank(importVO.getUnitMeasureCode2())) {
+                unitMeasureId2 = unitCodeIdMap.get(importVO.getUnitMeasureCode2());
+                if (unitMeasureId2 == null) {
+                    respVO.getFailureList().put(importVO.getName(), "杈呭崟浣�1缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode2());
+                    continue;
+                }
+            }
+            // 2.5 瑙f瀽杈呭崟浣�2
+            Long unitMeasureId3 = null;
+            if (StrUtil.isNotBlank(importVO.getUnitMeasureCode3())) {
+                unitMeasureId3 = unitCodeIdMap.get(importVO.getUnitMeasureCode3());
+                if (unitMeasureId3 == null) {
+                    respVO.getFailureList().put(importVO.getName(), "杈呭崟浣�2缂栫爜涓嶅瓨鍦細" + importVO.getUnitMeasureCode3());
+                    continue;
+                }
+            }
+
+            // 2.6 缁勮 SaveReqVO锛堝鐢� create/update 閫昏緫锛�
+            MdmItemSaveReqVO saveReqVO = new MdmItemSaveReqVO();
+            saveReqVO.setCode(importVO.getCode());
+            saveReqVO.setName(importVO.getName());
+            saveReqVO.setBarCode(importVO.getBarCode());
+            saveReqVO.setSpecification(importVO.getSpecification());
+            saveReqVO.setCategoryId(categoryId);
+            saveReqVO.setUnitMeasureId(unitMeasureId);
+            saveReqVO.setUnitMeasureId2(unitMeasureId2);
+            saveReqVO.setUnitMeasureRate1(importVO.getUnitMeasureRate1());
+            saveReqVO.setUnitMeasureId3(unitMeasureId3);
+            saveReqVO.setUnitMeasureRate2(importVO.getUnitMeasureRate2());
+            saveReqVO.setPurchasePrice(importVO.getPurchasePrice());
+            saveReqVO.setSalesPrice(importVO.getSalesPrice());
+            saveReqVO.setCostPrice(importVO.getCostPrice());
+            saveReqVO.setSafetyStock(importVO.getSafetyStock());
+            saveReqVO.setMinStock(importVO.getMinStock());
+            saveReqVO.setMaxStock(importVO.getMaxStock());
+            saveReqVO.setIsBatchManaged(importVO.getIsBatchManaged());
+            saveReqVO.setExpiryDay(importVO.getExpiryDay());
+            saveReqVO.setStatus(importVO.getStatus() != null ? importVO.getStatus() : cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
+            saveReqVO.setSyncMes(importVO.getSyncMes() != null ? importVO.getSyncMes() : false);
+            saveReqVO.setRemark(importVO.getRemark());
+            saveReqVO.setItemType(1); // 榛樿鐗╂枡绫诲瀷锛氱墿鏂�
+
+            // 2.7 宸插瓨鍦紙鎸夌紪鐮佸垽鏂級
+            MdmItemDO existByCode = itemMapper.selectByCode(importVO.getCode());
+            try {
+                if (existByCode != null) {
+                    if (!Boolean.TRUE.equals(isUpdateSupport)) {
+                        respVO.getFailureList().put(importVO.getName(), "鐗╂枡缂栫爜宸插瓨鍦�");
+                        continue;
+                    }
+                    saveReqVO.setId(existByCode.getId());
+                    updateItem(saveReqVO);
+                    respVO.getUpdateList().add(importVO.getName());
+                } else {
+                    createItem(saveReqVO);
+                    respVO.getCreateList().add(importVO.getName());
+                }
+            } catch (Exception ex) {
+                // 鍗曟潯澶辫触锛岃褰曞苟缁х画
+                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);
+            }
+        }
+        return respVO;
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3