package com.zy.acs.manager.core.scheduler; 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.stereotype.Component; import org.springframework.transaction.annotation.Transactional; /** * Created by vincent on 5/8/2024 */ @Slf4j @Component @SuppressWarnings("all") public class LogDataScheduler { public static final Integer EXPIRED_DAYS = 7; @Autowired private JdbcTemplate jdbcTemplate; @Scheduled(cron = "0 0 1 * * ?") @Transactional public void syncLog() { this.syncTaskLog(); this.syncActionLog(); this.syncSegmentLog(); this.syncJamLog(); this.syncTravelLog(); } public void syncTaskLog(){ try { String insertSql = "INSERT INTO man_task_log " + "SELECT * FROM man_task " + "WHERE create_time < NOW() - INTERVAL "+ EXPIRED_DAYS +" 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 "+ EXPIRED_DAYS +" 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(){ try { String insertSql = "INSERT INTO man_action_log " + "SELECT * FROM man_action " + "WHERE create_time < NOW() - INTERVAL "+ EXPIRED_DAYS +" 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 "+ EXPIRED_DAYS +" 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(){ try { String insertSql = "INSERT INTO man_segment_log " + "SELECT * FROM man_segment " + "WHERE create_time < NOW() - INTERVAL "+ EXPIRED_DAYS +" 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 "+ EXPIRED_DAYS +" 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(){ try { String insertSql = "INSERT INTO man_jam_log " + "SELECT * FROM man_jam " + "WHERE create_time < NOW() - INTERVAL "+ EXPIRED_DAYS +" 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 "+ EXPIRED_DAYS +" 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(){ try { String insertSql = "INSERT INTO man_travel_log " + "SELECT * FROM man_travel " + "WHERE create_time < NOW() - INTERVAL "+ EXPIRED_DAYS +" 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 "+ EXPIRED_DAYS +" 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; } } }