gaoluyang
2026-11-05 d39e4f7d839167c16425086dfa2f8747bccfdff9
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/dv/machinery/MesDvMachineryServiceImpl.java
@@ -4,8 +4,10 @@
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.framework.operatelog.core.util.OperateLogUtils;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryPageReqVO;
@@ -21,6 +23,7 @@
import cn.iocoder.yudao.module.mes.service.dv.repair.MesDvRepairService;
import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkshopService;
import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
import com.mzt.logapi.starter.annotation.LogRecord;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -34,11 +37,12 @@
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.mes.enums.LogRecordConstants.*;
/**
 * MES 设备台账 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -53,6 +57,9 @@
    @Resource
    @Lazy
    private MesMdWorkshopService workshopService;
    @Resource
    @Lazy
    private cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService workstationService;
    @Resource
    private MesWmBarcodeService barcodeService;
    @Resource
@@ -141,7 +148,8 @@
            return;
        }
        if (ObjUtil.notEqual(machinery.getId(), id)) {
            throw exception(DV_MACHINERY_CODE_DUPLICATE);
            throw new ServiceException(DV_MACHINERY_CODE_DUPLICATE.getCode(),
                    "设备编号[" + code + "]已被设备[" + machinery.getName() + "]使用,请更换编号,或点击“自动生成”重新生成");
        }
    }
@@ -194,6 +202,8 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    @LogRecord(type = MES_DV_MACHINERY_TYPE, subType = MES_DV_MACHINERY_IMPORT_SUB_TYPE, bizNo = "0",
            success = MES_DV_MACHINERY_IMPORT_SUCCESS)
    public MesDvMachineryImportRespVO importMachineryList(List<MesDvMachineryImportExcelVO> importMachineryList,
                                                          boolean updateSupport) {
        // 1. 参数校验
@@ -201,7 +211,7 @@
            throw exception(DV_MACHINERY_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 批量加载设备类型和车间,构建编码到实体的映射
        // 2. 批量加载设备类型、车间和工作站,构建编码到实体的映射
        List<MesDvMachineryTypeDO> allTypes = machineryTypeService.getMachineryTypeList(
                new cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeListReqVO());
        Map<String, MesDvMachineryTypeDO> typeCodeMap = allTypes.stream()
@@ -210,6 +220,13 @@
                cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
        Map<String, MesMdWorkshopDO> workshopCodeMap = allWorkshops.stream()
                .collect(Collectors.toMap(MesMdWorkshopDO::getCode, w -> w, (a, b) -> a));
        List<cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO> allWorkstations =
                workstationService.getWorkstationListByStatus(
                        cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus());
        Map<String, cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO> workstationCodeMap =
                allWorkstations.stream().collect(Collectors.toMap(
                        cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO::getCode,
                        w -> w, (a, b) -> a));
        // 3. 遍历,逐个创建 or 更新
        MesDvMachineryImportRespVO respVO = MesDvMachineryImportRespVO.builder()
@@ -221,7 +238,7 @@
            // 3.1 校验必填字段
            String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "设备编码不能为空");
                respVO.getFailureCodes().put(key, "设备编号不能为空");
                return;
            }
            if (StrUtil.isBlank(importItem.getName())) {
@@ -249,31 +266,47 @@
                return;
            }
            // 3.4 判断:创建 or 更新
            // 3.4 校验工作站编码(可选)
            Long workstationId = null;
            if (StrUtil.isNotBlank(importItem.getWorkstationCode())) {
                cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO workstation =
                        workstationCodeMap.get(importItem.getWorkstationCode());
                if (workstation == null) {
                    respVO.getFailureCodes().put(key, "工作站编码[" + importItem.getWorkstationCode() + "]不存在");
                    return;
                }
                workstationId = workstation.getId();
            }
            // 3.5 判断:创建 or 更新
            MesDvMachineryDO existMachinery = machineryMapper.selectByCode(importItem.getCode());
            if (existMachinery == null) {
                // 3.4.1 创建
                // 3.5.1 创建
                MesDvMachineryDO machinery = BeanUtils.toBean(importItem, MesDvMachineryDO.class);
                machinery.setMachineryTypeId(machineryType.getId());
                machinery.setWorkshopId(workshop.getId());
                machinery.setWorkstationId(workstationId);
                machineryMapper.insert(machinery);
                // 自动生成条码
                barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.MACHINERY.getValue(),
                        machinery.getId(), machinery.getCode(), machinery.getName());
                respVO.getCreateCodes().add(importItem.getCode());
            } else if (updateSupport) {
                // 3.4.2 更新
                // 3.5.2 更新
                MesDvMachineryDO updateObj = BeanUtils.toBean(importItem, MesDvMachineryDO.class);
                updateObj.setId(existMachinery.getId());
                updateObj.setMachineryTypeId(machineryType.getId());
                updateObj.setWorkshopId(workshop.getId());
                updateObj.setWorkstationId(workstationId);
                machineryMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(importItem.getCode());
            } else {
                // 不支持更新
                respVO.getFailureCodes().put(key, "设备编码已存在");
                respVO.getFailureCodes().put(key, "设备编号已存在,如需覆盖请勾选“是否覆盖”后重新导入");
            }
        });
        // 记录导入结果汇总,供 @LogRecord 的 success 文案引用
        OperateLogUtils.putImportSummary(respVO.getCreateCodes(), respVO.getUpdateCodes(), respVO.getFailureCodes());
        return respVO;
    }