|
|
@@ -0,0 +1,151 @@
|
|
|
+package com.zksy.cockpit.overview;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.zksy.cockpit.overview.mapper.CockpitOverviewModulesMapper;
|
|
|
+import org.apache.ibatis.builder.xml.XMLMapperBuilder;
|
|
|
+import org.apache.ibatis.mapping.BoundSql;
|
|
|
+import org.apache.ibatis.mapping.MappedStatement;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预警研判统计排除草稿的 SQL 契约(维护者 2026-11-12)。
|
|
|
+ *
|
|
|
+ * <p>维护者原话:「预警统计要排除草稿状态的预警。例:当前的总体概览-运行监测-预警研判中的
|
|
|
+ * 统计没有排除草稿状态的预警。」</p>
|
|
|
+ *
|
|
|
+ * <p>断言的是 MyBatis **解析并展开后**的 SQL({@code BoundSql}),不是 XML 文本:
|
|
|
+ * 片段被挪进注释、或塞进恒不成立的 {@code <if>} 时,文本断言仍会绿(与
|
|
|
+ * {@code WorkOrderAlarmConflictMapperTest} 同一写法)。</p>
|
|
|
+ *
|
|
|
+ * <p>两条口径都有断言盯着:</p>
|
|
|
+ * <ul>
|
|
|
+ * <li>计数(今日/昨日新增、区间总数、类型排行)排除草稿;</li>
|
|
|
+ * <li><b>新鲜度</b>({@code source_updated_at})<b>不</b>排除草稿 —— 它是「源数据最后动过的时间」,
|
|
|
+ * 跟着排除会让编辑草稿后面的板误判数据过旧;</li>
|
|
|
+ * <li>{@code status IS NULL} 的行保持改动前行为(仍计入),不因加筛选被默默丢掉。</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class CockpitOverviewWarningStudyContractTest {
|
|
|
+
|
|
|
+ private static MybatisConfiguration configuration;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void parseMapper() throws Exception {
|
|
|
+ configuration = new MybatisConfiguration();
|
|
|
+ // 直接读 **源码** mapper,不读 classpath 副本:`target/classes` 可能比源码旧
|
|
|
+ // (本仓库增量构建遇到过),拿旧副本解析会让本控制器假红/假绿。
|
|
|
+ java.nio.file.Path source = sourceMapper();
|
|
|
+ try (InputStream xml = java.nio.file.Files.newInputStream(source)) {
|
|
|
+ new XMLMapperBuilder(xml, configuration, source.toString(),
|
|
|
+ configuration.getSqlFragments()).parse();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 模块目录与仓库根目录两种工作目录下都能定位到源码 mapper。 */
|
|
|
+ private static java.nio.file.Path sourceMapper() {
|
|
|
+ java.nio.file.Path cwd = java.nio.file.Path.of(System.getProperty("user.dir")).toAbsolutePath();
|
|
|
+ String relative = "src/main/resources/mapper/cockpit/CockpitOverviewModulesMapper.xml";
|
|
|
+ for (java.nio.file.Path candidate : new java.nio.file.Path[]{
|
|
|
+ cwd.resolve(relative),
|
|
|
+ cwd.resolve("zksy-system").resolve(relative),
|
|
|
+ cwd.resolve("pipe-network-service").resolve("zksy-system").resolve(relative)}) {
|
|
|
+ java.nio.file.Path normalized = candidate.normalize();
|
|
|
+ if (java.nio.file.Files.exists(normalized)) {
|
|
|
+ return normalized;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new AssertionError("找不到源码 mapper(工作目录 " + cwd + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void todayAndYesterdayCountsExcludeDraftButKeepNullStatusRows() {
|
|
|
+ String sql = boundSql("selectWarningCounts");
|
|
|
+
|
|
|
+ // 每个计数分支都带排除条件(SUM 里两个 CASE,因此出现两次)
|
|
|
+ assertTrue(count(sql, draftExclusion()) == 2,
|
|
|
+ "今日/昨日新增两个计数分支都必须排除草稿:" + sql);
|
|
|
+ assertTrue(sql.contains("w.status IS NULL OR"), "NULL 行必须保留:那是加筛选前的既有行为");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void sourceUpdatedAtStillCoversDraftEdits() {
|
|
|
+ String sql = boundSql("selectWarningCounts");
|
|
|
+
|
|
|
+ // 新鲜度是整条查询唯一的 MAX,它在筛选之外:草稿更新也要刷新它。
|
|
|
+ assertTrue(sql.contains("MAX(w.update_time) AS source_updated_at"), sql);
|
|
|
+ // 只看主查询的 WHERE(前面的递归 CTE 也有 WHERE,不能拿首个 WHERE 就断)。
|
|
|
+ String where = sql.substring(sql.lastIndexOf(" FROM app_user.early_warning w WHERE"));
|
|
|
+ assertFalse(where.contains("DRAFT"),
|
|
|
+ "新鲜度不能被草稿筛选掉,否则编辑草稿后 freshness 会被误判过旧:" + where);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void warningCountInWindowExcludesDraft() {
|
|
|
+ assertTrue(boundSql("selectWarningCountInWindow").contains(draftExclusion()),
|
|
|
+ "类型排行的分母(区间总数)必须排除草稿");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void warningTypeRankingExcludesDraft() {
|
|
|
+ assertTrue(boundSql("selectWarningTypeCountsInWindow").contains(draftExclusion()),
|
|
|
+ "类型排行每一行都必须排除草稿");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void unresolvedStatusesKeepTheirOwnAllowList() {
|
|
|
+ String sql = boundSql("selectUnresolvedStatusLevelCounts");
|
|
|
+
|
|
|
+ assertTrue(sql.contains("IN ('PENDING', 'RECEIVED', 'PROCESSING', 'HANDLED')"),
|
|
|
+ "未解除集合保持白名单四态");
|
|
|
+ assertFalse(sql.contains("DRAFT"), "白名单已排除草稿,不应再叠一条条件");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String draftExclusion() {
|
|
|
+ return "(w.status IS NULL OR UPPER(TRIM(w.status)) <> 'DRAFT')";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int count(String text, String needle) {
|
|
|
+ int found = 0;
|
|
|
+ for (int at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length())) {
|
|
|
+ found += 1;
|
|
|
+ }
|
|
|
+ return found;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String boundSql(String id) {
|
|
|
+ MappedStatement statement = configuration
|
|
|
+ .getMappedStatement(CockpitOverviewModulesMapper.class.getName() + "." + id);
|
|
|
+ BoundSql bound = statement.getBoundSql(parameters());
|
|
|
+ return bound.getSql().replaceAll("\\s+", " ");
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 全部可选条件都关掉:这里只看状态口径,不看片区/行业筛选。 */
|
|
|
+ private static Map<String, Object> parameters() {
|
|
|
+ ZoneId zone = ZoneId.systemDefault();
|
|
|
+ Date yesterday = Date.from(LocalDate.parse("2026-11-11").atStartOfDay(zone).toInstant());
|
|
|
+ Date today = Date.from(LocalDate.parse("2026-11-12").atStartOfDay(zone).toInstant());
|
|
|
+ Date tomorrow = Date.from(LocalDate.parse("2026-11-13").atStartOfDay(zone).toInstant());
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("yesterdayStart", yesterday);
|
|
|
+ params.put("todayStart", today);
|
|
|
+ params.put("tomorrowStart", tomorrow);
|
|
|
+ params.put("startInclusive", yesterday);
|
|
|
+ params.put("endExclusive", tomorrow);
|
|
|
+ params.put("district", null);
|
|
|
+ params.put("unassigned", false);
|
|
|
+ params.put("industryRootTypeId", null);
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+}
|