whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/util/SubTablePageInfoUtils.java
@@ -1,19 +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.DevalarmDataHistory;
import com.whyc.pojo.PwrdevAlarmHistory;
import com.whyc.pojo.UserLog;
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;
@@ -38,6 +43,10 @@
    @Resource
    private PwrdevAlarmHistoryMapper powerAlarmHistoryMapper;
    @Autowired
    private SubTablePageInfoService subService;
    /**按年份表分页查询*/
    public PageInfo<Object> getPageInfo(int pageNum,int pageSize,
                                Date startTime,Date endTime,
                                String dbName,String tablePrefix,
@@ -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,16 +104,49 @@
                    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);
                }
            }
            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);
                    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);
                }
            }
            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);
            }
        }
@@ -142,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) {
@@ -154,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) {
@@ -166,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) {
@@ -179,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));
@@ -189,7 +253,284 @@
                    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);
                }
            }
            else if (pojo instanceof InterfaceParam){
                InterfaceParam data = (InterfaceParam) pojo;
                if(tablePrefix.equals("tb_battalarm_data_history")){
                    data.setStartTime(queryTime.get(0));
                    data.setEndTime(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    //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));
                    data.setEndTime(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    //List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    List list = subService.getListForInterface_dev(data);
                    dataList.addAll(list);
                }
            }
        }
        pageInfo.setList(dataList);
        return pageInfo;
    }
    /**按月分表,分页查询*/
    public PageInfo<Object> getPageInfoByMonthTable(int pageNum,int pageSize,
                                        Date startTime,Date endTime,
                                        String dbName,String tablePrefix,
                                        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) {
            String tableName;
            if(!tableYear.equals("default")){
                //数值
                tableName = tablePrefix+"_"+tableYear;
                //String existTableName = commonMapper.existTable(dbName, tableName);
                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;
            }
            pool.execute(()-> {
                //====== 根据不同类型类型对象对应调整 ======
                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 = battAlarmDataHistoryMapper.getCount(param);
                    int currentCount = subService.getCount(param);
                    queryCountMap.put(tableYear, currentCount);
                } 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 = devAlarmDataHistoryMapper.getCount(param);
                    //int currentCount = subService.getCount(param);
                    int currentCount = subService.getCount_stationId(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<>();
        int total = 0;
        Set<String> queryKeySet = queryCountMap.keySet();
        for (String queryKey : queryKeySet) {
            int size = queryCountMap.get(queryKey);
            total+=size;
        }
        int pages = (int) Math.ceil(Float.parseFloat(String.valueOf(total))/pageSize);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        //根据当前页所需记录,查询当前页记录
        int startNum = (pageNum-1)*pageSize+1;
        int endNum = pageNum*pageSize;
        //最后一个算法:上面不应该先查询所有记录,应该取count. 这后面定位到哪个表或哪几张表后,采取limit获取当前页记录数;
        //格式:{表名,[limit 2,20]}
        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) {
            List<Date> queryTime = queryTimeForSubTables.get(key);
            //====== 根据不同类型类型对象对应调整 ======
            String recordYear = key.equals("default") ? tablePrefix : tablePrefix + "_" + key;
            if(pojo instanceof UserLog) {
                UserLog userLog = (UserLog) pojo;
                userLog.setOperationTime(queryTime.get(0));
                userLog.setOperationTime2(queryTime.get(1));
                List<Integer> limitList = tableAndLimitMap.get(key);
                userLog.setLimitStart(limitList.get(0));
                userLog.setLimitEnd(limitList.get(1));
                userLog.setRecordYear(recordYear);
                //List<UserLog> list = userLogMapper.getList2(userLog);
                List<UserLog> list =  subService.getList2(userLog);
                dataList.addAll(list);
            }
            else if (pojo instanceof AlarmPar) {
                AlarmPar data = (AlarmPar) pojo;
                data.setAlmStartTime(queryTime.get(0));
                data.setAlmStartTime1(queryTime.get(1));
                List<Integer> limitList = tableAndLimitMap.get(key);
                data.setLimitStart(limitList.get(0));
                data.setLimitEnd(limitList.get(1));
                data.setRecordYear(recordYear);
                //List<AlarmRes> list = battAlarmDataHistoryMapper.getList2(data);
                List<AlarmRes> list = subService.getList2(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof DevAlarmPar) {
                DevAlarmPar data = (DevAlarmPar) pojo;
                data.setAlmStartTime(queryTime.get(0));
                data.setAlmStartTime1(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<DevalarmDataHistory> list =  subService.getList2(data);
                List<DevalarmDataHistory> list =  subService.getList2_stationId(data);
                dataList.addAll(list);
            }
            else if (pojo instanceof PwrdevAlarmHistory) {
                PwrdevAlarmHistory data = (PwrdevAlarmHistory) pojo;
                if(data.getRecordId()==1) {
                    data.setAlmStartTime(queryTime.get(0));
                    data.setAlmStartTime1(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    //List<PwrdevAlarmHistory> list = powerAlarmHistoryMapper.getList2(data);
                    List<PwrdevAlarmHistory> list= subService.getList2(data);
                    dataList.addAll(list);
                }else{
                    data.setAlmStartTime(queryTime.get(0));
                    data.setAlmStartTime1(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 = powerAlarmHistoryMapper.getListForTX(data);
                    List<PwrdevAlarmHistory> list= subService.getListForTX(data);
                    dataList.addAll(list);
                }
            }
            else if (pojo instanceof InterfaceParam){
                InterfaceParam data = (InterfaceParam) pojo;
                if(tablePrefix.equals("tb_battalarm_data_history")){
                    data.setStartTime(queryTime.get(0));
                    data.setEndTime(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    //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));
                    data.setEndTime(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    data.setLimitStart(limitList.get(0));
                    data.setLimitEnd(limitList.get(1));
                    data.setRecordYear(recordYear);
                    //List<AlarmRes> list = devAlarmDataHistoryMapper.getListForInterface(data);
                    List list = subService.getListForInterface_dev(data);
                    dataList.addAll(list);
                }
            }
@@ -217,16 +558,21 @@
        List<String> tableList = getTableList(dbName, tableLike, regex);
        List<Integer> levelList = new LinkedList();
        for (String tableName : tableList) {
            List<BattalarmDataHistory> levelInfoSubList;
            List<Integer> levelSubList;
            if(tableLike.equals("tb_battalarm_data_history")) {
                levelSubList = battAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                //levelInfoSubList = battAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                levelInfoSubList = subService.getLevelSubList_batt(uId, tableName);
            }
            else if(tableLike.equals("tb_devalarm_data_history")){
                levelSubList = devAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                //levelInfoSubList = devAlarmDataHistoryMapper.getLevelSubList(uId, tableName);
                levelInfoSubList = subService.getLevelSubList_dev(uId, tableName);
            }
            else{
                levelSubList = 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);
        }
        //分为四级查询个数
@@ -258,8 +604,10 @@
    }
    public static void main(String[] args) {
        String regex = "tb_battalarm_data_history_?[0-9]*";
        String table = "tb_battalarm_data_history";
        //String regex = "tb_battalarm_data_history_?[0-9]*";
        String regex = "tb_battalarm_data_history(_[0-9]{4}_[0-9]{2})?";
        //String table = "tb_battalarm_data_history";
        String table = "tb_battalarm_data_history_0222_04";
        System.out.println(Pattern.matches(regex,table));
    }
}