From be5783c8a7e13bbc76d33c95643d75779cce21db Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期六, 18 七月 2026 15:39:05 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_business' into dev_business

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemServiceImpl.java |  183 +++++++++++++++++++++++++++++++++++++++------
 1 files changed, 157 insertions(+), 26 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemServiceImpl.java
index 7585be0..2300bb2 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/md/item/MesMdItemServiceImpl.java
@@ -6,8 +6,6 @@
 import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
-import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
-import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
 import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemImportExcelVO;
 import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemImportRespVO;
 import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemPageReqVO;
@@ -24,6 +22,8 @@
 import cn.iocoder.yudao.module.mes.service.md.unitmeasure.MesMdUnitMeasureService;
 import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService;
 import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
+import cn.iocoder.yudao.module.mdm.api.item.MdmItemSyncApi;
+import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemSyncReqDTO;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -40,9 +40,7 @@
 /**
  * MES 鐗╂枡浜у搧 Service 瀹炵幇绫�
  *
- * 鏀归�犺鏄庯細MES 鐗╂枡涓绘暟鎹潵婧愪簬 MDM
- * - getItem/getItemList/validateItemExists 璋冪敤 MDM API
- * - MES 鐗规湁閰嶇疆锛堟壒娆¢厤缃�丅OM銆丼OP銆丼IP锛変繚鐣欏湪 MES 妯″潡绠$悊
+ * 鏀寔 MES 鍜� MDM 鐗╂枡鏁版嵁鍙屽悜鍚屾
  *
  * @author 鑺嬮亾婧愮爜
  */
@@ -52,9 +50,6 @@
 
     @Resource
     private MesMdItemMapper itemMapper;
-
-    @Resource
-    private MdmItemApi mdmItemApi;
 
     @Resource
     private MesMdItemTypeService itemTypeService;
@@ -73,7 +68,11 @@
     @Resource
     private MesMdAutoCodeRecordService autoCodeRecordService;
 
+    @Resource
+    private MdmItemSyncApi mdmItemSyncApi;
+
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Long createItem(MesMdItemSaveReqVO createReqVO) {
         // 鏍¢獙鏁版嵁
         validateItemSaveData(createReqVO);
@@ -87,10 +86,17 @@
         // 鑷姩鐢熸垚鏉$爜
         barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.ITEM.getValue(),
                 item.getId(), item.getCode(), item.getName());
