蓝显告警逻辑调整

This commit is contained in:
tiger_zhou 2023-12-21 09:19:23 +08:00
parent ece4ccbba1
commit 8a05537555
7 changed files with 328 additions and 173 deletions

View File

@ -33,9 +33,10 @@ public class DeviceChangeStatusConvertor extends DefaultConvertor {
protected void eventHandle(List<Builder> builders) { protected void eventHandle(List<Builder> builders) {
AlertManager alertManager = AlertManager.getDefault(); AlertManager alertManager = AlertManager.getDefault();
for (Builder builder : builders) { for (Builder builder : builders) {
if (builder instanceof Rtu.Builder rtuBuild) { /* if (builder instanceof Rtu.Builder rtuBuild) {
alertManager.emit(new BlueDisplayAlertEvent(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 SwitchAndTrackLedAlertEvent(trackBuild));
alertManager.emit(new AxleLedAlertEvent(trackBuild)); alertManager.emit(new AxleLedAlertEvent(trackBuild));

View File

@ -33,9 +33,10 @@ public class DeviceInitConvertor extends DefaultConvertor {
protected void eventHandle(List<Builder> builders) { protected void eventHandle(List<Builder> builders) {
AlertManager alertManager = AlertManager.getDefault(); AlertManager alertManager = AlertManager.getDefault();
for (Builder builder : builders) { for (Builder builder : builders) {
if (builder instanceof Rtu.Builder rtuBuild) { /* if (builder instanceof Rtu.Builder rtuBuild) {
alertManager.emit(new BlueDisplayAlertEvent(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 SwitchAndTrackLedAlertEvent(trackBuild));
alertManager.emit(new AxleLedAlertEvent(trackBuild)); alertManager.emit(new AxleLedAlertEvent(trackBuild));
} else if (builder instanceof Switch.Builder switchBuild) { } else if (builder instanceof Switch.Builder switchBuild) {

View File

@ -16,7 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component //@Component
@Slf4j @Slf4j
public class AllLineBlueAlertListener implements AlertSourceEventListener<AllLineBlueDisplayAlertEvent> { public class AllLineBlueAlertListener implements AlertSourceEventListener<AllLineBlueDisplayAlertEvent> {

View File

@ -31,7 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component //@Component
@Slf4j @Slf4j
public class BlueAlertListener implements AlertSourceEventListener<BlueDisplayAlertEvent> { public class BlueAlertListener implements AlertSourceEventListener<BlueDisplayAlertEvent> {

View File

@ -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<Integer, List<Station>> CONTROL_STATION_MAPER = new ConcurrentHashMap<>();
@Override
public String getName() {
return "BULE_DISPLAY";
}
private final String ALL_BULE_DISPLAY_NAME = "ALL_BULE_DISPLAY";
private Map<String, GeneratedMessageV3.Builder> findRtuDeviceSource(String lineIdStr) {
DeviceStatusData deviceStatusData = DeviceDataRepository.findDataSouce(lineIdStr, DataTypeEnum.DEVICE);
if (deviceStatusData.getAllDeviceMap().isEmpty()) {
return null;
}
Map<String, GeneratedMessageV3.Builder> builderMap = deviceStatusData.getAllDeviceMap().get(Rtu.getDescriptor().getName());
if (builderMap.isEmpty()) {
return null;
}
return builderMap;
}
private List<RtuWarnVO> findRtuBuild(Map<String, GeneratedMessageV3.Builder> builderMap) {
List<RtuWarnVO> collectRtuList = Lists.newArrayList();
for (Builder value : builderMap.values()) {
if (value instanceof DeviceStatusProto.Rtu.Builder rtu) {
Station station = findAllStation(rtu.getLineId(), rtu.getId());
Optional<AreaConfigVO> 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<String> allLineSet = DeviceDataRepository.getAllLines();
for (String lineIdStr : allLineSet) {
Integer lineId = Integer.parseInt(lineIdStr);
Map<String, GeneratedMessageV3.Builder> builderMap = this.findRtuDeviceSource(lineIdStr);
List<RtuWarnVO> collectRtuList = this.findRtuBuild(builderMap);
List<AreaConfigVO> allRtuArea = this.alertInfoService.findDevice2(AlertType.BLUE_DISPLAY, AlertDeviceType.DEVICE_TYPE_RTU, lineId);
Map<Integer, List<RtuWarnVO>> rtuVOMapList = collectRtuList.stream().collect(Collectors.groupingBy(d -> d.rtu.getRtuId()));
if (rtuVOMapList.keySet().size() != allRtuArea.size()) {
for (Entry<Integer, List<RtuWarnVO>> listEntry : rtuVOMapList.entrySet()) {
Optional<RtuWarnVO> 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<Station> stream = LineGraphicDataRepository.getDevices(lineId, Station.class);
Collection<Station> 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<Station> stationList = CONTROL_STATION_MAPER.get(lineId);
if (CollectionUtils.isEmpty(stationList)) {
return null;
}
String rtuName = Strings.padStart(rtuId, 2, '0');
Optional<Station> 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> areaConfigVO;
public RtuWarnVO(Rtu.Builder rtu, Station station, Optional<AreaConfigVO> areaConfigVO) {
this.rtu = rtu;
this.station = station;
this.areaConfigVO = areaConfigVO;
}
}
}

View File

@ -28,7 +28,7 @@ public class AlertInfoService {
this.deviceAreaConfigService = deviceAreaConfigService; this.deviceAreaConfigService = deviceAreaConfigService;
} }
private List<AreaConfigVO> findDevice2(AlertType alertType, AlertDeviceType dt, int lineId) { public List<AreaConfigVO> findDevice2(AlertType alertType, AlertDeviceType dt, int lineId) {
List<AreaConfigVO> areaConfigList = this.deviceAreaConfigService.getCache(lineId, dt); List<AreaConfigVO> areaConfigList = this.deviceAreaConfigService.getCache(lineId, dt);
if (CollectionUtils.isEmpty(areaConfigList)) { if (CollectionUtils.isEmpty(areaConfigList)) {
return Collections.emptyList(); return Collections.emptyList();

View File

@ -41,163 +41,163 @@ import org.springframework.stereotype.Service;
@Service @Service
public class DeviceAreaConfigService { public class DeviceAreaConfigService {
@Autowired @Autowired
private DeviceAreaConfigRepository deviceAreaConfigRepository; private DeviceAreaConfigRepository deviceAreaConfigRepository;
private final static Cache<Integer, Map<AlertDeviceType, List<AreaConfigVO>>> AREA_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(2 * 60, TimeUnit.MINUTES).build(); private final static Cache<Integer, Map<AlertDeviceType, List<AreaConfigVO>>> AREA_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(5 * 60, TimeUnit.MINUTES).build();
public List<AreaConfigVO> getCache(int lineId, AlertDeviceType dt) { public List<AreaConfigVO> getCache(int lineId, AlertDeviceType dt) {
try { try {
Map<AlertDeviceType, List<AreaConfigVO>> configList = AREA_CONFIG_CACHE.get(lineId, () -> { Map<AlertDeviceType, List<AreaConfigVO>> configList = AREA_CONFIG_CACHE.get(lineId, () -> {
List<DeviceAreaConfig> configs = deviceAreaConfigRepository.list(Wrappers.lambdaQuery(DeviceAreaConfig.class).eq(DeviceAreaConfig::getLineId, lineId)); List<DeviceAreaConfig> configs = deviceAreaConfigRepository.list(Wrappers.lambdaQuery(DeviceAreaConfig.class).eq(DeviceAreaConfig::getLineId, lineId));
List<AreaConfigVO> listVO = configs.stream().map(AreaConfigVO::new).toList(); List<AreaConfigVO> listVO = configs.stream().map(AreaConfigVO::new).toList();
return listVO.stream().collect(Collectors.groupingBy(AreaConfigVO::getDeviceType)); return listVO.stream().collect(Collectors.groupingBy(AreaConfigVO::getDeviceType));
}); });
return configList.get(dt); return configList.get(dt);
} catch (ExecutionException e) { } catch (ExecutionException e) {
return Collections.emptyList(); return Collections.emptyList();
} }
}
private String findDevice(Integer lineId, List<String> deviceIds) {
List<String> 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<DeviceAreaConfig> queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class);
queryWrapper.eq(DeviceAreaConfig::getDeviceType, dto.getDeviceType());
queryWrapper.ne(isUpdate, DeviceAreaConfig::getId, dto.getId());
List<String> 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<String> deviceIds) { List<DeviceAreaConfig> areaConfigList = this.deviceAreaConfigRepository.list(queryWrapper);
List<String> errorDevices = Lists.newArrayList();
for (String deviceId : deviceIds) { for (DeviceAreaConfig areaConfig : areaConfigList) {
MessageOrBuilder msgBuild = LineGraphicDataRepository.getDeviceByCodeNotException(lineId, deviceId); List<String> dbDatas = Splitter.on(StringPool.COMMA).splitToList(areaConfig.getData());
if (Objects.nonNull(msgBuild)) { List<String> saveDatas = Lists.newArrayList(dto.getData());
String deviceCode = DeviceStatusDataOperate.findFieldVal(msgBuild, "code", String.class); saveDatas.retainAll(dbDatas);
errorDevices.add(deviceCode); BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isEmpty(saveDatas),
} else { String.format("添加重复的设备区域[%s] 设备:%s", areaConfig.getAreaName(), this.findDevice(areaConfig.getLineId(), saveDatas)));
errorDevices.add(String.format("id[%s]未找到设备", deviceId)); }
} }
}
return Joiner.on(StringPool.COMMA).join(errorDevices);
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<DeviceAreaConfig> 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<String> datas = Splitter.on(StringPool.COMMA).splitToList(dto.getData());
deviceAreaConfig.setData(datas);
deviceAreaConfig.setDeviceType(dto.getDeviceType());
if (StringUtils.isNotEmpty(dto.getAlertTypes())) {
List<String> ll = Splitter.on(StringPool.COMMA).splitToList(dto.getAlertTypes());
deviceAreaConfig.setAlertTypes(ll);
}
return deviceAreaConfig;
}
public void bindAlertType(Long id, List<String> alertTypes) {
BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(alertTypes), "绑定数据类型不能为空");
DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id);
BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据");
LambdaUpdateWrapper<DeviceAreaConfig> 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<DeviceAreaConfigPageDto> page(DeviceAreaConfigQueryDto queryDTO, Integer lineId) {
LambdaQueryWrapper<DeviceAreaConfig> 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<DeviceAreaConfig> pageConfig = deviceAreaConfigRepository.page(queryDTO, queryWrapper);
return this.convertPage(pageConfig);
}
private void checkDataForSql(DeviceAreaConfigDto dto, boolean isUpdate) { private Page<DeviceAreaConfigPageDto> convertPage(Page<DeviceAreaConfig> pageConfig) {
LambdaQueryWrapper<DeviceAreaConfig> queryWrapper = Wrappers.lambdaQuery(DeviceAreaConfig.class);
queryWrapper.eq(DeviceAreaConfig::getDeviceType, dto.getDeviceType());
queryWrapper.ne(isUpdate, DeviceAreaConfig::getId, dto.getId());
List<String> 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<DeviceAreaConfig> areaConfigList = this.deviceAreaConfigRepository.list(queryWrapper);
for (DeviceAreaConfig areaConfig : areaConfigList) {
List<String> dbDatas = Splitter.on(StringPool.COMMA).splitToList(areaConfig.getData());
List<String> 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<DeviceAreaConfig> 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<String> datas = Splitter.on(StringPool.COMMA).splitToList(dto.getData());
deviceAreaConfig.setData(datas);
deviceAreaConfig.setDeviceType(dto.getDeviceType());
if (StringUtils.isNotEmpty(dto.getAlertTypes())) {
List<String> ll = Splitter.on(StringPool.COMMA).splitToList(dto.getAlertTypes());
deviceAreaConfig.setAlertTypes(ll);
}
return deviceAreaConfig;
}
public void bindAlertType(Long id, List<String> alertTypes) {
BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(alertTypes), "绑定数据类型不能为空");
DeviceAreaConfig areaConfig = this.deviceAreaConfigRepository.getById(id);
BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(areaConfig), "未找到对应的数据");
LambdaUpdateWrapper<DeviceAreaConfig> 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<DeviceAreaConfigPageDto> page(DeviceAreaConfigQueryDto queryDTO, Integer lineId) {
LambdaQueryWrapper<DeviceAreaConfig> 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<DeviceAreaConfig> pageConfig = deviceAreaConfigRepository.page(queryDTO, queryWrapper);
return this.convertPage(pageConfig);
}
private Page<DeviceAreaConfigPageDto> convertPage(Page<DeviceAreaConfig> pageConfig) {
// List<Long> areaConfigIds = pageConfig.getRecords().stream().map(DeviceAreaConfig::getId).toList(); // List<Long> areaConfigIds = pageConfig.getRecords().stream().map(DeviceAreaConfig::getId).toList();
List<DeviceAreaConfigPageDto> pageList = Lists.newArrayListWithCapacity(pageConfig.getRecords().size()); List<DeviceAreaConfigPageDto> pageList = Lists.newArrayListWithCapacity(pageConfig.getRecords().size());
// Map<Long, String> areaIdMapTipIds = iAlertTipRepository.findBindAreaConfigId(areaConfigIds); // Map<Long, String> areaIdMapTipIds = iAlertTipRepository.findBindAreaConfigId(areaConfigIds);
for (DeviceAreaConfig ac : pageConfig.getRecords()) { for (DeviceAreaConfig ac : pageConfig.getRecords()) {
pageList.add(new DeviceAreaConfigPageDto(ac)); pageList.add(new DeviceAreaConfigPageDto(ac));
}
Page<DeviceAreaConfigPageDto> dtoPage = Page.of(pageConfig.getCurrent(), pageConfig.getSize(), pageConfig.getTotal());
dtoPage.setRecords(pageList);
return dtoPage;
} }
Page<DeviceAreaConfigPageDto> dtoPage = Page.of(pageConfig.getCurrent(), pageConfig.getSize(), pageConfig.getTotal());
dtoPage.setRecords(pageList);
return dtoPage;
}
public List<AreaFinder> findAlertType(Integer lineId, String alertType) { public List<AreaFinder> findAlertType(Integer lineId, String alertType) {
LambdaQueryWrapper<DeviceAreaConfig> query = Wrappers.lambdaQuery(DeviceAreaConfig.class); LambdaQueryWrapper<DeviceAreaConfig> query = Wrappers.lambdaQuery(DeviceAreaConfig.class);
query.select(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName); query.select(DeviceAreaConfig::getId, DeviceAreaConfig::getAreaName);
query.eq(DeviceAreaConfig::getLineId, lineId); query.eq(DeviceAreaConfig::getLineId, lineId);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(String.format(" and find_in_set('%s',%s) > 0", alertType, DeviceAreaConfig.ALERT_TYPES)); 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())) { /*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)); 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())) { } 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())) { } 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)); 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()) 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.AXLE_LED_ORANGE_INTERLOCK_AREA.name())
|| StringUtils.equals(alertType, AlertType.SWITCH_LOST_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)); 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();
} }
query.last(sb.toString());
return this.deviceAreaConfigRepository.list(query).stream().map(AreaFinder::new).toList();
}
public void delete(Long id) { public void delete(Long id) {
deviceAreaConfigRepository.removeById(id); deviceAreaConfigRepository.removeById(id);
} }
@Data @Data
public static class AreaFinder { public static class AreaFinder {
private Long id; private Long id;
private String areaName; private String areaName;
public AreaFinder(DeviceAreaConfig areaConfig) { public AreaFinder(DeviceAreaConfig areaConfig) {
this.id = areaConfig.getId(); this.id = areaConfig.getId();
this.areaName = areaConfig.getAreaName(); this.areaName = areaConfig.getAreaName();
}
} }
}
} }