whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/util/SubTablePageInfoUtils.java
@@ -1,18 +1,24 @@
package com.whyc.util;
import com.github.pagehelper.PageInfo;
import com.whyc.constant.Com;
import com.whyc.dto.InterfaceParam;
import com.whyc.dto.UpsAlarmDTO;
import com.whyc.dto.paramter.AlarmPar;
import com.whyc.dto.paramter.DevAlarmPar;
import com.whyc.dto.result.AlarmRes;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.*;
import com.whyc.pojo.*;
import com.whyc.service.SubTablePageInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -36,6 +42,9 @@
    @Resource
    private PwrdevAlarmHistoryMapper powerAlarmHistoryMapper;
    @Autowired
    private SubTablePageInfoService subService;
    /**按年份表分页查询*/
    public PageInfo<Object> getPageInfo(int pageNum,int pageSize,
@@ -66,7 +75,8 @@
                userLog.setOperationTime(queryTime.get(0));
                userLog.setOperationTime2(queryTime.get(1));
                userLog.setRecordYear(tableName);
                int currentCount = userLogMapper.getCount(userLog);
                //int currentCount = userLogMapper.getCount(userLog);
                int currentCount = subService.getCount(userLog);
                queryCountMap.put(tableYear,currentCount);
            }
            else if(pojo instanceof AlarmPar){ //电池告警历史
@@ -74,7 +84,8 @@
                param.setAlmStartTime(queryTime.get(0));
                param.setAlmStartTime1(queryTime.get(1));
                param.setRecordYear(tableName);
                int currentCount = battAlarmDataHistoryMapper.getCount(param);
                //int currentCount = battAlarmDataHistoryMapper.getCount(param);
                int currentCount = subService.getCount(param);
                queryCountMap.put(tableYear,currentCount);
            }
            else if (pojo instanceof DevAlarmPar) {
@@ -82,7 +93,8 @@
                param.setAlmStartTime(queryTime.get(0));
                param.setAlmStartTime1(queryTime.get(1));
                param.setRecordYear(tableName);
                int currentCount = devAlarmDataHistoryMapper.getCount(param);
                //int currentCount = devAlarmDataHistoryMapper.getCount(param);
                int currentCount = subService.getCount(param);
                queryCountMap.put(tableYear,currentCount);
            }
@@ -92,13 +104,15 @@
                    param.setAlmStartTime(queryTime.get(0));
                    param.setAlmStartTime1(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = powerAlarmHistoryMapper.getCount(param);
                    //int currentCount = powerAlarmHistoryMapper.getCount(param);
                    int currentCount = subService.getCount(param);
                    queryCountMap.put(tableYear, currentCount);
                }else{
                    param.setAlmStartTime(queryTime.get(0));
                    param.setAlmStartTime1(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = powerAlarmHistoryMapper.getCountForTX(param);
                    //int currentCount = powerAlarmHistoryMapper.getCountForTX(param);
                    int currentCount = subService.getCountForTX(param);
                    queryCountMap.put(tableYear, currentCount);
                }
@@ -109,15 +123,30 @@
                    param.setStartTime(queryTime.get(0));
                    param.setEndTime(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = battAlarmDataHistoryMapper.getCountForInterface(param);
                    //int currentCount = battAlarmDataHistoryMapper.getCountForInterface(param);
                    int currentCount = subService.getCountForInterface_batt(param);
                    queryCountMap.put(tableYear,currentCount);
                }else if(tablePrefix.equals("tb_devalarm_data_history")){
                    param.setStartTime(queryTime.get(0));
                    param.setEndTime(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = devAlarmDataHistoryMapper.getCountForInterface(param);
                    //int currentCount = devAlarmDataHistoryMapper.getCountForInterface(param);
                    int currentCount = subService.getCountForInterface(param);
                    queryCountMap.put(tableYear,currentCount);
                }
            }
            else if (pojo instanceof UpsAlarmDTO) {
                //ups不存在初始表,跳过
                if(tableYear.equals("default")){
                    continue;
                }
                UpsAlarmDTO param = (UpsAlarmDTO) pojo;
                param.setAlmStartTime(queryTime.get(0));
                param.setAlmEndTime(queryTime.get(1));
                param.setRecordYear(tableName);
                //int currentCount = powerAlarmHistoryMapper.getCount(param);
                int currentCount = subService.getCountForUps(param);
                queryCountMap.put(tableYear, currentCount);
            }
        }
