yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
@@ -2,7 +2,6 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
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.module.mdm.controller.admin.item.vo.MdmItemPageReqVO;
@@ -10,20 +9,21 @@
import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO;
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.item.MdmItemBatchConfigMapper;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
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 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;
@@ -49,9 +49,10 @@
    private MdmBrandService brandService;
    @Resource
    @Lazy
    private MesMdItemApi mesMdItemApi;
    @Resource
    private MesMdItemTypeApi mesMdItemTypeApi;
    private MdmItemBatchConfigMapper itemBatchConfigMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -62,6 +63,9 @@
        // 插入 MDM 物料
        MdmItemDO item = BeanUtils.toBean(createReqVO, MdmItemDO.class);
        itemMapper.insert(item);
        // 保存批次配置(如果有)
        saveBatchConfig(item.getId(), createReqVO);
        // 同步到 MES(勾选 syncMes 时)
        if (Boolean.TRUE.equals(createReqVO.getSyncMes())) {
@@ -81,6 +85,9 @@
        // 更新 MDM 物料
        MdmItemDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemDO.class);
        itemMapper.updateById(updateObj);
        // 保存批次配置(如果有)
        saveBatchConfig(updateReqVO.getId(), updateReqVO);
        // 同步到 MES(勾选 syncMes 时)
        if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) {
@@ -110,25 +117,24 @@
            }
            unitCode = unitMeasure.getCode();
        }
        // 解析分类 id -> code(使用 MES 物料分类 API)
        // 解析分类 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) {
                itemTypeCode = category.getCode();
            }
        } 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);
            }
            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);
        }
        // 组装同步 DTO
        MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO();
@@ -143,6 +149,14 @@
        syncReqDTO.setHighValue(reqVO.getIsHighValue());
        syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged());
        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,update 走 updateItem(MES 侧按 code upsert)
        if (reqVO.getId() == null) {
            mesMdItemApi.createItem(syncReqDTO);
@@ -205,8 +219,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 +310,51 @@
        return itemMapper.selectByCode(code);
    }
    /**
     * 保存批次配置(到 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);
        }
    }
}