package com.yssh.service.impl; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import com.yssh.dao.*; import com.yssh.entity.*; 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.vo.WarningVo; import com.yssh.service.IAsyncService; import com.yssh.service.ICommonService; import com.yssh.service.IWarningAnalyseService; import com.yssh.utils.CalculateUtils; import com.yssh.utils.DateUtils; import com.yssh.utils.StringUtils; import javax.annotation.Resource; @Service public class WarningAnalyseServiceImpl implements IWarningAnalyseService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private WarningDetailMapper warningDetailMapper; @Resource private ICommonService commonService; @Resource private SuYuanMapper suYuanMapper; @Resource private DictRecordMapper dictRecordMapper; @Resource private IAsyncService asyncService; @Resource private QxshMapper qxshMapper; private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%"); private SimpleDateFormat ymdh = new SimpleDateFormat("yyyyMMddHH"); private SimpleDateFormat ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override @Async("threadPoolTaskExecutor") public void insertWarningDetails(List warning) throws Exception { //插入数据 List> list = Lists.partition(warning, IAsyncService.BATCH_INSERT_NUMBER); CountDownLatch countDownLatch = new CountDownLatch(list.size()); for (List corpReserveList : list) { asyncService.executeAsync("", corpReserveList, warningDetailMapper, countDownLatch); } countDownLatch.await(); } @Override 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 < 3; i++) { calendar.add(Calendar.HOUR, -1); List rs = alarmAnalyseOperation(calendar.getTime()); if (null != rs && rs.size() > 0) { list.addAll(rs); } } list.forEach(s -> { result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue())); }); return result; } private Calendar getCalendar(Date nowDate) { Calendar calendar = Calendar.getInstance(); calendar.setTime(nowDate); calendar.add(Calendar.HOUR, 1); return calendar; } @Override 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 < 3; i++) { calendar.add(Calendar.HOUR, -1); List rs = warningAnalyseOperation(calendar.getTime()); if (null != rs && rs.size() > 0) { list.addAll(rs); } } list.forEach(s -> { result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue())); }); return result; } public List alarmAnalyseOperation(Date date) { List result = new ArrayList<>(); String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids = CalculateUtils.assembleId(commonService.getCheckPoints3d()); if (ids.size() == 0) { return result; } DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (null == dictRecord) { return result; } String tableName = dictRecord.getTableName(); if (suYuanMapper.isTableExists(tableName) == 0) { return result; } List list = suYuanMapper.getAlarmsAnalyse(tableName, ids); if (null != list && list.size() > 0) { for (SuYuan2d s : list) { String locationName = commonService.select3dCheckPointById(s.getId()).getName(); result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 0, s.getValue())); } } return result; } public List warningAnalyseOperation(Date date) { List result = new ArrayList<>(); String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date); List ids = CalculateUtils.assembleId(commonService.getCheckPoints3d()); if (ids.size() == 0) { return result; } DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time)); if (null == dictRecord) { return result; } String tableName = dictRecord.getTableName(); if (suYuanMapper.isTableExists(tableName) == 0) { return result; } List list = suYuanMapper.getWarningAnalyse(tableName, ids); if (null != list && list.size() > 0) { for (SuYuan2d s : list) { String locationName = commonService.select3dCheckPointById(s.getId()).getName(); result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 1, s.getValue())); } } return result; } @Override public void warningOperationStorage(Date date) { List allData = new ArrayList<>(); List alarms = this.alarmAnalyseOperation(date); if (StringUtils.isNotEmpty(alarms)) { allData.addAll(alarms); } List warnings = this.warningAnalyseOperation(date); if (StringUtils.isNotEmpty(warnings)) { allData.addAll(warnings); } if (StringUtils.isNotEmpty(allData)) { try { insertWarningDetails(allData); } catch (Exception e) { logger.error("批量插入告警数据出现异常!!!", e); e.printStackTrace(); } } } @Override public Map countThisMonthAlarmAndWarning() { Map result = new HashMap<>(); Map param = new HashMap<>(); param.put("startTime", ymdhms.format(DateUtils.getMonthStart())); param.put("endTime", ymdhms.format(DateUtils.getMonthEnd())); param.put("type", 0); List alarms = warningDetailMapper.selectWarningDetailByMap(param); if (StringUtils.isNull(alarms)) { alarms = new ArrayList<>(); } result.put("alarmNumber", alarms.size()); param.put("type", 1); List warnings = warningDetailMapper.selectWarningDetailByMap(param); if (StringUtils.isNull(warnings)) { warnings = new ArrayList<>(); } result.put("warningNumber", warnings.size()); return result; } @Override public Map>> countEverydayAlarmAndWarning() { Map>> result = new HashMap<>(); Map param = new HashMap(); Date now = new Date(); Date last = DateUtils.getAPeriodOfTime(now, -7, Calendar.DATE); // 上周的今天 param.put("startTime", ymdhms.format(last)); param.put("endTime", ymdhms.format(now)); param.put("type", 0); List> alarmDayCount = warningDetailMapper.selectWarningDayCountByMap(param); result.put("alarmDayCount", alarmDayCount); param.put("type", 1); List> warningDayCount = warningDetailMapper.selectWarningDayCountByMap(param); result.put("warningDayCount", warningDayCount); return result; } @Override public Map> selectEachLocationDataChange() { /*Map> result = new LinkedHashMap<>(); List checkPoints = commonService.getCheckPoints3d(); Date nowDate = DateUtils.getNowDate(); Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate)); Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getAPeriodOfTime(nowDate, -3, Calendar.HOUR_OF_DAY))); List recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime); checkPoints.forEach(c -> { List tableNames = new ArrayList<>(); recordList.forEach(r -> { tableNames.add(r.getTableName()); }); List values = new ArrayList<>(); if (StringUtils.isNotNull(tableNames)) { List data = suYuanMapper.getMonitorData(tableNames, c.getId()); data.forEach(v -> { values.add(v.getValue()); }); result.put(c.getName(), values); } }); return result;*/ Map> result = new LinkedHashMap<>(); List checkPoints = commonService.getCheckPoints3d(); Date nowDate = DateUtils.getNowDate(); Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate)); Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getAPeriodOfTime(nowDate, -3, Calendar.HOUR_OF_DAY))); List recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime); List tableNames = new ArrayList<>(); for (DictRecord dr : recordList) { if (suYuanMapper.isTableExists(dr.getTableName()) > 0) { tableNames.add(dr.getTableName()); } } if (tableNames.isEmpty()) { return result; } for (MonitorPointPosition c : checkPoints) { List values = new ArrayList<>(); List data = suYuanMapper.getMonitorData(tableNames, c.getId()); if (null != data && data.size() > 0) { for (SuYuanMonitorData v : data) { values.add(v.getValue()); } } result.put(c.getName(), values); } return result; } @Override public List> selectThisMonthLocationValueDataTop10() { /*List> result = new ArrayList<>(); List checkPoints = commonService.getCheckPoints3d(); Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthStart())); Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthEnd())); List recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime); checkPoints.forEach(c -> { List tableNames = new ArrayList<>(); recordList.forEach(r -> { tableNames.add(r.getTableName()); }); Map map = suYuanMapper.getMonthValueDataMax(tableNames, c.getId()); if (StringUtils.isNotNull(map) && StringUtils.isNotEmpty(map)) { String suYuanId = map.get("id").toString(); map.put("name", commonService.select3dCheckPointById(suYuanId).getName()); map.remove("id"); } result.add(map); }); return CalculateUtils.sort(result, "value", true).subList(0, 10);*/ List> result = new ArrayList<>(); List checkPoints = commonService.getCheckPoints3d(); Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthStart())); Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthEnd())); //startTime = 2023040100L; //endTime = 2023043023L; List recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime); List tableNames = new ArrayList<>(); for (DictRecord dr : recordList) { if (suYuanMapper.isTableExists(dr.getTableName()) > 0) { tableNames.add(dr.getTableName()); } } if (tableNames.isEmpty()) { return result; } /*List idList = new ArrayList<>(); for (MonitorPointPosition c : checkPoints) { idList.add("'" + c.getId() + "'"); } String ids = String.join(",", idList);*/ for (MonitorPointPosition c : checkPoints) { Map map = suYuanMapper.getMonthValueDataMax(tableNames, c.getId()); if (null != map && map.size() > 0) { //String suYuanId = map.get("id").toString(); //map.put("name", commonService.select3dCheckPointById(suYuanId).getName()); //map.remove("id"); map.put("name", c.getName()); result.add(map); } } return CalculateUtils.sort(result, "value", true).subList(0, 10); } @Override public List selectMonthTop10() { String time = ym.format(new Date()); List list = qxshMapper.selectMonthTop10(time); return list; } @Override public Map> select3Hours() { Map> map = new LinkedHashMap<>(); List times = DateUtils.get3Hours(); List list = qxshMapper.select3Hours(times); if (null == list || list.isEmpty()) { return map; } for (Qxsh qxsh : list) { String name = qxsh.getName(); List values = map.computeIfAbsent(name, k -> new ArrayList<>()); values.add(qxsh.getValue()); } return map; } @Override public List getAlarmAndWarnByTime(Date begin, Date end) { List list = new ArrayList<>(); List ids = CalculateUtils.assembleId(commonService.getCheckPoints3d()); if (ids.size() == 0) { return list; } List dictList = dictRecordMapper.selectByTime(ymdhms.format(begin), ymdhms.format(end)); if (null == dictList || dictList.isEmpty()) { return list; } List tables = new ArrayList<>(); for (DictRecord dr : dictList) { if (suYuanMapper.isTableExists(dr.getTableName()) > 0) { tables.add(dr.getTableName()); } } if (tables.size() == 0) { return list; } for (String tab : tables) { List alarmList = getAlarmByTime(tab, ids); for (WarningDetail s : alarmList) { list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue())); } } for (String tab : tables) { List warnList = getWarnByTime(tab, ids); for (WarningDetail s : warnList) { list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue())); } } return list; } private List getAlarmByTime(String tableName, List ids) { List result = new ArrayList<>(); List list = suYuanMapper.getAlarmsAnalyse(tableName, ids); if (null != list && list.size() > 0) { Date date = getDateByTabName(tableName); for (SuYuan2d s : list) { String locationName = commonService.select3dCheckPointById(s.getId()).getName(); result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 0, s.getValue())); } } return result; } private List getWarnByTime(String tableName, List ids) { List result = new ArrayList<>(); List list = suYuanMapper.getWarningAnalyse(tableName, ids); if (null != list && list.size() > 0) { Date date = getDateByTabName(tableName); for (SuYuan2d s : list) { String locationName = commonService.select3dCheckPointById(s.getId()).getName(); result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 1, s.getValue())); } } return result; } private Date getDateByTabName(String tab) { try { return ymdh.parse(tab.replace("su_yuan_", "")); } catch (Exception e) { return new Date(); } } }