xiaoyi
3 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/api/item/MesMdItemApiImpl.java
@@ -1,12 +1,15 @@
package cn.iocoder.yudao.module.mes.api.item;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSafetyStockRespDTO;
import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSyncReqDTO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.MesMdItemSaveReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.batchconfig.MesMdItemBatchConfigSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO;
import cn.iocoder.yudao.module.mes.dal.mysql.md.item.MesMdItemMapper;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemBatchConfigService;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemTypeService;
@@ -15,6 +18,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@@ -25,7 +32,7 @@
 * 供 MDM 等模块同步物料主数据到 MES 使用。
 * 单位、分类按 code 在 MES 侧解析为对应 id。
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -35,6 +42,8 @@
    @Resource
    private MesMdItemService itemService;
    @Resource
    private MesMdItemMapper itemMapper;
    @Resource
    private MesMdItemTypeService itemTypeService;
    @Resource
    private MesMdUnitMeasureService unitMeasureService;
@@ -43,20 +52,36 @@
    @Override
    public Long createItem(MesMdItemSyncReqDTO syncReqDTO) {
        log.info("[createItem] 同步创建 MES 物料产品,code={}", syncReqDTO.getCode());
        log.info("[createItem] 同步创建 MES 物料产品,code={}, mdmItemId={}", syncReqDTO.getCode(), syncReqDTO.getMdmItemId());
        MesMdItemSaveReqVO saveReqVO = buildSaveReqVO(syncReqDTO);
        // 使用 MDM 物料 ID 作为 MES 物料 ID
        if (syncReqDTO.getMdmItemId() != null) {
            saveReqVO.setId(syncReqDTO.getMdmItemId());
        }
        Long itemId = itemService.createItem(saveReqVO);
        // 同步批次配置
        saveBatchConfig(itemId, syncReqDTO);
//        saveBatchConfig(itemId, syncReqDTO);
        return itemId;
    }
    @Override
    public void updateItem(MesMdItemSyncReqDTO syncReqDTO) {
        log.info("[updateItem] 同步更新 MES 物料产品,code={}", syncReqDTO.getCode());
        log.info("[updateItem] 同步更新 MES 物料产品,code={}, mdmItemId={}", syncReqDTO.getCode(), syncReqDTO.getMdmItemId());
        MesMdItemSaveReqVO saveReqVO = buildSaveReqVO(syncReqDTO);
        // 按 code 匹配:存在则更新,不存在则创建(upsert)
        MesMdItemDO existItem = itemService.getItemByCode(syncReqDTO.getCode());
        // 使用 MDM 物料 ID 作为 MES 物料 ID
        if (syncReqDTO.getMdmItemId() != null) {
            saveReqVO.setId(syncReqDTO.getMdmItemId());
        }
        // 按 ID 或 code 匹配:存在则更新,不存在则创建(upsert)
        MesMdItemDO existItem = null;
        // 优先按 ID 查找
        if (syncReqDTO.getMdmItemId() != null) {
            existItem = itemService.getItem(syncReqDTO.getMdmItemId());
        }
        // 如果按 ID 找不到,再按 code 查找
        if (existItem == null) {
            existItem = itemService.getItemByCode(syncReqDTO.getCode());
        }
        Long itemId;
        if (existItem == null) {
            itemId = itemService.createItem(saveReqVO);
@@ -66,7 +91,7 @@
            itemId = existItem.getId();
        }
        // 同步批次配置
        saveBatchConfig(itemId, syncReqDTO);
//        saveBatchConfig(itemId, syncReqDTO);
    }
    /**
@@ -90,6 +115,37 @@
        return saveReqVO;
    }
    @Override
    public void deleteItem(Long mdmItemId) {
        log.info("[deleteItem] 同步删除 MES 物料产品,mdmItemId={}", mdmItemId);
        MesMdItemDO existItem = itemService.getItemByMdmItemId(mdmItemId);
        if (existItem != null) {
            itemService.deleteItem(existItem.getId());
        }
    }
    @Override
    public void updateItemStatus(Long mdmItemId, Integer status) {
        log.info("[updateItemStatus] 同步更新 MES 物料产品状态,mdmItemId={}, status={}", mdmItemId, status);
        MesMdItemDO existItem = itemService.getItemByMdmItemId(mdmItemId);
        if (existItem != null) {
            itemService.updateItemStatus(existItem.getId(), status);
        }
    }
    @Override
    public Set<Long> getSyncedMdmItemIds(Collection<Long> mdmItemIds) {
        return itemMapper.selectMdmItemIdsByMdmItemIds(mdmItemIds);
    }
    @Override
    public List<MesMdItemSafetyStockRespDTO> getSafetyStockItemList() {
        List<MesMdItemDO> items = itemMapper.selectList(new LambdaQueryWrapperX<MesMdItemDO>()
                .eq(MesMdItemDO::getSafeStockFlag, true)
                .isNotNull(MesMdItemDO::getMinStock));
        return BeanUtils.toBean(items, MesMdItemSafetyStockRespDTO.class);
    }
    /**
     * 保存批次配置
     */