xyc
9 小时以前 1c9505d00bd076898cdc5a7d916106e175a91119
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
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);
        }
    }
}