2 天以前 f7273277023f3497ac071bf98f556a569cb4dd25
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/service/item/MdmItemServiceImpl.java
@@ -2,27 +2,20 @@
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.framework.mybatis.core.mapper.BaseMapperX;
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.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 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,13 +25,12 @@
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.module.mdm.enums.ErrorCodeConstants.*;
/**
 * MDM 物料主数据 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -47,8 +39,6 @@
    @Resource
    private MdmItemMapper itemMapper;
    @Resource
    private MdmItemCategoryService itemCategoryService;
    @Resource
    private MdmUnitMeasureService unitMeasureService;
    @Resource
@@ -59,9 +49,6 @@
    private MesMdItemApi mesMdItemApi;
    @Resource
    private MdmItemBatchConfigMapper itemBatchConfigMapper;
    @Resource
    private MesMdItemTypeApi mesMdItemTypeApi;
    @Override
@@ -77,9 +64,11 @@
        // 保存批次配置(如果有)
        saveBatchConfig(item.getId(), createReqVO);
        // 始终同步到 MES
        createReqVO.setId(item.getId());
        syncItemToMes(createReqVO);
        // 勾选同步时,同步到 MES
        if (Boolean.TRUE.equals(createReqVO.getSyncMes())) {
            createReqVO.setId(item.getId());
            syncItemToMes(createReqVO);
        }
        return item.getId();
    }
@@ -98,8 +87,12 @@
        // 保存批次配置(如果有)
        saveBatchConfig(updateReqVO.getId(), updateReqVO);
        // 始终同步到 MES
        syncItemToMes(updateReqVO);
        // 根据同步开关决定:打开则同步到 MES,关闭则取消同步(从 MES 删除)
        if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) {
            syncItemToMes(updateReqVO);
        } else {
            mesMdItemApi.deleteItem(updateReqVO.getId());
        }
    }
    private void validateItemSaveData(MdmItemSaveReqVO reqVO) {
@@ -108,10 +101,9 @@
    }
    /**
     * 同步物料到 MES(单位/分类按 code 映射)
     * 同步物料到 MES(单位按 code 映射)
     *
     * MDM 的 unitMeasureId/categoryId 与 MES 的 unitMeasureId/itemTypeId 分属两套主数据,
     * 这里先在 MDM 侧解析出 code,再交由 MesMdItemApi 在 MES 侧解析为对应 id。
     * 品种管理已去掉「品种分类」字段,同步不再传递 MES 物料分类(item_type_id 留空,可在 MES 侧补填)。
     * 同步失败将抛出异常,触发外层 @Transactional 整体回滚(MDM 操作一并回滚)。
     */
    private void syncItemToMes(MdmItemSaveReqVO reqVO) {
@@ -124,12 +116,6 @@
            }
            unitCode = unitMeasure.getCode();
        }
        // 解析分类 id -> code(使用 MDM 本地分类服务)
        String itemTypeCode = null;
        if (reqVO.getCategoryId() != null) {
            CommonResult<MesMdItemTypeRespDTO> itemType = mesMdItemTypeApi.getItemType(reqVO.getCategoryId());
            itemTypeCode = itemType.getData().getCode();
        }
        // 组装同步 DTO
        MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO();
        syncReqDTO.setCode(reqVO.getCode());
@@ -137,12 +123,12 @@
        syncReqDTO.setName(reqVO.getName());
        syncReqDTO.setSpecification(reqVO.getSpecification());
        syncReqDTO.setUnitCode(unitCode);
        syncReqDTO.setItemTypeCode(itemTypeCode);
        syncReqDTO.setSafeStockFlag(Boolean.TRUE); // 保留 MDM 的 minStock/maxStock,避免被 MES 清零
        syncReqDTO.setMinStock(reqVO.getMinStock());
        syncReqDTO.setMaxStock(reqVO.getMaxStock());
        syncReqDTO.setHighValue(reqVO.getIsHighValue());
        syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged());
        syncReqDTO.setStatus(reqVO.getStatus());
        syncReqDTO.setRemark(reqVO.getRemark());
        // 批次配置字段
        syncReqDTO.setProduceDateFlag(reqVO.getProduceDateFlag());
@@ -182,6 +168,35 @@
        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);
        }
    }
    /**
@@ -237,17 +252,6 @@
        }
    }
    private void validateCategoryExists(Long categoryId) {
        if (categoryId == null) {
            return;
        }
        // 使用 MDM 本地分类服务校验
        MdmItemCategoryDO category = itemCategoryService.getItemCategory(categoryId);
        if (category == null) {
            throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS);
        }
    }
    private void validateUnitMeasureExists(Long unitMeasureId) {
        if (unitMeasureService.getUnitMeasure(unitMeasureId) == null) {
            throw exception(MDM_UNIT_MEASURE_NOT_EXISTS);
@@ -291,17 +295,8 @@
    @Override
    public PageResult<MdmItemDO> getItemPage(MdmItemPageReqVO pageReqVO) {
        // 查找时,如果查找某个分类编号,则包含它的子分类
        Set<Long> categoryIds = null;
        if (pageReqVO.getCategoryId() != null) {
            categoryIds = new HashSet<>();
            categoryIds.add(pageReqVO.getCategoryId());
            List<cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO> children =
                    itemCategoryService.getItemCategoryChildrenList(pageReqVO.getCategoryId());
            categoryIds.addAll(convertSet(children, cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO::getId));
        }
        // 分页查询
        return itemMapper.selectPage(pageReqVO, categoryIds);
        return itemMapper.selectPage(pageReqVO, (Collection<Long>) null);
    }
    @Override
@@ -332,6 +327,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 本地表)
     */