whycxzp
5 小时以前 e5b3e3492a7d5f079cad2ca5958a724cabfec401
src/main/java/com/whyc/service/PwrdevHistorydataIdService.java
@@ -2,12 +2,17 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.constant.PwrCapperformanceEnum;
import com.whyc.dto.AnalysisAlm.ResAnalysis;
import com.whyc.dto.Real.CompareDto;
import com.whyc.dto.Real.PwrHisRealAcInDto;
import com.whyc.dto.Real.PwrHisRealDcoutInDto;
import com.whyc.dto.Real.QuarterDto;
import com.whyc.dto.RealDataStatic.ResRealDataAc;
import com.whyc.dto.RealDataStatic.ResRealExportAc;
import com.whyc.dto.RealDataStatic.SticRealDataAc;
import com.whyc.dto.Response;
import com.whyc.dto.Statistic.QuarterPwr7Res;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.db_data_history.BattRealdataId;
import com.whyc.pojo.db_data_history.PwrdevHistorydataId;
@@ -17,18 +22,24 @@
import com.whyc.pojo.db_param.PowerheartParam;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static java.lang.Thread.sleep;
@Service
public class PwrdevHistorydataIdService {
@@ -53,62 +64,121 @@
    private PowerheartParamService heartService;
    //系统概览前N笔交流输入统计
    public Response getPwrHisAcinData100(Integer powerId) {
        String dateTime = ActionUtil.sdfwithOutday.format(new Date());
    public Response getPwrHisAcinData100(Integer powerId) throws ParseException, InterruptedException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*10;//默认值
        Integer granularity=10;//默认值
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String halfHourAgoTime=halfHourAgo.format(formatter);
        String tableName ="db_data_history.tb_pwrdev_historydata_"+powerId+"_"+dateTime;
        String existTableName = commonMapper.existTable("db_data_history", "tb_pwrdev_historydata_"+powerId+"_"+dateTime);
        if(existTableName == null){
            return new Response().set(1,false,"当前电池组不存在前面小时数据");
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<PwrHisRealAcInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealAcInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getHalfHourPwrHisAcinData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        List<PwrHisRealAcInDto> datalist=subTablePageInfoService.getHalfHourPwrHisAcinData(tableName,granularity,halfHourAgoTime);
        return new Response().setII(1,datalist.size()>0,datalist,"获取半小时内交流输入统计");
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealAcInDto::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内交流输入统计");
    }
    //系统概览获取前N笔直流输出统计
    public Response getHalfHourPwrHisDcoutData(Integer powerId) {
        String dateTime = ActionUtil.sdfwithOutday.format(new Date());
    public Response getHalfHourPwrHisDcoutData(Integer powerId) throws InterruptedException, ParseException {
        //获取前笔数*时间间隔
        PowerheartParam heartParam=heartService.getHeartParamByPowerId(powerId);
        Integer interverCount=100*10;//默认值
        Integer granularity=10;//默认值
        Integer interverCount=100*5;//默认值
        Integer granularity=5;//默认值
        if(heartParam!=null){
            interverCount=heartParam.getAcinInterverCfg()*heartParam.getAcinCountCfg();
            granularity=heartParam.getAcinInterverCfg();
            interverCount=heartParam.getAcoutInterverCfg()*heartParam.getAcoutCountCfg();
            granularity=heartParam.getAcoutInterverCfg();//默认时间间隔一分钟一笔,所以时间间隔就是几笔取一笔
        }
        //获取前半个小时数据
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        // 计算半小时前的时间点
        String dateTime = now.format(formatter);
        Date endTime=ActionUtil.sdf.parse(dateTime);
        // 计算笔数*时间间隔的时间点
        LocalDateTime halfHourAgo = now.minusMinutes(interverCount);
        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String halfHourAgoTime=halfHourAgo.format(formatter);
        String tableName ="db_data_history.tb_pwrdev_historydata_"+powerId+"_"+dateTime;
        String existTableName = commonMapper.existTable("db_data_history", "tb_pwrdev_historydata_"+powerId+"_"+dateTime);
        if(existTableName == null){
            return new Response().set(1,false,"当前电源不存在前面小时数据");
        Date startTime=ActionUtil.sdf.parse(halfHourAgoTime);
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startTime,endTime);
        List<PwrHisRealDcoutInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealDcoutInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getHalfHourPwrHisDcoutData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        List<PwrHisRealDcoutInDto> datalist=subTablePageInfoService.getHalfHourPwrHisDcoutData(tableName,granularity,halfHourAgoTime);
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealDcoutInDto::getRecordDatetime)).collect(Collectors.toList());
        Long modelCfg=0l;
        PowerInf pinf=powerInfService.getPowerInfById(powerId);
        if(pinf!=null){
            modelCfg=pinf.getModelCfg();
        }
        return new Response().setIII(1,datalist.size()>0,datalist,modelCfg,"获取半小时内直流输出统计");
        return new Response().setIII(1,dataList.size()>0,dataListSorted,modelCfg,"获取半小时内直流输出统计");
    }
    //1.2.7优良电源统计上一季度的电源某一属性和参数阈值超过的次数
    public Integer getPwrQuarter7(Integer powerId,Integer powerType) throws NoSuchFieldException, IllegalAccessException {
@@ -195,4 +265,192 @@
            return PwrCapperformanceEnum.PWRSTATE_4.getStateId();
        }
    }
    //电源历史实时查询
    public Response getPwrRealDataHis(Integer powerId, Integer granularity, String startTime, String endTime) throws InterruptedException, ParseException {
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(ThreadLocalUtil.parse(startTime,1),ThreadLocalUtil.parse(endTime,1));
        List<PwrHisRealDcoutInDto> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String table = powerId + "_" + ThreadLocalUtil.format(recordDatetime,2);
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<PwrHisRealDcoutInDto> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    list=subTablePageInfoService.getPwrRealDataHis(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum);
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List dataListSorted = dataList.stream().sorted(Comparator.comparing(PwrHisRealDcoutInDto::getRecordDatetime)).collect(Collectors.toList());
        return new Response().setII(1,dataList.size()>0,dataListSorted,"获取半小时内直流输出统计");
    }
    //实时数据数据曲线统计-交流单元
    public Response getSticRealAcData(SticRealDataAc stic) throws ParseException, InterruptedException {
        Integer granularity=2;//默认值
        Map<String,Date> startAndEndTime=DateUtil.getStartAndEndTime(stic.getSticTime());
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startAndEndTime.get("startTime"),startAndEndTime.get("endTime"));
        List<ResRealDataAc> dataList = new LinkedList<>();
        Map<String, Map<String,List<String>>> allTimeMap=new HashMap<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            Map<String,List<String>> propertyInfo=stic.getPropertyInfo();
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String timeTable=ThreadLocalUtil.format(recordDatetime,2);
                String table = stic.getPowerId() + "_" + timeTable;
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<ResRealDataAc> list = new ArrayList();
                Map<String,List<String>> timeMap=new HashMap<>();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    //先按照频率查询出正常数据
                    list=subTablePageInfoService.getSticRealAcData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum,propertyInfo);
                    //所有属性的异动点
                    List<Integer> allChangeNums=new ArrayList<>();
                    //获取指定时间取出每个参数的异动次数和异动的recordNum
                    for (Map.Entry<String, List<String>> entry : propertyInfo.entrySet()) {
                        String param = entry.getKey();
                        List<String> valueList = entry.getValue();
                        for (String value : valueList) {
                            float upperLimit=550;
                            float lowerLimit=50;
                            //查询电源字段的异动
                            List changes= subTablePageInfoService.getPwrChangesRecordNum(tableName,recordDatetime,recordDatetime1,value,upperLimit,lowerLimit);
                            List<Integer> numList= (List<Integer>) changes.get(0);//record_num的集合
                            List<String> timeList= (List<String>) changes.get(1);//record_datetime的集合
                            if(numList!=null&&numList.size()>0){
                                allChangeNums.addAll(numList);
                            }
                            //存放每个参数的异动时间点
                            timeMap.put(value,timeList);
                        }
                    }
                    //只有存在异动点采取查询异动数据
                    if(allChangeNums.size()>0){
                        //查询大类参数下所有异动recordNum的记录之和
                        List<ResRealDataAc> paramList=subTablePageInfoService.getPwrPropertyChangesByRecordNum(tableName,recordDatetime,recordDatetime1,allChangeNums,propertyInfo);
                        list.addAll(paramList);
                    }
                }
                allTimeMap.put(timeTable,timeMap);
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List<ResRealDataAc> uniqueList = dataList.stream()
                .distinct()
                .collect(Collectors.toList());
        List dataListSorted = uniqueList.stream().sorted(Comparator.comparing(ResRealDataAc::getRecordNum)).collect(Collectors.toList());
        //所有异动点合并去重
        Map<String, List<String>> mergedMap =ActionUtil.getMergeMap(allTimeMap);
        return new Response().setIII(1,dataList.size()>0,dataListSorted,mergedMap,"实时数据数据曲线统计-交流单元");
    }
    //实时数据统计曲线-交流单元导出(2.5.2)
    public List<ResRealDataAc> getSticRealAcDataForExport(ResRealExportAc exportAc) throws ParseException, InterruptedException {
        Integer granularity=2;//默认值
        Map<String,Date> startAndEndTime=DateUtil.getStartAndEndTime(exportAc.getSticTime());
        //获取两个时间分割多少张表
        List<List<Date>> monthTimeList = DateUtil.getMonthTime(startAndEndTime.get("startTime"),startAndEndTime.get("endTime"));
        List<ResRealDataAc> dataList = new LinkedList<>();
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(monthTimeList.size());
        for (int i = 0; i < monthTimeList.size(); i++) {
            int finalI = i;
            Integer finalGranularity = granularity;
            Map<String,List<String>> propertyInfo=exportAc.getPropertyInfo();
            pool.execute(() -> {
                int finalII = finalI;
                Date recordDatetime=monthTimeList.get(finalII).get(0);
                Date recordDatetime1=monthTimeList.get(finalII).get(1);
                String timeTable=ThreadLocalUtil.format(recordDatetime,2);
                String table = exportAc.getPowerId() + "_" + timeTable;
                String tableName="db_data_history.tb_pwrdev_historydata_"+table;
                //判断表是否存在
                int tableNum = subTablePageInfoService.judgeTable_pwrhis(table);
                List<ResRealDataAc> list = new ArrayList();
                if (tableNum > 0) {
                    //获取指定时间段内最大最小recordNum确保数据的完整
                    List recordNums= subTablePageInfoService.getPwrMaxAndMinRecordNum(tableName,recordDatetime,recordDatetime1);
                    Integer maxRecordNum= 0;
                    Integer minRecordNum= 0;
                    if(recordNums.size()>0){
                        maxRecordNum=(Integer)recordNums.get(0);
                        minRecordNum=(Integer)recordNums.get(1);
                    }
                    //先按照频率查询出正常数据
                    list=subTablePageInfoService.getSticRealAcData(tableName, finalGranularity,recordDatetime,recordDatetime1,maxRecordNum,minRecordNum,propertyInfo);
                    //所有属性的异动点
                    List<Integer> allChangeNums=new ArrayList<>();
                    //获取指定时间取出每个参数的异动次数和异动的recordNum
                    for (Map.Entry<String, List<String>> entry : propertyInfo.entrySet()) {
                        String param = entry.getKey();
                        List<String> valueList = entry.getValue();
                        for (String value : valueList) {
                            float upperLimit=550;
                            float lowerLimit=50;
                            //查询电源字段的异动
                            List changes= subTablePageInfoService.getPwrChangesRecordNum(tableName,recordDatetime,recordDatetime1,value,upperLimit,lowerLimit);
                            List<Integer> numList= (List<Integer>) changes.get(0);//record_num的集合
                            if(numList!=null&&numList.size()>0){
                                allChangeNums.addAll(numList);
                            }
                        }
                    }
                    //只有存在异动点采取查询异动数据
                    if(allChangeNums.size()>0){
                        //查询大类参数下所有异动recordNum的记录之和
                        List<ResRealDataAc> paramList=subTablePageInfoService.getPwrPropertyChangesByRecordNum(tableName,recordDatetime,recordDatetime1,allChangeNums,propertyInfo);
                        list.addAll(paramList);
                    }
                }
                dataList.addAll(list);
                latch.countDown();
            });
            sleep(200);
        }
        latch.await(10, TimeUnit.MINUTES);
        List<ResRealDataAc> uniqueList = dataList.stream()
                .distinct()
                .collect(Collectors.toList());
        List dataListSorted = uniqueList.stream().sorted(Comparator.comparing(ResRealDataAc::getRecordNum)).collect(Collectors.toList());
        return dataListSorted;
    }
}