package com.yssh.service; import java.text.SimpleDateFormat; 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 com.yssh.entity.WarningVo; import javax.annotation.Resource; @Service public class WarningAnalyseService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private WarningDetailMapper warningDetailMapper; @Resource private CommonService commonService; @Resource private SuYuanMapper suYuanMapper; @Resource private DictRecordMapper dictRecordMapper; @Resource private AsyncService asyncService; @Resource private QxshMapper qxshMapper; @Resource private VocValsService vocValsService; private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%"); @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(Date endDate) { Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate)); Date startDate = DateUtils.getAPeriodOfTime(endDate, -5, Calendar.HOUR_OF_DAY); Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate)); return qxshMapper.selectAlarmByBeginAndEnd(start, end); } /** * 获取实时预警 */ public List getRunTimeWarningAnalyse(Date endDate) { Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate)); Date startDate = DateUtils.getAPeriodOfTime(endDate, -5, Calendar.HOUR_OF_DAY); Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate)); return qxshMapper.selectWarnByBeginAndEnd(start, end); } // 已废弃 * 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; } /** * 本月预警报警统计 */ 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)); 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); 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); result.put("warningDayCount", warnList); List> 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 times = DateUtils.get3Hours(); List list = qxshMapper.select3Hours(start, end); if (null == list || list.isEmpty()) { return null; } /* 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(new Date()); List list = qxshMapper.selectMonthTop10(time + "%"); 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); List ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50); DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(tab, ids3d); 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; } 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; } }