| | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springblade.core.mp.base.BaseServiceImpl; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.utils.SpringUtil; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import org.sxkj.fw.area.vo.FwAreaDivideStatisticsVO; |
| | | import org.sxkj.fw.area.vo.FwAreaDivideVO; |
| | | import org.sxkj.fw.area.vo.FwDefenseSceneVO; |
| | | import org.sxkj.fw.device.entity.FwDeviceEntity; |
| | | import org.sxkj.fw.device.service.IFwDeviceService; |
| | | import org.sxkj.system.cache.SysCache; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 区域划分表 服务实现类 |
| | |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class FwAreaDivideServiceImpl extends BaseServiceImpl<FwAreaDivideMapper, FwAreaDivideEntity> |
| | | implements IFwAreaDivideService { |
| | | public class FwAreaDivideServiceImpl extends BaseServiceImpl<FwAreaDivideMapper, FwAreaDivideEntity> implements IFwAreaDivideService { |
| | | |
| | | private final IFwAreaDivideExtService fwAreaDivideExtService; |
| | | |
| | |
| | | fwAreaDivide.setAreaTypeKeys(areaTypeKeys); |
| | | } |
| | | } |
| | | String deviceIds = fwAreaDivide.getDeviceIds(); |
| | | // 判断deviceIds是否和数据库里面一致,不一致需要处理 |
| | | boolean isCreate = fwAreaDivide.getId() == null; |
| | | // 处理设备启用禁用 |
| | | handleDeviceEnablementAndDisabling(fwAreaDivide, isCreate, deviceIds); |
| | | |
| | | boolean mainResult = saveOrUpdate(fwAreaDivide); |
| | | if (!mainResult) { |
| | | return false; |
| | |
| | | throw new RuntimeException("保存区域扩展面数据失败"); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param fwAreaDivide |
| | | * @param isCreate |
| | | * @param deviceIds |
| | | */ |
| | | private void handleDeviceEnablementAndDisabling(FwAreaDivideEntity fwAreaDivide, boolean isCreate, String deviceIds) { |
| | | // 查找新增和删除的设备ID |
| | | Set<String> addedDeviceIds = new HashSet<>(); |
| | | Set<String> removedDeviceIds = new HashSet<>(); |
| | | |
| | | if (!isCreate) { |
| | | // 更新操作,需要比较数据库中的deviceIds |
| | | FwAreaDivideEntity existingEntity = baseMapper.selectById(fwAreaDivide.getId()); |
| | | if (existingEntity != null) { |
| | | String oldDeviceIds = existingEntity.getDeviceIds(); |
| | | // 解析新旧deviceIds为集合 |
| | | Set<String> oldDeviceIdSet = parseDeviceIds(oldDeviceIds); |
| | | Set<String> newDeviceIdSet = parseDeviceIds(deviceIds); |
| | | |
| | | // 找出新增的设备ID |
| | | for (String id : newDeviceIdSet) { |
| | | if (!oldDeviceIdSet.contains(id)) { |
| | | addedDeviceIds.add(id); |
| | | } |
| | | } |
| | | |
| | | // 找出删除的设备ID |
| | | for (String id : oldDeviceIdSet) { |
| | | if (!newDeviceIdSet.contains(id)) { |
| | | removedDeviceIds.add(id); |
| | | } |
| | | } |
| | | } |
| | | // 判断区域是否绑定到场景中 |
| | | boolean isBindScene = baseMapper.checkAreaBindScene(fwAreaDivide.getId()); |
| | | if (isBindScene) { |
| | | IFwDeviceService fwDeviceService = SpringUtil.getBean(IFwDeviceService.class); |
| | | if (!addedDeviceIds.isEmpty()) { |
| | | fwDeviceService.update(Wrappers.<FwDeviceEntity>lambdaUpdate() |
| | | .set(FwDeviceEntity::getIsEnabled, 1) |
| | | .in(FwDeviceEntity::getId, addedDeviceIds)); |
| | | } |
| | | if (!removedDeviceIds.isEmpty()) { |
| | | fwDeviceService.update(Wrappers.<FwDeviceEntity>lambdaUpdate() |
| | | .set(FwDeviceEntity::getIsEnabled, 0) |
| | | .in(FwDeviceEntity::getId, removedDeviceIds)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void fillCreateFields(FwAreaDivideEntity fwAreaDivide) { |
| | |
| | | fwAreaDivide.setUpdateUser(userId); |
| | | } |
| | | fwAreaDivide.setUpdateTime(new Date()); |
| | | } |
| | | |
| | | /** |
| | | * 解析设备ID字符串为集合 |
| | | * |
| | | * @param deviceIds 设备ID字符串,逗号分隔 |
| | | * @return 设备ID集合 |
| | | */ |
| | | private Set<String> parseDeviceIds(String deviceIds) { |
| | | Set<String> deviceIdSet = new HashSet<>(); |
| | | if (StringUtil.isNotBlank(deviceIds)) { |
| | | String[] ids = deviceIds.split(","); |
| | | for (String id : ids) { |
| | | if (StringUtil.isNotBlank(id)) { |
| | | deviceIdSet.add(id.trim()); |
| | | } |
| | | } |
| | | } |
| | | return deviceIdSet; |
| | | } |
| | | |
| | | private void fillCreateFieldsExt(FwAreaDivideExtEntity fwAreaDivideExt) { |
| | |
| | | return idList; |
| | | } |
| | | |
| | | /** |
| | | * 禁用设备 |
| | | * @param deviceIds 设备ID列表 |
| | | */ |
| | | private void disableDevices(List<Long> deviceIds) { |
| | | IFwDeviceService fwDeviceService = SpringUtil.getBean(IFwDeviceService.class); |
| | | if (deviceIds != null && !deviceIds.isEmpty()) { |
| | | // 实现设备禁用逻辑 |
| | | // 这里假设FwDeviceEntity有enabled字段,1表示启用,0表示禁用 |
| | | for (Long deviceId : deviceIds) { |
| | | FwDeviceEntity device = new FwDeviceEntity(); |
| | | device.setId(deviceId); |
| | | device.setIsEnabled(false); |
| | | fwDeviceService.updateById(device); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 逻辑删除区域划分,并处理关联设备的禁用 |
| | | * @param ids 区域划分ID列表 |
| | | * @return 是否删除成功 |
| | | */ |
| | | @Override |
| | | public boolean deleteLogic(List<Long> ids) { |
| | | if (ids == null || ids.isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | // 先获取要删除的区域划分 |
| | | List<FwAreaDivideEntity> areas = listByIds(ids); |
| | | if (areas == null || areas.isEmpty()) { |
| | | return false; |
| | | } |
| | | // 过滤出配置了场景的区域数据 |
| | | List<FwAreaDivideEntity> sceneBoundAreas = areas.stream() |
| | | .filter(area -> area != null && baseMapper.checkAreaBindScene(area.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 收集所有相关的设备ID |
| | | List<Long> deviceIds = new ArrayList<>(); |
| | | for (FwAreaDivideEntity area : sceneBoundAreas) { |
| | | if (area != null && StringUtil.isNotBlank(area.getDeviceIds())) { |
| | | deviceIds.addAll(parseIdList(area.getDeviceIds())); |
| | | } |
| | | } |
| | | |
| | | // 禁用相关设备 |
| | | if (!deviceIds.isEmpty()) { |
| | | disableDevices(deviceIds); |
| | | } |
| | | |
| | | // 执行逻辑删除 |
| | | return super.deleteLogic(ids); |
| | | } |
| | | |
| | | } |