燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2023-11-20 8815391f1ea20fd13f77337dc184c6259eeb90b6
src/main/java/com/yssh/service/WarningAnalyseService.java
@@ -1,6 +1,5 @@
package com.yssh.service;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;
@@ -18,7 +17,6 @@
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.yssh.entity.WarningVo;
import javax.annotation.Resource;
@@ -27,31 +25,34 @@
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private WarningDetailMapper warningDetailMapper;
    WarningDetailMapper warningDetailMapper;
    @Resource
    private CommonService commonService;
    CommonService commonService;
    @Resource
    private SuYuanMapper suYuanMapper;
    SuYuanMapper suYuanMapper;
    @Resource
    private DictRecordMapper dictRecordMapper;
    DictRecordMapper dictRecordMapper;
    @Resource
    private AsyncService asyncService;
    AsyncService asyncService;
    @Resource
    private QxshMapper qxshMapper;
    QxshMapper qxshMapper;
    @Resource
    private VocValsService vocValsService;
    VocValsService vocValsService;
    private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%");
    @Resource
    SuYuanService suYuanService;
    private static HashMap<String, String> filterMap = new HashMap<>();
    @Async("threadPoolTaskExecutor")
    public void insertWarningDetails(List<WarningDetail> warning) throws Exception {
        //插入数据
        // 插入数据
        List<List<WarningDetail>> list = Lists.partition(warning, AsyncService.BATCH_INSERT_NUMBER);
        CountDownLatch countDownLatch = new CountDownLatch(list.size());
        for (List<WarningDetail> corpReserveList : list) {
@@ -60,111 +61,22 @@
        countDownLatch.await();
    }
    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);
    /**
     * 获取实时报警
     */
    public List<Qxsh> getRunTimeAlarmAnalyse(Integer start, Integer end) {
        List<Qxsh> list = qxshMapper.selectAlarmByBeginAndEnd(start, end);
        List<WarningDetail> list = new ArrayList<>();
        Calendar calendar = getCalendar(nowDate);
        for (int i = 0; i < 8; i++) {
            calendar.add(Calendar.HOUR, -1);
            List<WarningDetail> 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;
        return copeHasSuYuan(list);
    }
    private Calendar getCalendar(Date nowDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, 1);
    /**
     * 获取实时预警
     */
    public List<Qxsh> getRunTimeWarningAnalyse(Integer start, Integer end) {
        List<Qxsh> list = qxshMapper.selectWarnByBeginAndEnd(start, end);
        return calendar;
    }
    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 < 8; i++) {
            calendar.add(Calendar.HOUR, -1);
            List<WarningDetail> 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<WarningDetail> getAlarmWarnAnalyse(Date date, boolean isWarn) {
        List<String> 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<SuYuan2d> list = isWarn ? suYuanMapper.getWarningAnalyse(tableName, ids) : suYuanMapper.getAlarmsAnalyse(tableName, ids);
        if (null == list || list.isEmpty()) {
            return null;
        }
        int type = isWarn ? 1 : 0;
        List<WarningDetail> 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<WarningDetail> allData = new ArrayList<>();
        List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
        if (StringUtils.isNotEmpty(alarms)) {
            allData.addAll(alarms);
        }
        List<WarningDetail> 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();
            }
        }
        return copeHasSuYuan(list);
    }
    /**
@@ -213,7 +125,7 @@
        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
        //  List<String> times = DateUtils.get3Hours();
        List<Qxsh> list = qxshMapper.selectByBeginAndEnd(start, end);
        List<Qxsh> list = qxshMapper.select3Hours(start, end);
        if (null == list || list.isEmpty()) {
            return null;
        }
@@ -229,11 +141,39 @@
    }
    /**
     * 获取本月监测大数据站点最大值TOP10
     * 获取本月监测站点最大值TOP10
     */
    public List<Qxsh> selectMonthTop10() {
        String time = DateUtils.getYyyyMm(new Date());
        List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
        return list;
    }
    /**
     * 获取本周监测站点最大值TOP10
     */
    public List<Qxsh> selectWeekTop10() {
        Date now = new Date();
        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);
        Integer intMon = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, mon) + "00"); // 2023080700
        Integer intSun = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, sun) + "23"); // 2023081323
        List<Qxsh> list = qxshMapper.selectWeekTop10(intMon, intSun);
        return list;
    }
    /**
     * 获取本日监测站点最大值TOP10
     */
    public List<Qxsh> selectDayTop10() {
        String time = DateUtils.getYyyyMmDd(new Date());
        List<Qxsh> list = qxshMapper.selectDayTop10(time + "%");
        return list;
    }
@@ -262,8 +202,12 @@
            WarningDetail wd = new WarningDetail(0L, tab, suYuanId, qxsh.getName(), null, 0, qxsh.getValue());
            DistanceSuYuan suYuan = suYuanMapper.getSuYuanById(tab, suYuanId);
            List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
            DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(tab, ids3d);
            //List<String> 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);
            suMax.setAddr(getAddr(suMax.getId()));
            Report report = Report.calcReport(wd, suYuan, suMax);
@@ -276,6 +220,25 @@
        }
        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<String> 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<String, Boolean> getExistTabDict(List<Qxsh> rsList) {
@@ -307,4 +270,90 @@
        return null;
    }
    /**
     * 存储告警/预警信息
     */
    public void warningOperationStorage(Date date) {
        List<WarningDetail> allData = new ArrayList<>();
        List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
        if (StringUtils.isNotEmpty(alarms)) {
            allData.addAll(alarms);
        }
        List<WarningDetail> 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<WarningDetail> getAlarmWarnAnalyse(Date date, boolean isWarn) {
        List<String> 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<SuYuan2d> list = isWarn ? suYuanMapper.getWarningAnalyse(tableName, ids) : suYuanMapper.getAlarmsAnalyse(tableName, ids);
        if (null == list || list.isEmpty()) {
            return null;
        }
        int type = isWarn ? 1 : 0;
        List<WarningDetail> 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<Qxsh> copeHasSuYuan(List<Qxsh> list) {
        if (null == list || list.isEmpty()) {
            return null;
        }
        Map<String, Integer> 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;
    }
}