#
luxiaotao1123
2024-09-24 519f7e2b61cb98f42d6f530b5ae0f434f67a1dbd
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
package com.zy.acs.manager.core.scheduler;
 
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Optional;
 
/**
 * Created by vincent on 5/8/2024
 */
@Slf4j
//@Component
@SuppressWarnings("all")
public class LogDataScheduler {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ConfigService configService;
 
    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional
    public void syncLog() {
        Integer dataExpiredDays = Optional.ofNullable(configService.getVal("dataExpiredDays", Integer.class)).orElse(7);
        this.syncTaskLog(dataExpiredDays);
        this.syncActionLog(dataExpiredDays);
        this.syncSegmentLog(dataExpiredDays);
        this.syncJamLog(dataExpiredDays);
        this.syncTravelLog(dataExpiredDays);
    }
 
    public void syncTaskLog(Integer dataExpiredDays){
        try {
            String insertSql = "INSERT INTO man_task_log " +
                    "SELECT * FROM man_task " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsInserted = jdbcTemplate.update(insertSql);
            log.info("Inserted {} rows into man_task_log", rowsInserted);
 
            String deleteSql = "DELETE FROM man_task " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsDeleted = jdbcTemplate.update(deleteSql);
            log.info("Deleted {} rows from man_task", rowsDeleted);
 
        } catch (Exception e) {
            log.error("Error occurred while syncing task log: {}", e.getMessage());
            throw e;
        }
    }
 
 
    public void syncActionLog(Integer dataExpiredDays){
        try {
            String insertSql = "INSERT INTO man_action_log " +
                    "SELECT * FROM man_action " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsInserted = jdbcTemplate.update(insertSql);
            log.info("Inserted {} rows into man_action_log", rowsInserted);
 
            String deleteSql = "DELETE FROM man_action " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsDeleted = jdbcTemplate.update(deleteSql);
            log.info("Deleted {} rows from man_action", rowsDeleted);
 
        } catch (Exception e) {
            log.error("Error occurred while syncing action log: {}", e.getMessage());
            throw e;
        }
    }
 
    public void syncSegmentLog(Integer dataExpiredDays){
        try {
            String insertSql = "INSERT INTO man_segment_log " +
                    "SELECT * FROM man_segment " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsInserted = jdbcTemplate.update(insertSql);
            log.info("Inserted {} rows into man_segment_log", rowsInserted);
 
            String deleteSql = "DELETE FROM man_segment " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsDeleted = jdbcTemplate.update(deleteSql);
            log.info("Deleted {} rows from man_segment", rowsDeleted);
 
        } catch (Exception e) {
            log.error("Error occurred while syncing segment log: {}", e.getMessage());
            throw e;
        }
    }
 
    public void syncJamLog(Integer dataExpiredDays){
        try {
            String insertSql = "INSERT INTO man_jam_log " +
                    "SELECT * FROM man_jam " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsInserted = jdbcTemplate.update(insertSql);
            log.info("Inserted {} rows into man_jam_log", rowsInserted);
 
            String deleteSql = "DELETE FROM man_jam " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsDeleted = jdbcTemplate.update(deleteSql);
            log.info("Deleted {} rows from man_jam", rowsDeleted);
 
        } catch (Exception e) {
            log.error("Error occurred while syncing jam log: {}", e.getMessage());
            throw e;
        }
    }
 
    public void syncTravelLog(Integer dataExpiredDays){
        try {
            String insertSql = "INSERT INTO man_travel_log " +
                    "SELECT * FROM man_travel " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsInserted = jdbcTemplate.update(insertSql);
            log.info("Inserted {} rows into man_travel_log", rowsInserted);
 
            String deleteSql = "DELETE FROM man_travel " +
                    "WHERE create_time < NOW() - INTERVAL "+ dataExpiredDays +" DAY";
            int rowsDeleted = jdbcTemplate.update(deleteSql);
            log.info("Deleted {} rows from man_travel", rowsDeleted);
 
        } catch (Exception e) {
            log.error("Error occurred while syncing travel log: {}", e.getMessage());
            throw e;
        }
    }
 
}