|
|
|
@ -4,6 +4,7 @@ import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import java.sql.*; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
public class MySQLService { |
|
|
|
@ -101,4 +102,27 @@ public class MySQLService { |
|
|
|
} |
|
|
|
return total; |
|
|
|
} |
|
|
|
|
|
|
|
public int deleteBeforeDate(String schema, String table, LocalDate fileDate) { |
|
|
|
// 计算比较值: yyyyMMdd 格式的整数(例如 20251008)
|
|
|
|
int cutoff = fileDate.getYear() * 10000 + fileDate.getMonthValue() * 100 + fileDate.getDayOfMonth(); |
|
|
|
|
|
|
|
String sql = String.format( |
|
|
|
"DELETE FROM %s.%s WHERE (COALESCE(date_year,0) * 10000 + COALESCE(date_month,0) * 100 + COALESCE(date_day,0)) < ?", |
|
|
|
schema, table |
|
|
|
); |
|
|
|
logger.info("deleteBeforeDate sql: {}, cutoff:{}", sql, cutoff); |
|
|
|
|
|
|
|
try (Connection conn = java.sql.DriverManager.getConnection(MYSQL_URL, DB_USER, DB_PASSWORD); |
|
|
|
PreparedStatement ps = conn.prepareStatement(sql)) { |
|
|
|
ps.setInt(1, cutoff); |
|
|
|
int deleted = ps.executeUpdate(); |
|
|
|
logger.info("deleteBeforeDate: schema={}, table={}, cutoff={}, deleted={}", schema, table, cutoff, deleted); |
|
|
|
return deleted; |
|
|
|
} catch (SQLException e) { |
|
|
|
logger.error("deleteBeforeDate failed for {}/{} cutoff={}", schema, table, cutoff, e); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|