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;
@@ -109,26 +110,54 @@
    @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
    public void validateMachineryExists(Long id) {
        if (machineryMapper.selectById(id) == null) {
            throw exception(DV_MACHINERY_NOT_EXISTS);