在做配置选项(设备类型,所属楼层等)的时候,当删除某配置的时候,我需要检验该配置是否已被删除。
@Override
public BaseVO deleteOptionDetail(Integer id) {
// 合法性验证
if (null == id) {
return ParamErrorVO.getInstance();
}
ConfigOptionDetail configOptionDetail = configOptionDetailMapper.selectById(id);
if (null == configOptionDetail || 1 == configOptionDetail.getIsDeleted()) {
return new ErrorVO("该配置不存在");
}
if (configOptionDetail.getSystem() == 1) {
return new ErrorVO("系统属性不能删除");
}
if (configOptionDetail.getUseCount() = 0) {
return new ErrorVO("配置正在使用不能删除,请先删除使用配置的地方");
}
// 合法性通过
configOptionDetail.setIsDeleted(1);
configOptionDetail.setGmtModefied(Calendar.getInstance().getTime());
configOptionDetailMapper.updateById(configOptionDetail);
// 更新内存配置
ConfigOptionConstruct.updateOption();
return SuccessVO.getInstance();
}
思考之后我决定采用,给配置选项设备一个use_count字段,代表该配置被引用的次数。 只有当该字段值为 0 时,该配置选项记录才可被删除。
使用情况: 我需要批量删除房间, 删除房间的同时,room对象中使用到了所属楼层的配置选项,我需要将他们的引用减少
@Override
public BaseVO deleteRoomByIds(Integer[] ids) {
if (null == ids) {
return ParamErrorVO.getInstance();
}
EntityWrapper<Room> entityWrapper = new EntityWrapper<>();
entityWrapper.where("isdelete={0}", 0);
// 核查删除的房间中是否存在正在使用的设备
List<Integer> notDelete = deviceInfoService.checkRoomIds(ids