云
9 天以前 ac2e2b5af3726ee64dc37c3ebf26989a0346f22a
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java
@@ -7,9 +7,11 @@
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateItemDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.template.MesQcTemplateItemMapper;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -26,7 +28,7 @@
/**
 * MES 质检方案-产品关联 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -38,6 +40,8 @@
    @Resource
    @Lazy
    private MesQcTemplateService templateService;
    @Resource
    private MesMdItemService mesMdItemService;
    @Override
    public Long createTemplateItem(MesQcTemplateItemSaveReqVO createReqVO) {
@@ -112,10 +116,35 @@
    @Override
    public MesQcTemplateItemDO getRequiredTemplateByItemIdAndType(Long itemId, Integer qcType) {
        // 1. 查出 itemId 关联的所有 templateItem
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(itemId);
        if (CollUtil.isEmpty(templateItems)) {
        MesQcTemplateItemDO matched = findTemplateItem(itemId, qcType);
        if (matched == null) {
            throw exception(QC_NO_TEMPLATE);
        }
        return matched;
    }
    @Override
    public boolean hasEnabledTemplate(Long itemId, Integer qcType) {
        return findTemplateItem(itemId, qcType) != null;
    }
    /**
     * 按物料 + QC 类型查找启用的模板关联,未命中返回 null
     */
    private MesQcTemplateItemDO findTemplateItem(Long itemId, Integer qcType) {
        if (itemId == null || qcType == null) {
            return null;
        }
        // 1. 查出 itemId 关联的所有 templateItem
        // 兼容两套物料 ID:质检/仓储单据传入 mdm_item.id,PRO 工单/任务传入 mes_md_item.id
        Long templateItemId = itemId;
        MesMdItemDO mesItem = mesMdItemService.getItem(itemId);
        if (mesItem != null && mesItem.getMdmItemId() != null) {
            templateItemId = mesItem.getMdmItemId();
        }
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(templateItemId);
        if (CollUtil.isEmpty(templateItems)) {
            return null;
        }
        // 2. 筛选 types 包含 qcType 且状态为开启的模板
        List<MesQcTemplateDO> templates = templateService.getTemplateList(
@@ -123,7 +152,7 @@
        MesQcTemplateDO matchedTemplate = findFirst(templates,
                t -> CommonStatusEnum.isEnable(t.getStatus()) && CollUtil.contains(t.getTypes(), qcType));
        if (matchedTemplate == null) {
            throw exception(QC_NO_TEMPLATE);
            return null;
        }
        // 3. 返回对应的 templateItem
        return findFirst(templateItems,