From d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期日, 25 十月 2026 16:29:53 +0800
Subject: [PATCH] feat(system): 优化用户搜索功能并完善ERP和MES模块验证逻辑

---
 yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java |  306 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 275 insertions(+), 31 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 5188ab8..c8ced1b 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,37 +2,50 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjUtil;
-import cn.iocoder.yudao.framework.common.pojo.CommonResult;
+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.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportExcelVO;
+import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportReqVO;
+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.brand.MdmBrandDO;
 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.brand.MdmBrandMapper;
+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.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.stream.Collectors;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
 import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*;
 
 /**
  * MDM 鐗╂枡涓绘暟鎹� Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -49,9 +62,17 @@
     private MdmBrandService brandService;
 
     @Resource
+    private MdmItemCategoryMapper itemCategoryMapper;
+    @Resource
+    private MdmUnitMeasureMapper unitMeasureMapper;
+    @Resource
+    private MdmBrandMapper brandMapper;
+
+    @Resource
+    @Lazy
     private MesMdItemApi mesMdItemApi;
     @Resource
-    private MesMdItemTypeApi mesMdItemTypeApi;
+    private MdmItemBatchConfigMapper itemBatchConfigMapper;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -63,8 +84,12 @@
         MdmItemDO item = BeanUtils.toBean(createReqVO, MdmItemDO.class);
         itemMapper.insert(item);
 
-        // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+        // 淇濆瓨鎵规閰嶇疆锛堝鏋滄湁锛�
+        saveBatchConfig(item.getId(), createReqVO);
+
+        // 鍕鹃�夊悓姝ユ椂锛屽悓姝ュ埌 MES
         if (Boolean.TRUE.equals(createReqVO.getSyncMes())) {
+            createReqVO.setId(item.getId());
             syncItemToMes(createReqVO);
         }
         return item.getId();
@@ -82,9 +107,14 @@
         MdmItemDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemDO.class);
         itemMapper.updateById(updateObj);
 
-        // 鍚屾鍒� MES锛堝嬀閫� syncMes 鏃讹級
+        // 淇濆瓨鎵规閰嶇疆锛堝鏋滄湁锛�
+        saveBatchConfig(updateReqVO.getId(), updateReqVO);
+
+        // 鏍规嵁鍚屾寮�鍏冲喅瀹氾細鎵撳紑鍒欏悓姝ュ埌 MES锛屽叧闂垯鍙栨秷鍚屾锛堜粠 MES 鍒犻櫎锛�
         if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) {
             syncItemToMes(updateReqVO);
+        } else {
+            mesMdItemApi.deleteItem(updateReqVO.getId());
         }
     }
 
@@ -110,29 +140,19 @@
             }
             unitCode = unitMeasure.getCode();
         }
-        // 瑙f瀽鍒嗙被 id -> code锛堜娇鐢� MES 鐗╂枡鍒嗙被 API锛�
+        // 瑙f瀽鍒嗙被 id -> code锛堜娇鐢� MDM 鏈湴鍒嗙被鏈嶅姟锛�
         String itemTypeCode = null;
         if (reqVO.getCategoryId() != null) {
-            CommonResult<MesMdItemTypeRespDTO> result = mesMdItemTypeApi.getItemType(reqVO.getCategoryId());
-            if (result != null && result.isSuccess() && result.getData() != null) {
-                itemTypeCode = result.getData().getCode();
+            MdmItemCategoryDO category = itemCategoryService.getItemCategory(reqVO.getCategoryId());
+            if (category == null) {
+                throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS);
             }
-        } else if (reqVO.getItemType() != null) {
-            // 鏍规嵁 itemType 鏌ユ壘瀵瑰簲鍒嗙被缂栫爜
-            // itemType: 1鍘熸枡, 2鍗婃垚鍝�, 3鎴愬搧, 4杈呮枡
-            String itemTypeName = convertItemTypeToName(reqVO.getItemType());
-            CommonResult<List<MesMdItemTypeRespDTO>> listResult = mesMdItemTypeApi.getItemTypeList(null);
-            if (listResult != null && listResult.isSuccess() && listResult.getData() != null) {
-                itemTypeCode = listResult.getData().stream()
-                        .filter(c -> c.getName() != null && c.getName().equals(itemTypeName))
-                        .findFirst()
-                        .map(MesMdItemTypeRespDTO::getCode)
-                        .orElse(null);
-            }
+            itemTypeCode = category.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);
@@ -142,7 +162,16 @@
         syncReqDTO.setMaxStock(reqVO.getMaxStock());
         syncReqDTO.setHighValue(reqVO.getIsHighValue());
         syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged());
+        syncReqDTO.setStatus(reqVO.getStatus());
         syncReqDTO.setRemark(reqVO.getRemark());
+        // 鎵规閰嶇疆瀛楁
+        syncReqDTO.setProduceDateFlag(reqVO.getProduceDateFlag());
+        syncReqDTO.setExpireDateFlag(reqVO.getExpireDateFlag());
+        syncReqDTO.setReceiptDateFlag(reqVO.getReceiptDateFlag());
+        syncReqDTO.setVendorFlag(reqVO.getVendorFlag());
+        syncReqDTO.setPurchaseOrderCodeFlag(reqVO.getPurchaseOrderCodeFlag());
+        syncReqDTO.setLotNumberFlag(reqVO.getLotNumberFlag());
+        syncReqDTO.setQualityStatusFlag(reqVO.getQualityStatusFlag());
         // create 璧� createItem锛寀pdate 璧� updateItem锛圡ES 渚ф寜 code upsert锛�
         if (reqVO.getId() == null) {
             mesMdItemApi.createItem(syncReqDTO);
@@ -152,11 +181,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
@@ -164,13 +196,171 @@
     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
+    @Transactional(rollbackFor = Exception.class)
+    public MdmItemImportRespVO importItemList(List<MdmItemImportExcelVO> importItems,
+                                              MdmItemImportReqVO importReqVO) {
+        // 1. 鏍¢獙闈炵┖锛堜互鐗╂枡缂栫爜浣滀负瀵煎叆涓婚敭锛�
+        importItems = filterList(importItems, item -> StrUtil.isNotBlank(item.getCode()));
+        if (CollUtil.isEmpty(importItems)) {
+            throw exception(MDM_ITEM_IMPORT_LIST_IS_EMPTY);
+        }
+
+        // 2. 閫愭潯澶勭悊
+        MdmItemImportRespVO respVO = MdmItemImportRespVO.builder()
+                .createItemCodes(new ArrayList<>())
+                .updateItemCodes(new ArrayList<>())
+                .failureItemCodes(new LinkedHashMap<>())
+                .build();
+        importItems.forEach(importItem -> {
+            try {
+                // 2.1 瑙f瀽澶栭敭锛氬垎绫� / 涓诲崟浣� / 杈呭崟浣� / 鍝佺墝锛堟寜鍚嶇О瑙f瀽锛�
+                Long categoryId = resolveCategoryId(importItem.getCategoryName());
+                Long unitMeasureId = resolveUnitMeasureId(importItem.getUnitMeasureName());
+                Long unitMeasureId2 = resolveUnitMeasureId(importItem.getUnitMeasureName2());
+                Long unitMeasureId3 = resolveUnitMeasureId(importItem.getUnitMeasureName3());
+                Long brandId = resolveBrandId(importItem.getBrandName());
+                // 2.2 鍒ゆ柇鏄惁瀛樺湪锛堟寜缂栫爜鍞竴锛�
+                MdmItemDO existItem = itemMapper.selectByCode(importItem.getCode());
+                if (existItem == null) {
+                    // 2.2.1 鏂板
+                    MdmItemDO item = BeanUtils.toBean(importItem, MdmItemDO.class)
+                            .setCategoryId(categoryId)
+                            .setUnitMeasureId(unitMeasureId)
+                            .setUnitMeasureId2(unitMeasureId2)
+                            .setUnitMeasureId3(unitMeasureId3)
+                            .setBrandId(brandId);
+                    if (item.getStatus() == null) {
+                        item.setStatus(CommonStatusEnum.ENABLE.getStatus());
+                    }
+                    itemMapper.insert(item);
+                    respVO.getCreateItemCodes().add(importItem.getCode());
+                    return;
+                }
+                // 2.2.2 宸插瓨鍦紝鍒ゆ柇鏄惁鍏佽鏇存柊
+                if (!importReqVO.getUpdateSupport()) {
+                    respVO.getFailureItemCodes().put(importItem.getCode(),
+                            StrUtil.format(MDM_ITEM_CODE_EXISTS.getMsg(), importItem.getCode()));
+                    return;
+                }
+                MdmItemDO updateItem = BeanUtils.toBean(importItem, MdmItemDO.class)
+                        .setId(existItem.getId())
+                        .setCategoryId(categoryId)
+                        .setUnitMeasureId(unitMeasureId)
+                        .setUnitMeasureId2(unitMeasureId2)
+                        .setUnitMeasureId3(unitMeasureId3)
+                        .setBrandId(brandId);
+                itemMapper.updateById(updateItem);
+                respVO.getUpdateItemCodes().add(importItem.getCode());
+            } catch (ServiceException ex) {
+                respVO.getFailureItemCodes().put(importItem.getCode(), ex.getMessage());
+            }
+        });
+        return respVO;
+    }
+
+    /**
+     * 鎸夊悕绉拌В鏋愮墿鏂欏垎绫荤紪鍙�
+     */
+    private Long resolveCategoryId(String categoryName) {
+        if (StrUtil.isBlank(categoryName)) {
+            return null;
+        }
+        MdmItemCategoryDO category = itemCategoryMapper.selectByName(categoryName);
+        if (category == null) {
+            throw exception(MDM_ITEM_IMPORT_CATEGORY_NOT_EXISTS, categoryName);
+        }
+        return category.getId();
+    }
+
+    /**
+     * 鎸夊悕绉拌В鏋愯閲忓崟浣嶇紪鍙�
+     */
+    private Long resolveUnitMeasureId(String unitMeasureName) {
+        if (StrUtil.isBlank(unitMeasureName)) {
+            return null;
+        }
+        MdmUnitMeasureDO unitMeasure = unitMeasureMapper.selectByName(unitMeasureName);
+        if (unitMeasure == null) {
+            throw exception(MDM_ITEM_IMPORT_UNIT_NOT_EXISTS, unitMeasureName);
+        }
+        return unitMeasure.getId();
+    }
+
+    /**
+     * 鎸夊悕绉拌В鏋愬搧鐗岀紪鍙�
+     */
+    private Long resolveBrandId(String brandName) {
+        if (StrUtil.isBlank(brandName)) {
+            return null;
+        }
+        MdmBrandDO brand = brandMapper.selectByName(brandName);
+        if (brand == null) {
+            throw exception(MDM_ITEM_IMPORT_BRAND_NOT_EXISTS, brandName);
+        }
+        return brand.getId();
     }
 
     @Override
