代码调整

This commit is contained in:
tiger_zhou 2023-09-12 16:46:45 +08:00
parent becec2764c
commit d938b084fc
2 changed files with 62 additions and 14 deletions

View File

@ -1,48 +1,92 @@
package club.joylink.xiannccda.service;
import club.joylink.xiannccda.alert.NccAlertInfo;
import club.joylink.xiannccda.alert.core.AlertDeviceType;
import club.joylink.xiannccda.dto.record.AlertRecordQueryDTO;
import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType;
import club.joylink.xiannccda.dto.record.AlertRecordReportDTO;
import club.joylink.xiannccda.dto.record.AlertRecordReportResponseDTO;
import club.joylink.xiannccda.entity.AlertRecord;
import club.joylink.xiannccda.entity.AlertTip;
import club.joylink.xiannccda.entity.DeviceAreaConfig;
import club.joylink.xiannccda.exception.BusinessExceptionAssertEnum;
import club.joylink.xiannccda.repository.IAlertRecordRepository;
import club.joylink.xiannccda.repository.impl.AlertRecordRepository;
import club.joylink.xiannccda.repository.impl.AlertTipRepository;
import club.joylink.xiannccda.service.config.DeviceAreaConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class AlertRecordService {
private final AlertRecordRepository alertRecordRepository;
private final AlertTipRepository alertTipRepository;
private final DeviceAreaConfigService areaConfigService;
public AlertRecordService(AlertRecordRepository alertRecordRepository, AlertTipRepository alertTipRepository) {
public AlertRecordService(AlertRecordRepository alertRecordRepository, AlertTipRepository alertTipRepository, DeviceAreaConfigService areaConfigService) {
this.alertRecordRepository = alertRecordRepository;
this.alertTipRepository = alertTipRepository;
this.areaConfigService = areaConfigService;
}
public AlertTip confirm(Long recordId, String tipType, Long locationId) {
private Long findAreaConfig(AlertRecord ar, String tipType) {
String deviceId = ar.getAlertDeviceId();
if (StringUtils.isEmpty(deviceId)) {
log.info("告警数据确认数据中未找到对应的设备id线路[{}] 记录id[{}] ", ar.getLineId(), ar.getId());
return null;
}
AlertType alertType = AlertType.valueOf(tipType);
switch (alertType) {
case AXLE_LED_RED, AXLE_LED_ORANGE, SWITCH_All_LOST, SWITCH_DW_LOST, SWITCH_FW_LOST -> {
List<DeviceAreaConfig> areaConfigList = this.areaConfigService.getCache(ar.getLineId(), AlertDeviceType.valueOf(ar.getAlertDeviceType()));
List<DeviceAreaConfig> findList = areaConfigList.stream().filter(d -> StringUtils.contains(d.getAlertTypes(), tipType)).filter(d -> {
String[] datas = d.getData().split(",");
return Arrays.binarySearch(datas, deviceId) >= 0;
}).toList();
String findIds = findList.stream().map(DeviceAreaConfig::getId).map(Object::toString).collect(Collectors.joining(","));
log.info("告警数据确认线路[{}] 记录id[{}] 查找区数[{}] 区域id[{}]", ar.getLineId(), ar.getId(), findList.size(), findIds);
if (CollectionUtils.isEmpty(findList)) {
return null;
}
return findList.get(0).getId();
}
default -> {
return null;
}
}
}
public AlertTip confirm(Long recordId, String tipType, Long areaConfigId) {
AlertRecord ar = this.alertRecordRepository.getById(recordId);
Long newAreaConfigId = this.findAreaConfig(ar, tipType);
if (Objects.isNull(newAreaConfigId)) {
newAreaConfigId = areaConfigId;
}
LambdaQueryWrapper<AlertTip> qw = Wrappers.lambdaQuery(AlertTip.class).eq(AlertTip::getAlertType, tipType);
if (Objects.nonNull(locationId) && locationId > 0L) {
qw.eq(AlertTip::getAreaConfigId, locationId);
if (Objects.nonNull(newAreaConfigId) && newAreaConfigId > 0L) {
qw.eq(AlertTip::getAreaConfigId, newAreaConfigId);
}
AlertTip tip = alertTipRepository.getOne(qw, false);
BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(tip), "未找到对应的决策辅助信息");
this.alertRecordRepository.update(Wrappers.lambdaUpdate(AlertRecord.class)
.set(AlertRecord::getAlertTipId, tip.getId()).set(AlertRecord::getAlarmStatus, 1)
.set(Objects.nonNull(newAreaConfigId), AlertRecord::getAlertLocationId, newAreaConfigId)
.eq(AlertRecord::getId, recordId).isNull(AlertRecord::getAlarmStatus));
return tip;
}
@ -57,9 +101,13 @@ public class AlertRecordService {
private List<NccAlertInfo> changeData(List<AlertRecord> ars) {
List<NccAlertInfo> alertInfos = Lists.newArrayList();
for (AlertRecord record : ars) {
AlertDeviceType deviceType = null;
if (StringUtils.isNotEmpty(record.getAlertDeviceType())) {
deviceType = AlertDeviceType.valueOf(record.getAlertDeviceType());
}
NccAlertInfo nccAlertInfo = new NccAlertInfo(record.getAlertTime(),
AlertType.valueOf(record.getAlertType()), Objects.isNull(record.getAlertTipId()) ? null : record.getAlertTipId().intValue(), record.getLineId(),
record.getAlertObject(), "", record.getAlertLocationId());
record.getAlertObject(), "", record.getAlertLocationId(), deviceType);
nccAlertInfo.setId(record.getId());
nccAlertInfo.setAlarmStatus(record.getAlarmStatus());
alertInfos.add(nccAlertInfo);
@ -78,9 +126,9 @@ public class AlertRecordService {
}
public List<AlertRecordReportResponseDTO> report(Integer lineId, AlertRecordReportDTO reportDTO) {
if (Objects.nonNull(reportDTO.getBeginDateTime()) && Objects.nonNull(reportDTO.getEndDateTime())) {
/* if (Objects.nonNull(reportDTO.getBeginDateTime()) && Objects.nonNull(reportDTO.getEndDateTime())) {
BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertTrue(reportDTO.getEndDateTime().isAfter(reportDTO.getBeginDateTime()), "开始时间不能大于结束时间");
}
}*/
return this.alertRecordRepository.getBaseMapper().report(reportDTO, lineId);
}

View File

@ -7,17 +7,17 @@
from alert_record
where line_id = #{lineId}
<if test="alertTypes != null and alertTypes.size() > 0">
<foreach collection="alertTypes" open=" and alert_type in (" separator="," close=")" item="t">
<if test="dto.alertTypes != null and dto.alertTypes.size() > 0">
<foreach collection="dto.alertTypes" open=" and alert_type in (" separator="," close=")" item="t">
#{t}
</foreach>
</if>
<choose>
<when test="beginDateTime != null">
and alert_time <![CDATA[>=]]> #{beginDateTime}
<when test="dto.beginDateTime != null">
and alert_time <![CDATA[>=]]> #{dto.beginDateTime}
</when>
<when test="endDateTime != null">
and alert_time <![CDATA[<=]]> #{endDateTime}
<when test="dto.endDateTime != null">
and alert_time <![CDATA[<=]]> #{dto.endDateTime}
</when>
</choose>
GROUP BY alert_type