+
+        // 鍚屾鍒� MDM锛堝嬀閫� syncMdm 鏃讹級
+        if (Boolean.TRUE.equals(createReqVO.getSyncMdm())) {
+            syncItemToMdm(item, createReqVO);
+        }
+
         return item.getId();
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void updateItem(MesMdItemSaveReqVO updateReqVO) {
         // 鏍¢獙瀛樺湪
         validateItemExists(updateReqVO.getId());
@@ -101,6 +107,12 @@
         MesMdItemDO updateObj = BeanUtils.toBean(updateReqVO, MesMdItemDO.class);
         clearStockIfNotSafe(updateObj); // 濡傛灉鏈惎鐢ㄥ畨鍏ㄥ簱瀛橈紝娓呴浂搴撳瓨涓婁笅闄�
         itemMapper.updateById(updateObj);
+
+        // 鍚屾鍒� MDM锛堝嬀閫� syncMdm 鏃讹級
+        if (Boolean.TRUE.equals(updateReqVO.getSyncMdm())) {
+            MesMdItemDO item = itemMapper.selectById(updateReqVO.getId());
+            syncItemToMdm(item, updateReqVO);
+        }
     }
 
     private void validateItemSaveData(MesMdItemSaveReqVO reqVO) {
@@ -156,14 +168,20 @@
 
     @Override
     public MesMdItemDO validateItemExists(Long id) {
-        MdmItemRespDTO item = mdmItemApi.validateItemExists(id).getCheckedData();
-        return BeanUtils.toBean(item, MesMdItemDO.class);
+        MesMdItemDO item = itemMapper.selectById(id);
+        if (item == null) {
+            throw exception(MD_ITEM_NOT_EXISTS);
+        }
+        return item;
     }
 
     @Override
     public MesMdItemDO validateItemExistsAndEnable(Long id) {
-        MdmItemRespDTO item = mdmItemApi.validateItemExistsAndEnable(id).getCheckedData();
-        return BeanUtils.toBean(item, MesMdItemDO.class);
+        MesMdItemDO item = validateItemExists(id);
+        if (ObjUtil.notEqual(CommonStatusEnum.ENABLE.getStatus(), item.getStatus())) {
+            throw exception(MD_ITEM_IS_DISABLE);
+        }
+        return item;
     }
 
     private void validateItemCodeUnique(Long id, String code) {
@@ -242,18 +260,26 @@
 
     @Override
     public MesMdItemDO getItem(Long id) {
-        // 浠� MDM 鑾峰彇鐗╂枡涓绘暟鎹�
-        MdmItemRespDTO item = mdmItemApi.getItem(id).getCheckedData();
-        return BeanUtils.toBean(item, MesMdItemDO.class);
+        return itemMapper.selectById(id);
+    }
+
+    @Override
+    public MesMdItemDO getItemByCode(String code) {
+        return itemMapper.selectByCode(code);
     }
 
     @Override
     public PageResult<MesMdItemDO> getItemPage(MesMdItemPageReqVO pageReqVO) {
-        // 璋冪敤 MDM API 鍒嗛〉鏌ヨ锛圡ES 鐗╂枡瀵瑰簲 MDM 鐗╂枡绫诲瀷=鐗╂枡锛�
-        cn.iocoder.yudao.framework.common.pojo.PageResult<MdmItemRespDTO> mdmPageResult =
-                mdmItemApi.getItemPage(1, pageReqVO.getStatus(), pageReqVO.getPageNo(), pageReqVO.getPageSize()).getCheckedData();
-        List<MesMdItemDO> itemList = BeanUtils.toBean(mdmPageResult.getList(), MesMdItemDO.class);
-        return new PageResult<>(itemList, mdmPageResult.getTotal());
+        // 鏌ユ壘鏃讹紝濡傛灉鏌ユ壘鏌愪釜鍒嗙被缂栧彿锛屽垯鍖呭惈瀹冪殑瀛愬垎绫�
+        Set<Long> itemTypeIds = null;
+        if (pageReqVO.getItemTypeId() != null) {
+            itemTypeIds = new HashSet<>();
+            itemTypeIds.add(pageReqVO.getItemTypeId());
+            List<MesMdItemTypeDO> children = itemTypeService.getItemTypeChildrenList(pageReqVO.getItemTypeId());
+            itemTypeIds.addAll(convertSet(children, MesMdItemTypeDO::getId));
+        }
+        // 鍒嗛〉鏌ヨ
+        return itemMapper.selectPage(pageReqVO, itemTypeIds);
     }
 
     @Override
@@ -261,19 +287,17 @@
         if (CollUtil.isEmpty(ids)) {
             return Collections.emptyList();
         }
-        List<MdmItemRespDTO> itemList = mdmItemApi.getItemList(ids).getCheckedData();
-        return BeanUtils.toBean(itemList, MesMdItemDO.class);
+        return itemMapper.selectByIds(ids);
     }
 
     @Override
     public Long getItemCountByItemTypeId(Long itemTypeId) {
-        // MES 鍒嗙被闇�瑕佹槧灏勫埌 MDM 鍒嗙被
-        return mdmItemApi.getItemCountByCategoryId(itemTypeId).getCheckedData();
+        return itemMapper.selectCountByItemTypeId(itemTypeId);
     }
 
     @Override
     public Long getItemCountByUnitMeasureId(Long unitMeasureId) {
-        return mdmItemApi.getItemCountByUnitMeasureId(unitMeasureId).getCheckedData();
+        return itemMapper.selectCountByUnitMeasureId(unitMeasureId);
     }
 
     @Override
@@ -364,4 +388,111 @@
         return respVO;
     }
 
