刘光辉
6 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
package jnpf.dmsIntegration.audit;
 
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jnpf.audit.AuditConsts;
import jnpf.audit.diff.AuditFieldDiff;
import jnpf.audit.model.AuditEventDTO;
import jnpf.audit.model.AuditRelatedSignDTO;
import jnpf.audit.sdk.AuditClient;
import jnpf.base.ModuleApi;
import jnpf.base.entity.ModuleEntity;
import jnpf.dmsEntity.fenfa.DmsWenjianDayinJiluEntity;
import jnpf.dmsEntity.fenfa.DmsWenjianDayinRenwuEntity;
import jnpf.dmsEntity.fenfa.DmsWenjianFenfaRenwuEntity;
import jnpf.dmsEntity.fenfa.DmsWenjianHuishouMingxiEntity;
import jnpf.dmsEntity.permission.entity.DmsAccessExclusionEntity;
import jnpf.dmsEntity.permission.entity.DmsFilePermissionEntity;
import jnpf.util.JsonUtil;
import jnpf.util.ServletUtil;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
 
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Component
public class DmsAuditAdapter {
    private static final String FOLDER_TABLE = "dms_folders";
    private static final String FOLDER_BIZ_MODULE = "DMS";
    private static final String FOLDER_BIZ_TYPE = "DMS_FOLDER";
    private static final String FILE_TABLE = "dms_documents";
    private static final String FILE_VERSION_TABLE = "dms_file_versions";
    private static final String VERSION_STATE_TABLE = "dms_version_states";
    private static final String FILE_BIZ_TYPE = "DMS_FILE";
    private static final String FILE_ENTRY_TYPE = "MENU";
    private static final String FILE_ENTRY_ID = "dms-file-center";
    private static final String FILE_ENTRY_NAME = "DMS 文件中心";
    private static final String PERMISSION_BIZ_TYPE = "DMS_PERMISSION";
    private static final String PERMISSION_ENTRY_ID = "dms-permission-management";
    private static final String PERMISSION_ENTRY_NAME = "DMS 权限管理";
    private static final String RECYCLE_MAIN_TABLE = "dms_wenjian_huishou";
    private static final String RECYCLE_DETAIL_TABLE = "dms_wenjian_huishou_zixiang";
    private static final String PRINT_RECORD_TABLE = "dms_wenjian_dayin_jilu";
    private static final String RECYCLE_BIZ_TYPE = "DMS_FILE_RECYCLE";
    private static final String RECYCLE_ENTRY_ID = "POST:/api/dms/biz/wenjian/fenfa/huishou";
    private static final String RECYCLE_ENTRY_NAME = "文件扫码回收";
    private static final String PRINT_TASK_TABLE = "dms_wenjian_dayin_renwu";
    private static final String DISTRIBUTION_TASK_TABLE = "dms_wenjian_fenfa_renwu";
    private static final String PRINT_BIZ_TYPE = "DMS_FILE_PRINT";
    private static final String PRINT_SOURCE = "LIST_PRINT";
    private static final long MENU_CACHE_MILLIS = 5 * 60 * 1000L;
    private static final Map<String, AuditFieldDiff.FieldMeta> FOLDER_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> FILE_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> PERMISSION_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> EXCLUSION_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> RECYCLE_MAIN_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> RECYCLE_DETAIL_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> RECYCLE_PRINT_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> PRINT_TASK_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> PRINT_RECORD_FIELD_META;
    private static final Map<String, AuditFieldDiff.FieldMeta> DISTRIBUTION_TASK_FIELD_META;
 
