|
@@ -0,0 +1,106 @@
|
|
|
+package com.ozs.web.core.util;
|
|
|
+
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.ozs.service.entity.MqLog;
|
|
|
+import com.ozs.service.service.MqLogService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+@EnableAsync
|
|
|
+public class TableCreationUtil {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(TableCreationUtil.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+ @Autowired
|
|
|
+ ThreadPoolExecutor executor;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MqLogService mqLogService;
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Scheduled(cron = "0 0 1 1 * ?")
|
|
|
+ public void tableCreationAndDelete() {
|
|
|
+ log.info("开始执行");
|
|
|
+ // 获取当前日期
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+
|
|
|
+ // 定义格式
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
+
|
|
|
+ // 格式化当前日期
|
|
|
+ String formattedDate = today.format(formatter);
|
|
|
+ //执行1
|
|
|
+ CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
|
|
|
+ createTable(formattedDate);
|
|
|
+ return 1;
|
|
|
+ }, executor);
|
|
|
+
|
|
|
+ // 执行2,确保在 future1 完成后执行
|
|
|
+ CompletableFuture<String> future2 = future1.thenApplyAsync(f1 -> batchInsertData(formattedDate), executor);
|
|
|
+
|
|
|
+ // 执行3,确保在 future1 和 future2 完成之后执行
|
|
|
+ future1.thenCombine(future2, (f1, f2) -> {
|
|
|
+ deleteMqLogData(f2);
|
|
|
+ return null; // 用于满足返回值
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void createTable(String date) {
|
|
|
+ log.info("开始执行---createTable");
|
|
|
+ // 创建表的SQL语句
|
|
|
+ String createTableSql = "CREATE TABLE mq_log_" + date + " (\n" +
|
|
|
+ " `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
|
|
|
+ " `camera_code` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '相机编码',\n" +
|
|
|
+ " `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',\n" +
|
|
|
+ " PRIMARY KEY (`id`) USING BTREE\n" +
|
|
|
+ ") ENGINE = InnoDB AUTO_INCREMENT = 1851171506483142658 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;";
|
|
|
+
|
|
|
+ // 使用JdbcTemplate执行SQL语句
|
|
|
+ jdbcTemplate.execute(createTableSql);
|
|
|
+ log.info("Table created successfully after application startup!");
|
|
|
+ }
|
|
|
+
|
|
|
+ public String batchInsertData(String date) {
|
|
|
+ log.info("开始执行---batchInsertData"+date);
|
|
|
+ // 获取当前日期
|
|
|
+ Date currentDate = new Date();
|
|
|
+ // 创建SimpleDateFormat对象,指定所需的日期格式
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ // 格式化日期
|
|
|
+ String formattedDate = formatter.format(currentDate);
|
|
|
+ log.info("-----formattedDate------"+currentDate);
|
|
|
+ // 批量插入的SQL语句
|
|
|
+ String insertSql = "INSERT INTO mq_log_" + date + " (camera_code, create_time) " +
|
|
|
+ "SELECT camera_code, create_time FROM mq_log WHERE create_time <= ?";
|
|
|
+
|
|
|
+ // 使用JdbcTemplate执行插入操作
|
|
|
+ int rowsAffected = jdbcTemplate.update(insertSql, formattedDate);
|
|
|
+
|
|
|
+ log.info("Batch insert completed, {} rows inserted from mq_log_ to my_table.", rowsAffected);
|
|
|
+ return formattedDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteMqLogData(String date) {
|
|
|
+ log.info("开始执行---deleteMqLogData"+date);
|
|
|
+ mqLogService.remove(new LambdaQueryWrapper<MqLog>().le(MqLog::getCreateTime,date));
|
|
|
+ }
|
|
|
+}
|