From 3990ae574c221e3764ce9cbc2e4e3eb71b7b1b52 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期六, 24 十月 2026 23:41:23 +0800
Subject: [PATCH] fix(mes): 修复批次正反向追溯链路

---
 yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/alternativeplan/MesProAlternativePlanServiceImpl.java |   73 ++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/alternativeplan/MesProAlternativePlanServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/alternativeplan/MesProAlternativePlanServiceImpl.java
index 15dc926..a47bc9d 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/alternativeplan/MesProAlternativePlanServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/alternativeplan/MesProAlternativePlanServiceImpl.java
@@ -2,22 +2,33 @@
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
+import cn.iocoder.yudao.module.mes.controller.admin.pro.alternativeplan.vo.MesProAlternativePlanImportExcelVO;
+import cn.iocoder.yudao.module.mes.controller.admin.pro.alternativeplan.vo.MesProAlternativePlanImportRespVO;
 import cn.iocoder.yudao.module.mes.controller.admin.pro.alternativeplan.vo.MesProAlternativePlanPageReqVO;
 import cn.iocoder.yudao.module.mes.controller.admin.pro.alternativeplan.vo.MesProAlternativePlanSaveReqVO;
 import cn.iocoder.yudao.module.mes.dal.dataobject.pro.alternativeplan.MesProAlternativePlanDO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.pro.process.MesProProcessDO;
 import cn.iocoder.yudao.module.mes.dal.mysql.pro.alternativeplan.MesProAlternativePlanMapper;
+import cn.iocoder.yudao.module.mes.service.pro.process.MesProProcessService;
 import jakarta.annotation.Resource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
+import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_ALTERNATIVE_PLAN_IMPORT_LIST_IS_EMPTY;
 import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_ALTERNATIVE_PLAN_NOT_EXISTS;
 import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_ALTERNATIVE_PLAN_NOT_PREPARE;
 
@@ -35,6 +46,9 @@
 
     @Resource
     private MesProAlternativePlanMapper planMapper;
+
+    @Resource
+    private MesProProcessService processService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -88,6 +102,65 @@
         planMapper.updateById(new MesProAlternativePlanDO().setId(id).setStatus(STATUS_PUBLISHED));
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public MesProAlternativePlanImportRespVO importPlanList(List<MesProAlternativePlanImportExcelVO> importList,
+                                                            boolean updateSupport) {
+        if (CollUtil.isEmpty(importList)) {
+            throw exception(MES_PRO_ALTERNATIVE_PLAN_IMPORT_LIST_IS_EMPTY);
+        }
+        MesProAlternativePlanImportRespVO respVO = MesProAlternativePlanImportRespVO.builder()
+                .createNames(new ArrayList<>()).updateNames(new ArrayList<>())
+                .failureNames(new LinkedHashMap<>()).build();
+        AtomicInteger index = new AtomicInteger(1);
+        importList.forEach(importItem -> {
+            int currentIndex = index.getAndIncrement();
+            String key = StrUtil.blankToDefault(importItem.getName(), "绗� " + currentIndex + " 琛�");
+            if (StrUtil.isBlank(importItem.getName())) {
+                respVO.getFailureNames().put(key, "鏂规鍚嶇О涓嶈兘涓虹┖");
+                return;
+            }
+            Long mainProcessId = null;
+            if (StrUtil.isNotBlank(importItem.getMainProcessName())) {
+                MesProProcessDO process = processService.getProcessByName(importItem.getMainProcessName().trim());
+                if (process == null) {
+                    respVO.getFailureNames().put(key, "涓诲伐搴忓悕绉癧" + importItem.getMainProcessName() + "]涓嶅瓨鍦�");
+                    return;
+                }
+                mainProcessId = process.getId();
+            }
+            Long altProcessId = null;
+            if (StrUtil.isNotBlank(importItem.getAltProcessName())) {
+                MesProProcessDO process = processService.getProcessByName(importItem.getAltProcessName().trim());
+                if (process == null) {
+                    respVO.getFailureNames().put(key, "鏇夸唬宸ュ簭鍚嶇О[" + importItem.getAltProcessName() + "]涓嶅瓨鍦�");
+                    return;
+                }
+                altProcessId = process.getId();
+            }
+            MesProAlternativePlanSaveReqVO reqVO = BeanUtils.toBean(importItem, MesProAlternativePlanSaveReqVO.class);
+            reqVO.setMainProcessId(mainProcessId);
+            reqVO.setAltProcessId(altProcessId);
+            MesProAlternativePlanDO existPlan = planMapper.selectByName(importItem.getName());
+            try {
+                if (existPlan == null) {
+                    reqVO.setId(null);
+                    createPlan(reqVO);
+                    respVO.getCreateNames().add(importItem.getName());
+                } else if (updateSupport) {
+                    reqVO.setId(existPlan.getId());
+                    updatePlan(reqVO);
+                    respVO.getUpdateNames().add(importItem.getName());
+                } else {
+                    respVO.getFailureNames().put(key, "鏂规鍚嶇О宸插瓨鍦�");
+                }
+            } catch (ServiceException e) {
+                respVO.getFailureNames().put(key, e.getMessage());
+            }
+        });
+        return respVO;
+    }
+
     private String generateCode() {
         return "TD" + DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss")
                 + String.format("%03d", (int) (Math.random() * 1000));

--
Gitblit v1.9.3