fix:修复删除逻辑,有被引用的数据不能删除。

plp
HuangHuiKang 3 weeks ago
parent b58325c344
commit c560897b89

@ -44,6 +44,7 @@ public interface ErrorCodeConstants {
ErrorCode DEVICE_ATTRIBUTE_NOT_EXISTS = new ErrorCode(1_003_000_000, "设备属性不存在");
ErrorCode DEVICE_ATTRIBUTE_TYPE_NOT_EXISTS = new ErrorCode(1_003_000_000, "采集点分类不存在");
ErrorCode DEVICE_CODE_EXISTS = new ErrorCode(1_003_000_000, "采集点编码已存在");
ErrorCode DEVICE_ATTRIBUTE_TYPE_REFERENCES_EXISTS = new ErrorCode(1_003_000_000, "存在采集点类型已被引用,请先删除对应引用");
ErrorCode ENDPOINT_DOES_NOT_EXIS = new ErrorCode(1_003_000_000, "暂未设置设备端点");
ErrorCode DEVICE_DOES_NOT_EXIST= new ErrorCode(1_003_000_000, "该采集设备不存在");

@ -59,18 +59,18 @@ public class DeviceController {
tDengineService.initDatabaseAndTable(device.getId());
//添加定时任务
// 创建 JobSaveReqVO 对象实例
JobSaveReqVO jobSaveReqVO = new JobSaveReqVO();
// 设置任务属性(根据您的业务需求设置具体值)
jobSaveReqVO.setName("deviceJob_" + device.getId()); // 处理器名称唯一
jobSaveReqVO.setHandlerName("deviceJob"); // 处理器名称唯一
jobSaveReqVO.setHandlerParam("{\"deviceId\": \"" + device.getId() + "\"}"); // 使用设备ID作为参数值
jobSaveReqVO.setCronExpression("*/5 * * * * ?"); // CRON表达式每5秒执行一次[1,3](@ref)
jobSaveReqVO.setRetryCount(3); // 重试次数
jobSaveReqVO.setRetryInterval(5000); // 重试间隔(毫秒)
jobSaveReqVO.setMonitorTimeout(30000); // 监控超时时间(毫秒)
jobSaveReqVO.setDeviceId(device.getId());
jobService.createJob(jobSaveReqVO);
// JobSaveReqVO jobSaveReqVO = new JobSaveReqVO();
// // 设置任务属性(根据您的业务需求设置具体值)
// jobSaveReqVO.setName("deviceJob_" + device.getId()); // 处理器名称唯一
// jobSaveReqVO.setHandlerName("deviceJob"); // 处理器名称唯一
// jobSaveReqVO.setHandlerParam("{\"deviceId\": \"" + device.getId() + "\"}"); // 使用设备ID作为参数值
// jobSaveReqVO.setCronExpression("*/5 * * * * ?"); // CRON表达式每5秒执行一次[1,3](@ref)
// jobSaveReqVO.setRetryCount(3); // 重试次数
// jobSaveReqVO.setRetryInterval(5000); // 重试间隔(毫秒)
// jobSaveReqVO.setMonitorTimeout(30000); // 监控超时时间(毫秒)
// jobSaveReqVO.setDeviceId(device.getId());
//
// jobService.createJob(jobSaveReqVO);
return success(device);
}

@ -1,7 +1,13 @@
package cn.iocoder.yudao.module.iot.service.deviceattributetype;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodelattribute.DeviceModelAttributeDO;
import cn.iocoder.yudao.module.iot.dal.devicecontactmodel.DeviceContactModelDO;
import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactModelMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicemodel.DeviceModelMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicemodelattribute.DeviceModelAttributeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
@ -33,6 +39,12 @@ public class DeviceAttributeTypeServiceImpl implements DeviceAttributeTypeServic
@Resource
private DeviceAttributeTypeMapper deviceAttributeTypeMapper;
@Resource
private DeviceModelAttributeMapper deviceModelAttributeMapper;
@Resource
private DeviceContactModelMapper deviceContactModelMapper;
@Override
public Long createDeviceAttributeType(DeviceAttributeTypeSaveReqVO createReqVO) {
// 重复判断
@ -62,8 +74,10 @@ public class DeviceAttributeTypeServiceImpl implements DeviceAttributeTypeServic
if (count > 0) {
throw exception(DEVICE_CODE_EXISTS);
}
DeviceAttributeTypeDO deviceAttributeTypeDO = deviceAttributeTypeMapper.selectById(updateReqVO.getId());
// 校验存在
validateDeviceAttributeTypeExists(updateReqVO.getId());
validateDeviceAttributeTypeExists(deviceAttributeTypeDO);
// 更新
DeviceAttributeTypeDO updateObj = BeanUtils.toBean(updateReqVO, DeviceAttributeTypeDO.class);
deviceAttributeTypeMapper.updateById(updateObj);
@ -71,17 +85,41 @@ public class DeviceAttributeTypeServiceImpl implements DeviceAttributeTypeServic
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteDeviceAttributeType( List<Long> ids) {
for (Long id : ids) {
// 校验存在
validateDeviceAttributeTypeExists(id);
public void deleteDeviceAttributeType(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return;
}
// 批量校验存在性
List<DeviceAttributeTypeDO> existList = deviceAttributeTypeMapper.selectBatchIds(ids);
if (existList.size() != ids.size()) {
throw exception(DEVICE_ATTRIBUTE_TYPE_NOT_EXISTS);
}
// 删除
// 批量校验引用
validateBatchReferences(ids);
// 批量删除
deviceAttributeTypeMapper.deleteByIds(ids);
}
private void validateDeviceAttributeTypeExists(Long id) {
if (deviceAttributeTypeMapper.selectById(id) == null) {
private void validateBatchReferences(List<Long> ids) {
// 批量查询引用
boolean hasModelRef = deviceModelAttributeMapper.exists(
Wrappers.<DeviceModelAttributeDO>lambdaQuery()
.in(DeviceModelAttributeDO::getAttributeType, ids));
boolean hasContactRef = deviceContactModelMapper.exists(
Wrappers.<DeviceContactModelDO>lambdaQuery()
.in(DeviceContactModelDO::getAttributeType, ids));
if (hasModelRef || hasContactRef) {
throw exception(DEVICE_ATTRIBUTE_TYPE_REFERENCES_EXISTS);
}
}
private void validateDeviceAttributeTypeExists(DeviceAttributeTypeDO deviceAttributeTypeDO) {
if (deviceAttributeTypeDO == null) {
throw exception(DEVICE_ATTRIBUTE_TYPE_NOT_EXISTS);
}
}

@ -76,7 +76,9 @@ public interface ErrorCodeConstants {
ErrorCode DV_CHECK_NOT_EXISTS = new ErrorCode(5_0087, "维保计划不存在");
ErrorCode DV_SUBJECT_NOT_EXISTS = new ErrorCode(5_0087, "维保项目不存在");
ErrorCode DV_SUBJECT_NOT_EXISTS = new ErrorCode(5_0087, "项目不存在");
ErrorCode DV_SUBJECT_REFERENCES = new ErrorCode(5_0087, "存在项目已被引用,请先删除所引用");
ErrorCode DV_REPAIR_NOT_EXISTS = new ErrorCode(5_0087, "设备维修记录不存在");
ErrorCode DV_REPAIR_CODE_EXISTS = new ErrorCode(5_0087, "设备维修记录编码已存在");
@ -90,6 +92,8 @@ public interface ErrorCodeConstants {
//======================================设备管理相关 1002000000=================================================
ErrorCode DEVICE_TYPE_NOT_EXISTS = new ErrorCode(1002000000, "设备类型不存在");
ErrorCode DEVICE_TYPE_REFERENCES = new ErrorCode(1002000000, "该设备类型已被引用,请先删除相关引用");
ErrorCode DEVICE_TYPE_PARENT_NOT_EXISTS = new ErrorCode(1002000001, "父级设备类型不存在");
ErrorCode DEVICE_TYPE_PARENT_IS_SELF = new ErrorCode(1002000002, "不能设置自己为父级");
ErrorCode DEVICE_TYPE_PARENT_IS_CHILD = new ErrorCode(1002000003, "不能设置子节点为父级");
@ -97,6 +101,8 @@ public interface ErrorCodeConstants {
ErrorCode DEVICE_LEDGER_NOT_EXISTS = new ErrorCode(1002000005, "该设备不存在");
ErrorCode DEVICE_LEDGER_CODE_NOT_ONLY = new ErrorCode(1002000006, "设备类型编码已存在");
ErrorCode PLAN_MAINTENANCE_NOT_EXISTS = new ErrorCode(1002000007, "方案维护不存在");
ErrorCode PLAN_MAINTENANCE_REFERENCES = new ErrorCode(1002000007, "该方案维护已被引用,请先删除引用");
ErrorCode SUBJECT_PLAN_NOT_EXISTS = new ErrorCode(1002000008, "项目方案关联不存在");
ErrorCode SUBJECT_ID_NOT_EXISTS = new ErrorCode(1002000009, "项目Id不存在");
ErrorCode DEVICE_LEDGER_EXISTS = new ErrorCode(1002000010, "设备台账编码已存在");
@ -106,6 +112,8 @@ public interface ErrorCodeConstants {
ErrorCode TICKET_RESULTS_NOT_EXISTS = new ErrorCode(1002000013, "工单检验结果不存在");
ErrorCode TICKET_RESULTS_ID_NOT_NULL = new ErrorCode(1002000014, "工单检验结果Id不存在");
ErrorCode CRITICAL_COMPONENT_NOT_EXISTS = new ErrorCode(1002000015, "设备关键件不存在");
ErrorCode CRITICAL_COMPONENT_REFERENCES= new ErrorCode(1002000015, "存在设备关键件已被引用,请先删除引用");
ErrorCode REPAIR_TEMS_NOT_EXISTS = new ErrorCode(1002000016, "维修项目不存在");
ErrorCode REPAIR_TEMS_CODE_EXISTS = new ErrorCode(1002000016, "维修项目编码已存在");

@ -1,11 +1,23 @@
package cn.iocoder.yudao.module.mes.service.criticalcomponent;
import cn.iocoder.yudao.module.iot.dal.dataobject.deviceattributetype.DeviceAttributeTypeDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodelattribute.DeviceModelAttributeDO;
import cn.iocoder.yudao.module.iot.dal.devicecontactmodel.DeviceContactModelDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.repairtems.RepairTemsDO;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.repairtems.RepairTemsMapper;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
import cn.iocoder.yudao.module.mes.controller.admin.criticalcomponent.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -15,6 +27,8 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.dal.mysql.criticalcomponent.CriticalComponentMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_ATTRIBUTE_TYPE_NOT_EXISTS;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_ATTRIBUTE_TYPE_REFERENCES_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
/**
@ -29,6 +43,13 @@ public class CriticalComponentServiceImpl implements CriticalComponentService {
@Resource
private CriticalComponentMapper criticalComponentMapper;
@Resource
private DeviceLedgerMapper deviceLedgerMapper;
@Resource
private RepairTemsMapper repairTemsMapper;
@Override
public Long createCriticalComponent(CriticalComponentSaveReqVO createReqVO) {
// 插入
@ -48,14 +69,75 @@ public class CriticalComponentServiceImpl implements CriticalComponentService {
}
@Override
public void deleteCriticalComponent(List<Long> idList) {
for (Long id : idList) {
// 校验存在
validateCriticalComponentExists(id);
public void deleteCriticalComponent(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return;
}
// 批量校验存在性
List<CriticalComponentDO> existList = criticalComponentMapper.selectBatchIds(ids);
if (existList.size() != ids.size()) {
throw exception(CRITICAL_COMPONENT_NOT_EXISTS);
}
// 检验设备台账引用
validateDeviceLedgerBatchReferences(ids);
//检验项目维护引用
validateDvjectBatchReferences(ids);
// 删除
criticalComponentMapper.deleteByIds(idList);
criticalComponentMapper.deleteByIds(ids);
}
private void validateDvjectBatchReferences(List<Long> ids) {
boolean isExists = repairTemsMapper.exists(
Wrappers.<RepairTemsDO>lambdaQuery()
.in(RepairTemsDO::getComponentId, ids));
if (isExists){
throw exception(CRITICAL_COMPONENT_REFERENCES);
}
}
private void validateDeviceLedgerBatchReferences(List<Long> ids) {
// 查询所有包含这些ID的记录
List<DeviceLedgerDO> ledgers = deviceLedgerMapper.selectList(
Wrappers.<DeviceLedgerDO>lambdaQuery()
.isNotNull(DeviceLedgerDO::getComponentId)
.select(DeviceLedgerDO::getComponentId)
);
if (CollectionUtils.isEmpty(ledgers)) {
return;
}
// 检查每个componentId字段是否包含目标ID
for (DeviceLedgerDO ledger : ledgers) {
if (StringUtils.isBlank(ledger.getComponentId())) {
continue;
}
// 将逗号分隔的ID转换为Set
Set<Long> componentIds = Arrays.stream(ledger.getComponentId().split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.map(Long::valueOf)
.collect(Collectors.toSet());
// 检查是否有交集
for (Long id : ids) {
if (componentIds.contains(id)) {
throw exception(CRITICAL_COMPONENT_REFERENCES);
}
}
}
}
private void validateCriticalComponentExists(Long id) {

@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.mes.service.devicetype;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodel.DeviceModelDO;
import cn.iocoder.yudao.module.mes.controller.admin.devicetype.vo.DeviceTypeTreeRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
@ -41,6 +43,9 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
@Resource
private DeviceTypeMapper deviceTypeMapper;
@Resource
private DeviceLedgerMapper deviceLedgerMapper;
@Override
public Long createDeviceType(DeviceTypeSaveReqVO createReqVO) {
@ -79,7 +84,8 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
@Override
public void updateDeviceType(DeviceTypeSaveReqVO updateReqVO) {
// 1. 校验存在
validateDeviceTypeExists(updateReqVO.getId());
DeviceTypeDO deviceTypeDO = deviceTypeMapper.selectById(updateReqVO.getId());
validateDeviceTypeExists(deviceTypeDO);
//编码重复判断
Long count = deviceTypeMapper.selectCount(new LambdaQueryWrapper<DeviceTypeDO>()
.eq(DeviceTypeDO::getCode, updateReqVO.getCode())
@ -123,17 +129,29 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
@Override
public void deleteDeviceType(Long id) {
// 1. 校验存在
validateDeviceTypeExists(id);
DeviceTypeDO deviceTypeDO = deviceTypeMapper.selectById(id);
validateDeviceTypeExists(deviceTypeDO);
// 2. 校验是否有子节点
if (hasChildren(id)) {
throw exception(DEVICE_TYPE_EXITS_CHILDREN);
}
// 3. 校验是否有引用
validateReferences(deviceTypeDO);
// 3. 删除
// 4. 删除
deviceTypeMapper.deleteById(id);
}
private void validateReferences(DeviceTypeDO deviceTypeDO) {
boolean isExists = deviceLedgerMapper.exists(Wrappers.<DeviceLedgerDO>lambdaQuery().eq(DeviceLedgerDO::getDeviceType,deviceTypeDO.getId()));
if (isExists){
throw exception(DEVICE_TYPE_REFERENCES);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteDeviceTypeBatch(List<Long> ids) {
@ -142,8 +160,8 @@ public class DeviceTypeServiceImpl implements DeviceTypeService {
}
}
private void validateDeviceTypeExists(Long id) {
if (deviceTypeMapper.selectById(id) == null) {
private void validateDeviceTypeExists(DeviceTypeDO deviceTypeDO) {
if (deviceTypeDO == null) {
throw exception(DEVICE_TYPE_NOT_EXISTS);
}
}

@ -7,10 +7,14 @@ import cn.iocoder.yudao.module.mes.controller.admin.dvsubject.vo.DvSubjectPageRe
import cn.iocoder.yudao.module.mes.controller.admin.dvsubject.vo.DvSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.devicetype.DeviceTypeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvsubject.DvSubjectDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectplan.SubjectPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvsubject.DvSubjectMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.subjectplan.SubjectPlanMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
@ -33,6 +37,9 @@ public class DvSubjectServiceImpl implements DvSubjectService {
@Resource
private DvSubjectMapper dvSubjectMapper;
@Resource
private SubjectPlanMapper subjectPlanMapper;
@Override
public Long createDvSubject(DvSubjectSaveReqVO createReqVO) {
@ -70,17 +77,38 @@ public class DvSubjectServiceImpl implements DvSubjectService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteDvSubject(List<Long> idList) {
if (CollectionUtils.isEmpty(idList)) {
return;
}
for (Long id : idList) {
// 校验存在
validateDvSubjectExists(id);
// 批量校验存在性
List<DvSubjectDO> existList = dvSubjectMapper.selectBatchIds(idList);
if (existList.size() != idList.size()) {
throw exception(DV_SUBJECT_NOT_EXISTS);
}
// 删除
// 批量校验引用
validateBatchReferences(idList);
// 批量删除
dvSubjectMapper.deleteByIds(idList);
}
private void validateBatchReferences(List<Long> idList) {
boolean exists = subjectPlanMapper.exists(
Wrappers.<SubjectPlanDO>lambdaQuery()
.in(SubjectPlanDO::getSubjectId, idList)
);
if (exists) {
throw exception(DV_SUBJECT_REFERENCES);
}
}
private void validateDvSubjectExists(Long id) {
if (dvSubjectMapper.selectById(id) == null) {
throw exception(DV_SUBJECT_NOT_EXISTS);

@ -2,10 +2,13 @@ package cn.iocoder.yudao.module.mes.service.planmaintenance;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvsubject.DvSubjectDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectplan.SubjectPlanDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.taskmanagement.TaskManagementDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dvsubject.DvSubjectMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.subjectplan.SubjectPlanMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.taskmanagement.TaskManagementMapper;
import cn.iocoder.yudao.module.mes.service.subjectplan.SubjectPlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
@ -44,6 +47,9 @@ public class PlanMaintenanceServiceImpl implements PlanMaintenanceService {
@Resource
private DvSubjectMapper dvSubjectMapper;
@Resource
private TaskManagementMapper taskManagementMapper;
@Override
@Transactional(rollbackFor = Exception.class)
@ -118,17 +124,36 @@ public class PlanMaintenanceServiceImpl implements PlanMaintenanceService {
@Override
@Transactional(rollbackFor = Exception.class)
public void deletePlanMaintenance( List<Long> idList) {
for (Long id : idList) {
// 校验存在
validatePlanMaintenanceExists(id);
public void deletePlanMaintenance(List<Long> idList) {
if (CollectionUtils.isEmpty(idList)) {
return;
}
// 批量校验存在性
List<PlanMaintenanceDO> existList = planMaintenanceMapper.selectBatchIds(idList);
if (existList.size() != idList.size()) {
throw exception(PLAN_MAINTENANCE_NOT_EXISTS);
}
// 删除
// 批量校验引用
validateBatchReferences(idList);
// 删除主表
planMaintenanceMapper.deleteByIds(idList);
//删除关联表数据
// 删除关联表数据
deleteSubjectPlan(idList);
}
private void validateBatchReferences(List<Long> idList) {
boolean exists = taskManagementMapper.exists(
Wrappers.<TaskManagementDO>lambdaQuery()
.in(TaskManagementDO::getProjectForm, idList)
);
if (exists) {
throw exception(PLAN_MAINTENANCE_REFERENCES);
}
}
private void deleteSubjectPlan(List<Long> idList) {

Loading…
Cancel
Save