    static {
        Map<String, AuditFieldDiff.FieldMeta> meta = new LinkedHashMap<>();
        meta.put("name", new AuditFieldDiff.FieldMeta("文件夹名称", null, null));
        meta.put("parent_id", new AuditFieldDiff.FieldMeta("父文件夹", null, null));
        FOLDER_FIELD_META = Collections.unmodifiableMap(meta);
 
        Map<String, AuditFieldDiff.FieldMeta> fileMeta = new LinkedHashMap<>();
        fileMeta.put("document_no", meta("文件编号"));
        fileMeta.put("name", meta("文件名称"));
        fileMeta.put("document_type", meta("文件类型"));
        fileMeta.put("responsible_dept_id", meta("归属部门"));
        fileMeta.put("folder_id", meta("所属文件夹"));
        fileMeta.put("version_no", meta("版本序号"));
        fileMeta.put("version_label", meta("版本号"));
        fileMeta.put("file_name", meta("文件名"));
        fileMeta.put("mime_type", meta("MIME 类型"));
        fileMeta.put("size_bytes", meta("文件大小"));
        fileMeta.put("checksum_sha256", meta("SHA-256"));
        fileMeta.put("prepared_by", meta("制定人"));
        fileMeta.put("prepared_on", meta("制定日期"));
        fileMeta.put("lifecycle_status", lifecycleStatusMeta());
        fileMeta.put("effective_from", meta("生效时间"));
        fileMeta.put("effective_to", meta("失效时间"));
        fileMeta.put("status_reason", meta("状态变更原因"));
        FILE_FIELD_META = Collections.unmodifiableMap(fileMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> permissionMeta = new LinkedHashMap<>();
        permissionMeta.put("resource_type", options("资源类型", "FOLDER", "文件夹",
                "DOCUMENT", "文件", "FILE_VERSION", "文件版本"));
        permissionMeta.put("resource_id", meta("授权资源"));
        permissionMeta.put("resource_scope", options("授权范围", "SELF", "当前资源",
                "SUBTREE", "当前文件夹及子树"));
        permissionMeta.put("principal_type", options("主体类型", "USER", "用户", "ORG", "组织",
                "POSITION", "岗位", "ROLE", "角色", "GROUP", "权限组"));
        permissionMeta.put("principal_id", meta("授权主体"));
        permissionMeta.put("include_child_orgs", booleanMeta("包含子组织"));
        permissionMeta.put("approved_org_scope", options("审核查阅部门范围", "SELF", "本部门",
                "CHILDREN", "本部门及直接子部门", "DESCENDANTS", "本部门及全部下级"));
        permissionMeta.put("action_code", actionMeta());
        permissionMeta.put("source_type", sourceTypeMeta());
        permissionMeta.put("source_id", meta("来源标识"));
        permissionMeta.put("valid_from", meta("生效时间"));
        permissionMeta.put("valid_to", meta("失效时间"));
        permissionMeta.put("max_use_count", meta("最大使用次数"));
        permissionMeta.put("used_count", meta("已使用次数"));
        permissionMeta.put("grant_reason", meta("授权原因"));
        permissionMeta.put("granted_by_type", options("授权人类型", "USER", "用户", "SYSTEM", "系统"));
        permissionMeta.put("granted_by_id", meta("授权人"));
        permissionMeta.put("revoked_at", meta("撤销时间"));
        permissionMeta.put("revoked_by_type", options("撤销人类型", "USER", "用户", "SYSTEM", "系统"));
        permissionMeta.put("revoked_by_id", meta("撤销人"));
        permissionMeta.put("revoke_reason", meta("撤销原因"));
        permissionMeta.put("revision", meta("修订号"));
        PERMISSION_FIELD_META = Collections.unmodifiableMap(permissionMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> exclusionMeta = new LinkedHashMap<>();
        exclusionMeta.put("resource_type", permissionMeta.get("resource_type"));
        exclusionMeta.put("resource_id", meta("排除资源"));
        exclusionMeta.put("user_id", meta("排除用户"));
        exclusionMeta.put("action_code", actionMeta());
        exclusionMeta.put("source_type", sourceTypeMeta());
        exclusionMeta.put("source_id", meta("来源标识"));
        exclusionMeta.put("valid_from", meta("生效时间"));
        exclusionMeta.put("valid_to", meta("失效时间"));
        exclusionMeta.put("exclude_reason", meta("排除原因"));
        exclusionMeta.put("excluded_by_type", options("排除操作人类型", "USER", "用户", "SYSTEM", "系统"));
        exclusionMeta.put("excluded_by_id", meta("排除操作人"));
        exclusionMeta.put("revoked_at", meta("撤销时间"));
        exclusionMeta.put("revoked_by_type", permissionMeta.get("revoked_by_type"));
        exclusionMeta.put("revoked_by_id", permissionMeta.get("revoked_by_id"));
        exclusionMeta.put("revoke_reason", permissionMeta.get("revoke_reason"));
        exclusionMeta.put("revision", permissionMeta.get("revision"));
        EXCLUSION_FIELD_META = Collections.unmodifiableMap(exclusionMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> recycleMainMeta = new LinkedHashMap<>();
        recycleMainMeta.put("beizhu_shuoming", meta("回收备注"));
        RECYCLE_MAIN_FIELD_META = Collections.unmodifiableMap(recycleMainMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> recycleDetailMeta = new LinkedHashMap<>();
        recycleDetailMeta.put("f_foreign_id", meta("回收单"));
        recycleDetailMeta.put("dayin_jilu_id", meta("打印记录"));
        recycleDetailMeta.put("dayin_liushuihao", meta("打印流水号"));
        recycleDetailMeta.put("wenjian_bianhao", meta("文件编号"));
        recycleDetailMeta.put("wenjian_mingcheng", meta("文件名称"));
        recycleDetailMeta.put("banbenhao", meta("版本号"));
        recycleDetailMeta.put("qianshou_bumen", meta("签收部门"));
        recycleDetailMeta.put("qianshou_ren", meta("签收人"));
        recycleDetailMeta.put("qianshou_shijian", meta("签收时间"));
        recycleDetailMeta.put("huishou_ren_id", meta("回收人"));
        recycleDetailMeta.put("huishou_shijian", meta("回收时间"));
        RECYCLE_DETAIL_FIELD_META = Collections.unmodifiableMap(recycleDetailMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> recyclePrintMeta = new LinkedHashMap<>();
        recyclePrintMeta.put("biz_status", options(
                "文件副本状态", "daifafang", "待发放", "daiqianshou", "待签收",
                "daihuishou", "待回收", "daixiaohui", "待销毁"));
        RECYCLE_PRINT_FIELD_META = Collections.unmodifiableMap(recyclePrintMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> printTaskMeta = new LinkedHashMap<>();
        printTaskMeta.put("biz_status", options(
                "打印状态", "daidayin", "待打印", "bufendayin", "部分打印", "yidayin", "已打印"));
        PRINT_TASK_FIELD_META = Collections.unmodifiableMap(printTaskMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> printRecordMeta = new LinkedHashMap<>();
        printRecordMeta.put("dayin_renwu_id", meta("打印任务"));
        printRecordMeta.put("dayin_xuhao", meta("打印序号"));
        printRecordMeta.put("dayin_liushuihao", meta("打印流水号"));
        printRecordMeta.put("dayin_jishu_liushuihao", meta("计数流水号"));
        printRecordMeta.put("dayin_ren_id", meta("打印人"));
        printRecordMeta.put("dayin_bumen_id", meta("打印部门"));
        printRecordMeta.put("dayin_shijian", meta("打印时间"));
        printRecordMeta.put("biz_status", options(
                "分发状态", "daifafang", "待发放", "yiguidang", "已归档"));
        PRINT_RECORD_FIELD_META = Collections.unmodifiableMap(printRecordMeta);
 
        Map<String, AuditFieldDiff.FieldMeta> distributionTaskMeta = new LinkedHashMap<>();
        distributionTaskMeta.put("dayin_renwu_id", meta("打印任务"));
        distributionTaskMeta.put("fenfa_bianhao", meta("分发编号"));
        distributionTaskMeta.put("fafang_leixing", options(
                "发放类型", "zidongfenfa", "自动分发"));
        distributionTaskMeta.put("biz_status", options(
                "分发状态", "daifafang", "待发放"));
        DISTRIBUTION_TASK_FIELD_META = Collections.unmodifiableMap(distributionTaskMeta);
    }
 
    private final ObjectProvider<AuditClient> auditClient;
    private final ObjectProvider<DmsPermissionAuditDisplayResolver> permissionDisplayResolver;
    private final ObjectProvider<ModuleApi> moduleApi;
    private final Map<String, MenuCacheEntry> menuCache = new ConcurrentHashMap<>();
 
    @Autowired
    public DmsAuditAdapter(ObjectProvider<AuditClient> auditClient,
                           ObjectProvider<DmsPermissionAuditDisplayResolver> permissionDisplayResolver,
                           ObjectProvider<ModuleApi> moduleApi) {
        this.auditClient = auditClient;
        this.permissionDisplayResolver = permissionDisplayResolver;
        this.moduleApi = moduleApi;
    }
 
    /** Test and compatibility constructor for callers without menu resolution. */
    public DmsAuditAdapter(ObjectProvider<AuditClient> auditClient,
                           ObjectProvider<DmsPermissionAuditDisplayResolver> permissionDisplayResolver) {
        this.auditClient = auditClient;
        this.permissionDisplayResolver = permissionDisplayResolver;
        this.moduleApi = null;
    }
 
    /** Test and compatibility constructor for callers that do not need permission display resolution. */
    public DmsAuditAdapter(ObjectProvider<AuditClient> auditClient) {
        this.auditClient = auditClient;
        this.permissionDisplayResolver = null;
        this.moduleApi = null;
    }
 
    public void dataChange(String actionCode, String actionLabel, String table, String id,
                           String reason, String extra) {
        record(AuditConsts.TYPE_DATA_CHANGE, actionCode, actionLabel, table, id, reason, extra);
    }
 
    public void permissionChange(String actionCode, String actionLabel,
                                 List<DmsFilePermissionEntity> before,
                                 List<DmsFilePermissionEntity> after,
                                 List<AuditRelatedSignDTO> relatedSigns) {
        try {
            List<DmsFilePermissionEntity> retained = retainedPermissions(before, after);
            if (retained.isEmpty()) return;
            DmsPermissionAuditDisplayResolver.DisplayValues displays = permissionDisplays(retained);
            List<Map<String, Object>> targets = new ArrayList<>();
            Map<UUID, DmsFilePermissionEntity> beforeById = permissionsById(before);
            for (DmsFilePermissionEntity item : retained) {
                List<Map<String, Object>> changes = permissionDiffs(beforeById.get(item.getId()), item,
                        displays, false);
                targets.add(permissionTarget(item, changes, displays));
            }
            DmsFilePermissionEntity first = retained.get(0);
            PermissionActionSummary actions = permissionActions(retained);
            Map<String, Object> extra = batchExtra(targets);
            extra.put("permissionId", value(first.getId()));
            extra.put("actionCodes", actions.codes);
            extra.put("actionNames", actions.names);
            extra.put("sourceType", first.getSourceType());
            extra.put("sourceId", first.getSourceId());
            List<Map<String, Object>> firstDiffs = permissionDiffs(
                    beforeById.get(first.getId()), first, displays, true);
            combineActionDiff(firstDiffs, actions);
            Map<String, Object> firstSnapshot = permissionSnapshot(first, displays);
            combineActionSnapshot(firstSnapshot, actions);
            recordPermissionEvent(actionCode, actionLabel, first,
                    firstDiffs, firstSnapshot, extra,
                    first.getRevokeReason() == null ? first.getGrantReason() : first.getRevokeReason(),
                    permissionTitle(first, displays, actions.display), relatedSigns);
        } catch (RuntimeException ex) {
            log.warn("DMS permission audit event construction failed actionCode={}", actionCode, ex);
        }
    }
 
    public void accessExclusionChange(String actionCode, String actionLabel,
                                      List<DmsAccessExclusionEntity> before,
                                      List<DmsAccessExclusionEntity> after,
                                      List<AuditRelatedSignDTO> relatedSigns) {
        try {
            List<DmsAccessExclusionEntity> retained = retainedExclusions(before, after);
            if (retained.isEmpty()) return;
            DmsPermissionAuditDisplayResolver.DisplayValues displays = exclusionDisplays(retained);
            List<Map<String, Object>> targets = new ArrayList<>();
            Map<UUID, DmsAccessExclusionEntity> beforeById = exclusionsById(before);
            for (DmsAccessExclusionEntity item : retained) {
                List<Map<String, Object>> changes = exclusionDiffs(beforeById.get(item.getId()), item,
                        displays, false);
                targets.add(exclusionTarget(item, changes, displays));
            }
            DmsAccessExclusionEntity first = retained.get(0);
            Map<String, Object> extra = batchExtra(targets);
            extra.put("exclusionId", value(first.getId()));
            extra.put("sourceType", first.getSourceType());
            extra.put("sourceId", first.getSourceId());
            recordExclusionEvent(actionCode, actionLabel, first,
                    exclusionDiffs(beforeById.get(first.getId()), first, displays, true),
                    exclusionSnapshot(first, displays), extra,
                    first.getRevokeReason() == null ? first.getExcludeReason() : first.getRevokeReason(),
                    exclusionTitle(first, displays), relatedSigns);
        } catch (RuntimeException ex) {
            log.warn("DMS access exclusion audit event construction failed actionCode={}", actionCode, ex);
        }
    }
 
    public void folderChange(DmsFolderAuditEvent event) {
        if (event == null || event.getFolderId() == null) {
            throw new IllegalArgumentException("文件夹审计事件和 folderId 不能为空");
        }
        DmsFolderAuditSnapshot before = event.getBefore();
        DmsFolderAuditSnapshot after = event.getAfter();
        Map<String, Object> extra = new LinkedHashMap<>();
        extra.put("schemaVersion", 1);
        extra.put("oldParentId", value(before == null ? null : before.getParentId()));
        extra.put("newParentId", value(after == null ? null : after.getParentId()));
        extra.put("oldParentName", before == null ? null : before.getParentName());
        extra.put("newParentName", after == null ? null : after.getParentName());
        extra.put("oldPath", before == null ? null : before.getPath());
        extra.put("newPath", after == null ? null : after.getPath());
        extra.put("requestRevision", event.getRequestRevision());
        extra.put("resultRevision", after == null ? null : after.getRevision());
        extra.put("signatureRequired", event.isSignatureRequired());
 
        DmsFolderAuditSnapshot retained = after == null ? before : after;
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(AuditConsts.TYPE_DATA_CHANGE)
                .actionCode(event.getActionCode())
                .actionLabel(event.getActionLabel())
                .bizModule(FOLDER_BIZ_MODULE)
                .bizType(FOLDER_BIZ_TYPE)
                .bizCode(event.getFolderId().toString())
                .targetTable(FOLDER_TABLE)
                .targetId(event.getFolderId().toString())
                .fieldDiffs(AuditFieldDiff.diff(diffData(before), diffData(after),
                        Collections.emptySet(), FOLDER_FIELD_META))
                .dataSnapshot(JsonUtil.getObjectToString(snapshotData(retained)))
                .extra(JsonUtil.getObjectToString(extra))
                .recordTitle(title(before, after, event.getFolderId().toString()))
                .entryType(event.getEntryType())
                .entryId(event.getEntryId())
                .entryName(event.getEntryName())
                .relatedSigns(event.getRelatedSigns())
                .sourceLayer(3)
                .build()));
    }
 
    public void fileChange(DmsFileAuditEvent event) {
        if (event == null || event.getDocumentId() == null) {
            throw new IllegalArgumentException("文件审计事件和 documentId 不能为空");
        }
        DmsFileAuditSnapshot before = event.getBefore();
        DmsFileAuditSnapshot after = event.getAfter();
        DmsFileAuditSnapshot retained = after == null ? before : after;
        List<Map<String, Object>> diffs = new ArrayList<>();
        addScopedDiffs(diffs, FILE_TABLE, event.getDocumentId().toString(),
                documentDiffData(before), documentDiffData(after));
 
        DmsFileVersionAuditSnapshot beforeVersion = before == null ? null : before.getCurrentVersion();
        DmsFileVersionAuditSnapshot afterVersion = after == null ? null : after.getCurrentVersion();
        if (afterVersion != null && (beforeVersion == null
                || !afterVersion.getFileVersionId().equals(beforeVersion.getFileVersionId()))) {
            addScopedDiffs(diffs, FILE_VERSION_TABLE, value(afterVersion.getFileVersionId()),
                    null, versionDiffData(afterVersion));
            addScopedDiffs(diffs, VERSION_STATE_TABLE, value(afterVersion.getFileVersionId()),
                    null, stateDiffData(afterVersion));
        }
        if (beforeVersion != null && event.getPreviousVersionAfter() != null) {
            addScopedDiffs(diffs, VERSION_STATE_TABLE, value(beforeVersion.getFileVersionId()),
                    stateDiffData(beforeVersion), stateDiffData(event.getPreviousVersionAfter()));
        }
 
        Map<String, Object> extra = new LinkedHashMap<>();
        extra.put("schemaVersion", 1);
        extra.put("sourceServiceId", event.getSourceServiceId());
        extra.put("sourceId", event.getSourceId());
        extra.put("baseFileVersionId", beforeVersion == null ? null : value(beforeVersion.getFileVersionId()));
        extra.put("newFileVersionId", afterVersion == null ? null : value(afterVersion.getFileVersionId()));
        extra.put("oldVersionLabel", beforeVersion == null ? null : beforeVersion.getVersionLabel());
        extra.put("newVersionLabel", afterVersion == null ? null : afterVersion.getVersionLabel());
        extra.put("oldChecksumSha256", beforeVersion == null ? null : beforeVersion.getChecksumSha256());
        extra.put("newChecksumSha256", afterVersion == null ? null : afterVersion.getChecksumSha256());
 
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(AuditConsts.TYPE_DATA_CHANGE)
                .actionCode(event.getActionCode())
                .actionLabel(event.getActionLabel())
                .bizModule(FOLDER_BIZ_MODULE)
                .bizType(FILE_BIZ_TYPE)
                .bizCode(retained == null || retained.getDocumentNo() == null
                        ? event.getDocumentId().toString() : retained.getDocumentNo())
                .targetTable(FILE_TABLE)
                .targetId(event.getDocumentId().toString())
                .fieldDiffs(JsonUtil.getObjectToString(diffs))
                .dataSnapshot(JsonUtil.getObjectToString(fileSnapshotData(retained)))
                .reason(event.getReason())
                .extra(JsonUtil.getObjectToString(extra))
                .recordTitle(fileTitle(before, after, event.getDocumentId().toString()))
                .entryType(FILE_ENTRY_TYPE)
                .entryId(FILE_ENTRY_ID)
                .entryName(FILE_ENTRY_NAME)
                .relatedSigns(event.getRelatedSigns())
                .sourceLayer(3)
                .build()));
    }
 
    /** 文件归档两阶段状态变更;不借用回收单据承载归档审计。 */
    public void archiveStatus(List<String> recordIds, String before, String after,
                              Map<String, ?> signParameters) {
        if (recordIds == null || recordIds.isEmpty()) return;
        List<Map<String, Object>> diffs = new ArrayList<>();
        Map<String, AuditFieldDiff.FieldMeta> statusMeta = Collections.singletonMap("biz_status",
                options("文件副本状态", "daihuishou", "待回收", "daiguidang", "待归档", "yiguidang", "已归档"));
        for (String id : recordIds) {
            addScopedDiffs(diffs, PRINT_RECORD_TABLE, id,
                    Collections.<String, Object>singletonMap("biz_status", before),
                    Collections.<String, Object>singletonMap("biz_status", after), statusMeta);
        }
        Map<String, Object> snapshot = new LinkedHashMap<>();
        snapshot.put("dayin_jilu_ids", recordIds);
        snapshot.put("before_status", before);
        snapshot.put("after_status", after);
        boolean completed = "yiguidang".equals(after);
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(AuditConsts.TYPE_DATA_CHANGE)
                .actionCode(completed ? "FILE_ARCHIVE_COMPLETE" : "FILE_ARCHIVE_CONFIRM")
                .actionLabel(completed ? "文件归档完成" : "文件确认归档")
                .bizModule(FOLDER_BIZ_MODULE).bizType("DMS_FILE_ARCHIVE")
                .bizCode(recordIds.get(0)).targetTable(PRINT_RECORD_TABLE).targetId(recordIds.get(0))
                .fieldDiffs(JsonUtil.getObjectToString(diffs))
                .dataSnapshot(JsonUtil.getObjectToString(snapshot))
                .relatedSigns(DmsAuditRequestSignResolver.resolve(signParameters))
                .sourceLayer(3).build()));
    }
 
    public void recycle(DmsWenjianHuishouAuditEvent event) {
        if (event == null || blank(event.getHuishouDanjuId())) {
            throw new IllegalArgumentException("文件回收审计事件和回收单ID不能为空");
        }
        if (event.getDetails() == null || event.getDetails().isEmpty()) {
            throw new IllegalArgumentException("文件回收审计明细不能为空");
        }
        try {
            List<Map<String, Object>> diffs = new ArrayList<>();
            Map<String, Object> mainAfter = new LinkedHashMap<>();
            mainAfter.put("beizhu_shuoming", event.getBeizhuShuoming());
            addScopedDiffs(diffs, RECYCLE_MAIN_TABLE, event.getHuishouDanjuId(),
                    null, mainAfter, RECYCLE_MAIN_FIELD_META);
 
            List<Map<String, Object>> targets = new ArrayList<>();
            List<String> subtitles = new ArrayList<>();
            for (DmsWenjianHuishouMingxiEntity detail : event.getDetails()) {
                addScopedDiffs(diffs, RECYCLE_DETAIL_TABLE, detail.getId(), null,
                        recycleDetailDiffData(detail), RECYCLE_DETAIL_FIELD_META);
                String beforeStatus = event.getBeforeStatuses() == null ? null
                        : event.getBeforeStatuses().get(detail.getDayinJiluId());
                Map<String, Object> oldStatus = Collections.<String, Object>singletonMap(
                        "biz_status", beforeStatus);
                Map<String, Object> newStatus = Collections.<String, Object>singletonMap(
                        "biz_status", "daixiaohui");
                addScopedDiffs(diffs, PRINT_RECORD_TABLE, detail.getDayinJiluId(),
                        oldStatus, newStatus, RECYCLE_PRINT_FIELD_META);
                Map<String, Object> target = recycleTarget(detail, beforeStatus);
                targets.add(target);
                subtitles.add(String.valueOf(target.get("recordTitle")));
            }
 
            Map<String, Object> extra = new LinkedHashMap<>();
            extra.put("schemaVersion", 1);
            extra.put("batch", targets.size() > 1);
            extra.put("batchSize", targets.size());
            extra.put("affected", targets.size());
            extra.put("affectedTargets", targets);
            extra.put("recordSubTitles", subtitles);
 
            auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                    .eventType(AuditConsts.TYPE_DATA_CHANGE)
                    .actionCode("FILE_RECYCLE")
                    .actionLabel("文件回收")
                    .bizModule(FOLDER_BIZ_MODULE)
                    .bizType(RECYCLE_BIZ_TYPE)
                    .bizCode(event.getHuishouDanjuId())
                    .targetTable(RECYCLE_MAIN_TABLE)
                    .targetId(event.getHuishouDanjuId())
                    .fieldDiffs(JsonUtil.getObjectToString(diffs))
                    .dataSnapshot(JsonUtil.getObjectToString(recycleSnapshot(event, targets)))
                    .reason(event.getBeizhuShuoming())
                    .extra(JsonUtil.getObjectToString(extra))
                    .recordTitle(recycleTitle(event.getDetails()))
                    .entryType("API")
                    .entryId(RECYCLE_ENTRY_ID)
                    .entryName(RECYCLE_ENTRY_NAME)
                    .relatedSigns(event.getRelatedSigns())
                    .sourceLayer(3)
                    .build()));
        } catch (RuntimeException ex) {
            log.warn("DMS recycle audit event construction failed recycleId={}",
                    event.getHuishouDanjuId(), ex);
        }
    }
 
    public void print(DmsWenjianDayinAuditEvent event) {
        try {
            List<DmsWenjianDayinRenwuEntity> tasks = event == null
                    ? Collections.emptyList() : orEmpty(event.getTasks());
            List<DmsWenjianDayinJiluEntity> records = event == null
                    ? Collections.emptyList() : orEmpty(event.getRecords());
            if (tasks.isEmpty() || records.isEmpty()) {
                throw new IllegalArgumentException("文件打印审计任务和打印记录不能为空");
            }
 
            Map<String, DmsWenjianDayinRenwuEntity> taskById = new LinkedHashMap<>();
            Map<String, List<DmsWenjianDayinJiluEntity>> recordsByTask = new LinkedHashMap<>();
            for (DmsWenjianDayinRenwuEntity task : tasks) {
                if (task == null || blank(task.getId()) || taskById.put(task.getId(), task) != null) {
                    throw new IllegalArgumentException("文件打印审计任务无效或重复");
                }
                recordsByTask.put(task.getId(), new ArrayList<>());
            }
            for (DmsWenjianDayinJiluEntity record : records) {
                List<DmsWenjianDayinJiluEntity> taskRecords = record == null
                        ? null : recordsByTask.get(record.getDayinRenwuId());
                if (record == null || blank(record.getId()) || taskRecords == null) {
                    throw new IllegalArgumentException("打印记录未关联本次打印任务");
                }
                taskRecords.add(record);
            }
 
            List<Map<String, Object>> diffs = new ArrayList<>();
            List<Map<String, Object>> targets = new ArrayList<>(tasks.size());
            List<String> subtitles = new ArrayList<>(tasks.size());
            for (DmsWenjianDayinRenwuEntity task : tasks) {
                List<DmsWenjianDayinJiluEntity> taskRecords = recordsByTask.get(task.getId());
                if (taskRecords.isEmpty()) {
                    throw new IllegalArgumentException("打印任务没有生成打印记录");
                }
                if (event.getAfterStatuses() == null || blank(event.getAfterStatuses().get(task.getId()))) {
                    throw new IllegalArgumentException("打印审计缺少实际更新状态");
                }
                addScopedDiffs(diffs, PRINT_TASK_TABLE, task.getId(),
                        Collections.<String, Object>singletonMap("biz_status", task.getBizStatus()),
                        Collections.<String, Object>singletonMap("biz_status", event.getAfterStatuses().get(task.getId())),
                        PRINT_TASK_FIELD_META);
                for (DmsWenjianDayinJiluEntity record : taskRecords) {
                    addScopedDiffs(diffs, PRINT_RECORD_TABLE, record.getId(), null,
                            printRecordDiffData(record), PRINT_RECORD_FIELD_META);
                }
                Map<String, Object> target = printTarget(task, taskRecords);
                targets.add(target);
                subtitles.add(String.valueOf(target.get("title")));
            }
            for (DmsWenjianFenfaRenwuEntity distributionTask : orEmpty(
                    event.getDistributionTasks())) {
                if (distributionTask != null && !blank(distributionTask.getId())) {
                    addScopedDiffs(diffs, DISTRIBUTION_TASK_TABLE, distributionTask.getId(), null,
                            distributionTaskDiffData(distributionTask),
                            DISTRIBUTION_TASK_FIELD_META);
                }
            }
 
            boolean batch = tasks.size() > 1;
            int totalCopies = records.size();
            PrintMenuContext menu = resolvePrintMenu();
            Map<String, Object> extra = new LinkedHashMap<>();
            extra.put("schemaVersion", 1);
            extra.put("printName", "受控文件打印");
            extra.put("targetTable", PRINT_TASK_TABLE);
            extra.put("bizType", PRINT_BIZ_TYPE);
            extra.put("source", PRINT_SOURCE);
            if (menu.menuId != null) extra.put("menuId", menu.menuId);
            if (menu.menuName != null) extra.put("menuName", menu.menuName);
            extra.put("menuResolution", menu.resolution);
            extra.put("printMode", batch ? "BATCH" : "SINGLE");
            extra.put("printCount", tasks.size());
            extra.put("totalCopies", totalCopies);
            extra.put("printTargets", targets);
            extra.put("recordSubTitles", subtitles);
 
            DmsWenjianDayinRenwuEntity single = batch ? null : tasks.get(0);
            String eventId = "PRINT-" + UUID.randomUUID().toString().replace("-", "");
            auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                    .clientEventId(eventId)
                    .operationId(eventId)
                    .eventType(AuditConsts.TYPE_PRINT)
                    .actionCode("PRINT")
                    .actionLabel("发起打印")
                    .bizModule(menu.menuName == null ? "待打印台账" : menu.menuName)
                    .bizType(PRINT_BIZ_TYPE)
                    .bizCode(single == null ? null : single.getWenjianBianhao())
                    .targetTable(PRINT_TASK_TABLE)
                    .targetId(single == null ? null : single.getId())
                    .fieldDiffs(JsonUtil.getObjectToString(diffs))
                    .dataSnapshot(JsonUtil.getObjectToString(printSnapshot(targets, totalCopies)))
                    .extra(JsonUtil.getObjectToString(extra))
                    .recordTitle(printTitle(tasks, totalCopies))
                    .entryType(menu.menuId == null ? null : "MENU")
                    .entryId(menu.menuId)
                    .entryName(menu.menuName)
                    .sourceLayer(3)
                    .build()));
        } catch (RuntimeException ex) {
            log.warn("DMS print audit event construction failed", ex);
        }
    }
 
    private void recordPermissionEvent(String actionCode, String actionLabel,
                                       DmsFilePermissionEntity first,
                                       List<Map<String, Object>> diffs,
                                       Map<String, Object> snapshot,
                                       Map<String, Object> extra,
                                       String reason,
                                       String recordTitle,
                                       List<AuditRelatedSignDTO> relatedSigns) {
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(AuditConsts.TYPE_DATA_CHANGE)
                .actionCode(actionCode)
                .actionLabel(actionLabel)
                .bizModule(FOLDER_BIZ_MODULE)
                .bizType(PERMISSION_BIZ_TYPE)
                .bizCode(value(first.getResourceId()))
                .targetTable(resourceTable(first.getResourceType()))
                .targetId(value(first.getResourceId()))
                .fieldDiffs(JsonUtil.getObjectToString(diffs))
                .dataSnapshot(JsonUtil.getObjectToString(snapshot))
                .reason(reason)
                .extra(JsonUtil.getObjectToString(extra))
                .recordTitle(batchTitle(recordTitle, extra))
                .entryType(FILE_ENTRY_TYPE)
                .entryId(PERMISSION_ENTRY_ID)
                .entryName(PERMISSION_ENTRY_NAME)
                .relatedSigns(relatedSigns)
                .sourceLayer(3)
                .build()));
    }
 
    private void recordExclusionEvent(String actionCode, String actionLabel,
                                      DmsAccessExclusionEntity first,
                                      List<Map<String, Object>> diffs,
                                      Map<String, Object> snapshot,
                                      Map<String, Object> extra,
                                      String reason,
                                      String recordTitle,
                                      List<AuditRelatedSignDTO> relatedSigns) {
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(AuditConsts.TYPE_DATA_CHANGE)
                .actionCode(actionCode)
                .actionLabel(actionLabel)
                .bizModule(FOLDER_BIZ_MODULE)
                .bizType(PERMISSION_BIZ_TYPE)
                .bizCode(value(first.getResourceId()))
                .targetTable(resourceTable(first.getResourceType()))
                .targetId(value(first.getResourceId()))
                .fieldDiffs(JsonUtil.getObjectToString(diffs))
                .dataSnapshot(JsonUtil.getObjectToString(snapshot))
                .reason(reason)
                .extra(JsonUtil.getObjectToString(extra))
                .recordTitle(batchTitle(recordTitle, extra))
                .entryType(FILE_ENTRY_TYPE)
                .entryId(PERMISSION_ENTRY_ID)
                .entryName(PERMISSION_ENTRY_NAME)
                .relatedSigns(relatedSigns)
                .sourceLayer(3)
                .build()));
    }
 
    private static Map<String, Object> batchExtra(List<Map<String, Object>> targets) {
        Map<String, Object> extra = new LinkedHashMap<>();
        extra.put("schemaVersion", 1);
        extra.put("batch", targets.size() > 1);
        extra.put("batchSize", targets.size());
        extra.put("affected", targets.size());
        extra.put("affectedTargets", targets);
        List<String> titles = new ArrayList<>();
        for (Map<String, Object> target : targets) {
            Object title = target.get("recordTitle");
            if (title != null) titles.add(String.valueOf(title));
        }
        extra.put("recordSubTitles", titles);
        return extra;
    }
 
    private static String batchTitle(String title, Map<String, Object> extra) {
        int size = Integer.parseInt(String.valueOf(extra.get("batchSize")));
        return size > 1 ? title + "(共 " + size + " 条)" : title;
    }
 
    private static List<Map<String, Object>> permissionDiffs(DmsFilePermissionEntity before,
                                                              DmsFilePermissionEntity after,
                                                              DmsPermissionAuditDisplayResolver.DisplayValues displays,
                                                              boolean scoped) {
        return auditDiffs(permissionData(before), permissionData(after), PERMISSION_FIELD_META,
                "dms_file_permissions", after == null ? value(before.getId()) : value(after.getId()),
                (field, raw, oldValue) -> permissionDisplay(field, raw, oldValue ? before : after, displays),
                scoped);
    }
 
    private static List<Map<String, Object>> exclusionDiffs(DmsAccessExclusionEntity before,
                                                             DmsAccessExclusionEntity after,
                                                             DmsPermissionAuditDisplayResolver.DisplayValues displays,
                                                             boolean scoped) {
        return auditDiffs(exclusionData(before), exclusionData(after), EXCLUSION_FIELD_META,
                "dms_access_exclusions", after == null ? value(before.getId()) : value(after.getId()),
                (field, raw, oldValue) -> exclusionDisplay(field, raw, oldValue ? before : after, displays),
                scoped);
    }
 
    @SuppressWarnings("unchecked")
    private static List<Map<String, Object>> auditDiffs(Map<String, Object> before, Map<String, Object> after,
                                                        Map<String, AuditFieldDiff.FieldMeta> meta,
                                                        String table, String id,
                                                        DisplayLookup displayLookup, boolean scoped) {
        List<Map> parsed = JSON.parseArray(AuditFieldDiff.diff(before, after,
                Collections.emptySet(), meta), Map.class);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map item : parsed) {
            Map<String, Object> diff = new LinkedHashMap<>();
            if (scoped) {
                diff.put("targetTable", table);
                diff.put("targetId", id);
            }
            diff.putAll(item);
            String field = String.valueOf(item.get("field"));
            String oldRaw = String.valueOf(item.get("oldData"));
            String newRaw = String.valueOf(item.get("newData"));
            diff.put("oldDisplay", displayLookup.display(field, oldRaw, true));
            diff.put("newDisplay", displayLookup.display(field, newRaw, false));
            result.add(diff);
        }
        return result;
    }
 
    private static String permissionDisplay(String field, String raw, DmsFilePermissionEntity row,
                                            DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        if (raw == null || raw.isEmpty() || "null".equals(raw)) return "";
        if (row != null && "resource_id".equals(field)) return displays.resource(row.getResourceType(), row.getResourceId());
        if (row != null && "principal_id".equals(field)) return displays.principal(row.getPrincipalType(), row.getPrincipalId());
        if (row != null && ("granted_by_id".equals(field) || "revoked_by_id".equals(field))) return displays.user(raw);
        return optionDisplay(PERMISSION_FIELD_META.get(field), raw);
    }
 
    private static String exclusionDisplay(String field, String raw, DmsAccessExclusionEntity row,
                                           DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        if (raw == null || raw.isEmpty() || "null".equals(raw)) return "";
        if (row != null && "resource_id".equals(field)) return displays.resource(row.getResourceType(), row.getResourceId());
        if ("user_id".equals(field) || "excluded_by_id".equals(field) || "revoked_by_id".equals(field)) {
            return displays.user(raw);
        }
        return optionDisplay(EXCLUSION_FIELD_META.get(field), raw);
    }
 
    private static String optionDisplay(AuditFieldDiff.FieldMeta meta, String raw) {
        String display = meta == null ? null : meta.optionDisplay(raw);
        return display == null ? raw : display;
    }
 
    private static Map<String, Object> permissionTarget(DmsFilePermissionEntity value,
                                                         List<Map<String, Object>> changes,
                                                         DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        Map<String, Object> target = permissionSnapshot(value, displays);
        target.put("changes", changes);
        target.put("recordTitle", permissionTitle(value, displays));
        return target;
    }
 
    private static Map<String, Object> exclusionTarget(DmsAccessExclusionEntity value,
                                                        List<Map<String, Object>> changes,
                                                        DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        Map<String, Object> target = exclusionSnapshot(value, displays);
        target.put("changes", changes);
        target.put("recordTitle", exclusionTitle(value, displays));
        return target;
    }
 
    private static Map<String, Object> permissionSnapshot(DmsFilePermissionEntity value,
                                                           DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("permissionId", value(value.getId()));
        data.put("resourceType", value.getResourceType());
        data.put("resourceId", value(value.getResourceId()));
        data.put("resourceName", displays.resource(value.getResourceType(), value.getResourceId()));
        data.put("resourceScope", value.getResourceScope());
        data.put("principalType", value.getPrincipalType());
        data.put("principalId", value.getPrincipalId());
        data.put("principalName", displays.principal(value.getPrincipalType(), value.getPrincipalId()));
        data.put("includeChildOrgs", value.getIncludeChildOrgs());
        if (value.getApprovedOrgScope() != null) data.put("approvedOrgScope", value.getApprovedOrgScope());
        data.put("actionCode", value.getActionCode());
        data.put("actionName", optionDisplay(PERMISSION_FIELD_META.get("action_code"), value.getActionCode()));
        data.put("sourceType", value.getSourceType());
        data.put("sourceId", value.getSourceId());
        data.put("validFrom", value.getValidFrom());
        data.put("validTo", value.getValidTo());
        data.put("maxUseCount", value.getMaxUseCount());
        data.put("usedCount", value.getUsedCount());
        data.put("grantReason", value.getGrantReason());
        data.put("grantedByType", value.getGrantedByType());
        data.put("grantedById", value.getGrantedById());
        data.put("grantedByName", displays.user(value.getGrantedById()));
        data.put("revokedAt", value.getRevokedAt());
        data.put("revokedByType", value.getRevokedByType());
        data.put("revokedById", value.getRevokedById());
        data.put("revokedByName", displays.user(value.getRevokedById()));
        data.put("revokeReason", value.getRevokeReason());
        data.put("revision", value.getRevision());
        return data;
    }
 
    private static Map<String, Object> exclusionSnapshot(DmsAccessExclusionEntity value,
                                                          DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("exclusionId", value(value.getId()));
        data.put("resourceType", value.getResourceType());
        data.put("resourceId", value(value.getResourceId()));
        data.put("resourceName", displays.resource(value.getResourceType(), value.getResourceId()));
        data.put("userId", value.getUserId());
        data.put("userName", displays.user(value.getUserId()));
        data.put("actionCode", value.getActionCode());
        data.put("actionName", optionDisplay(EXCLUSION_FIELD_META.get("action_code"), value.getActionCode()));
        data.put("sourceType", value.getSourceType());
        data.put("sourceId", value.getSourceId());
        data.put("validFrom", value.getValidFrom());
        data.put("validTo", value.getValidTo());
        data.put("excludeReason", value.getExcludeReason());
        data.put("excludedByType", value.getExcludedByType());
        data.put("excludedById", value.getExcludedById());
        data.put("excludedByName", displays.user(value.getExcludedById()));
        data.put("revokedAt", value.getRevokedAt());
        data.put("revokedByType", value.getRevokedByType());
        data.put("revokedById", value.getRevokedById());
        data.put("revokedByName", displays.user(value.getRevokedById()));
        data.put("revokeReason", value.getRevokeReason());
        data.put("revision", value.getRevision());
        return data;
    }
 
    private static Map<String, Object> permissionData(DmsFilePermissionEntity value) {
        if (value == null) return null;
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("resource_type", value.getResourceType());
        data.put("resource_id", value.getResourceId());
        data.put("resource_scope", value.getResourceScope());
        data.put("principal_type", value.getPrincipalType());
        data.put("principal_id", value.getPrincipalId());
        data.put("include_child_orgs", value.getIncludeChildOrgs());
        if (value.getApprovedOrgScope() != null) data.put("approved_org_scope", value.getApprovedOrgScope());
        data.put("action_code", value.getActionCode());
        data.put("source_type", value.getSourceType());
        data.put("source_id", value.getSourceId());
        data.put("valid_from", value.getValidFrom());
        data.put("valid_to", value.getValidTo());
        data.put("max_use_count", value.getMaxUseCount());
        data.put("used_count", value.getUsedCount());
        data.put("grant_reason", value.getGrantReason());
        data.put("granted_by_type", value.getGrantedByType());
        data.put("granted_by_id", value.getGrantedById());
        data.put("revoked_at", value.getRevokedAt());
        data.put("revoked_by_type", value.getRevokedByType());
        data.put("revoked_by_id", value.getRevokedById());
        data.put("revoke_reason", value.getRevokeReason());
        data.put("revision", value.getRevision());
        return data;
    }
 
    private static Map<String, Object> exclusionData(DmsAccessExclusionEntity value) {
        if (value == null) return null;
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("resource_type", value.getResourceType());
        data.put("resource_id", value.getResourceId());
        data.put("user_id", value.getUserId());
        data.put("action_code", value.getActionCode());
        data.put("source_type", value.getSourceType());
        data.put("source_id", value.getSourceId());
        data.put("valid_from", value.getValidFrom());
        data.put("valid_to", value.getValidTo());
        data.put("exclude_reason", value.getExcludeReason());
        data.put("excluded_by_type", value.getExcludedByType());
        data.put("excluded_by_id", value.getExcludedById());
        data.put("revoked_at", value.getRevokedAt());
        data.put("revoked_by_type", value.getRevokedByType());
        data.put("revoked_by_id", value.getRevokedById());
        data.put("revoke_reason", value.getRevokeReason());
        data.put("revision", value.getRevision());
        return data;
    }
 
    private static String permissionTitle(DmsFilePermissionEntity value,
                                          DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        return permissionTitle(value, displays,
                optionDisplay(PERMISSION_FIELD_META.get("action_code"), value.getActionCode()));
    }
 
    private static String permissionTitle(DmsFilePermissionEntity value,
                                          DmsPermissionAuditDisplayResolver.DisplayValues displays,
                                          String actionDisplay) {
        return displays.resource(value.getResourceType(), value.getResourceId()) + " - "
                + displays.principal(value.getPrincipalType(), value.getPrincipalId()) + " - "
                + actionDisplay;
    }
 
    private static PermissionActionSummary permissionActions(List<DmsFilePermissionEntity> values) {
        LinkedHashSet<String> remaining = new LinkedHashSet<>();
        for (DmsFilePermissionEntity item : values) {
            if (item != null && item.getActionCode() != null && !item.getActionCode().trim().isEmpty()) {
                remaining.add(item.getActionCode().trim());
            }
        }
        List<String> codes = new ArrayList<>();
        for (String known : new String[]{"CREATE", "READ", "PRINT", "DOWNLOAD", "UPDATE", "DELETE"}) {
            if (remaining.remove(known)) codes.add(known);
        }
        codes.addAll(remaining);
        List<String> names = new ArrayList<>();
        for (String code : codes) names.add(optionDisplay(PERMISSION_FIELD_META.get("action_code"), code));
        return new PermissionActionSummary(codes, names);
    }
 
    private static void combineActionDiff(List<Map<String, Object>> diffs, PermissionActionSummary actions) {
        for (Map<String, Object> diff : diffs) {
            if (!"action_code".equals(diff.get("field"))) continue;
            diff.put("newData", actions.raw);
            diff.put("newDisplay", actions.display);
            return;
        }
    }
 
    private static void combineActionSnapshot(Map<String, Object> snapshot, PermissionActionSummary actions) {
        snapshot.put("actionCode", actions.raw);
        snapshot.put("actionName", actions.display);
        snapshot.put("actionCodes", actions.codes);
        snapshot.put("actionNames", actions.names);
    }
 
    private static String exclusionTitle(DmsAccessExclusionEntity value,
                                         DmsPermissionAuditDisplayResolver.DisplayValues displays) {
        return displays.resource(value.getResourceType(), value.getResourceId()) + " - "
                + displays.user(value.getUserId()) + " - 排除查看";
    }
 
    private DmsPermissionAuditDisplayResolver.DisplayValues permissionDisplays(
            List<DmsFilePermissionEntity> values) {
        DmsPermissionAuditDisplayResolver resolver = permissionDisplayResolver == null
                ? null : permissionDisplayResolver.getIfAvailable();
        return resolver == null ? new DmsPermissionAuditDisplayResolver.DisplayValues()
                : resolver.resolvePermissions(values.get(0).getTenantId(), values);
    }
 
    private DmsPermissionAuditDisplayResolver.DisplayValues exclusionDisplays(
            List<DmsAccessExclusionEntity> values) {
        DmsPermissionAuditDisplayResolver resolver = permissionDisplayResolver == null
                ? null : permissionDisplayResolver.getIfAvailable();
        return resolver == null ? new DmsPermissionAuditDisplayResolver.DisplayValues()
                : resolver.resolveExclusions(values.get(0).getTenantId(), values);
    }
 
    private static List<DmsFilePermissionEntity> retainedPermissions(List<DmsFilePermissionEntity> before,
                                                                      List<DmsFilePermissionEntity> after) {
        return after == null || after.isEmpty() ? orEmpty(before) : after;
    }
 
    private static List<DmsAccessExclusionEntity> retainedExclusions(List<DmsAccessExclusionEntity> before,
                                                                      List<DmsAccessExclusionEntity> after) {
        return after == null || after.isEmpty() ? orEmpty(before) : after;
    }
 
    private static Map<UUID, DmsFilePermissionEntity> permissionsById(List<DmsFilePermissionEntity> values) {
        Map<UUID, DmsFilePermissionEntity> result = new HashMap<>();
        for (DmsFilePermissionEntity value : orEmpty(values)) result.put(value.getId(), value);
        return result;
    }
 
    private static Map<UUID, DmsAccessExclusionEntity> exclusionsById(List<DmsAccessExclusionEntity> values) {
        Map<UUID, DmsAccessExclusionEntity> result = new HashMap<>();
        for (DmsAccessExclusionEntity value : orEmpty(values)) result.put(value.getId(), value);
        return result;
    }
 
    private static <T> List<T> orEmpty(List<T> values) {
        return values == null ? Collections.emptyList() : values;
    }
 
    private static String resourceTable(String type) {
        if ("FOLDER".equals(type)) return FOLDER_TABLE;
        if ("DOCUMENT".equals(type)) return FILE_TABLE;
        if ("FILE_VERSION".equals(type)) return FILE_VERSION_TABLE;
        return "dms_permission_resources";
    }
 
    private interface DisplayLookup {
        String display(String field, String raw, boolean oldValue);
    }
 
    private static final class PermissionActionSummary {
        private final List<String> codes;
        private final List<String> names;
        private final String raw;
        private final String display;
 
        private PermissionActionSummary(List<String> codes, List<String> names) {
            this.codes = codes;
            this.names = names;
            this.raw = String.join(",", codes);
            this.display = String.join("、", names);
        }
    }
 
    private static Map<String, Object> recycleDetailDiffData(
            DmsWenjianHuishouMingxiEntity detail) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("f_foreign_id", detail.getForeignId());
        data.put("dayin_jilu_id", detail.getDayinJiluId());
        data.put("dayin_liushuihao", detail.getDayinLiushuihao());
        data.put("wenjian_bianhao", detail.getWenjianBianhao());
        data.put("wenjian_mingcheng", detail.getWenjianMingcheng());
        data.put("banbenhao", detail.getBanbenhao());
        data.put("qianshou_bumen", detail.getQianshouBumen());
        data.put("qianshou_ren", detail.getQianshouRen());
        data.put("qianshou_shijian", detail.getQianshouShijian());
        data.put("huishou_ren_id", detail.getHuishouRenId());
        data.put("huishou_shijian", detail.getHuishouShijian());
        return data;
    }
 
    private static Map<String, Object> recycleTarget(DmsWenjianHuishouMingxiEntity detail, String beforeStatus) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("detailId", detail.getId());
        data.put("dayinJiluId", detail.getDayinJiluId());
        data.put("dayinLiushuihao", detail.getDayinLiushuihao());
        data.put("wenjianBianhao", detail.getWenjianBianhao());
        data.put("wenjianMingcheng", detail.getWenjianMingcheng());
        data.put("banbenhao", detail.getBanbenhao());
        data.put("qianshouBumen", detail.getQianshouBumen());
        data.put("qianshouRen", detail.getQianshouRen());
        data.put("qianshouShijian", detail.getQianshouShijian());
        data.put("oldStatus", beforeStatus);
        data.put("newStatus", "daixiaohui");
        data.put("recordTitle", recycleDetailTitle(detail));
        return data;
    }
 
    private static Map<String, Object> recycleSnapshot(DmsWenjianHuishouAuditEvent event,
                                                        List<Map<String, Object>> targets) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("huishouDanjuId", event.getHuishouDanjuId());
        data.put("beizhuShuoming", event.getBeizhuShuoming());
        data.put("huishouRenId", event.getHuishouRenId());
        data.put("huishouShijian", event.getHuishouShijian());
        data.put("huishouFenshu", targets.size());
        data.put("huishouMingxi", targets);
        return data;
    }
 
    private static String recycleTitle(List<DmsWenjianHuishouMingxiEntity> details) {
        String first = recycleDetailTitle(details.get(0));
        return details.size() > 1 ? first + "(共 " + details.size() + " 份)" : first;
    }
 
    private static String recycleDetailTitle(DmsWenjianHuishouMingxiEntity detail) {
        String document = "[" + detail.getWenjianBianhao() + "] " + detail.getWenjianMingcheng();
        return document + " - " + detail.getBanbenhao() + " / " + detail.getDayinLiushuihao();
    }
 
    private static Map<String, Object> printRecordDiffData(DmsWenjianDayinJiluEntity record) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("dayin_renwu_id", record.getDayinRenwuId());
        data.put("dayin_xuhao", record.getDayinXuhao());
        data.put("dayin_liushuihao", record.getDayinLiushuihao());
        data.put("dayin_jishu_liushuihao", record.getDayinJishuLiushuihao());
        data.put("dayin_ren_id", record.getDayinRenId());
        data.put("dayin_bumen_id", record.getDayinBumenId());
        data.put("dayin_shijian", record.getDayinShijian());
        data.put("biz_status", record.getBizStatus());
        return data;
    }
 
    private static Map<String, Object> distributionTaskDiffData(
            DmsWenjianFenfaRenwuEntity task) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("dayin_renwu_id", task.getDayinRenwuId());
        data.put("fenfa_bianhao", task.getFenfaBianhao());
        data.put("fafang_leixing", task.getFafangLeixing());
        data.put("biz_status", task.getBizStatus());
        return data;
    }
 
    private static Map<String, Object> printTarget(DmsWenjianDayinRenwuEntity task,
                                                    List<DmsWenjianDayinJiluEntity> records) {
        Map<String, Object> data = new LinkedHashMap<>();
        List<String> recordIds = new ArrayList<>(records.size());
        List<String> serialNumbers = new ArrayList<>(records.size());
        for (DmsWenjianDayinJiluEntity record : records) {
            recordIds.add(record.getId());
            serialNumbers.add(record.getDayinLiushuihao());
        }
        data.put("id", task.getId());
        data.put("bizCode", task.getWenjianBianhao());
        data.put("title", printTaskTitle(task));
        data.put("wenjianId", task.getWenjianId());
        data.put("version", task.getBanbenhao());
        data.put("copies", records.size());
        data.put("printRecordIds", recordIds);
        data.put("serialNumbers", serialNumbers);
        return data;
    }
 
    private static Map<String, Object> printSnapshot(List<Map<String, Object>> targets,
                                                     int totalCopies) {
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("printTaskCount", targets.size());
        data.put("totalCopies", totalCopies);
        data.put("printTargets", targets);
        return data;
    }
 
    private static String printTitle(List<DmsWenjianDayinRenwuEntity> tasks, int totalCopies) {
        String first = printTaskTitle(tasks.get(0));
        if (tasks.size() > 1) {
            return first + "(批量打印 " + tasks.size() + " 个任务,共 " + totalCopies + " 份)";
        }
        return first + "(打印 " + totalCopies + " 份)";
    }
 
    private static String printTaskTitle(DmsWenjianDayinRenwuEntity task) {
        return "[" + task.getWenjianBianhao() + "] " + task.getWenjianMingcheng()
                + " - " + task.getBanbenhao();
    }
 
    private PrintMenuContext resolvePrintMenu() {
        String menuId;
        try {
            HttpServletRequest request = ServletUtil.getRequest();
            if (request == null) return PrintMenuContext.missing();
            menuId = textOrNull(request.getHeader("Jnpf-Menu-Id"));
            if (menuId != null && menuId.length() > 100) {
                return PrintMenuContext.invalid();
            }
        } catch (Throwable ex) {
            log.warn("DMS print audit cannot read menu header", ex);
            return PrintMenuContext.invalid();
        }
        if (menuId == null) return PrintMenuContext.missing();
 
        long now = System.currentTimeMillis();
        MenuCacheEntry cached = menuCache.get(menuId);
        if (cached != null && cached.expiresAt > now) {
            return PrintMenuContext.resolved(menuId, cached.menuName);
        }
        if (cached != null) menuCache.remove(menuId, cached);
 
        ModuleApi resolver = moduleApi == null ? null : moduleApi.getIfAvailable();
        if (resolver == null) return PrintMenuContext.failed(menuId);
        try {
            ModuleEntity menu = resolver.getModuleByList(menuId);
            String menuName = menu == null || Integer.valueOf(1).equals(menu.getDeleteMark())
                    ? null : textOrNull(menu.getFullName());
            if (menuName == null) return PrintMenuContext.unresolved(menuId);
            menuCache.put(menuId, new MenuCacheEntry(menuName, now + MENU_CACHE_MILLIS));
            return PrintMenuContext.resolved(menuId, menuName);
        } catch (Throwable ex) {
            log.warn("DMS print audit cannot resolve menu menuId={}", menuId, ex);
            return PrintMenuContext.failed(menuId);
        }
    }
 
    private static String textOrNull(String value) {
        if (value == null) return null;
        String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }
 
    private static boolean blank(String value) {
        return value == null || value.trim().isEmpty();
    }
 
    private static final class PrintMenuContext {
        private final String menuId;
        private final String menuName;
        private final String resolution;
 
        private PrintMenuContext(String menuId, String menuName, String resolution) {
            this.menuId = menuId;
            this.menuName = menuName;
            this.resolution = resolution;
        }
 
        private static PrintMenuContext resolved(String menuId, String menuName) {
            return new PrintMenuContext(menuId, menuName, "RESOLVED");
        }
 
        private static PrintMenuContext unresolved(String menuId) {
            return new PrintMenuContext(menuId, null, "UNRESOLVED");
        }
 
        private static PrintMenuContext failed(String menuId) {
            return new PrintMenuContext(menuId, null, "FAILED");
        }
 
        private static PrintMenuContext missing() {
            return new PrintMenuContext(null, null, "MISSING");
        }
 
        private static PrintMenuContext invalid() {
            return new PrintMenuContext(null, null, "INVALID");
        }
    }
 
    private static final class MenuCacheEntry {
        private final String menuName;
        private final long expiresAt;
 
        private MenuCacheEntry(String menuName, long expiresAt) {
            this.menuName = menuName;
            this.expiresAt = expiresAt;
        }
    }
 
    private void record(String eventType, String actionCode, String actionLabel, String table, String id,
                        String reason, String extra) {
        auditClient.ifAvailable(client -> client.record(AuditEventDTO.builder()
                .eventType(eventType)
                .actionCode(actionCode)
                .actionLabel(actionLabel)
                .bizModule("DMS_PERMISSION")
                .bizType("DMS_PERMISSION")
                .targetTable(table)
                .targetId(id)
                .reason(reason)
                .extra(extra)
                .sourceLayer(3)
                .build()));
    }
 
    private static Map<String, Object> diffData(DmsFolderAuditSnapshot snapshot) {
        if (snapshot == null) {
            return null;
        }
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("name", snapshot.getName());
        data.put("parent_id", value(snapshot.getParentId()));
        return data;
    }
 
    @SuppressWarnings("unchecked")
    private static void addScopedDiffs(List<Map<String, Object>> combined, String table, String id,
                                       Map<String, Object> before, Map<String, Object> after) {
        addScopedDiffs(combined, table, id, before, after, FILE_FIELD_META);
    }
 
    @SuppressWarnings("unchecked")
    private static void addScopedDiffs(List<Map<String, Object>> combined, String table, String id,
                                       Map<String, Object> before, Map<String, Object> after,
                                       Map<String, AuditFieldDiff.FieldMeta> fieldMeta) {
        List<Map> items = JSON.parseArray(AuditFieldDiff.diff(before, after,
                Collections.emptySet(), fieldMeta), Map.class);
        for (Map item : items) {
            Map<String, Object> scoped = new LinkedHashMap<>();
            scoped.put("targetTable", table);
            scoped.put("targetId", id);
            scoped.putAll(item);
            combined.add(scoped);
        }
    }
 
    private static Map<String, Object> documentDiffData(DmsFileAuditSnapshot snapshot) {
        if (snapshot == null) return null;
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("document_no", snapshot.getDocumentNo());
        data.put("name", snapshot.getDocumentName());
        data.put("document_type", snapshot.getDocumentType());
        data.put("responsible_dept_id", snapshot.getResponsibleDeptId());
        data.put("folder_id", value(snapshot.getFolderId()));
        return data;
    }
 
    private static Map<String, Object> versionDiffData(DmsFileVersionAuditSnapshot snapshot) {
        if (snapshot == null) return null;
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("version_no", snapshot.getVersionNo());
        data.put("version_label", snapshot.getVersionLabel());
        data.put("file_name", snapshot.getFileName());
        data.put("mime_type", snapshot.getMimeType());
        data.put("size_bytes", snapshot.getSizeBytes());
        data.put("checksum_sha256", snapshot.getChecksumSha256());
        data.put("prepared_by", snapshot.getPreparedBy());
        data.put("prepared_on", snapshot.getPreparedOn());
        return data;
    }
 
    private static Map<String, Object> stateDiffData(DmsFileVersionAuditSnapshot snapshot) {
        if (snapshot == null) return null;
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("lifecycle_status", snapshot.getLifecycleStatus());
        data.put("effective_from", snapshot.getEffectiveFrom());
        data.put("effective_to", snapshot.getEffectiveTo());
        data.put("status_reason", snapshot.getStatusReason());
        return data;
    }
 
    private static Map<String, Object> fileSnapshotData(DmsFileAuditSnapshot snapshot) {
        if (snapshot == null) return Collections.emptyMap();
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("documentId", value(snapshot.getDocumentId()));
        data.put("documentNo", snapshot.getDocumentNo());
        data.put("documentName", snapshot.getDocumentName());
        data.put("documentType", snapshot.getDocumentType());
        data.put("folderId", value(snapshot.getFolderId()));
        data.put("folderPath", snapshot.getFolderPath());
        data.put("responsibleDeptId", snapshot.getResponsibleDeptId());
        DmsFileVersionAuditSnapshot version = snapshot.getCurrentVersion();
        if (version != null) {
            Map<String, Object> current = new LinkedHashMap<>();
            current.put("fileVersionId", value(version.getFileVersionId()));
            current.put("versionNo", version.getVersionNo());
            current.put("versionLabel", version.getVersionLabel());
            current.put("fileName", version.getFileName());
            current.put("mimeType", version.getMimeType());
            current.put("sizeBytes", version.getSizeBytes());
            current.put("checksumSha256", version.getChecksumSha256());
            current.put("preparedBy", version.getPreparedBy());
            current.put("preparedOn", version.getPreparedOn());
            current.put("lifecycleStatus", version.getLifecycleStatus());
            current.put("effectiveFrom", version.getEffectiveFrom());
            current.put("effectiveTo", version.getEffectiveTo());
            current.put("statusReason", version.getStatusReason());
            data.put("currentVersion", current);
        }
        return data;
    }
 
    private static String fileTitle(DmsFileAuditSnapshot before, DmsFileAuditSnapshot after,
                                    String fallback) {
        DmsFileAuditSnapshot retained = after == null ? before : after;
        if (retained == null) return fallback;
        String base = "[" + retained.getDocumentNo() + "] " + retained.getDocumentName();
        DmsFileVersionAuditSnapshot oldVersion = before == null ? null : before.getCurrentVersion();
        DmsFileVersionAuditSnapshot newVersion = after == null ? null : after.getCurrentVersion();
        if (oldVersion != null && newVersion != null
                && !oldVersion.getFileVersionId().equals(newVersion.getFileVersionId())) {
            return base + ":" + oldVersion.getVersionLabel() + " -> " + newVersion.getVersionLabel();
        }
        return newVersion == null ? base : base + " - " + newVersion.getVersionLabel();
    }
 
    private static AuditFieldDiff.FieldMeta meta(String label) {
        return new AuditFieldDiff.FieldMeta(label, null, null);
    }
 
    private static AuditFieldDiff.FieldMeta booleanMeta(String label) {
        return options(label, "true", "是", "false", "否");
    }
 
    private static AuditFieldDiff.FieldMeta actionMeta() {
        return options("权限动作", "CREATE", "新建", "READ", "查看", "PRINT", "打印",
                "DOWNLOAD", "下载", "UPDATE", "修改", "DELETE", "删除");
    }
 
    private static AuditFieldDiff.FieldMeta sourceTypeMeta() {
        return options("授权来源", "MANUAL", "手工", "WORKFLOW", "工作流", "SYSTEM", "系统",
                "MIGRATION", "数据迁移");
    }
 
    private static AuditFieldDiff.FieldMeta options(String label, String... values) {
        Map<String, String> labels = new LinkedHashMap<>();
        for (int i = 0; i + 1 < values.length; i += 2) labels.put(values[i], values[i + 1]);
        return new AuditFieldDiff.FieldMeta(label, null, null, "string", "select", null, labels);
    }
 
    private static AuditFieldDiff.FieldMeta lifecycleStatusMeta() {
        Map<String, String> labels = new LinkedHashMap<>();
        labels.put("PENDING_EFFECTIVE", "待生效");
        labels.put("EFFECTIVE", "生效");
        labels.put("SUPERSEDED", "已替代");
        labels.put("EXPIRED", "已过期");
        labels.put("REVOKED", "已撤销");
        labels.put("INVALIDATED", "已失效");
        labels.put("OBSOLETE", "已作废");
        return new AuditFieldDiff.FieldMeta("生命周期状态", null, null,
                "string", null, null, labels);
    }
 
    private static Map<String, Object> snapshotData(DmsFolderAuditSnapshot snapshot) {
        if (snapshot == null) {
            return Collections.emptyMap();
        }
        Map<String, Object> data = new LinkedHashMap<>();
        data.put("id", value(snapshot.getId()));
        data.put("name", snapshot.getName());
        data.put("parentId", value(snapshot.getParentId()));
        data.put("parentName", snapshot.getParentName());
        data.put("folderPath", snapshot.getPath());
        data.put("revision", snapshot.getRevision());
        return data;
    }
 
    private static String title(DmsFolderAuditSnapshot before, DmsFolderAuditSnapshot after,
                                String fallback) {
        DmsFolderAuditSnapshot snapshot = after == null ? before : after;
        if (snapshot == null) {
            return fallback;
        }
        if (snapshot.getPath() != null && !snapshot.getPath().trim().isEmpty()) {
            return snapshot.getPath();
        }
        return snapshot.getName() == null ? fallback : snapshot.getName();
    }
 
    private static String value(Object value) {
        return value == null ? null : String.valueOf(value);
    }
}