whyclxw
7 天以前 cfdb274a6e9303f78315b21c2dc66c2a2839fb39
src/main/java/com/whyc/service/BattRealdataIdService.java
@@ -18,6 +18,8 @@
import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
@@ -164,18 +166,24 @@
    public Response getHalfHourBattDevData(Integer battgroupId,Integer granularity) {
        String dateTime = ActionUtil.sdfwithOutday.format(new Date());
        String tableName ="db_data_history.tb_batt_realdata_"+battgroupId+"_"+dateTime;
        //获取前半个小时数据
        LocalDateTime now = LocalDateTime.now();
        // 计算半小时前的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(30);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String halfHourAgoTime=halfHourAgo.format(formatter);
        String existTableName = commonMapper.existTable("db_data_history", "tb_batt_realdata_"+battgroupId+"_"+dateTime);
        if(existTableName == null){
            return new Response().set(1,false,"当前电池组不存在前面小时数据");
        }
        List<BattHisRealDto> datalist=subTablePageInfoService.getHalfHourBattDevData(tableName,granularity);
        List<BattHisRealDto> datalist=subTablePageInfoService.getHalfHourBattDevData(tableName,granularity,halfHourAgoTime);
        return new Response().setII(1,datalist.size()>0,datalist,"获取半小时内核容设备信息");
    }
    //历史实时数据
    public Response getBattRealDataHis(Integer battgroupId, Integer granularity,String startTime,String endTime) throws ParseException, InterruptedException {
        List<RealDateDTO> dataList = new LinkedList<>();
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(ThreadLocalUtil.parse(startTime,1), ThreadLocalUtil.parse(endTime,1));
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
@@ -191,7 +199,45 @@
                int tableNum = subTablePageInfoService.judgeTable_realdata(table);
                List<RealDateDTO> list = new ArrayList();
                if (tableNum > 0) {
                    list = subTablePageInfoService.serchByCondition2(realdata,granularity);
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getMaxAndMinRecordNum(realdata);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list = subTablePageInfoService.getBattRealDataHis(realdata,granularity,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(RealDateDTO::getRecordTime)).collect(Collectors.toList());
        return new Response().set(1, dataListSorted);
    }
    //电池告警点击具体告警信息查看从告警开始时间到现在的历史实时数据
    public Response getBattHisRealInAlm(Integer battgroupId, String startTime, Integer almId) throws ParseException, InterruptedException {
        List<RealDateDTO> dataList = new LinkedList<>();
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(ThreadLocalUtil.parse(startTime,1),new Date());
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            pool.execute(() -> {
                int finalII = finalI;
                BattRealdataId realdata = new BattRealdataId();
                realdata.setRecordTime(monthTimeList.get(finalII).get(0));
                realdata.setRecordTime1(monthTimeList.get(finalII).get(1));
                String table = battgroupId + "_" + ThreadLocalUtil.format(realdata.getRecordTime(),2);
                realdata.setTableName(table);//表名时间格式部分
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_realdata(table);
                List<RealDateDTO> list = new ArrayList();
                if (tableNum > 0) {
                    list = subTablePageInfoService.getBattHisRealInAlm(realdata);
                }
                dataList.addAll(list);
                latch.countDown();