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() { List result = new ArrayList<>(); Date nowDate = DateUtils.getNowDate(); //String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate); //DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); //if (StringUtils.isNull(dictRecord)) { // nowDate = DateUtils.getAPeriodOfTime(nowDate, -1, Calendar.HOUR_OF_DAY); //} //List list = alarmAnalyseOperation(nowDate); List list = new ArrayList<>(); Calendar calendar = getCalendar(nowDate); for (int i = 0; i < 8; i++) { calendar.add(Calendar.HOUR, -1); List rs = getAlarmWarnAnalyse(calendar.getTime(), false); if (null != rs && rs.size() > 0) { list.addAll(rs); } } list.forEach(s -> { String time = DateUtils.getYyyyMmDdHh(s.getCreateTime()); result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time)); }); return result; } private Calendar getCalendar(Date nowDate) { Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); calendar.add(Calendar.HOUR, 1); return calendar; } public List getRunTimeWarningAnalyse() { List result = new ArrayList<>(); Date nowDate = DateUtils.getNowDate(); //String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate); //DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); //if (StringUtils.isNull(dictRecord)) { // nowDate = DateUtils.getAPeriodOfTime(nowDate, -1, Calendar.HOUR_OF_DAY); //} //List list = warningAnalyseOperation(nowDate); List list = new ArrayList<>(); Calendar calendar = getCalendar(nowDate); for (int i = 0; i < 8; i++) { calendar.add(Calendar.HOUR, -1); List rs = getAlarmWarnAnalyse(calendar.getTime(), true); if (null != rs && rs.size() > 0) { list.addAll(rs); } } list.forEach(s -> { String time = DateUtils.getYyyyMmDdHh(s.getCreateTime()); result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time)); }); return result; } 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 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(); } } } public Map countThisMonthAlarmAndWarning() { Map result = new HashMap<>(); Map param = new HashMap<>(); param.put("startTime", DateUtils.getYyyyMmDdHhMmSs(DateUtils.getMonthStart())); param.put("endTime", DateUtils.getYyyyMmDdHhMmSs(DateUtils.getMonthEnd())); /*param.put("type", 0); List alarms = warningDetailMapper.selectWarningDetailByMap(param); if (StringUtils.isNull(alarms)) { alarms = new ArrayList<>(); } result.put("alarmNumber", alarms.size());*/ Integer alarmNum = warningDetailMapper.countAlarmByMap(param); result.put("alarmNumber", null == alarmNum ? 0 : alarmNum); param.put("type", 1); /*List warnings = warningDetailMapper.selectWarningDetailByMap(param); if (StringUtils.isNull(warnings)) { warnings = new ArrayList<>(); } result.put("warningNumber", warnings.size());*/ Integer warnNum = warningDetailMapper.countWarnByMap(param); result.put("warningNumber", null == warnNum ? 0 : warnNum); 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.selectByBeginAndEnd(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; } }