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 org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.stereotype.Service;
|
|
import com.google.common.collect.Lists;
|
import com.yssh.dao.AlertConfigMapper;
|
import com.yssh.dao.DictRecordMapper;
|
import com.yssh.dao.SuYuanMapper;
|
import com.yssh.dao.WarningDetailMapper;
|
import com.yssh.entity.DictRecord;
|
import com.yssh.entity.MonitorPointPosition;
|
import com.yssh.entity.SuYuan2d;
|
import com.yssh.entity.SuYuanMonitorData;
|
import com.yssh.entity.WarningDetail;
|
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;
|
|
@Service
|
public class WarningAnalyseServiceImpl implements IWarningAnalyseService {
|
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
@Autowired
|
private WarningDetailMapper warningDetailMapper;
|
|
@Autowired
|
private AlertConfigMapper alertConfigMapper;
|
|
@Autowired
|
private ICommonService commonService;
|
|
@Autowired
|
private SuYuanMapper suYuanMapper;
|
|
@Autowired
|
private DictRecordMapper dictRecordMapper;
|
|
@Autowired
|
private IAsyncService asyncService;
|
|
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
|
|
@Override
|
@Async("threadPoolTaskExecutor")
|
public void insertWarningDetails(List<WarningDetail> warning) throws Exception {
|
//插入数据
|
List<List<WarningDetail>> list = Lists.partition(warning, IAsyncService.BATCH_INSERT_NUMBER);
|
CountDownLatch countDownLatch = new CountDownLatch(list.size());
|
for (List<WarningDetail> corpReserveList : list) {
|
asyncService.executeAsync("", corpReserveList, warningDetailMapper, countDownLatch);
|
}
|
countDownLatch.await();
|
}
|
|
@Override
|
public List<WarningVo> getRunTimeAlarmAnalyse() {
|
List<WarningVo> 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<WarningDetail> list = alarmAnalyseOperation(nowDate);
|
|
List<WarningDetail> list = new ArrayList<>();
|
Calendar calendar = getCalendar(nowDate);
|
for (int i = 0; i < 3; i++) {
|
calendar.add(Calendar.HOUR, -1);
|
List<WarningDetail> 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<WarningVo> getRunTimeWarningAnalyse() {
|
List<WarningVo> 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<WarningDetail> list = warningAnalyseOperation(nowDate);
|
List<WarningDetail> list = new ArrayList<>();
|
Calendar calendar = getCalendar(nowDate);
|
for (int i = 0; i < 3; i++) {
|
calendar.add(Calendar.HOUR, -1);
|
List<WarningDetail> 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<WarningDetail> alarmAnalyseOperation(Date date) {
|
List<WarningDetail> result = new ArrayList<WarningDetail>();
|
String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
|
List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
|
DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
|
if (StringUtils.isNull(dictRecord)) {
|
return result;
|
}
|
String tableName = dictRecord.getTableName();
|
List<SuYuan2d> list = suYuanMapper.getAlarmsAnalyse(tableName, ids);
|
list.forEach(s -> {
|
String locationName = commonService.select3dCheckPointById(s.getId()).getName();
|
result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 0, s.getValue()));
|
});
|
return result;
|
}
|
|
public List<WarningDetail> warningAnalyseOperation(Date date) {
|
List<WarningDetail> result = new ArrayList<WarningDetail>();
|
String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
|
List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
|
DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
|
if (StringUtils.isNull(dictRecord)) {
|
return result;
|
}
|
String tableName = dictRecord.getTableName();
|
List<SuYuan2d> list = suYuanMapper.getWarningAnalyse(tableName, ids);
|
list.forEach(s -> {
|
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<WarningDetail> allData = new ArrayList<>();
|
List<WarningDetail> alarms = this.alarmAnalyseOperation(date);
|
if (StringUtils.isNotEmpty(alarms)) {
|
allData.addAll(alarms);
|
}
|
List<WarningDetail> warnings = this.warningAnalyseOperation(date);
|
if (StringUtils.isNotEmpty(warnings)) {
|
allData.addAll(warnings);
|
}
|
if (StringUtils.isNotEmpty(allData)) {
|
try {
|
insertWarningDetails(allData);
|
} catch (Exception e) {
|
logger.error("批量插入告警数据出现异常!!!");
|
e.printStackTrace();
|
}
|
}
|
}
|
|
@Override
|
public Map<String, Integer> countThisMonthAlarmAndWarning() {
|
Map<String, Integer> result = new HashMap<>();
|
Map<String, Object> param = new HashMap<String, Object>();
|
param.put("type", 0);
|
param.put("startTime", DateUtils.getMonthStart());
|
param.put("endTime", DateUtils.getMonthEnd());
|
List<WarningVo> alarms = warningDetailMapper.selectWarningDetailByMap(param);
|
if (StringUtils.isNull(alarms)) {
|
alarms = new ArrayList<>();
|
}
|
result.put("alarmNumber", alarms.size());
|
param.put("type", 1);
|
List<WarningVo> warnings = warningDetailMapper.selectWarningDetailByMap(param);
|
if (StringUtils.isNull(warnings)) {
|
warnings = new ArrayList<>();
|
}
|
result.put("warningNumber", warnings.size());
|
return result;
|
}
|
|
@Override
|
public Map<String, List<Map<String, Object>>> countEverydayAlarmAndWarning() {
|
Map<String, List<Map<String, Object>>> result = new HashMap<>();
|
Map<String, Object> param = new HashMap<String, Object>();
|
param.put("type", 0);
|
Date endTime = DateUtils.getNowDate();
|
//上周的今天
|
param.put("startTime", DateUtils.getAPeriodOfTime(endTime, -7, Calendar.DATE));
|
param.put("endTime", endTime);
|
List<Map<String, Object>> alarmDayCount = warningDetailMapper.selectWarningDayCountByMap(param);
|
result.put("alarmDayCount", alarmDayCount);
|
param.put("type", 1);
|
List<Map<String, Object>> warningDayCount = warningDetailMapper.selectWarningDayCountByMap(param);
|
result.put("warningDayCount", warningDayCount);
|
return result;
|
}
|
|
@Override
|
public Map<String, List<Double>> selectEachLocationDataChange() {
|
/*Map<String, List<Double>> result = new LinkedHashMap<>();
|
List<MonitorPointPosition> 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<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
|
checkPoints.forEach(c -> {
|
List<String> tableNames = new ArrayList<>();
|
recordList.forEach(r -> {
|
tableNames.add(r.getTableName());
|
});
|
List<Double> values = new ArrayList<>();
|
if (StringUtils.isNotNull(tableNames)) {
|
List<SuYuanMonitorData> data = suYuanMapper.getMonitorData(tableNames, c.getId());
|
data.forEach(v -> {
|
values.add(v.getValue());
|
});
|
result.put(c.getName(), values);
|
}
|
});
|
return result;*/
|
|
Map<String, List<Double>> result = new LinkedHashMap<>();
|
List<MonitorPointPosition> 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<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
|
|
List<String> 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<Double> values = new ArrayList<>();
|
List<SuYuanMonitorData> data = suYuanMapper.getMonitorData(tableNames, c.getId());
|
data.forEach(v -> {
|
values.add(v.getValue());
|
});
|
result.put(c.getName(), values);
|
}
|
return result;
|
}
|
|
@Override
|
public List<Map<String, Object>> selectThisMonthLocationValueDataTop10() {
|
List<Map<String, Object>> result = new ArrayList<>();
|
List<MonitorPointPosition> checkPoints = commonService.getCheckPoints3d();
|
Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthStart()));
|
Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthEnd()));
|
List<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
|
checkPoints.forEach(c -> {
|
List<String> tableNames = new ArrayList<>();
|
recordList.forEach(r -> {
|
tableNames.add(r.getTableName());
|
});
|
Map<String, Object> 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);
|
}
|
|
}
|