高
3 天以前 c6a2b472cde9dff4d21b95637ee6d99a412c785c
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java
@@ -7,18 +7,19 @@
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;
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 +29,7 @@
/**
 * MES 质检方案-产品关联 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -40,8 +41,6 @@
    @Resource
    @Lazy
    private MesQcTemplateService templateService;
    @Resource
    private MesMdItemService mesMdItemService;
    @Override
    public Long createTemplateItem(MesQcTemplateItemSaveReqVO createReqVO) {
@@ -116,23 +115,34 @@
    @Override
    public MesQcTemplateItemDO getRequiredTemplateByItemIdAndType(Long itemId, Integer qcType) {
        // 1. 查出 itemId 关联的所有 templateItem
        MesMdItemDO item1 = mesMdItemService.getItem(itemId);
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(item1.getMdmItemId());
        List<MesQcTemplateItemDO> templateItems = getTemplateItemList(itemId, qcType);
        if (CollUtil.isEmpty(templateItems)) {
            throw exception(QC_NO_TEMPLATE);
        }
        // 2. 筛选 types 包含 qcType 且状态为开启的模板
        return templateItems.get(0);
    }
    @Override
    public List<MesQcTemplateItemDO> getTemplateItemList(Long itemId, Integer qcType) {
        // 1. 产品关联表 item_id 存的是 MDM 物料 ID(mdm_item.id),质检侧统一按 MDM 物料查询
        if (itemId == null) {
            return Collections.emptyList();
        }
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(itemId);
        if (CollUtil.isEmpty(templateItems)) {
            return Collections.emptyList();
        }
        // 2. 过滤:方案启用且检测种类包含 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);
        }
        // 3. 返回对应的 templateItem
        return findFirst(templateItems,
                item -> Objects.equals(item.getTemplateId(), matchedTemplate.getId()));
        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());
    }
}