@@ -158,7 +187,8 @@
                userLog.setLimitStart(limitList.get(0));
                userLog.setLimitEnd(limitList.get(1));
                userLog.setRecordYear(recordYear);
                List<UserLog> list = userLogMapper.getList2(userLog);
                //List<UserLog> list = userLogMapper.getList2(userLog);
                List<UserLog> list =  subService.getList2(userLog);
                dataList.addAll(list);
            }
            else if (pojo instanceof AlarmPar) {
@@ -170,7 +200,8 @@
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                List<AlarmRes> list = battAlarmDataHistoryMapper.getList2(data);
                //List<AlarmRes> list = battAlarmDataHistoryMapper.getList2(data);
                List<AlarmRes> list = subService.getList2(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof DevAlarmPar) {
@@ -182,7 +213,23 @@
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                List<DevalarmDataHistory> list = devAlarmDataHistoryMapper.getList2(data);
                //List<DevalarmDataHistory> list = devAlarmDataHistoryMapper.getList2(data);
                List<DevalarmDataHistory> list =  subService.getList2(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof UpsAlarmDTO) {
                UpsAlarmDTO data = (UpsAlarmDTO) pojo;
                data.setAlmStartTime(queryTime.get(0));
                data.setAlmEndTime(queryTime.get(1));
                List<Integer> limitList = tableAndLimitMap.get(key);
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                //List<DevalarmDataHistory> list = devAlarmDataHistoryMapper.getList2(data);
                List<UpspwrdevAlarmHistory> list =  subService.getList2(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof PwrdevAlarmHistory) {
@@ -195,7 +242,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getList2(data);
                    //List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getList2(data);
                    List<PwrdevAlarmHistory> list= subService.getList2(data);
                    dataList.addAll(list);
                }else{
                    data.setAlmStartTime(queryTime.get(0));
@@ -205,7 +253,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getListForTX(data);
                    //List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getListForTX(data);
                    List<PwrdevAlarmHistory> list= subService.getListForTX(data);
                    dataList.addAll(list);
                }
            }
@@ -219,7 +268,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<AlarmRes> list = battAlarmDataHistoryMapper.getListForInterface(data);
                    //List<AlarmRes> list = battAlarmDataHistoryMapper.getListForInterface(data);
                    List<AlarmRes> list = subService.getListForInterface(data);
                    dataList.addAll(list);
                }else if(tablePrefix.equals("tb_devalarm_data_history")){
                    data.setStartTime(queryTime.get(0));
@@ -229,7 +279,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    //List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    List list = subService.getListForInterface_dev(data);
                    dataList.addAll(list);
                }
            }
