diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java index b258be0..1a3da56 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java @@ -33,9 +33,10 @@ public class DeviceChangeStatusConvertor extends DefaultConvertor { protected void eventHandle(List builders) { AlertManager alertManager = AlertManager.getDefault(); for (Builder builder : builders) { - if (builder instanceof Rtu.Builder rtuBuild) { + /* if (builder instanceof Rtu.Builder rtuBuild) { alertManager.emit(new BlueDisplayAlertEvent(rtuBuild)); - } else if (builder instanceof Track.Builder trackBuild) { + } else*/ + if (builder instanceof Track.Builder trackBuild) { // alertManager.emit(new SwitchAndTrackLedAlertEvent(trackBuild)); alertManager.emit(new AxleLedAlertEvent(trackBuild)); diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceInitConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceInitConvertor.java index ba8115e..ff5e237 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceInitConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceInitConvertor.java @@ -33,9 +33,10 @@ public class DeviceInitConvertor extends DefaultConvertor { protected void eventHandle(List builders) { AlertManager alertManager = AlertManager.getDefault(); for (Builder builder : builders) { - if (builder instanceof Rtu.Builder rtuBuild) { + /* if (builder instanceof Rtu.Builder rtuBuild) { alertManager.emit(new BlueDisplayAlertEvent(rtuBuild)); - } else if (builder instanceof Track.Builder trackBuild) { + } else */ + if (builder instanceof Track.Builder trackBuild) { // alertManager.emit(new SwitchAndTrackLedAlertEvent(trackBuild)); alertManager.emit(new AxleLedAlertEvent(trackBuild)); } else if (builder instanceof Switch.Builder switchBuild) { diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/AllLineBlueAlertListener.java b/src/main/java/club/joylink/xiannccda/ats/warn/AllLineBlueAlertListener.java index a6d47dc..2218340 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/AllLineBlueAlertListener.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/AllLineBlueAlertListener.java @@ -16,7 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -@Component +//@Component @Slf4j public class AllLineBlueAlertListener implements AlertSourceEventListener { diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/BlueAlertListener.java b/src/main/java/club/joylink/xiannccda/ats/warn/BlueAlertListener.java index 5eee2fe..ce7a170 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/BlueAlertListener.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/BlueAlertListener.java @@ -31,7 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -@Component +//@Component @Slf4j public class BlueAlertListener implements AlertSourceEventListener { diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/BuleDisplayMonitoringTask.java b/src/main/java/club/joylink/xiannccda/ats/warn/BuleDisplayMonitoringTask.java new file mode 100644 index 0000000..163c225 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/ats/warn/BuleDisplayMonitoringTask.java @@ -0,0 +1,153 @@ +package club.joylink.xiannccda.ats.warn; + +import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; +import club.joylink.xiannccda.alert.core.AlertManager; +import club.joylink.xiannccda.alert.core.AlertMonitoringTask; +import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; +import club.joylink.xiannccda.ats.message.collect.DeviceDataRepository; +import club.joylink.xiannccda.ats.message.collect.DeviceDataRepository.DataTypeEnum; +import club.joylink.xiannccda.ats.message.collect.datasource.DeviceStatusData; +import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; +import club.joylink.xiannccda.dto.protos.DeviceStatusProto; +import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Rtu; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Station; +import club.joylink.xiannccda.service.AlertInfoService; +import club.joylink.xiannccda.service.config.DeviceGuardConfigService; +import club.joylink.xiannccda.vo.AreaConfigVO; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.google.common.base.Strings; +import com.google.common.collect.Lists; +import com.google.protobuf.GeneratedMessageV3; +import com.google.protobuf.GeneratedMessageV3.Builder; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class BuleDisplayMonitoringTask implements AlertMonitoringTask { + + private final DeviceGuardConfigService configService; + private final AlertInfoService alertInfoService; + + public BuleDisplayMonitoringTask(DeviceGuardConfigService configService, AlertInfoService alertInfoService) { + this.configService = configService; + this.alertInfoService = alertInfoService; + } + + private final AlertManager alertManager = AlertManager.getDefault(); + + private final static Map> CONTROL_STATION_MAPER = new ConcurrentHashMap<>(); + + @Override + public String getName() { + return "BULE_DISPLAY"; + } + + private final String ALL_BULE_DISPLAY_NAME = "ALL_BULE_DISPLAY"; + + private Map findRtuDeviceSource(String lineIdStr) { + DeviceStatusData deviceStatusData = DeviceDataRepository.findDataSouce(lineIdStr, DataTypeEnum.DEVICE); + if (deviceStatusData.getAllDeviceMap().isEmpty()) { + return null; + } + Map builderMap = deviceStatusData.getAllDeviceMap().get(Rtu.getDescriptor().getName()); + if (builderMap.isEmpty()) { + return null; + } + return builderMap; + } + + private List findRtuBuild(Map builderMap) { + List collectRtuList = Lists.newArrayList(); + for (Builder value : builderMap.values()) { + if (value instanceof DeviceStatusProto.Rtu.Builder rtu) { + Station station = findAllStation(rtu.getLineId(), rtu.getId()); + Optional alertInfoMostOptional = this.alertInfoService.findAreaDevice(AlertType.BLUE_DISPLAY, AlertDeviceType.DEVICE_TYPE_RTU, station.getCommon().getId(), rtu.getLineId()); + collectRtuList.add(new RtuWarnVO(rtu, station, alertInfoMostOptional)); + } + } + return collectRtuList; + } + + @Override + public void run() { + Set allLineSet = DeviceDataRepository.getAllLines(); + for (String lineIdStr : allLineSet) { + Integer lineId = Integer.parseInt(lineIdStr); + Map builderMap = this.findRtuDeviceSource(lineIdStr); + List collectRtuList = this.findRtuBuild(builderMap); + List allRtuArea = this.alertInfoService.findDevice2(AlertType.BLUE_DISPLAY, AlertDeviceType.DEVICE_TYPE_RTU, lineId); + Map> rtuVOMapList = collectRtuList.stream().collect(Collectors.groupingBy(d -> d.rtu.getRtuId())); + if (rtuVOMapList.keySet().size() != allRtuArea.size()) { + for (Entry> listEntry : rtuVOMapList.entrySet()) { + Optional rwVO = listEntry.getValue().stream().filter(d -> d.rtu.getIpRtuStusDown()).findAny(); + + boolean allOnLine = listEntry.getValue().stream().noneMatch(d -> d.rtu.getIpRtuStusDown()); + if (rwVO.isPresent()) { + RtuWarnVO rtuVO = rwVO.get(); + if (alertManager.putAlterDevice(lineId, this.getName(), String.valueOf(rtuVO.rtu.getRtuId()))) { + String alertInfoMostName = rtuVO.areaConfigVO.map(AreaConfigVO::getAreaName).orElse(String.format("%s 集中站中断连接 ", rtuVO.station.getName())); + NccAlertInfo alertInfo = this.alertInfoService.createAlert2(rtuVO.areaConfigVO, AlertType.BLUE_DISPLAY, rtuVO.rtu, alertInfoMostName, String.valueOf(rtuVO.station.getCommon().getId()), + AlertDeviceType.DEVICE_TYPE_RTU, + false); + alertManager.emit(alertInfo); + } + } else if (allOnLine) { + DeviceStatusProto.Rtu.Builder rtu = listEntry.getValue().get(0).rtu; + alertManager.removeAlterDevice(rtu.getLineId(), this.getName(), String.valueOf(rtu.getRtuId())); + alertManager.removeAlterDevice(rtu.getLineId(), ALL_BULE_DISPLAY_NAME, "All"); + } + } + } else if (alertManager.putAlterDevice(lineId, ALL_BULE_DISPLAY_NAME, "All")) { + NccAlertInfo alertInfo = this.alertInfoService.createAlert2(Optional.empty(), AlertType.BLUE_DISPLAY, collectRtuList.get(0).rtu, "全线蓝线", null, + AlertDeviceType.DEVICE_TYPE_RTU, + false); + alertManager.emit(alertInfo); + } + + } + } + + + private static synchronized Station findAllStation(int lineId, String rtuId) { + if (CollectionUtils.isEmpty(CONTROL_STATION_MAPER)) { + Stream stream = LineGraphicDataRepository.getDevices(lineId, Station.class); + Collection stationList = stream.filter(Station::getConcentrationStations) + .collect(Collectors.toMap(d -> d.getCommon().getId(), Function.identity(), (o1, o2) -> o2)).values(); + CONTROL_STATION_MAPER.put(lineId, new ArrayList<>(stationList)); + } + List stationList = CONTROL_STATION_MAPER.get(lineId); + if (CollectionUtils.isEmpty(stationList)) { + return null; + } + String rtuName = Strings.padStart(rtuId, 2, '0'); + Optional optionalStation = stationList.stream().filter(d -> StringUtils.equalsIgnoreCase(d.getCode(), rtuName)).findAny(); + return optionalStation.orElse(null); + } + + private static class RtuWarnVO { + + DeviceStatusProto.Rtu.Builder rtu; + Station station; + Optional areaConfigVO; + + public RtuWarnVO(Rtu.Builder rtu, Station station, Optional areaConfigVO) { + this.rtu = rtu; + this.station = station; + this.areaConfigVO = areaConfigVO; + } + } +} diff --git a/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java b/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java index 4359232..5850248 100644 --- a/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java +++ b/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java @@ -28,7 +28,7 @@ public class AlertInfoService { this.deviceAreaConfigService = deviceAreaConfigService; } - private List findDevice2(AlertType alertType, AlertDeviceType dt, int lineId) { + public List findDevice2(AlertType alertType, AlertDeviceType dt, int lineId) { List areaConfigList = this.deviceAreaConfigService.getCache(lineId, dt); if (CollectionUtils.isEmpty(areaConfigList)) { return Collections.emptyList(); diff --git a/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java b/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java index 9a055a7..ffc5cfa 100644 --- a/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java +++ b/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java @@ -41,163 +41,163 @@ import org.springframework.stereotype.Service; @Service public class DeviceAreaConfigService { - @Autowired - private DeviceAreaConfigRepository deviceAreaConfigRepository; + @Autowired + private DeviceAreaConfigRepository deviceAreaConfigRepository; - private final static Cache>> AREA_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(2 * 60, TimeUnit.MINUTES).build(); + private final static Cache>> AREA_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(5 * 60, TimeUnit.MINUTES).build(); - public List getCache(int lineId, AlertDeviceType dt) { - try { - Map> configList = AREA_CONFIG_CACHE.get(lineId, () -> { - List configs = deviceAreaConfigRepository.list(Wrappers.lambdaQuery(DeviceAreaConfig.class).eq(DeviceAreaConfig::getLineId, lineId)); - List listVO = configs.stream().map(AreaConfigVO::new).toList(); - return listVO.stream().collect(Collectors.groupingBy(AreaConfigVO::getDeviceType)); - }); - return configList.get(dt); - } catch (ExecutionException e) { - return Collections.emptyList(); - } + public List getCache(int lineId, AlertDeviceType dt) { + try { + Map> configList = AREA_CONFIG_CACHE.get(lineId, () -> { + List configs = deviceAreaConfigRepository.list(Wrappers.lambdaQuery(DeviceAreaConfig.class).eq(DeviceAreaConfig::getLineId, lineId)); + List listVO = configs.stream().map(AreaConfigVO::new).toList(); + return listVO.stream().collect(Collectors.groupingBy(AreaConfigVO::getDeviceType)); + }); + return configList.get(dt); + } catch (ExecutionException e) { + return Collections.emptyList(); + } + } + + private String findDevice(Integer lineId, List deviceIds) { + List errorDevices = Lists.newArrayList(); + + for (String deviceId : deviceIds) { + MessageOrBuilder msgBuild = LineGraphicDataRepository.getDeviceByCodeNotException(lineId, deviceId); + if (Objects.nonNull(msgBuild)) { + String deviceCode = DeviceStatusDataOperate.findFieldVal(msgBuild, "code", String.class); + errorDevices.add(deviceCode); + } else { + errorDevices.add(String.format("id[%s]未找到设备", deviceId)); + } + } + return Joiner.on(StringPool.COMMA).join(errorDevices); + } + + + private void checkDataForSql(DeviceAreaConfigDto dto, boolean isUpdate) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class); + queryWrapper.eq(DeviceAreaConfig::getDeviceType, dto.getDeviceType()); + queryWrapper.ne(isUpdate, DeviceAreaConfig::getId, dto.getId()); + List lastSqlList = Lists.newArrayList(); + for (String alertType : dto.getAlertTypes()) { + lastSqlList.add(String.format(" find_in_set('%s',%s) > 0 ", alertType, DeviceAreaConfig.ALERT_TYPES)); + } + if (CollectionUtils.isNotEmpty(lastSqlList)) { + StringBuilder queryTypes = new StringBuilder(" and ( "); + queryTypes.append(Joiner.on(" or ").join(lastSqlList)).append(") "); + queryWrapper.last(queryTypes.toString()); } - private String findDevice(Integer lineId, List deviceIds) { - List errorDevices = Lists.newArrayList(); + List areaConfigList = this.deviceAreaConfigRepository.list(queryWrapper); - for (String deviceId : deviceIds) { - MessageOrBuilder msgBuild = LineGraphicDataRepository.getDeviceByCodeNotException(lineId, deviceId); - if (Objects.nonNull(msgBuild)) { - String deviceCode = DeviceStatusDataOperate.findFieldVal(msgBuild, "code", String.class); - errorDevices.add(deviceCode); - } else { - errorDevices.add(String.format("id[%s]未找到设备", deviceId)); - } - } - return Joiner.on(StringPool.COMMA).join(errorDevices); + for (DeviceAreaConfig areaConfig : areaConfigList) { + List dbDatas = Splitter.on(StringPool.COMMA).splitToList(areaConfig.getData()); + List saveDatas = Lists.newArrayList(dto.getData()); + saveDatas.retainAll(dbDatas); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isEmpty(saveDatas), + String.format("添加重复的设备区域[%s] 设备:%s", areaConfig.getAreaName(), this.findDevice(areaConfig.getLineId(), saveDatas))); + } + } + + + public void saveOrUpdate(DeviceAreaConfigDto dto) { + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(dto.getAlertTypes()), "绑定数据类型不能为空"); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotTrue(dto.getAlertTypes().contains(AlertType.INTERLOCK_LEVEL_ONE.name()) && dto.getAlertTypes().size() != 1, "联锁区只能选择一个类型"); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotTrue(dto.getAlertTypes().contains(AlertType.INTERLOCK_LEVEL_ONE.name()) && dto.getData().size() > 1, "联锁区只能添加一个设备"); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(dto.getData()), "绑定数据不能为空"); + + boolean isUpdate = false; + DeviceAreaConfig areaConfig = this.convertDB(dto); + if (Objects.nonNull(dto.getId()) && dto.getId() != 0L) { + LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceAreaConfig.class); + qw.eq(DeviceAreaConfig::getId, dto.getId()); + Long counter = this.deviceAreaConfigRepository.count(qw); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(counter > 0L, "未找到对应的数据"); + areaConfig.setId(dto.getId()); + isUpdate = true; + + } + this.checkDataForSql(dto, isUpdate); + this.deviceAreaConfigRepository.saveOrUpdate(areaConfig); + AREA_CONFIG_CACHE.invalidate(dto.getLineId()); + } + + private DeviceAreaConfig convertDB(DeviceAreaConfigDto dto) { + DeviceAreaConfig deviceAreaConfig = new DeviceAreaConfig(); + deviceAreaConfig.setLineId(dto.getLineId()); + deviceAreaConfig.setAreaName(dto.getAreaName()); + deviceAreaConfig.setData(dto.getData().stream().distinct().collect(Collectors.joining(StringPool.COMMA))); + deviceAreaConfig.setDeviceType(dto.getDeviceType()); + deviceAreaConfig.setAlertTypes(dto.getAlertTypes().stream().distinct().collect(Collectors.joining(StringPool.COMMA))); + return deviceAreaConfig; + } + + + private DeviceAreaConfigDto convertDto(DeviceAreaConfig dto) { + DeviceAreaConfigDto deviceAreaConfig = new DeviceAreaConfigDto(); + deviceAreaConfig.setLineId(dto.getLineId()); + deviceAreaConfig.setAreaName(dto.getAreaName()); + List datas = Splitter.on(StringPool.COMMA).splitToList(dto.getData()); + + deviceAreaConfig.setData(datas); + deviceAreaConfig.setDeviceType(dto.getDeviceType()); + if (StringUtils.isNotEmpty(dto.getAlertTypes())) { + List ll = Splitter.on(StringPool.COMMA).splitToList(dto.getAlertTypes()); + deviceAreaConfig.setAlertTypes(ll); + } + return deviceAreaConfig; + } + + public void bindAlertType(Long id, List alertTypes) { + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(alertTypes), "绑定数据类型不能为空"); + DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据"); + LambdaUpdateWrapper qw = Wrappers.lambdaUpdate(DeviceAreaConfig.class); + qw.set(DeviceAreaConfig::getAlertTypes, alertTypes.stream().distinct().collect(Collectors.joining())).eq(DeviceAreaConfig::getId, id); + this.deviceAreaConfigRepository.update(qw); + } + + public DeviceAreaConfigDto findInfo(Long id) { + DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据"); + return this.convertDto(areaConfig); + } + + public Page page(DeviceAreaConfigQueryDto queryDTO, Integer lineId) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class); + queryWrapper.select(List.of(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName, DeviceAreaConfig::getLineId, DeviceAreaConfig::getDeviceType)); + queryWrapper.eq(DeviceAreaConfig::getLineId, lineId); + queryWrapper.like(StringUtils.isNotEmpty(queryDTO.getAreaName()), DeviceAreaConfig::getAreaName, queryDTO.getAreaName()); + if (Objects.nonNull(queryDTO.getDeviceType())) { + queryWrapper.eq(DeviceAreaConfig::getDeviceType, queryDTO.getDeviceType().name()); + } + if (Objects.nonNull(queryDTO.getAlertType())) { + queryWrapper.last(String.format(" and find_in_set('%s',%s) > 0", queryDTO.getAlertType().name(), DeviceAreaConfig.ALERT_TYPES)); } + Page pageConfig = deviceAreaConfigRepository.page(queryDTO, queryWrapper); + return this.convertPage(pageConfig); + } - private void checkDataForSql(DeviceAreaConfigDto dto, boolean isUpdate) { - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class); - queryWrapper.eq(DeviceAreaConfig::getDeviceType, dto.getDeviceType()); - queryWrapper.ne(isUpdate, DeviceAreaConfig::getId, dto.getId()); - List lastSqlList = Lists.newArrayList(); - for (String alertType : dto.getAlertTypes()) { - lastSqlList.add(String.format(" find_in_set('%s',%s) > 0 ", alertType, DeviceAreaConfig.ALERT_TYPES)); - } - if (CollectionUtils.isNotEmpty(lastSqlList)) { - StringBuilder queryTypes = new StringBuilder(" and ( "); - queryTypes.append(Joiner.on(" or ").join(lastSqlList)).append(") "); - queryWrapper.last(queryTypes.toString()); - } - - List areaConfigList = this.deviceAreaConfigRepository.list(queryWrapper); - - for (DeviceAreaConfig areaConfig : areaConfigList) { - List dbDatas = Splitter.on(StringPool.COMMA).splitToList(areaConfig.getData()); - List saveDatas = Lists.newArrayList(dto.getData()); - saveDatas.retainAll(dbDatas); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isEmpty(saveDatas), - String.format("添加重复的设备区域[%s] 设备:%s", areaConfig.getAreaName(), this.findDevice(areaConfig.getLineId(), saveDatas))); - } - } - - - public void saveOrUpdate(DeviceAreaConfigDto dto) { - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(dto.getAlertTypes()), "绑定数据类型不能为空"); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotTrue(dto.getAlertTypes().contains(AlertType.INTERLOCK_LEVEL_ONE.name()) && dto.getAlertTypes().size() != 1, "联锁区只能选择一个类型"); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertNotTrue(dto.getAlertTypes().contains(AlertType.INTERLOCK_LEVEL_ONE.name()) && dto.getData().size() > 1, "联锁区只能添加一个设备"); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(dto.getData()), "绑定数据不能为空"); - - boolean isUpdate = false; - DeviceAreaConfig areaConfig = this.convertDB(dto); - if (Objects.nonNull(dto.getId()) && dto.getId() != 0L) { - LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceAreaConfig.class); - qw.eq(DeviceAreaConfig::getId, dto.getId()); - Long counter = this.deviceAreaConfigRepository.count(qw); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(counter > 0L, "未找到对应的数据"); - areaConfig.setId(dto.getId()); - isUpdate = true; - - } - this.checkDataForSql(dto, isUpdate); - this.deviceAreaConfigRepository.saveOrUpdate(areaConfig); - AREA_CONFIG_CACHE.invalidate(dto.getLineId()); - } - - private DeviceAreaConfig convertDB(DeviceAreaConfigDto dto) { - DeviceAreaConfig deviceAreaConfig = new DeviceAreaConfig(); - deviceAreaConfig.setLineId(dto.getLineId()); - deviceAreaConfig.setAreaName(dto.getAreaName()); - deviceAreaConfig.setData(dto.getData().stream().distinct().collect(Collectors.joining(StringPool.COMMA))); - deviceAreaConfig.setDeviceType(dto.getDeviceType()); - deviceAreaConfig.setAlertTypes(dto.getAlertTypes().stream().distinct().collect(Collectors.joining(StringPool.COMMA))); - return deviceAreaConfig; - } - - - private DeviceAreaConfigDto convertDto(DeviceAreaConfig dto) { - DeviceAreaConfigDto deviceAreaConfig = new DeviceAreaConfigDto(); - deviceAreaConfig.setLineId(dto.getLineId()); - deviceAreaConfig.setAreaName(dto.getAreaName()); - List datas = Splitter.on(StringPool.COMMA).splitToList(dto.getData()); - - deviceAreaConfig.setData(datas); - deviceAreaConfig.setDeviceType(dto.getDeviceType()); - if (StringUtils.isNotEmpty(dto.getAlertTypes())) { - List ll = Splitter.on(StringPool.COMMA).splitToList(dto.getAlertTypes()); - deviceAreaConfig.setAlertTypes(ll); - } - return deviceAreaConfig; - } - - public void bindAlertType(Long id, List alertTypes) { - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(alertTypes), "绑定数据类型不能为空"); - DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据"); - LambdaUpdateWrapper qw = Wrappers.lambdaUpdate(DeviceAreaConfig.class); - qw.set(DeviceAreaConfig::getAlertTypes, alertTypes.stream().distinct().collect(Collectors.joining())).eq(DeviceAreaConfig::getId, id); - this.deviceAreaConfigRepository.update(qw); - } - - public DeviceAreaConfigDto findInfo(Long id) { - DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据"); - return this.convertDto(areaConfig); - } - - public Page page(DeviceAreaConfigQueryDto queryDTO, Integer lineId) { - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class); - queryWrapper.select(List.of(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName, DeviceAreaConfig::getLineId, DeviceAreaConfig::getDeviceType)); - queryWrapper.eq(DeviceAreaConfig::getLineId, lineId); - queryWrapper.like(StringUtils.isNotEmpty(queryDTO.getAreaName()), DeviceAreaConfig::getAreaName, queryDTO.getAreaName()); - if (Objects.nonNull(queryDTO.getDeviceType())) { - queryWrapper.eq(DeviceAreaConfig::getDeviceType, queryDTO.getDeviceType().name()); - } - if (Objects.nonNull(queryDTO.getAlertType())) { - queryWrapper.last(String.format(" and find_in_set('%s',%s) > 0", queryDTO.getAlertType().name(), DeviceAreaConfig.ALERT_TYPES)); - } - - Page pageConfig = deviceAreaConfigRepository.page(queryDTO, queryWrapper); - return this.convertPage(pageConfig); - } - - private Page convertPage(Page pageConfig) { + private Page convertPage(Page pageConfig) { // List areaConfigIds = pageConfig.getRecords().stream().map(DeviceAreaConfig::getId).toList(); - List pageList = Lists.newArrayListWithCapacity(pageConfig.getRecords().size()); + List pageList = Lists.newArrayListWithCapacity(pageConfig.getRecords().size()); // Map areaIdMapTipIds = iAlertTipRepository.findBindAreaConfigId(areaConfigIds); - for (DeviceAreaConfig ac : pageConfig.getRecords()) { - pageList.add(new DeviceAreaConfigPageDto(ac)); - } - Page dtoPage = Page.of(pageConfig.getCurrent(), pageConfig.getSize(), pageConfig.getTotal()); - dtoPage.setRecords(pageList); - return dtoPage; + for (DeviceAreaConfig ac : pageConfig.getRecords()) { + pageList.add(new DeviceAreaConfigPageDto(ac)); } + Page dtoPage = Page.of(pageConfig.getCurrent(), pageConfig.getSize(), pageConfig.getTotal()); + dtoPage.setRecords(pageList); + return dtoPage; + } - public List findAlertType(Integer lineId, String alertType) { - LambdaQueryWrapper query = Wrappers.lambdaQuery(DeviceAreaConfig.class); - query.select(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName); - query.eq(DeviceAreaConfig::getLineId, lineId); - StringBuilder sb = new StringBuilder(); - sb.append(String.format(" and find_in_set('%s',%s) > 0", alertType, DeviceAreaConfig.ALERT_TYPES)); + public List findAlertType(Integer lineId, String alertType) { + LambdaQueryWrapper query = Wrappers.lambdaQuery(DeviceAreaConfig.class); + query.select(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName); + query.eq(DeviceAreaConfig::getLineId, lineId); + StringBuilder sb = new StringBuilder(); + sb.append(String.format(" and find_in_set('%s',%s) > 0", alertType, DeviceAreaConfig.ALERT_TYPES)); /*if (StringUtils.equals(alertType, AlertType.AXLE_LED_RED_MOST.name()) || StringUtils.equals(alertType, AlertType.AXLE_LED_ORANGE_MOST.name())) { sb.append(String.format(" or find_in_set('%s',%s) > 0", AlertType.INTERLOCK_LEVEL_TWO.name(), DeviceAreaConfig.ALERT_TYPES)); } else if (StringUtils.equals(alertType, AlertType.AXLE_LED_RED_INTERLOCK_AREA.name()) || StringUtils.equals(alertType, AlertType.AXLE_LED_ORANGE_INTERLOCK_AREA.name())) { @@ -205,29 +205,29 @@ public class DeviceAreaConfigService { } else if (StringUtils.equals(alertType, AlertType.SWITCH_LOST_MOST.name())) { sb.append(String.format(" or find_in_set('%s',%s) > 0", AlertType.INTERLOCK_LEVEL_TWO.name(), DeviceAreaConfig.ALERT_TYPES)); }*/ - if (StringUtils.equals(alertType, AlertType.AXLE_LED_RED_INTERLOCK_AREA.name()) - || StringUtils.equals(alertType, AlertType.AXLE_LED_ORANGE_INTERLOCK_AREA.name()) - || StringUtils.equals(alertType, AlertType.SWITCH_LOST_INTERLOCK_AREA.name())) { - sb.append(String.format(" or find_in_set('%s',%s) > 0", AlertType.INTERLOCK_LEVEL_ONE.name(), DeviceAreaConfig.ALERT_TYPES)); - } - query.last(sb.toString()); - return this.deviceAreaConfigRepository.list(query).stream().map(AreaFinder::new).toList(); + if (StringUtils.equals(alertType, AlertType.AXLE_LED_RED_INTERLOCK_AREA.name()) + || StringUtils.equals(alertType, AlertType.AXLE_LED_ORANGE_INTERLOCK_AREA.name()) + || StringUtils.equals(alertType, AlertType.SWITCH_LOST_INTERLOCK_AREA.name())) { + sb.append(String.format(" or find_in_set('%s',%s) > 0", AlertType.INTERLOCK_LEVEL_ONE.name(), DeviceAreaConfig.ALERT_TYPES)); } + query.last(sb.toString()); + return this.deviceAreaConfigRepository.list(query).stream().map(AreaFinder::new).toList(); + } - public void delete(Long id) { - deviceAreaConfigRepository.removeById(id); - } - - @Data - public static class AreaFinder { - - private Long id; - private String areaName; - - public AreaFinder(DeviceAreaConfig areaConfig) { - this.id = areaConfig.getId(); - this.areaName = areaConfig.getAreaName(); - } + public void delete(Long id) { + deviceAreaConfigRepository.removeById(id); + } + + @Data + public static class AreaFinder { + + private Long id; + private String areaName; + + public AreaFinder(DeviceAreaConfig areaConfig) { + this.id = areaConfig.getId(); + this.areaName = areaConfig.getAreaName(); } + } }