package com.yssh.service;
|
|
import java.util.*;
|
import java.util.concurrent.CountDownLatch;
|
|
import com.yssh.entity.*;
|
import com.yssh.mapper.DictRecordMapper;
|
import com.yssh.mapper.QxshMapper;
|
import com.yssh.mapper.SuYuanMapper;
|
import com.yssh.mapper.WarningDetailMapper;
|
import com.yssh.utils.CalculateUtils;
|
import com.yssh.utils.DateUtils;
|
import com.yssh.utils.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.stereotype.Service;
|
|
import com.google.common.collect.Lists;
|
|
import javax.annotation.Resource;
|
|
@Service
|
public class WarningAnalyseService {
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
@Resource
|
WarningDetailMapper warningDetailMapper;
|
|
@Resource
|
CommonService commonService;
|
|
@Resource
|
SuYuanMapper suYuanMapper;
|
|
@Resource
|
DictRecordMapper dictRecordMapper;
|
|
@Resource
|
AsyncService asyncService;
|
|
@Resource
|
QxshMapper qxshMapper;
|
|
@Resource
|
VocValsService vocValsService;
|
|
@Resource
|
SuYuanService suYuanService;
|
|
private static HashMap<String, String> filterMap = new HashMap<>();
|
|
@Async("threadPoolTaskExecutor")
|
public void insertWarningDetails(List<WarningDetail> warning) throws Exception {
|
// 插入数据
|
List<List<WarningDetail>> list = Lists.partition(warning, AsyncService.BATCH_INSERT_NUMBER);
|
CountDownLatch countDownLatch = new CountDownLatch(list.size());
|
for (List<WarningDetail> corpReserveList : list) {
|
asyncService.executeAsync("", corpReserveList, warningDetailMapper, countDownLatch);
|
}
|
countDownLatch.await();
|
}
|
|
/**
|
* 获取实时报警
|
*/
|
public List<Qxsh> getRunTimeAlarmAnalyse(Integer start, Integer end) {
|
List<Qxsh> list = qxshMapper.selectAlarmByBeginAndEnd(start, end);
|
|
return copeHasSuYuan(list);
|
}
|
|
/**
|
* 获取实时预警
|
*/
|
public List<Qxsh> getRunTimeWarningAnalyse(Integer start, Integer end) {
|
List<Qxsh> list = qxshMapper.selectWarnByBeginAndEnd(start, end);
|
|
return copeHasSuYuan(list);
|
}
|
|
/**
|
* 本月预警报警统计
|
*/
|
public Map<String, Integer> monthCount(Date date) {
|
Date startDate = DateUtils.getMonthStart(date);
|
Date endDate = DateUtils.getMonthEnd(date);
|
Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
|
Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
|
|
Map<String, Integer> result = new HashMap<>();
|
Integer warnNum = qxshMapper.countMonthForWarn(start, end);
|
result.put("warningNumber", null == warnNum ? 0 : warnNum);
|
|
Integer alarmNum = qxshMapper.countMonthForAlarm(start, end);
|
result.put("alarmNumber", null == alarmNum ? 0 : alarmNum);
|
|
return result;
|
}
|
|
/**
|
* 每日预警、报警数量变化趋势
|
*/
|
public Map<String, List<Map<String, Object>>> everydayCount(Date endDate) {
|
Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
|
Date startDate = DateUtils.getAPeriodOfTime(endDate, -6, Calendar.DATE);
|
Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
|
|
Map<String, List<Map<String, Object>>> result = new HashMap<>();
|
List<Map<String, Object>> warnList = qxshMapper.count7DayForWarn(start, end);
|
result.put("warningDayCount", warnList);
|
|
List<Map<String, Object>> alarmList = qxshMapper.count7DayForAlarm(start, end);
|
result.put("alarmDayCount", alarmList);
|
|
return result;
|
}
|
|
/**
|
* 三小时监测站点数据变化趋势
|
*/
|
public Object select3Hours(Date endDate) {
|
Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
|
Date startDate = DateUtils.getAPeriodOfTime(endDate, -2, Calendar.HOUR_OF_DAY);
|
Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
|
|
// List<String> times = DateUtils.get3Hours();
|
List<Qxsh> list = qxshMapper.select3Hours(start, end);
|
if (null == list || list.isEmpty()) {
|
return null;
|
}
|
|
/* Map<String, List<Double>> map = new LinkedHashMap<>();
|
for (Qxsh qxsh : list) {
|
String name = qxsh.getName();
|
List<Double> values = map.computeIfAbsent(name, k -> new ArrayList<>());
|
values.add(qxsh.getValue());
|
}*/
|
|
return list;
|
}
|
|
/**
|
* 获取本月监测站点最大值TOP10
|
*/
|
public List<Qxsh> selectMonthTop10() {
|
String time = DateUtils.getYyyyMm(new Date());
|
List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
|
|
return list;
|
}
|
|
/**
|
* 获取本周监测站点最大值TOP10
|
*/
|
public List<Qxsh> selectWeekTop10() {
|
Date now = new Date();
|
int weekOfYear = DateUtils.getWeekOfYear(now);
|
int year = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYY, now));
|
Date lastSun = DateUtils.getWeekOfYearForSun(year, weekOfYear);
|
Date mon = DateUtils.getAPeriodOfTime(lastSun, 1, Calendar.DATE);
|
Date sun = DateUtils.getAPeriodOfTime(lastSun, 7, Calendar.DATE);
|
Integer intMon = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, mon) + "00"); // 2023080700
|
Integer intSun = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, sun) + "23"); // 2023081323
|
|
List<Qxsh> list = qxshMapper.selectWeekTop10(intMon, intSun);
|
|
return list;
|
}
|
|
/**
|
* 获取本日监测站点最大值TOP10
|
*/
|
public List<Qxsh> selectDayTop10() {
|
String time = DateUtils.getYyyyMmDd(new Date());
|
List<Qxsh> list = qxshMapper.selectDayTop10(time + "%");
|
|
return list;
|
}
|
|
/**
|
* 根据时间获取报警和预警信息
|
*/
|
public List<Report> getAlarmAndWarnByTime(Date beginDate, Date endDate) {
|
String strStart = DateUtils.getYyyyMmDdHh(beginDate);
|
String strEnd = DateUtils.getYyyyMmDdHh(endDate);
|
Integer start = Integer.parseInt(strStart);
|
Integer end = Integer.parseInt(strEnd);
|
|
List<Qxsh> rsList = qxshMapper.selectForReport(start, end);
|
if (null == rsList || rsList.isEmpty()) return null;
|
|
Hashtable<String, Boolean> dict = getExistTabDict(rsList);
|
List<Report> list = new ArrayList<>();
|
for (Qxsh qxsh : rsList) {
|
String tab = "su_yuan_" + qxsh.getTime();
|
if (!dict.get(tab)) continue;
|
|
MonitorPointPosition point = commonService.select3dCheckPointByName(qxsh.getName());
|
String suYuanId = point.getId().substring(0, point.getId().lastIndexOf("_") + 1) + "0";
|
Double lastVal = qxshMapper.selectLastYearVal(Integer.parseInt(qxsh.getTime()) - 1000000, qxsh.getName());
|
|
WarningDetail wd = new WarningDetail(0L, tab, suYuanId, qxsh.getName(), null, 0, qxsh.getValue());
|
DistanceSuYuan suYuan = suYuanMapper.getSuYuanById(tab, suYuanId);
|
//List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
|
//DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(tab, ids3d);
|
|
//String filter = CalculateUtils.getFilterByExtend(point, 50);
|
String filter = getFilterByPoint(point, 50);
|
DistanceSuYuan suMax = suYuanMapper.getSuYuan500MaxByFilter(tab, filter);
|
suMax.setAddr(getAddr(suMax.getId()));
|
|
Report report = Report.calcReport(wd, suYuan, suMax);
|
report.setLastVal(lastVal);
|
|
SuYuan700 su = suYuanMapper.selectSuYuan46ById(suYuanId, qxsh.getTime().substring(0, 4) + "-" + qxsh.getTime().substring(4, 6) + "-" + qxsh.getTime().substring(6, 8) + " " + qxsh.getTime().substring(8, 10) + ":00:00");
|
report.setSu(su);
|
|
list.add(report);
|
}
|
|
return list;
|
}
|
|
public static String getFilterByPoint(MonitorPointPosition point, int range) {
|
String key = String.format("%d_%d_%d", point.getX(), point.getY(), range);
|
if (filterMap.containsKey(key)) {
|
return filterMap.get(key);
|
}
|
|
List<String> ids = CalculateUtils.aloneCrosswiseExtend(point, range);
|
for (int i = 0, c = ids.size(); i < c; i++) {
|
ids.set(i, "'" + ids.get(i) + "'");
|
}
|
|
String filter = "id in (" + StringUtils.join(ids, ",") + ")";
|
if (!filterMap.containsKey(key)) {
|
filterMap.put(key, filter);
|
}
|
|
return filter;
|
}
|
|
private Hashtable<String, Boolean> getExistTabDict(List<Qxsh> rsList) {
|
Hashtable<String, Boolean> dict = new Hashtable<>();
|
for (Qxsh qxsh : rsList) {
|
String tab = "su_yuan_" + qxsh.getTime();
|
if (dict.containsKey(tab)) {
|
continue;
|
}
|
|
Boolean b = suYuanMapper.isTableExists(tab) > 0;
|
dict.put(tab, b);
|
}
|
|
return dict;
|
}
|
|
private String getAddr(String id) {
|
String[] strs = id.split("_");
|
int x = Integer.parseInt(strs[0]) / 10;
|
int y = Integer.parseInt(strs[1]) / 10;
|
|
List<VocCoords> list = vocValsService.selectCoords(x, y);
|
for (VocCoords vc : list) {
|
if (null != vc.getAddr() && vc.getAddr().length() > 0) {
|
return vc.getAddr();
|
}
|
}
|
|
return null;
|
}
|
|
/**
|
* 存储告警/预警信息
|
*/
|
public void warningOperationStorage(Date date) {
|
List<WarningDetail> allData = new ArrayList<>();
|
List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
|
if (StringUtils.isNotEmpty(alarms)) {
|
allData.addAll(alarms);
|
}
|
|
List<WarningDetail> warnings = this.getAlarmWarnAnalyse(date, true);
|
if (StringUtils.isNotEmpty(warnings)) {
|
allData.addAll(warnings);
|
}
|
|
if (StringUtils.isNotEmpty(allData)) {
|
try {
|
insertWarningDetails(allData);
|
} catch (Exception e) {
|
logger.error("批量插入告警数据出现异常!!!", e);
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/**
|
* 获取告警/预警分析
|
*/
|
private List<WarningDetail> getAlarmWarnAnalyse(Date date, boolean isWarn) {
|
List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
|
|
String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
|
DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
|
if (null == dictRecord) return null;
|
|
String tableName = dictRecord.getTableName();
|
if (suYuanMapper.isTableExists(tableName) == 0) return null;
|
|
List<SuYuan2d> list = isWarn ? suYuanMapper.getWarningAnalyse(tableName, ids) : suYuanMapper.getAlarmsAnalyse(tableName, ids);
|
if (null == list || list.isEmpty()) {
|
return null;
|
}
|
|
int type = isWarn ? 1 : 0;
|
List<WarningDetail> result = new ArrayList<>();
|
for (SuYuan2d s : list) {
|
String locationName = commonService.select3dCheckPointById(s.getId()).getName();
|
result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, type, s.getValue()));
|
}
|
|
return result;
|
}
|
|
/**
|
* 处理是否含有溯源信息
|
*/
|
private List<Qxsh> copeHasSuYuan(List<Qxsh> list) {
|
if (null == list || list.isEmpty()) {
|
return null;
|
}
|
|
Map<String, Integer> tabs = new HashMap<>();
|
for (Qxsh qxsh : list) {
|
String tab = "su_yuan_" + qxsh.getTime();
|
if (!tabs.containsKey(tab)) {
|
tabs.put(tab, suYuanMapper.isTableExists(tab));
|
}
|
|
int rows = tabs.get(tab);
|
if (0 == rows) {
|
continue;
|
}
|
|
int isSuYuan = hasSuYuan(qxsh);
|
qxsh.setIsSuYuan(isSuYuan);
|
}
|
|
return list;
|
}
|
|
/**
|
* 查询溯源
|
*/
|
private Integer hasSuYuan(Qxsh qxsh) {
|
String suYuanId = suYuanService.selectSuYuanIdByName(qxsh.getName());
|
String createTime = qxsh.getTime().substring(0, 4) + "-" + qxsh.getTime().substring(4, 6) + "-" + qxsh.getTime().substring(6, 8) + " " + qxsh.getTime().substring(8, 10) + ":00:00";
|
|
return qxshMapper.hasSuYuan(suYuanId, createTime);
|
}
|
}
|