@@ -205,8 +395,9 @@
         if (categoryId == null) {
             return;
         }
-        CommonResult<MesMdItemTypeRespDTO> result = mesMdItemTypeApi.getItemType(categoryId);
-        if (result == null || !result.isSuccess() || result.getData() == null) {
+        // 浣跨敤 MDM 鏈湴鍒嗙被鏈嶅姟鏍¢獙
+        MdmItemCategoryDO category = itemCategoryService.getItemCategory(categoryId);
+        if (category == null) {
             throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS);
         }
     }
@@ -295,4 +486,57 @@
         return itemMapper.selectByCode(code);
     }
 
-}
\ No newline at end of file
+    @Override
+    public List<MdmItemDO> getItemSimpleList() {
+        return itemMapper.selectList(MdmItemDO::getStatus,
+                cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
+    }
+
+    /**
+     * 淇濆瓨鎵规閰嶇疆锛堝埌 MDM 鏈湴琛級
+     */
+    private void saveBatchConfig(Long itemId, MdmItemSaveReqVO reqVO) {
+        // 妫�鏌ユ槸鍚︽湁鎵规閰嶇疆鏁版嵁
+        if (reqVO.getProduceDateFlag() == null
+                && reqVO.getExpireDateFlag() == null
+                && reqVO.getReceiptDateFlag() == null
+                && reqVO.getVendorFlag() == null
+                && reqVO.getPurchaseOrderCodeFlag() == null
+                && reqVO.getLotNumberFlag() == null
+                && reqVO.getQualityStatusFlag() == null) {
+            return;
+        }
+
+        // 鏌ヨ宸叉湁閰嶇疆
+        cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO existing =
+                itemBatchConfigMapper.selectOne(
+                        cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO::getItemId, itemId);
+
+        cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO config =
+                new cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO();
+        config.setItemId(itemId);
+        config.setProduceDateFlag(Boolean.TRUE.equals(reqVO.getProduceDateFlag()));
+        config.setExpireDateFlag(Boolean.TRUE.equals(reqVO.getExpireDateFlag()));
+        config.setReceiptDateFlag(Boolean.TRUE.equals(reqVO.getReceiptDateFlag()));
+        config.setVendorFlag(Boolean.TRUE.equals(reqVO.getVendorFlag()));
+        config.setPurchaseOrderCodeFlag(Boolean.TRUE.equals(reqVO.getPurchaseOrderCodeFlag()));
+        config.setLotNumberFlag(Boolean.TRUE.equals(reqVO.getLotNumberFlag()));
+        config.setQualityStatusFlag(Boolean.TRUE.equals(reqVO.getQualityStatusFlag()));
+        // 娓呯┖涓嶉渶瑕佺殑瀛楁
+        config.setClientFlag(false);
+        config.setSalesOrderCodeFlag(false);
+        config.setWorkOrderFlag(false);
+        config.setTaskFlag(false);
+        config.setWorkstationFlag(false);
+        config.setToolFlag(false);
+        config.setMoldFlag(false);
+
+        if (existing != null) {
+            config.setId(existing.getId());
+            itemBatchConfigMapper.updateById(config);
+        } else {
+            itemBatchConfigMapper.insert(config);
+        }
+    }
+
+}

--
Gitblit v1.9.3