lxw
2023-07-29 d1675cea759f0090c43860efabdeb9448d25c41d
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
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;
 
    /**
     *
     * 电源告警历史 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);
            }
            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);
                    temp.clear();
                }
            }
            commonMapper.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);
            }
            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);
                    temp.clear();
                }
            }
            commonMapper.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);
            }
            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);
                    temp.clear();
                }
            }
            commonMapper.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);
            }
            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);
                    temp.clear();
                }
            }
            commonMapper.insertBatch4UserLogByYear(year,temp);
        }
        //删除总表中已经另外存档的数据
        userLogService.deleteBeforeRetentionMonth(retentionTime);
    }
}