package com.yssh.service; import java.util.*; import java.util.concurrent.CountDownLatch; import com.yssh.config.InitConfig; 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.CacheUtils; 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 filterMap = new HashMap<>(); @Async("threadPoolTaskExecutor") public void insertWarningDetails(List warning) throws Exception { // 插入数据 List> list = Lists.partition(warning, AsyncService.BATCH_INSERT_NUMBER); CountDownLatch countDownLatch = new CountDownLatch(list.size()); for (List corpReserveList : list) { asyncService.executeAsync("", corpReserveList, warningDetailMapper, countDownLatch); } countDownLatch.await(); } /** * 获取实时报警 */ public List getRunTimeAlarmAnalyse(Integer start, Integer end) { //List list = qxshMapper.selectAlarmByBeginAndEnd(start, end); String key = "qxshMapper.selectAlarmByBeginAndEnd." + start + "." + end; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.selectAlarmByBeginAndEnd(start, end); copeHasSuYuan(list); CacheUtils.putListByKey(key, list); } return list; } /** * 获取实时预警 */ public List getRunTimeWarningAnalyse(Integer start, Integer end) { //List list = qxshMapper.selectWarnByBeginAndEnd(start, end); String key = "qxshMapper.selectWarnByBeginAndEnd." + start + "." + end; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.selectWarnByBeginAndEnd(start, end); copeHasSuYuan(list); CacheUtils.putListByKey(key, list); } return list; } /** * 本月预警报警统计 */ public Map 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)); String key = "WAS.monthCount." + start + "." + end; Object obj = CacheUtils.get(key); if (obj instanceof Map) { return (Map) obj; } Map 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); CacheUtils.put(key, result); return result; } /** * 每日预警、报警数量变化趋势 */ public Map>> 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>> result = new HashMap<>(); //List> warnList = qxshMapper.count7DayForWarn(start, end); String key = "qxshMapper.count7DayForWarn." + start + "." + end; List> warnList = CacheUtils.getListByKey(key); if (null == warnList) { warnList = qxshMapper.count7DayForWarn(start, end); CacheUtils.putListByKey(key, warnList); } result.put("warningDayCount", warnList); //List> alarmList = qxshMapper.count7DayForAlarm(start, end); String key2 = "qxshMapper.count7DayForAlarm." + start + "." + end; List> alarmList = CacheUtils.getListByKey(key2); if (null == alarmList) { alarmList = qxshMapper.count7DayForAlarm(start, end); CacheUtils.putListByKey(key2, alarmList); } 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 times = DateUtils.get3Hours(); //List list = qxshMapper.select3Hours(start, end); //if (null == list || list.isEmpty()) { // return null; //} String key = "qxshMapper.select3Hours." + start + "." + end; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.select3Hours(start, end); CacheUtils.putListByKey(key, list); } /* Map> map = new LinkedHashMap<>(); for (Qxsh qxsh : list) { String name = qxsh.getName(); List values = map.computeIfAbsent(name, k -> new ArrayList<>()); values.add(qxsh.getValue()); }*/ return list; } /** * 获取本月监测站点最大值TOP10 */ public List selectMonthTop10() { String time = DateUtils.getYyyyMm(InitConfig.getDate()); //List list = qxshMapper.selectMonthTop10(time + "%"); String key = "qxshMapper.selectMonthTop10." + time; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.selectMonthTop10(time + "%"); CacheUtils.putListByKey(key, list); } return list; } /** * 获取本周监测站点最大值TOP10 */ public List selectWeekTop10() { Date now = InitConfig.getDate(); //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); Calendar cal = Calendar.getInstance(); cal.setTime(now); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); int diff = Calendar.MONDAY - dayOfWeek; if (diff > 0) diff -= 7; cal.add(Calendar.DAY_OF_MONTH, diff); Date mon = cal.getTime(); cal.add(Calendar.DAY_OF_MONTH, 6); Date sun = cal.getTime(); Integer intMon = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, mon) + "00"); // 2023080700 Integer intSun = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, sun) + "23"); // 2023081323 //List list = qxshMapper.selectWeekTop10(intMon, intSun); String key = "qxshMapper.selectWeekTop10." + intMon + "." + intSun; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.selectWeekTop10(intMon, intSun); CacheUtils.putListByKey(key, list); } return list; } /** * 获取本日监测站点最大值TOP10 */ public List selectDayTop10() { String time = DateUtils.getYyyyMmDd(InitConfig.getDate()); //List list = qxshMapper.selectDayTop10(time + "%"); String key = "qxshMapper.selectDayTop10." + time; List list = CacheUtils.getListByKey(key); if (null == list) { list = qxshMapper.selectDayTop10(time + "%"); CacheUtils.putListByKey(key, list); } return list; } /** * 根据时间获取报警和预警信息 */ public List 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 rsList = qxshMapper.selectForReport(start, end); if (null == rsList || rsList.isEmpty()) return null; Hashtable dict = getExistTabDict(rsList); List 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); if (null == suYuan) { String[] strs = suYuanId.split("_"); suYuan = new DistanceSuYuan(null, null, 0.0, suYuanId, Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 0, 0, null, 0.0, 0.0, 0.0, 0.0); } //List 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); if (null == suMax) { String[] strs = suYuanId.split("_"); suMax = new DistanceSuYuan(null, null, 0.0, suYuanId, Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 0, 0, null, 0.0, 0.0, 0.0, 0.0); } 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 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 getExistTabDict(List rsList) { Hashtable 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 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 allData = new ArrayList<>(); List alarms = this.getAlarmWarnAnalyse(date, false); if (StringUtils.isNotEmpty(alarms)) { allData.addAll(alarms); } List 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 getAlarmWarnAnalyse(Date date, boolean isWarn) { List 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 list = isWarn ? suYuanMapper.getWarningAnalyse(tableName, ids) : suYuanMapper.getAlarmsAnalyse(tableName, ids); if (null == list || list.isEmpty()) { return null; } int type = isWarn ? 1 : 0; List 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 copeHasSuYuan(List list) { if (null == list || list.isEmpty()) { return null; } Map 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; } 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"; Integer isSuYuan = qxshMapper.hasSuYuan(suYuanId, createTime); qxsh.setIsSuYuan(isSuYuan); Integer isFast = qxshMapper.countFastSuYuan(suYuanId, createTime); qxsh.setIsFast(isFast); } return list; } }