8 小时以前 086b2186789ea04362db4bce71c6112686c43caa
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java
@@ -12,13 +12,18 @@
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 cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService;
import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
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;
@@ -28,7 +33,7 @@
/**
 * MES 质检方案-产品关联 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -42,6 +47,12 @@
    private MesQcTemplateService templateService;
    @Resource
    private MesMdItemService mesMdItemService;
    @Resource
    @Lazy
    private MesProTaskService proTaskService;
    @Resource
    @Lazy
    private MesProWorkOrderService proWorkOrderService;
    @Override
    public Long createTemplateItem(MesQcTemplateItemSaveReqVO createReqVO) {
@@ -115,24 +126,60 @@
    }
    @Override
    public MesQcTemplateItemDO getRequiredTemplateByItemIdAndType(Long itemId, Integer qcType) {
        // 1. 查出 itemId 关联的所有 templateItem
        MesMdItemDO item1 = mesMdItemService.getItem(itemId);
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(item1.getMdmItemId());
    public MesQcTemplateItemDO getRequiredTemplateByItemIdAndType(Long mdmItemId, Integer qcType) {
        List<MesQcTemplateItemDO> templateItems = getTemplateItemList(mdmItemId, qcType, null, null);
        if (CollUtil.isEmpty(templateItems)) {
            throw exception(QC_NO_TEMPLATE);
        }
        // 2. 筛选 types 包含 qcType 且状态为开启的模板
        return templateItems.get(0);
    }
    @Override
    public List<MesQcTemplateItemDO> getTemplateItemList(Long mdmItemId, Integer qcType, Long workOrderId, Long taskId) {
        // 1. 解析 MDM 产品物料 ID:任务优先,其次工单;两者存的是 MES 物料 ID,需转换为 MDM 物料 ID。
        //    未传工单/任务时,入参本身即 MDM 物料 ID(质检单前端选择的就是 MDM 物料)
        Long resolvedMdmItemId;
        if (taskId != null) {
            resolvedMdmItemId = convertMesItemIdToMdmItemId(proTaskService.validateTaskExists(taskId).getItemId());
        } else if (workOrderId != null) {
            resolvedMdmItemId = convertMesItemIdToMdmItemId(
                    proWorkOrderService.validateWorkOrderExists(workOrderId).getProductId());
        } else {
            resolvedMdmItemId = mdmItemId;
        }
        if (resolvedMdmItemId == null) {
            return Collections.emptyList();
        }
        // 2. 查出该物料关联的所有产品关联
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(resolvedMdmItemId);
        if (CollUtil.isEmpty(templateItems)) {
            return Collections.emptyList();
        }
        // 3. 过滤:方案启用且检测种类包含 qcType,返回全部匹配项
        List<MesQcTemplateDO> templates = templateService.getTemplateList(
                convertSet(templateItems, MesQcTemplateItemDO::getTemplateId));
        MesQcTemplateDO matchedTemplate = findFirst(templates,
                t -> CommonStatusEnum.isEnable(t.getStatus()) && CollUtil.contains(t.getTypes(), qcType));
        if (matchedTemplate == null) {
            throw exception(QC_NO_TEMPLATE);
        Set<Long> matchedTemplateIds = templates.stream()
                .filter(t -> CommonStatusEnum.isEnable(t.getStatus())
                        && CollUtil.contains(t.getTypes(), qcType))
                .map(MesQcTemplateDO::getId)
                .collect(Collectors.toSet());
        return templateItems.stream()
                .filter(t -> matchedTemplateIds.contains(t.getTemplateId()))
                .collect(Collectors.toList());
    }
    /**
     * MES 物料 ID 转 MDM 物料 ID
     *
     * @param mesItemId MES 物料 ID(mes_md_item.id)
     * @return MDM 物料 ID(mdm_item.id);MES 物料不存在或未关联 MDM 物料时返回 null
     */
    private Long convertMesItemIdToMdmItemId(Long mesItemId) {
        if (mesItemId == null) {
            return null;
        }
        // 3. 返回对应的 templateItem
        return findFirst(templateItems,
                item -> Objects.equals(item.getTemplateId(), matchedTemplate.getId()));
        MesMdItemDO item = mesMdItemService.getItem(mesItemId);
        return item != null ? item.getMdmItemId() : null;
    }
}