云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/cal/plan/MesCalPlanServiceImpl.java
@@ -1,25 +1,33 @@
package cn.iocoder.yudao.module.mes.service.cal.plan;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
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.cal.plan.vo.MesCalPlanImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.MesCalPlanSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.cal.plan.MesCalPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.cal.plan.MesCalPlanMapper;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalPlanStatusEnum;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalShiftTypeEnum;
import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum;
import cn.iocoder.yudao.module.mes.service.cal.team.MesCalTeamShiftService;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
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.*;
@@ -45,10 +53,17 @@
    @Resource
    @Lazy
    private MesCalTeamShiftService teamShiftService;
    @Resource
    private MesMdAutoCodeRecordService autoCodeRecordService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createPlan(MesCalPlanSaveReqVO createReqVO) {
        // 前端通常会预生成编码;接口侧仍提供兜底,避免移动端、导入外的调用方漏传后流程中断。
        if (StrUtil.isBlank(createReqVO.getCode())) {
            createReqVO.setCode(autoCodeRecordService.generateAutoCode(
                    MesMdAutoCodeRuleCodeEnum.CAL_PLAN_CODE.getCode()));
        }
        // 1. 校验编码唯一
        validatePlanCodeUnique(null, createReqVO.getCode());
@@ -132,6 +147,70 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesCalPlanImportRespVO importPlanList(List<MesCalPlanImportExcelVO> importList,
                                                 boolean updateSupport) {
        // 1. 校验非空
        if (CollUtil.isEmpty(importList)) {
            throw exception(CAL_PLAN_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐行处理
        MesCalPlanImportRespVO respVO = MesCalPlanImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "计划编码不能为空");
                return;
            }
            if (StrUtil.isBlank(importItem.getName())) {
                respVO.getFailureCodes().put(key, "计划名称不能为空");
                return;
            }
            if (importItem.getCalendarType() == null) {
                respVO.getFailureCodes().put(key, "班组类型不能为空");
                return;
            }
            if (importItem.getStartDate() == null || importItem.getEndDate() == null) {
                respVO.getFailureCodes().put(key, "开始日期、结束日期不能为空");
                return;
            }
            if (importItem.getShiftType() == null) {
                respVO.getFailureCodes().put(key, "轮班方式不能为空");
                return;
            }
            // 2.1 判断:创建 or 更新
            MesCalPlanDO existPlan = planMapper.selectByCode(importItem.getCode());
            if (existPlan == null) {
                // 2.1.1 新增
                MesCalPlanDO plan = BeanUtils.toBean(importItem, MesCalPlanDO.class);
                plan.setId(null);
                plan.setStatus(MesCalPlanStatusEnum.PREPARE.getStatus());
                planMapper.insert(plan);
                // 自动生成默认班次
                if (plan.getShiftType() != null) {
                    planShiftService.addDefaultPlanShift(plan.getId(), plan.getShiftType());
                }
                respVO.getCreateCodes().add(importItem.getCode());
            } else if (updateSupport) {
                // 2.1.2 更新(不允许通过 import 修改状态)
                MesCalPlanDO updateObj = BeanUtils.toBean(importItem, MesCalPlanDO.class);
                updateObj.setId(existPlan.getId());
                updateObj.setStatus(null);
                planMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(importItem.getCode());
            } else {
                respVO.getFailureCodes().put(key, "计划编码已存在");
            }
        });
        return respVO;
    }
    @Override
    public MesCalPlanDO validatePlanPrepare(Long planId) {
        MesCalPlanDO plan = validatePlanExists(planId);
        if (ObjUtil.notEqual(MesCalPlanStatusEnum.PREPARE.getStatus(), plan.getStatus())) {