Просмотр исходного кода

fix(cockpit): 预警类型排行与总数同源并归并空类型

Kazerin 3 недель назад
Родитель
Сommit
6e2d81c076

+ 16 - 0
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitOverviewModulesServiceTest.java

@@ -327,6 +327,22 @@ class CockpitOverviewModulesServiceTest {
         assertEquals(1, response.getByLevel().get(1).getCount());
     }
 
+    @Test
+    void warningTypeRankingShowsPlaceholderCodeAsChineseName() {
+        // mapper 把空 / NULL 类型归成 UNKNOWN(否则空行会被丢弃、行之和小于「共 XX 起」)。
+        // 这个占位码不能直接上界面 —— 界面上只看得到中文。
+        when(adapter.loadWarningStudy(any(), any(), any())).thenReturn(warnings(0, 0, 4,
+                Collections.emptyList(),
+                Arrays.asList(typeCount("WARN-A", 3), typeCount("UNKNOWN", 1)),
+                names("WARN-A", "压力预警"), Collections.emptyMap()));
+
+        CockpitOverviewWarningStudyResponse.TypeRanking ranking =
+                service.loadWarningStudy("warning-007").getTypeRanking();
+
+        assertEquals("未分类", ranking.getTop().get(1).getName(), "占位码必须给中文名,不在界面露码值");
+        assertEquals("UNKNOWN", ranking.getTop().get(1).getCode(), "码值仍如实下发(点击筛选需要它)");
+    }
+
     @Test
     void warningTypeRankingTieBreaksByCodeAndDegradesUnknownName() {
         when(adapter.loadWarningStudy(any(), any(), any())).thenReturn(warnings(0, 0, 10,

+ 21 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/overview/CockpitOverviewModulesService.java

@@ -53,6 +53,15 @@ public class CockpitOverviewModulesService {
     // 排行不再截断(维护者 2026-10-26:近 30 日共 27 起,页面只列出 14+6+3=23 起,读者对不上数)。
     // 保留常量为 0 表达「全部」,比删掉更明确 —— 调用点读到它就知道这是刻意口径。
     private static final int RANKING_TOP_SIZE = 0;
+    /**
+     * 类型排行里代表「预警类型未填」的占位码(mapper 把空 / NULL 归入它)。
+     *
+     * 为什么需要它:{@code warning_type} 是 NOT NULL 但可为空串。旧实现让空串单独成一行,
+     * 服务层又把空 code 丢掉,于是「近30日共 XX 起」总是大于各行之和(维护者 2026-11-12)。
+     * 归一个显式占位码后行之和与 total 同源;界面用中文名,不露码值。
+     */
+    private static final String UNKNOWN_WARNING_TYPE = "UNKNOWN";
+    private static final String UNKNOWN_WARNING_TYPE_NAME = "未分类";
     private static final int TREND_DAYS = 7;
     private static final int WORK_ORDER_CLOSED = 5;
     private static final int OPER_TYPE_RECEIVE = 2;
@@ -404,7 +413,7 @@ public class CockpitOverviewModulesService {
                 snapshot == null ? null : snapshot.getTypeCounts())) {
             String code = row.getTypeCode();
             top.add(new CockpitOverviewWarningStudyResponse.TopType(
-                    code, typeName(code, typeNames, normalizedNames), row.getCount()));
+                    code, rankingTypeName(code, typeNames, normalizedNames), row.getCount()));
         }
 
         int rankingTotal = snapshot == null ? 0 : snapshot.getRankingTotal();
@@ -661,6 +670,17 @@ public class CockpitOverviewModulesService {
         return result;
     }
 
+    /**
+     * 排行行的展示名:占位码给中文名(界面不露码值),其余沿用 warning_type 词表的降级链。
+     */
+    private static String rankingTypeName(
+            String code, Map<String, String> typeNames, Map<String, String> normalizedNames) {
+        if (UNKNOWN_WARNING_TYPE.equals(code)) {
+            return UNKNOWN_WARNING_TYPE_NAME;
+        }
+        return typeName(code, typeNames, normalizedNames);
+    }
+
     private static String typeName(
             String code, Map<String, String> typeNames, Map<String, String> normalizedNames) {
         String name = code == null ? null : typeNames.get(code);

+ 10 - 3
pipe-network-service/zksy-system/src/main/resources/mapper/cockpit/CockpitOverviewModulesMapper.xml

@@ -178,16 +178,23 @@
         <include refid="deviceDistrictFilter"/>
     </select>
 
+    <!-- 类型排行:排行的每行与标题「共 XX 起」必须同源,否则读者对不上数
+         (维护者 2026-10-26:「页面「共27起」而三行只凑出 23 起」;2026-11-12 要求审查本项)。
+         所以这里与 selectWarningCountInWindow 用**完全相同的 WHERE**:时间窗 + 草稿排除 + 片区/行业。
+         注意 `warning_type` 是 NOT NULL 但可为空串:空串不能单独占一行(服务层会把它丢掉,
+         那样行之和就小于 total),所以归入一个明确的占位码 UNKNOWN,由同一套名称降级逻辑展示。 -->
     <select id="selectWarningTypeCountsInWindow" resultType="java.util.LinkedHashMap">
         <include refid="typeTree"/>
-        SELECT w.warning_type AS type_code,
+        SELECT CASE WHEN w.warning_type IS NULL OR TRIM(w.warning_type) = ''
+                    THEN 'UNKNOWN' ELSE w.warning_type END AS type_code,
                COUNT(*) AS type_count
         FROM app_user.early_warning w
         WHERE w.create_time &gt;= #{startInclusive} AND w.create_time &lt; #{endExclusive}
         <include refid="countableWarningFilter"/>
         <include refid="deviceDistrictFilter"/>
-        GROUP BY w.warning_type
-        ORDER BY COUNT(*) DESC, w.warning_type ASC
+        GROUP BY CASE WHEN w.warning_type IS NULL OR TRIM(w.warning_type) = ''
+                      THEN 'UNKNOWN' ELSE w.warning_type END
+        ORDER BY COUNT(*) DESC, type_code ASC
     </select>
 
     <!-- 基础集合 = alarm_data.audit_status = 1(维护者 2026-10-22 裁定,规格 §0)。

+ 34 - 0
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/overview/CockpitOverviewWarningStudyContractTest.java

@@ -15,6 +15,7 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -103,6 +104,39 @@ class CockpitOverviewWarningStudyContractTest {
                 "类型排行每一行都必须排除草稿");
     }
 
+    /**
+     * 类型排行必须与标题「共 XX 起」同源(维护者 2026-11-12 要求审查本项)。
+     *
+     * <p>两数来自两条查询,只有 WHERE 条件完全一致时才可能相等。历史缺陷:空类型
+     * ({@code warning_type} 为 NOT NULL 但可为空串)被 SQL 分出一行、又被服务层丢弃,
+     * 于是行之和恒小于 total。</p>
+     */
+    @Test
+    void warningTypeRankingSharesTheSameWhereAsTheTotal() {
+        String ranking = boundSql("selectWarningTypeCountsInWindow");
+        String total = boundSql("selectWarningCountInWindow");
+
+        assertTrue(ranking.contains("CASE WHEN w.warning_type IS NULL OR TRIM(w.warning_type) = ''"),
+                "空类型必须归成命名占位码,不能留下会被服务层丢掉的行:" + ranking);
+        assertTrue(ranking.contains("COUNT(*) AS type_count") && total.contains("COUNT(*) AS warning_count"),
+                "两条查询都是逐条计数,口径可以直接相加比对");
+        assertEquals(whereOf(ranking), whereOf(total),
+                "排行与总数的 WHERE 必须逐字一致,否则「共 XX 起」与各行之和对不上");
+    }
+
+    /** 取主查询的 WHERE 子句(到 GROUP BY / ORDER BY 为止;跳过前面的递归 CTE)。 */
+    private static String whereOf(String sql) {
+        int from = sql.lastIndexOf(" FROM app_user.early_warning w ");
+        assertTrue(from >= 0, "找不到主查询:" + sql);
+        String tail = sql.substring(from);
+        int end = tail.length();
+        for (String stop : new String[]{" GROUP BY ", " ORDER BY "}) {
+            int at = tail.indexOf(stop);
+            if (at >= 0 && at < end) end = at;
+        }
+        return tail.substring(0, end);
+    }
+
     @Test
     void unresolvedStatusesKeepTheirOwnAllowList() {
         String sql = boundSql("selectUnresolvedStatusLevelCounts");