whycxzp
2023-02-16 e7c8018af7c38165f58db57bce13ac904bd7aa6c
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
package com.whyc.service;
 
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.UserLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service
public class HistoryDataArchivingService {
 
    @Resource
    private UserLogService userLogService;
 
    @Resource
    private CommonMapper commonMapper;
 
    @Transactional
    public void historyDataArchiving(String dbName,String tableName,int retentionMonth){
        //查询指定retentionMonth之前的数据
        List<UserLog> dataList = userLogService.getDataBeforeRetentionMonth(retentionMonth);
        //对这些数据按照年份进行划分
        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(retentionMonth);
    }
}