package com.whyc.service;
|
|
import com.whyc.mapper.CommonMapper;
|
import com.whyc.pojo.BattalarmDataHistory;
|
import com.whyc.pojo.DevalarmDataHistory;
|
import com.whyc.pojo.PwrdevAlarmHistory;
|
import com.whyc.pojo.UserLog;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class HistoryDataArchivingService {
|
|
@Autowired
|
private PwrdevAlarmHistoryService powerAlarmHistoryService;
|
|
@Autowired
|
private BattalarmDataHistoryService battAlarmDataHistoryService;
|
|
@Autowired
|
private DevalarmDataHistoryService devAlarmDataHistoryService;
|
|
@Autowired
|
private UserLogService userLogService;
|
|
@Resource
|
private CommonMapper commonMapper;
|
|
@Autowired
|
private SubTablePageInfoService subService;
|
|
/**
|
*
|
* 电源告警历史 db_pwrdev_alarm.tb_pwrdev_alarm_history
|
* 电池告警历史 db_alarm.tb_battalarm_data_history
|
* 设备告警历史 db_alarm.tb_devalarm_data_history
|
* 操作事件 db_user.tb_user_log
|
*/
|
public void historyDataArchiving(String dbName,String tableName,int retentionMonth){
|
switch (tableName){
|
case "tb_pwrdev_alarm_history":historyDataArchiving1(retentionMonth);break;
|
case "tb_battalarm_data_history":historyDataArchiving2(retentionMonth);break;
|
case "tb_devalarm_data_history":historyDataArchiving3(retentionMonth);break;
|
case "tb_user_log":historyDataArchiving4(retentionMonth);break;
|
}
|
}
|
|
@Transactional
|
private void historyDataArchiving1(int retentionMonth){
|
Calendar instance = Calendar.getInstance();
|
instance.add(Calendar.MONTH,-retentionMonth);
|
Date retentionTime = instance.getTime();
|
|
//查询指定retentionMonth之前的数据
|
List<PwrdevAlarmHistory> dataList = powerAlarmHistoryService.getDataBeforeRetentionMonth(retentionTime);
|
//对这些数据按照年份进行划分
|
Map<String, List<PwrdevAlarmHistory>> map = dataList.stream().collect(Collectors.groupingBy(PwrdevAlarmHistory::getRecordYear));
|
//每个年份的存一张表
|
Set<String> yearKeySet = map.keySet();
|
for (String year : yearKeySet){
|
String tableNameExists = commonMapper.existTable("db_pwrdev_alarm", "tb_pwrdev_alarm_history_" + year);
|
if(tableNameExists == null) {
|
//commonMapper.createTable4PowerAlarmHistoryByYear(year);
|
subService.createTable4PowerAlarmHistoryByYear(year);
|
}
|
List<PwrdevAlarmHistory> data = map.get(year);
|
List<PwrdevAlarmHistory> temp = new LinkedList<>();
|
for (int i = 0; i < data.size(); i++) {
|
temp.add(data.get(i));
|
if(i!=0 && i%1000 == 0){ //1000一比
|
//commonMapper.insertBatch4PowerAlarmHistoryByYear(year,temp);
|
subService.insertBatch4PowerAlarmHistoryByYear(year,temp);
|
temp.clear();
|
}
|
}
|
//commonMapper.insertBatch4PowerAlarmHistoryByYear(year,temp);
|
subService.insertBatch4PowerAlarmHistoryByYear(year,temp);
|
}
|
//删除总表中已经另外存档的数据
|
powerAlarmHistoryService.deleteBeforeRetentionMonth(retentionTime);
|
}
|
|
@Transactional
|
private void historyDataArchiving2(int retentionMonth){
|
Calendar instance = Calendar.getInstance();
|
instance.add(Calendar.MONTH,-retentionMonth);
|
Date retentionTime = instance.getTime();
|
//查询指定retentionMonth之前的数据
|
List<BattalarmDataHistory> dataList = battAlarmDataHistoryService.getDataBeforeRetentionMonth(retentionTime);
|
//对这些数据按照年份进行划分
|
Map<String, List<BattalarmDataHistory>> map = dataList.stream().collect(Collectors.groupingBy(BattalarmDataHistory::getRecordYear));
|
//每个年份的存一张表
|
Set<String> yearKeySet = map.keySet();
|
for (String year : yearKeySet){
|
String tableNameExists = commonMapper.existTable("db_alarm", "tb_battalarm_data_history_" + year);
|
if(tableNameExists == null) {
|
//commonMapper.createTable4BattAlarmDataHistoryByYear(year);
|
subService.createTable4BattAlarmDataHistoryByYear(year);
|
}
|
List<BattalarmDataHistory> data = map.get(year);
|
List<BattalarmDataHistory> temp = new LinkedList<>();
|
for (int i = 0; i < data.size(); i++) {
|
temp.add(data.get(i));
|
if(i!=0 && i%1000 == 0){ //1000一比
|
//commonMapper.insertBatch4BattAlarmDataHistoryByYear(year,temp);
|
subService.insertBatch4BattAlarmDataHistoryByYear(year,temp);
|
temp.clear();
|
}
|
}
|
//commonMapper.insertBatch4BattAlarmDataHistoryByYear(year,temp);
|
subService.insertBatch4BattAlarmDataHistoryByYear(year,temp);
|
}
|
//删除总表中已经另外存档的数据
|
battAlarmDataHistoryService.deleteBeforeRetentionMonth(retentionTime);
|
}
|
|
@Transactional
|
private void historyDataArchiving3(int retentionMonth){
|
Calendar instance = Calendar.getInstance();
|
instance.add(Calendar.MONTH,-retentionMonth);
|
Date retentionTime = instance.getTime();
|
//查询指定retentionMonth之前的数据
|
List<DevalarmDataHistory> dataList = devAlarmDataHistoryService.getDataBeforeRetentionMonth(retentionTime);
|
//对这些数据按照年份进行划分
|
Map<String, List<DevalarmDataHistory>> map = dataList.stream().collect(Collectors.groupingBy(DevalarmDataHistory::getRecordYear));
|
//每个年份的存一张表
|
Set<String> yearKeySet = map.keySet();
|
for (String year : yearKeySet){
|
String tableNameExists = commonMapper.existTable("db_alarm", "tb_devalarm_data_history_" + year);
|
if(tableNameExists == null) {
|
//commonMapper.createTable4DevAlarmDataHistoryByYear(year);
|
subService.createTable4DevAlarmDataHistoryByYear(year);
|
}
|
List<DevalarmDataHistory> data = map.get(year);
|
List<DevalarmDataHistory> temp = new LinkedList<>();
|
for (int i = 0; i < data.size(); i++) {
|
temp.add(data.get(i));
|
if(i!=0 && i%1000 == 0){ //1000一比
|
//commonMapper.insertBatch4DevAlarmDataHistoryByYear(year,temp);
|
subService.insertBatch4DevAlarmDataHistoryByYear(year,temp);
|
temp.clear();
|
}
|
}
|
//commonMapper.insertBatch4DevAlarmDataHistoryByYear(year,temp);
|
subService.insertBatch4DevAlarmDataHistoryByYear(year,temp);
|
}
|
//删除总表中已经另外存档的数据
|
devAlarmDataHistoryService.deleteBeforeRetentionMonth(retentionTime);
|
}
|
|
@Transactional
|
private void historyDataArchiving4(int retentionMonth){
|
Calendar instance = Calendar.getInstance();
|
instance.add(Calendar.MONTH,-retentionMonth);
|
Date retentionTime = instance.getTime();
|
//查询指定retentionMonth之前的数据
|
List<UserLog> dataList = userLogService.getDataBeforeRetentionMonth(retentionTime);
|
//对这些数据按照年份进行划分
|
Map<String, List<UserLog>> userLogMap = dataList.stream().collect(Collectors.groupingBy(UserLog::getRecordYear));
|
//每个年份的存一张表
|
Set<String> yearKeySet = userLogMap.keySet();
|
for (String year : yearKeySet){
|
String tableNameExists = commonMapper.existTable("db_user", "tb_user_log_" + year);
|
if(tableNameExists == null) {
|
//commonMapper.createTable4UserLogByYear(year);
|
subService.createTable4UserLogByYear(year);
|
}
|
List<UserLog> userLogs = userLogMap.get(year);
|
List<UserLog> temp = new LinkedList<>();
|
for (int i = 0; i < userLogs.size(); i++) {
|
temp.add(userLogs.get(i));
|
if(i!=0 && i%1000 == 0){ //1000一比
|
//commonMapper.insertBatch4UserLogByYear(year,temp);
|
subService.insertBatch4UserLogByYear(year,temp);
|
temp.clear();
|
}
|
}
|
//commonMapper.insertBatch4UserLogByYear(year,temp);
|
subService.insertBatch4UserLogByYear(year,temp);
|
}
|
//删除总表中已经另外存档的数据
|
userLogService.deleteBeforeRetentionMonth(retentionTime);
|
}
|
}
|