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);
|
}
|
}
|