gaoluyang
2 天以前 bc9d77f6061a5c7f7deceaabc7c5ed6c17ad8c2f
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/dv/machinery/MesDvMachineryServiceImpl.java
@@ -4,6 +4,7 @@
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.module.mes.controller.admin.dv.machinery.vo.MesDvMachineryImportExcelVO;
@@ -38,7 +39,7 @@
/**
 * MES 设备台账 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -53,6 +54,9 @@
    @Resource
    @Lazy
    private MesMdWorkshopService workshopService;
    @Resource
    @Lazy
    private cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService workstationService;
    @Resource
    private MesWmBarcodeService barcodeService;
    @Resource
@@ -106,23 +110,51 @@
    @Override
    public void deleteMachinery(Long id) {
        // 校验存在
        validateMachineryExists(id);
        MesDvMachineryDO machinery = machineryMapper.selectById(id);
        if (machinery == null) {
            throw exception(DV_MACHINERY_NOT_EXISTS);
        }
        // 校验关联数据
        if (checkPlanMachineryService.getCheckPlanMachineryCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_CHECK_PLAN);
        }
        if (checkRecordService.getCheckRecordCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_CHECK_RECORD);
        }
        if (maintenRecordService.getMaintenRecordCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_MAINTEN_RECORD);
        }
        if (repairService.getRepairCountByMachineryId(id) > 0) {
            throw exception(DV_MACHINERY_HAS_REPAIR);
        }
        validateMachineryDeletable(machinery);
        // 删除
        machineryMapper.deleteById(id);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMachineryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 逐个删除(复用单条删除逻辑,保持校验),任一失败则整体回滚
        ids.forEach(this::deleteMachinery);
    }
    /**
     * 校验设备是否可删除(关联点检计划/点检记录/保养记录/维修记录任一存在则不允许)
     *
     * @param machinery 设备
     */
    private void validateMachineryDeletable(MesDvMachineryDO machinery) {
        Long id = machinery.getId();
        String label = StrUtil.format("{}({})", machinery.getName(), machinery.getCode());
        if (checkPlanMachineryService.getCheckPlanMachineryCountByMachineryId(id) > 0) {
            throw new ServiceException(DV_MACHINERY_HAS_CHECK_PLAN.getCode(),
                    StrUtil.format("设备{}已关联点检计划,无法删除;请先在点检计划中移除该设备后重试", label));
        }
        if (checkRecordService.getCheckRecordCountByMachineryId(id) > 0) {
            throw new ServiceException(DV_MACHINERY_HAS_CHECK_RECORD.getCode(),
                    StrUtil.format("设备{}已存在点检记录,无法删除;如需删除请先处理该设备的点检记录", label));
        }
        if (maintenRecordService.getMaintenRecordCountByMachineryId(id) > 0) {
            throw new ServiceException(DV_MACHINERY_HAS_MAINTEN_RECORD.getCode(),
                    StrUtil.format("设备{}已存在保养记录,无法删除;如需删除请先处理该设备的保养记录", label));
        }
        if (repairService.getRepairCountByMachineryId(id) > 0) {
            throw new ServiceException(DV_MACHINERY_HAS_REPAIR.getCode(),
                    StrUtil.format("设备{}已存在维修记录,无法删除;如需删除请先处理该设备的维修记录", label));
        }
    }
    @Override
@@ -201,7 +233,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 +242,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()
@@ -249,24 +288,38 @@
                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 {