@@ -241,89 +292,128 @@
    public PageInfo<Object> getPageInfoByMonthTable(int pageNum,int pageSize,
                                        Date startTime,Date endTime,
                                        String dbName,String tablePrefix,
                                        Object pojo) throws ParseException {
                                        Object pojo) throws ParseException, InterruptedException {
        Map<String, List<Date>> queryTimeForSubTables = DateUtil.getQueryTimeForSubTablesByMonthDesc(startTime, endTime);
        //查询分表是否存在,存在则查询结果
        Map<String,Integer> queryCountMap = new LinkedHashMap<>();
        Set<String> tableYearKeySet = queryTimeForSubTables.keySet();
        //优化1-查询所有的符合月份规则的表和主表
        List<String> tableNameListDB = getTableList(dbName, tablePrefix, tablePrefix + "(_[0-9]{4}_[0-9]{2})?");
        List<String> tableYearListInDB = new LinkedList<>(); //查询时间段内的年月分表集合且在数据库中存在
        //过滤掉时间段内数据库中不存在的年月分表
        for (String tableYear : tableYearKeySet) {
            List<Date> queryTime = queryTimeForSubTables.get(tableYear);
            String tableName;
            if(!tableYear.equals("default")){
                //数值
                tableName = tablePrefix+"_"+tableYear;
                //String existTableName = commonMapper.existTable(dbName, tableName);
                if(tableNameListDB.contains(tableName)){ //优化1-数据库中存在判断,去除多次查表,一次查出
                if(!tableNameListDB.contains(tableName)){ //优化1-数据库中不存在,去除多次查表,一次查出
                    continue;
                }
            }
            tableYearListInDB.add(tableYear);
        }
        //查询年月分表的记录数
        //优化2:多线程读取
        ThreadPoolExecutor pool = ThreadPoolExecutorFactory.getPoolExecutor();
        CountDownLatch latch = new CountDownLatch(tableYearListInDB.size());
        for (String tableYear : tableYearListInDB) {
            List<Date> queryTime = queryTimeForSubTables.get(tableYear);
            String tableName;
            if(!tableYear.equals("default")){
                //数值
                tableName = tablePrefix+"_"+tableYear;
            }else{
                tableName = tablePrefix;
            }
            //====== 根据不同类型类型对象对应调整 ======
            if(pojo instanceof UserLog) {
                UserLog userLog = (UserLog) pojo;
                userLog.setOperationTime(queryTime.get(0));
                userLog.setOperationTime2(queryTime.get(1));
                userLog.setRecordYear(tableName);
                int currentCount = userLogMapper.getCount(userLog);
                queryCountMap.put(tableYear,currentCount);
            }
            else if(pojo instanceof AlarmPar){ //电池告警历史
                AlarmPar param = (AlarmPar) pojo;
                param.setAlmStartTime(queryTime.get(0));
                param.setAlmStartTime1(queryTime.get(1));
                param.setRecordYear(tableName);
                int currentCount = battAlarmDataHistoryMapper.getCount(param);
                queryCountMap.put(tableYear,currentCount);
            }
            else if (pojo instanceof DevAlarmPar) {
                DevAlarmPar param = (DevAlarmPar) pojo;
                param.setAlmStartTime(queryTime.get(0));
                param.setAlmStartTime1(queryTime.get(1));
                param.setRecordYear(tableName);
                int currentCount = devAlarmDataHistoryMapper.getCount(param);
                queryCountMap.put(tableYear,currentCount);
            pool.execute(()-> {
            }
            else if (pojo instanceof PwrdevAlarmHistory) {
                PwrdevAlarmHistory param = (PwrdevAlarmHistory) pojo;
                if(param.getRecordId() == 1) {
                //====== 根据不同类型类型对象对应调整 ======
                if (pojo instanceof UserLog) {
                    //UserLog userLog = (UserLog) pojo;
                    UserLog userLog = new UserLog();
                    BeanUtils.copyProperties(pojo,userLog);
                    userLog.setOperationTime(queryTime.get(0));
                    userLog.setOperationTime2(queryTime.get(1));
                    userLog.setRecordYear(tableName);
                    //int currentCount = userLogMapper.getCount(userLog);
                    int currentCount = subService.getCount(userLog);
                    queryCountMap.put(tableYear, currentCount);
                } else if (pojo instanceof AlarmPar) { //电池告警历史
                    //AlarmPar param = (AlarmPar) pojo;
                    AlarmPar param = new AlarmPar();
                    BeanUtils.copyProperties(pojo,param);
                    param.setAlmStartTime(queryTime.get(0));
                    param.setAlmStartTime1(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = powerAlarmHistoryMapper.getCount(param);
                    //int currentCount = battAlarmDataHistoryMapper.getCount(param);
                    int currentCount = subService.getCount(param);
                    queryCountMap.put(tableYear, currentCount);
                }else{
                } else if (pojo instanceof DevAlarmPar) {
                    //DevAlarmPar param = (DevAlarmPar) pojo;
                    DevAlarmPar param = new DevAlarmPar();
                    BeanUtils.copyProperties(pojo,param);
                    param.setAlmStartTime(queryTime.get(0));
                    param.setAlmStartTime1(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = powerAlarmHistoryMapper.getCountForTX(param);
                    //int currentCount = devAlarmDataHistoryMapper.getCount(param);
                    //int currentCount = subService.getCount(param);
                    int currentCount = subService.getCount_stationId(param);
                    queryCountMap.put(tableYear, currentCount);
                }
            }
            else if (pojo instanceof InterfaceParam){
                InterfaceParam param = (InterfaceParam) pojo;
                if(tablePrefix.equals("tb_battalarm_data_history")){
                    param.setStartTime(queryTime.get(0));
                    param.setEndTime(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = battAlarmDataHistoryMapper.getCountForInterface(param);
                    queryCountMap.put(tableYear,currentCount);
                }else if(tablePrefix.equals("tb_devalarm_data_history")){
                    param.setStartTime(queryTime.get(0));
                    param.setEndTime(queryTime.get(1));
                    param.setRecordYear(tableName);
                    int currentCount = devAlarmDataHistoryMapper.getCountForInterface(param);
                    queryCountMap.put(tableYear,currentCount);
                } else if (pojo instanceof PwrdevAlarmHistory) {
                    //PwrdevAlarmHistory param = (PwrdevAlarmHistory) pojo;
                    PwrdevAlarmHistory param = new PwrdevAlarmHistory();
                    BeanUtils.copyProperties(pojo,param);
                    if (param.getRecordId() == 1) {
                        param.setAlmStartTime(queryTime.get(0));
                        param.setAlmStartTime1(queryTime.get(1));
                        param.setRecordYear(tableName);
                        //int currentCount = powerAlarmHistoryMapper.getCount(param);
                        int currentCount = subService.getCount(param);
                        queryCountMap.put(tableYear, currentCount);
                    } else {
                        param.setAlmStartTime(queryTime.get(0));
                        param.setAlmStartTime1(queryTime.get(1));
                        param.setRecordYear(tableName);
                        //int currentCount = powerAlarmHistoryMapper.getCountForTX(param);
                        int currentCount = subService.getCountForTX(param);
                        queryCountMap.put(tableYear, currentCount);
                    }
                } else if (pojo instanceof InterfaceParam) {
                    //InterfaceParam param = (InterfaceParam) pojo;
                    InterfaceParam param = new InterfaceParam();
                    BeanUtils.copyProperties(pojo,param);
                    if (tablePrefix.equals("tb_battalarm_data_history")) {
                        param.setStartTime(queryTime.get(0));
                        param.setEndTime(queryTime.get(1));
                        param.setRecordYear(tableName);
                        //int currentCount = battAlarmDataHistoryMapper.getCountForInterface(param);
                        int currentCount = subService.getCountForInterface_batt(param);
                        queryCountMap.put(tableYear, currentCount);
                    } else if (tablePrefix.equals("tb_devalarm_data_history")) {
                        param.setStartTime(queryTime.get(0));
                        param.setEndTime(queryTime.get(1));
                        param.setRecordYear(tableName);
                        //int currentCount = devAlarmDataHistoryMapper.getCountForInterface(param);
                        int currentCount = subService.getCountForInterface(param);
                        queryCountMap.put(tableYear, currentCount);
                    }
                }
            }
                latch.countDown();
            });
        }
        latch.await();
        //因为多线程后无序,需要重排序
        List<String> keySetDesc = queryCountMap.keySet().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        Map<String,Integer> queryCountMapDesc = new LinkedHashMap<>();
        for (String key : keySetDesc) {
            Integer count = queryCountMap.get(key);
            queryCountMapDesc.put(key,count);
        }
        //分页信息
        //确认总页数,总记录数
        PageInfo<Object> pageInfo = new PageInfo<>();
@@ -345,7 +435,7 @@
        //最后一个算法:上面不应该先查询所有记录,应该取count. 这后面定位到哪个表或哪几张表后,采取limit获取当前页记录数;
        //格式:{表名,[limit 2,20]}
        Map<String,List<Integer>> tableAndLimitMap = MathUtil.getQueryTableAndLimit(startNum,endNum,pageSize,queryCountMap);
        Map<String,List<Integer>> tableAndLimitMap = MathUtil.getQueryTableAndLimit(startNum,endNum,pageSize,queryCountMapDesc);
        Set<String> keySet = tableAndLimitMap.keySet();
        List<Object> dataList = new LinkedList<>();
        for (String key : keySet) {
@@ -361,7 +451,8 @@
                userLog.setLimitStart(limitList.get(0));
                userLog.setLimitEnd(limitList.get(1));
                userLog.setRecordYear(recordYear);
                List<UserLog> list = userLogMapper.getList2(userLog);
                //List<UserLog> list = userLogMapper.getList2(userLog);
                List<UserLog> list =  subService.getList2(userLog);
                dataList.addAll(list);
            }
            else if (pojo instanceof AlarmPar) {
@@ -373,7 +464,8 @@
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                List<AlarmRes> list = battAlarmDataHistoryMapper.getList2(data);
                //List<AlarmRes> list = battAlarmDataHistoryMapper.getList2(data);
                List<AlarmRes> list = subService.getList2(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof DevAlarmPar) {
@@ -385,7 +477,9 @@
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                List<DevalarmDataHistory> list = devAlarmDataHistoryMapper.getList2(data);
                //List<DevalarmDataHistory> list = devAlarmDataHistoryMapper.getList2(data);
                //List<DevalarmDataHistory> list =  subService.getList2(data);
                List<DevalarmDataHistory> list =  subService.getList2_stationId(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof PwrdevAlarmHistory) {
@@ -398,7 +492,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getList2(data);
                    //List<PwrdevAlarmHistory> list = powerAlarmHistoryMapper.getList2(data);
                    List<PwrdevAlarmHistory> list= subService.getList2(data);
                    dataList.addAll(list);
                }else{
                    data.setAlmStartTime(queryTime.get(0));
@@ -408,7 +503,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getListForTX(data);
                    //List<DevalarmDataHistory> list = powerAlarmHistoryMapper.getListForTX(data);
                    List<PwrdevAlarmHistory> list= subService.getListForTX(data);
                    dataList.addAll(list);
                }
            }
@@ -422,7 +518,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<AlarmRes> list = battAlarmDataHistoryMapper.getListForInterface(data);
                    //List<AlarmRes> list = battAlarmDataHistoryMapper.getListForInterface(data);
                    List<AlarmRes> list = subService.getListForInterface(data);
                    dataList.addAll(list);
                }else if(tablePrefix.equals("tb_devalarm_data_history")){
                    data.setStartTime(queryTime.get(0));
@@ -432,7 +529,8 @@
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    //List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    List list = subService.getListForInterface_dev(data);
                    dataList.addAll(list);
                }
            }
@@ -463,13 +561,16 @@
            List<BattalarmDataHistory> levelInfoSubList;
            List<Integer> levelSubList;
            if(tableLike.equals("tb_battalarm_data_history")) {
                levelInfoSubList = battAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                //levelInfoSubList = battAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                levelInfoSubList = subService.getLevelSubList_batt(uId, tableName);
            }
            else if(tableLike.equals("tb_devalarm_data_history")){
                levelInfoSubList = devAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                //levelInfoSubList = devAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                levelInfoSubList = subService.getLevelSubList_dev(uId, tableName);
            }
            else{
                levelInfoSubList = powerAlarmHistoryMapper.getLevelSubList(uId, tableName,almTypes);
                //levelInfoSubList = powerAlarmHistoryMapper.getLevelSubList(uId, tableName,almTypes);
                levelInfoSubList = subService.getLevelSubList_pwr(uId, tableName,almTypes);
            }
            levelSubList = levelInfoSubList.stream().map(BattalarmDataHistory::getAlmLevel).collect(Collectors.toList());
            levelList.addAll(levelSubList);