whycxzp
2025-03-31 a127882c8f15593783fcf0a1489835c16db70682
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.whyc.util;
 
import com.github.pagehelper.PageInfo;
import com.whyc.constant.DevAlmEnum;
import com.whyc.dto.DevA200AlarmDto;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.db_alarm.BattAlarmHistory;
import com.whyc.pojo.db_alarm.DevLithiumAlarmDataYear;
import com.whyc.pojo.db_lithium_ram_db.DevLithiumInf;
import com.whyc.pojo.db_power_alarm.PowerAlarmHistory;
import com.whyc.pojo.db_power_history.BattStationTempHistory;
import com.whyc.pojo.db_user.UserInf;
import com.whyc.service.DevLithiumInfService;
import com.whyc.service.SubTableService;
import com.whyc.service.UserInfService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
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;
 
/**
 * 数据量极大的表,按照年份分表,分页查询时获取数据
 */
@Component
public class SubTablePageInfoUtil {
    @Autowired(required = false)
    private CommonMapper commonMapper;
 
    @Autowired
    private SubTableService service;
 
    @Autowired
    private DevLithiumInfService devLithiumInfService;
 
    @Autowired
    private UserInfService userInfService;
 
    /**按年份表分页查询*/
    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.getQueryTimeForSubTablesDescWithOutDefault(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 = tablePrefix+"_"+tableYear;
            boolean existTableName = commonMapper.existTable(dbName, tableName);
            if(!existTableName){
                continue;
            }
            //====== 根据不同类型类型对象对应调整 ======
            if(pojo instanceof BattAlarmHistory) {
                BattAlarmHistory battAlarmHistory = new BattAlarmHistory();
                BeanUtils.copyProperties(pojo, battAlarmHistory);
                battAlarmHistory.setAlmStartTime(queryTime.get(0));
                battAlarmHistory.setAlmEndTime(queryTime.get(1));
                battAlarmHistory.setRecordYear(tableName);
                int currentCount = service.getBattHisCount(battAlarmHistory);
                queryCountMap.put(tableYear, currentCount);
            }
            else if(pojo instanceof PowerAlarmHistory) {
                PowerAlarmHistory powerAlarmHistory = new PowerAlarmHistory();
                BeanUtils.copyProperties(pojo, powerAlarmHistory);
                powerAlarmHistory.setAlmStartTime(queryTime.get(0));
                powerAlarmHistory.setAlmEndTime(queryTime.get(1));
                powerAlarmHistory.setRecordYear(tableName);
                int currentCount = service.getPowerHisCount(powerAlarmHistory);
                queryCountMap.put(tableYear, currentCount);
            }
            else if (pojo instanceof DevA200AlarmDto) {
                DevA200AlarmDto dto = (DevA200AlarmDto) pojo;
                dto.setStartTime(queryTime.get(0));
                dto.setEndTime(queryTime.get(1));
                dto.setTableName(tableName);
                int currentCount = service.getCountForDevAlm(dto);
                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 = tablePrefix + "_" + key;
            if(pojo instanceof BattAlarmHistory) {
                BattAlarmHistory battAlarmHistory = new BattAlarmHistory();
                BeanUtils.copyProperties(pojo, battAlarmHistory);
                battAlarmHistory.setAlmStartTime(queryTime.get(0));
                battAlarmHistory.setAlmEndTime(queryTime.get(1));
                battAlarmHistory.setRecordYear(recordYear);
                List<Integer> limitList = tableAndLimitMap.get(key);
                battAlarmHistory.setLimitStart(limitList.get(0));
                battAlarmHistory.setLimitEnd(limitList.get(1));
                List<BattAlarmHistory> list =  service.getBattHisList(battAlarmHistory);
                dataList.addAll(list);
            }
            else if(pojo instanceof PowerAlarmHistory) {
                PowerAlarmHistory powerAlarmHistory = new PowerAlarmHistory();
                BeanUtils.copyProperties(pojo, powerAlarmHistory);
                powerAlarmHistory.setAlmStartTime(queryTime.get(0));
                powerAlarmHistory.setAlmEndTime(queryTime.get(1));
                powerAlarmHistory.setRecordYear(recordYear);
                List<Integer> limitList = tableAndLimitMap.get(key);
                powerAlarmHistory.setLimitStart(limitList.get(0));
                powerAlarmHistory.setLimitEnd(limitList.get(1));
                List<PowerAlarmHistory> list =  service.getPowerHisList(powerAlarmHistory);
                dataList.addAll(list);
            }
            else if (pojo instanceof DevA200AlarmDto) {
                if(!key.equals("default")){
                    DevA200AlarmDto dto = (DevA200AlarmDto) pojo;
                    dto.setStartTime(queryTime.get(0));
                    dto.setEndTime(queryTime.get(1));
                    List<Integer> limitList = tableAndLimitMap.get(key);
                    dto.setLimitStart(limitList.get(0));
                    dto.setLimitEnd(limitList.get(1));
                    dto.setTableName(recordYear);
                    List<DevLithiumAlarmDataYear> list =  service.getListDevAlm(dto);
                    for (DevLithiumAlarmDataYear year:list) {
                        int devId=year.getDevId();
                        int uid=year.getConfirmedUid();
                        DevLithiumInf dinf=devLithiumInfService.getDinfByDevId(devId);
                        UserInf uinf= userInfService.getUinfByUId(uid);
                        int almId=year.getAlmId();
                        String almName= DevAlmEnum.getValue(almId);
                        year.setAlmName(almName!=null?almName:"");
                        year.setDevInf(dinf);
                        year.setConfirmedUname(uinf!=null?uinf.getUname():"");
                    }
                    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.getQueryTimeForSubTablesByMonthDesc2(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 BattAlarmHistory) {
                    BattAlarmHistory battAlarmHistory = new BattAlarmHistory();
                    BeanUtils.copyProperties(pojo, battAlarmHistory);
                    battAlarmHistory.setAlmStartTime(queryTime.get(0));
                    battAlarmHistory.setAlmEndTime(queryTime.get(1));
                    battAlarmHistory.setRecordYear(tableName);
                    int currentCount = service.getBattHisCount(battAlarmHistory);
                    queryCountMap.put(tableYear, currentCount);
                 }
                else if (pojo instanceof BattStationTempHistory) {
                    BattStationTempHistory tempHistory = new BattStationTempHistory();
                    tempHistory.setStartTime(queryTime.get(0));
                    tempHistory.setEndTime(queryTime.get(1));
                    tempHistory.setRecordYearMonth(tableName);
                    int currentCount = service.getBattStationTempHisCount(tempHistory);
                    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 BattAlarmHistory) {
                BattAlarmHistory battAlarmHistory = new BattAlarmHistory();
                BeanUtils.copyProperties(pojo, battAlarmHistory);
                battAlarmHistory.setAlmStartTime(queryTime.get(0));
                battAlarmHistory.setAlmEndTime(queryTime.get(1));
                battAlarmHistory.setRecordYear(recordYear);
                List<Integer> limitList = tableAndLimitMap.get(key);
                battAlarmHistory.setLimitStart(limitList.get(0));
                battAlarmHistory.setLimitEnd(limitList.get(1));
                List<BattAlarmHistory> list =  service.getBattHisList(battAlarmHistory);
                dataList.addAll(list);
            }else if (pojo instanceof BattStationTempHistory){
                BattStationTempHistory tempHistory = new BattStationTempHistory();
                tempHistory.setStartTime(queryTime.get(0));
                tempHistory.setEndTime(queryTime.get(1));
                tempHistory.setRecordYearMonth(recordYear);
                List<Integer> limitList = tableAndLimitMap.get(key);
                tempHistory.setLimitStart(limitList.get(0));
                tempHistory.setLimitEnd(limitList.get(1));
                List<BattStationTempHistory> list =  service.getBattStationTempHisList(tempHistory);
                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;
    }
}