package com.trafficaudit.auditengine.service;
|
|
import com.trafficaudit.common.util.RegionUtil;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.time.YearMonth;
|
import java.time.format.DateTimeParseException;
|
import java.util.LinkedHashSet;
|
import java.util.List;
|
import java.util.Set;
|
|
/** Resolves city-level track mileage source rules configured by report period. */
|
@Service
|
public class TrackMileageCitySourceRuleService {
|
private static final String DEFAULT_XIAOGAN_CITY = "孝感市";
|
private static final String DEFAULT_XIAOGAN_EFFECTIVE_PERIOD = "2026-11";
|
|
@Resource
|
private JdbcTemplate jdbcTemplate;
|
|
public Set<String> mandatoryCities(String reportPeriod) {
|
String period = normalizePeriod(reportPeriod);
|
List<String> rows = jdbcTemplate.queryForList(
|
"SELECT city_name FROM track_mileage_city_source_rule "
|
+ "WHERE enabled = 1 AND effective_from_period <= ? "
|
+ "AND (effective_to_period IS NULL OR effective_to_period >= ?) "
|
+ "ORDER BY effective_from_period DESC, id DESC",
|
String.class, period, period);
|
Set<String> cities = new LinkedHashSet<>();
|
for (String city : rows) {
|
String normalized = RegionUtil.normalizeCityName(city);
|
if (normalized != null && !normalized.isEmpty()) cities.add(normalized);
|
}
|
return cities;
|
}
|
|
public void ensureTableAndSeed() {
|
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS track_mileage_city_source_rule ("
|
+ "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
|
+ "city_name VARCHAR(50) NOT NULL,"
|
+ "effective_from_period VARCHAR(7) NOT NULL,"
|
+ "effective_to_period VARCHAR(7) NULL,"
|
+ "enabled TINYINT NOT NULL DEFAULT 1,"
|
+ "remark VARCHAR(255) NULL,"
|
+ "created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
|
+ "updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
|
+ "UNIQUE KEY uk_track_mileage_city_source_rule (city_name, effective_from_period),"
|
+ "KEY idx_track_mileage_city_source_rule_period "
|
+ "(enabled, effective_from_period, effective_to_period)"
|
+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='City track mileage source rules'");
|
jdbcTemplate.update("INSERT IGNORE INTO track_mileage_city_source_rule "
|
+ "(city_name, effective_from_period, effective_to_period, enabled, remark) "
|
+ "VALUES (?, ?, NULL, 1, ?)",
|
DEFAULT_XIAOGAN_CITY, DEFAULT_XIAOGAN_EFFECTIVE_PERIOD,
|
"From 2026-11, Xiaogan above-scale enterprises use Xiaogan third-party track mileage");
|
}
|
|
static String normalizePeriod(String reportPeriod) {
|
try {
|
return YearMonth.parse(reportPeriod == null ? "" : reportPeriod.trim()).toString();
|
} catch (DateTimeParseException ex) {
|
throw new IllegalArgumentException("报表期格式应为 yyyy-MM:" + reportPeriod, ex);
|
}
|
}
|
}
|