From 086b2186789ea04362db4bce71c6112686c43caa Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 21 九月 2026 15:33:26 +0800
Subject: [PATCH] feat(crm): 支持合同编号自定义配置

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java |   73 ++++++++++++++++++++++++++++++------
 1 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java
index ffba036..8ff8e40 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/qc/template/MesQcTemplateItemServiceImpl.java
+++ b/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. 瑙f瀽 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锛坢es_md_item.id锛�
+     * @return MDM 鐗╂枡 ID锛坢dm_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;
     }
 
 }

--
Gitblit v1.9.3