package jnpf.bizcommon.audit.service.listener; import com.alibaba.fastjson.JSON; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; /** 子表差异从 VisualLog listLog 进入统一审计契约的轻量探针。 */ public final class AuditChildTableDiffProbe { private AuditChildTableDiffProbe() { } public static void main(String[] args) { Map childField = map("prop", "xiangmu", "label", "项目", "jnpfKey", "input"); Map childRow = map("xiangmu", "新项目", "jnpf_old_xiangmu", "旧项目", "jnpf_type", 1); Map tableLog = map( "field", "tableField1", "fieldName", "检测项目", "jnpfKey", "table", "type", 1, "targetTable", "lims_jianyan_xiang", "chidField", Collections.singletonList(childField), "chidData", Collections.singletonList(childRow)); Map mainLog = map("field", "biz_status", "fieldName", "状态", "jnpfKey", "select"); List> childDiffs = AuditVisualLogListener.childDiffs(Arrays.asList(mainLog, tableLog)); check("只提取一个子表项", childDiffs.size() == 1); check("保留子表数据", childDiffs.get(0).get("chidData") instanceof List); check("保留子表表头", childDiffs.get(0).get("chidField") instanceof List); check("保留子表物理表名", "lims_jianyan_xiang".equals(childDiffs.get(0).get("targetTable"))); String merged = AuditVisualLogListener.appendChildDiffs( "[{\"field\":\"biz_status\",\"oldData\":\"created\",\"newData\":\"confirmed\"}]", childDiffs); List mergedItems = JSON.parseArray(merged); check("主表和子表差异同时保留", mergedItems.size() == 2); Map row = map("biz_status", "confirmed", "tableField1", "[{...}]"); Map withoutChild = AuditVisualLogListener.withoutFields( row, new LinkedHashSet<>(Collections.singletonList("tablefield1"))); check("普通字段 diff 排除子表原始数组", withoutChild.containsKey("biz_status") && !withoutChild.containsKey("tableField1")); } private static Map map(Object... values) { Map result = new LinkedHashMap<>(); for (int i = 0; i < values.length; i += 2) { result.put(String.valueOf(values[i]), values[i + 1]); } return result; } private static void check(String message, boolean condition) { if (!condition) { throw new AssertionError(message); } System.out.println("PASS: " + message); } }