whycxzp
2023-12-07 62b3ae5384422b63f098c6fb02f390f429d02522
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//package com.whyc.util;
//
//import com.github.pagehelper.PageInfo;
//import com.whyc.mapper.CommonMapper;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.text.ParseException;
//import java.util.*;
//import java.util.regex.Pattern;
//
///**
// * 数据量极大的表,按照年份分表,分页查询时获取数据
// */
//@Component
//public class SubTablePageInfoUtils {
//
//    @Resource
//    private CommonMapper commonMapper;
//
//    @Resource
//    private UserLogMapper userLogMapper;
//
//    /**按年份表分页查询*/
//    public PageInfo<Object> getPageInfo(int pageNum,int pageSize,
//                                Date startTime,Date endTime,
//                                String dbName,String tablePrefix,
//                                Object pojo) throws ParseException {
//        Map<String, List<Date>> queryTimeForSubTables = DateUtil.getQueryTimeForSubTablesDesc(startTime, endTime);
//        //查询分表是否存在,存在则查询结果
//        Map<String,Integer> queryCountMap = new LinkedHashMap<>();
//        Set<String> tableYearKeySet = queryTimeForSubTables.keySet();
//        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(existTableName == null){
//                    continue;
//                }
//            }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);
//                int currentCount = subService.getCount(userLog);
//                queryCountMap.put(tableYear,currentCount);
//            }
//        }
//
//        //分页信息
//        //确认总页数,总记录数
//        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,queryCountMap);
//        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);
//            }
//        }
//        pageInfo.setList(dataList);
//        return pageInfo;
//    }
//
//
//    /**查询是否包含特定规则的表,存在,则返回表名*/
//    public List<String> getTableList(String dbName,String tableLike,String regex){
//        List<String> resultTableList = new LinkedList<>();
//        List<String> tableList = commonMapper.getTableListLike(dbName,tableLike);
//        for (String tableName : tableList) {
//            boolean matches = Pattern.matches(regex, tableName);
//            if(matches){
//                resultTableList.add(tableName);
//            }
//        }
//        return resultTableList;
//    }
//
//}