| | |
| | | package com.whyc.util; |
| | | |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.Com; |
| | | import com.whyc.dto.InterfaceParam; |
| | | 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 org.springframework.beans.BeanUtils; |
| | | 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; |
| | | |
| | |
| | | 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")){ |
| | |
| | | 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); |
| | | 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); |
| | | 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); |
| | | 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); |
| | | queryCountMap.put(tableYear, currentCount); |
| | | } else { |
| | | param.setAlmStartTime(queryTime.get(0)); |
| | | param.setAlmStartTime1(queryTime.get(1)); |
| | | param.setRecordYear(tableName); |
| | | int currentCount = powerAlarmHistoryMapper.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); |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | 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<>(); |
| | |
| | | |
| | | //最后一个算法:上面不应该先查询所有记录,应该取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) { |