-}
+    /**
+     * 鍚屾鐗╂枡鍒� MDM
+     *
+     * @param item     MES 鐗╂枡鏁版嵁
+     * @param saveReqVO 淇濆瓨璇锋眰锛堝寘鍚壒娆¢厤缃級
+     */
+    private void syncItemToMdm(MesMdItemDO item, MesMdItemSaveReqVO saveReqVO) {
+        // 1. 鑾峰彇鐗╂枡绫诲瀷瀵瑰簲鐨� itemType
+        Long itemType = null;
+        if (item.getItemTypeId() != null) {
+            MesMdItemTypeDO itemTypeDO = itemTypeService.getItemType(item.getItemTypeId());
+            if (itemTypeDO != null) {
+                // MES: ITEM=鐗╂枡, PRODUCT=浜у搧 -> MDM: 1=鍘熸枡, 2=鍗婃垚鍝�, 3=鎴愬搧, 4=杈呮枡
+//                itemType = convertMesItemTypeToMdm(itemTypeDO.getItemOrProduct(), itemTypeDO.getName());
+                itemType = itemTypeDO.getId();
+            }
+        }
+
+        // 2. 鑾峰彇璁¢噺鍗曚綅缂栫爜
+        String unitCode = null;
+        Long unitMeasureId = null;
+        if (item.getUnitMeasureId() != null) {
+            MesMdUnitMeasureDO unitMeasure = unitMeasureService.getUnitMeasure(item.getUnitMeasureId());
+            if (unitMeasure != null) {
+                unitCode = unitMeasure.getCode();
+            }
+            unitMeasureId = item.getUnitMeasureId();
+        }
+
+        // 3. 鑾峰彇鎵规閰嶇疆
+        MesMdItemBatchConfigDO batchConfig = itemBatchConfigService.getItemBatchConfigByItemId(item.getId());
+
+        // 4. 缁勮鍚屾 DTO
+        MdmItemSyncReqDTO syncReqDTO = new MdmItemSyncReqDTO();
+        syncReqDTO.setCode(item.getCode());
+        syncReqDTO.setName(item.getName());
+        syncReqDTO.setSpecification(item.getSpecification());
+        syncReqDTO.setItemType(itemType);
+        syncReqDTO.setUnitMeasureId(unitMeasureId);
+        syncReqDTO.setCategoryId(item.getItemTypeId());
+        syncReqDTO.setStatus(item.getStatus());
+        syncReqDTO.setMinStock(item.getMinStock());
+        syncReqDTO.setMaxStock(item.getMaxStock());
+        syncReqDTO.setIsBatchManaged(item.getBatchFlag());
+        syncReqDTO.setIsHighValue(item.getHighValue());
+        syncReqDTO.setRemark(item.getRemark());
+
+        // 5. 鍚屾鎵规閰嶇疆
+        if (batchConfig != null) {
+            syncReqDTO.setProduceDateFlag(batchConfig.getProduceDateFlag());
+            syncReqDTO.setExpireDateFlag(batchConfig.getExpireDateFlag());
+            syncReqDTO.setReceiptDateFlag(batchConfig.getReceiptDateFlag());
+            syncReqDTO.setVendorFlag(batchConfig.getVendorFlag());
+            syncReqDTO.setPurchaseOrderCodeFlag(batchConfig.getPurchaseOrderCodeFlag());
+            syncReqDTO.setLotNumberFlag(batchConfig.getLotNumberFlag());
+            syncReqDTO.setQualityStatusFlag(batchConfig.getQualityStatusFlag());
+        }
+
+        // 6. 璋冪敤 MDM 鍚屾 API
+        mdmItemSyncApi.syncItemToMdm(syncReqDTO);
+    }
+
+    /**
+     * 灏� MES 鐗╂枡绫诲瀷杞崲涓� MDM 鐗╂枡绫诲瀷
+     * MES: ITEM(鐗╂枡)/PRODUCT(浜у搧)
+     * MDM: 1鍘熸枡, 2鍗婃垚鍝�, 3鎴愬搧, 4杈呮枡
+     */
+    private Integer convertMesItemTypeToMdm(String itemOrProduct, String typeName) {
+        if (itemOrProduct == null) {
+            return null;
+        }
+        // 鏍规嵁鍚嶇О鍖归厤
+        if (typeName != null) {
+            if (typeName.contains("鍘熸枡")) {
+                return 1;
+            } else if (typeName.contains("鍗婃垚鍝�")) {
+                return 2;
+            } else if (typeName.contains("鎴愬搧")) {
+                return 3;
+            } else if (typeName.contains("杈呮枡")) {
+                return 4;
+            }
+        }
+        // 鏍规嵁 ITEM/PRODUCT 鍏滃簳
+        if ("PRODUCT".equals(itemOrProduct)) {
+            return 3; // 浜у搧 -> 鎴愬搧
+        }
+        return 1; // 榛樿鍘熸枡
+    }
+
+    @Override
+    public MesMdItemDO getItemByMdmItemId(Long mdmItemId) {
+        if (mdmItemId == null) {
+            return null;
+        }
+        return itemMapper.selectByMdmItemId(mdmItemId);
+    }
+
+    @Override
+    public MesMdItemDO validateItemExistsByMdmItemId(Long mdmItemId) {
+        MesMdItemDO item = getItemByMdmItemId(mdmItemId);
+        if (item == null) {
+            throw exception(MD_ITEM_NOT_EXISTS);
+        }
+        return item;
+    }
+
+}
\ No newline at end of file

--
Gitblit v1.9.3