刘光辉
2 小时以前 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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="jnpf.dmsMapper.DmsWenjianFenfaMapper">
 
    <resultMap id="ScopeResultMap" type="jnpf.dmsEntity.fenfa.DmsWenjianFenfaFanweiEntity">
        <id column="f_id" property="id"/>
        <result column="f_foreign_id" property="foreignId"/>
        <result column="f_tenant_id" property="tenantId"/>
        <result column="bumen_id" property="bumenId"/>
        <result column="wenjian_leibie" property="wenjianLeibie"/>
        <result column="fenshu" property="fenshu"/>
    </resultMap>
 
    <resultMap id="TaskResultMap" type="jnpf.dmsEntity.fenfa.DmsWenjianFenfaRenwuEntity">
        <id column="f_id" property="id"/>
        <result column="f_tenant_id" property="tenantId"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="fenfa_bianhao" property="fenfaBianhao"/>
        <result column="fafang_leixing" property="fafangLeixing"/>
        <result column="jieshou_ren_id" property="jieshouRenId"/>
        <result column="fafang_ren_id" property="fafangRenId"/>
        <result column="fafang_bumen_id" property="fafangBumenId"/>
        <result column="fafang_shijian" property="fafangShijian"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="f_creator_user_id" property="creatorUserId"/>
        <result column="f_creator_time" property="creatorTime"/>
        <result column="f_last_modify_user_id" property="lastModifyUserId"/>
        <result column="f_last_modify_time" property="lastModifyTime"/>
        <result column="f_delete_mark" property="deleteMark"/>
    </resultMap>
 
    <resultMap id="PrintResultMap" type="jnpf.dmsEntity.fenfa.DmsWenjianDayinJiluEntity">
        <id column="f_id" property="id"/>
        <result column="f_tenant_id" property="tenantId"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="dayin_xuhao" property="dayinXuhao"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="dayin_ren_id" property="dayinRenId"/>
        <result column="dayin_bumen_id" property="dayinBumenId"/>
        <result column="dayin_shijian" property="dayinShijian"/>
        <result column="fafang_ren_id" property="fafangRenId"/>
        <result column="fafang_bumen_id" property="fafangBumenId"/>
        <result column="fafang_shijian" property="fafangShijian"/>
        <result column="qianshou_ren_id" property="qianshouRenId"/>
        <result column="qianshou_bumen_id" property="qianshouBumenId"/>
        <result column="qianshou_shijian" property="qianshouShijian"/>
        <result column="biz_user_id" property="bizUserId"/>
        <result column="biz_review_user" property="bizReviewUser"/>
        <result column="biz_sign" property="bizSign"/>
        <result column="biz_review_sign" property="bizReviewSign"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="f_creator_user_id" property="creatorUserId"/>
        <result column="f_creator_time" property="creatorTime"/>
        <result column="f_last_modify_user_id" property="lastModifyUserId"/>
        <result column="f_last_modify_time" property="lastModifyTime"/>
        <result column="f_delete_mark" property="deleteMark"/>
        <result column="dayin_jishu_id" property="dayinJishuId"/>
        <result column="dayin_jishu_xuhao" property="dayinJishuXuhao"/>
    </resultMap>
 
    <resultMap id="PrintTaskResultMap" type="jnpf.dmsEntity.fenfa.DmsWenjianDayinRenwuEntity">
        <id column="f_id" property="id"/>
        <result column="f_tenant_id" property="tenantId"/>
        <result column="laiyuan_leixing" property="laiyuanLeixing"/>
        <result column="laiyuan_danju_id" property="laiyuanDanjuId"/>
        <result column="laiyuan_mingxi_id" property="laiyuanMingxiId"/>
        <result column="wenjian_id" property="wenjianId"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="shenhe_wancheng_riqi" property="shenheWanchengRiqi"/>
        <result column="peixun_wancheng_riqi" property="peixunWanchengRiqi"/>
        <result column="shengxiao_riqi" property="shengxiaoRiqi"/>
        <result column="jieshou_bumen_id" property="jieshouBumenId"/>
        <result column="wenjian_leibie" property="wenjianLeibie"/>
        <result column="dayin_fenshu" property="dayinFenshu"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="f_creator_user_id" property="creatorUserId"/>
        <result column="f_creator_time" property="creatorTime"/>
        <result column="f_last_modify_user_id" property="lastModifyUserId"/>
        <result column="f_last_modify_time" property="lastModifyTime"/>
        <result column="f_delete_mark" property="deleteMark"/>
    </resultMap>
 
    <resultMap id="TaskVoResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianDayinRenwuVO">
        <id column="f_id" property="id"/>
        <result column="laiyuan_leixing" property="laiyuanLeixing"/>
        <result column="laiyuan_danju_id" property="laiyuanDanjuId"/>
        <result column="laiyuan_mingxi_id" property="laiyuanMingxiId"/>
        <result column="wenjian_id" property="wenjianId"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="shenhe_wancheng_riqi" property="shenheWanchengRiqi"/>
        <result column="peixun_wancheng_riqi" property="peixunWanchengRiqi"/>
        <result column="shengxiao_riqi" property="shengxiaoRiqi"/>
        <result column="jieshou_bumen_id" property="jieshouBumenId"/>
        <result column="dayin_fenshu" property="dayinFenshu"/>
        <result column="yidayin_fenshu" property="yidayinFenshu"/>
        <result column="weidayin_fenshu" property="weidayinFenshu"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="f_creator_time" property="creatorTime"/>
    </resultMap>
 
    <resultMap id="PrintVoResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianDayinJiluVO">
        <id column="f_id" property="id"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="fenfa_renwu_id" property="fenfaRenwuId"/>
        <result column="wenjian_id" property="wenjianId"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="shengxiao_riqi" property="shengxiaoRiqi"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="dayin_xuhao" property="dayinXuhao"/>
        <result column="dayin_ren_id" property="dayinRenId"/>
        <result column="dayin_shijian" property="dayinShijian"/>
        <result column="fenfa_bumen_id" property="fenfaBumenId"/>
        <result column="dayin_bumen_id" property="dayinBumenId"/>
        <result column="dayin_bumen" property="dayinBumen"/>
        <result column="dayin_ren" property="dayinRen"/>
        <result column="dayin_fenshu" property="dayinFenshu"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="fafang_ren_id" property="fafangRenId"/>
        <result column="fafang_bumen_id" property="fafangBumenId"/>
        <result column="fafang_shijian" property="fafangShijian"/>
        <result column="qianshou_ren_id" property="qianshouRenId"/>
        <result column="qianshou_bumen_id" property="qianshouBumenId"/>
        <result column="qianshou_shijian" property="qianshouShijian"/>
        <result column="biz_status" property="bizStatus"/>
    </resultMap>
 
    <resultMap id="DistributionVoResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianFenfaRenwuVO">
        <id column="f_id" property="id"/>
        <result column="fenfa_renwu_id" property="fenfaRenwuId"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="fenfa_bianhao" property="fenfaBianhao"/>
        <result column="wenjian_id" property="wenjianId"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="dayin_xuhao" property="dayinXuhao"/>
        <result column="dayin_ren_id" property="dayinRenId"/>
        <result column="dayin_ren" property="dayinRen"/>
        <result column="dayin_shijian" property="dayinShijian"/>
        <result column="fafang_leixing" property="fafangLeixing"/>
        <result column="jieshou_bumen_id" property="jieshouBumenId"/>
        <result column="jieshou_bumen" property="jieshouBumen"/>
        <result column="jieshou_ren_id" property="jieshouRenId"/>
        <result column="jieshou_ren" property="jieshouRen"/>
        <result column="ying_fafang_fenshu" property="yingFafangFenshu"/>
        <result column="yi_fafang_fenshu" property="yiFafangFenshu"/>
        <result column="ying_qianshou_fenshu" property="yingQianshouFenshu"/>
        <result column="yi_qianshou_fenshu" property="yiQianshouFenshu"/>
        <result column="fafang_ren_id" property="fafangRenId"/>
        <result column="fafang_ren" property="fafangRen"/>
        <result column="fafang_bumen_id" property="fafangBumenId"/>
        <result column="fafang_shijian" property="fafangShijian"/>
        <result column="qianshou_ren_id" property="qianshouRenId"/>
        <result column="qianshou_ren" property="qianshouRen"/>
        <result column="qianshou_shijian" property="qianshouShijian"/>
        <result column="biz_sign_biaoshi" property="bizSignBiaoshi"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="f_creator_time" property="creatorTime"/>
    </resultMap>
 
    <resultMap id="HandoverDetailResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianJiaojieXiangqingVO">
        <id column="f_id" property="id"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="fenfa_bianhao" property="fenfaBianhao"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="dayin_fenshu" property="dayinFenshu"/>
        <result column="dayin_ren_id" property="dayinRenId"/>
        <result column="dayin_ren" property="dayinRen"/>
        <result column="dayin_shijian" property="dayinShijian"/>
        <result column="jieshou_bumen_id" property="jieshouBumenId"/>
        <result column="jieshou_bumen" property="jieshouBumen"/>
        <result column="qianshou_bumen_id" property="qianshouBumenId"/>
        <result column="qianshou_bumen" property="qianshouBumen"/>
        <result column="biz_user_id" property="bizUserId"/>
        <result column="biz_user" property="bizUser"/>
        <result column="biz_review_user" property="bizReviewUser"/>
        <result column="biz_review_user_name" property="bizReviewUserName"/>
        <result column="beizhu_shuoming" property="beizhuShuoming"/>
        <result column="biz_status" property="bizStatus"/>
    </resultMap>
 
    <resultMap id="RecycleDetailResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianHuishouMingxiVO">
        <id column="dayin_jilu_id" property="dayinJiluId"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="qianshou_bumen" property="qianshouBumen"/>
        <result column="qianshou_ren" property="qianshouRen"/>
        <result column="qianshou_shijian" property="qianshouShijian"/>
        <result column="biz_status" property="bizStatus"/>
        <result column="dayin_renwu_id" property="dayinRenwuId"/>
        <result column="early_recycle_eligible" property="earlyRecycleEligible"/>
    </resultMap>
 
    <resultMap id="DestructionDetailResultMap" type="jnpf.dmsEntity.fenfa.vo.DmsWenjianXiaohuiMingxiVO">
        <id column="dayin_jilu_id" property="dayinJiluId"/>
        <result column="wenjian_id" property="wenjianId"/>
        <result column="dayin_liushuihao" property="dayinLiushuihao"/>
        <result column="dayin_jishu_liushuihao" property="dayinJishuLiushuihao"/>
        <result column="wenjian_bianhao" property="wenjianBianhao"/>
        <result column="wenjian_mingcheng" property="wenjianMingcheng"/>
        <result column="banbenhao" property="banbenhao"/>
        <result column="qianshou_bumen_id" property="qianshouBumenId"/>
        <result column="biz_status" property="bizStatus"/>
    </resultMap>
 
    <sql id="TaskColumns">
        f_id, f_tenant_id, dayin_renwu_id, fenfa_bianhao, fafang_leixing,
        jieshou_ren_id, fafang_ren_id, fafang_bumen_id, fafang_shijian,
        beizhu_shuoming, biz_status, f_creator_user_id, f_creator_time,
        f_last_modify_user_id, f_last_modify_time, f_delete_mark
    </sql>
 
    <sql id="PrintColumns">
        f_id, f_tenant_id, dayin_renwu_id, dayin_xuhao, dayin_liushuihao, dayin_jishu_id, dayin_jishu_xuhao, dayin_jishu_liushuihao,
        dayin_ren_id, dayin_bumen_id, dayin_shijian, fafang_ren_id, fafang_bumen_id,
        fafang_shijian, qianshou_ren_id, qianshou_bumen_id, qianshou_shijian,
        biz_user_id, biz_review_user, biz_sign, biz_review_sign,
        beizhu_shuoming, biz_status, f_creator_user_id, f_creator_time,
        f_last_modify_user_id, f_last_modify_time, f_delete_mark
    </sql>
 
    <sql id="PrintTaskColumns">
        f_id, f_tenant_id, laiyuan_leixing, laiyuan_danju_id, laiyuan_mingxi_id,
        wenjian_id, wenjian_bianhao, wenjian_mingcheng, banbenhao,
        shenhe_wancheng_riqi, peixun_wancheng_riqi, shengxiao_riqi,
        jieshou_bumen_id, wenjian_leibie,
        dayin_fenshu,
        beizhu_shuoming, biz_status,
        f_creator_user_id, f_creator_time, f_last_modify_user_id,
        f_last_modify_time, f_delete_mark
    </sql>
 
    <select id="selectNewScopes" resultMap="ScopeResultMap">
        SELECT s.f_id, s.f_foreign_id, s.f_tenant_id,
               s.bumen_id, s.wenjian_leibie, s.fenshu
        FROM dms_wenjian_xinzeng_shenhe a
        JOIN dms_wenjian_xinzeng_shenhe_fenfa_bumen s
          ON s.f_tenant_id = a.f_tenant_id
         AND s.f_foreign_id = a.f_id
         AND s.f_delete_mark IS NULL
        WHERE a.f_tenant_id = #{tenantId}
          AND a.f_id = #{auditId}
          AND a.f_delete_mark IS NULL
        ORDER BY s.f_id
    </select>
 
    <select id="selectRevisionScopes" resultMap="ScopeResultMap">
        SELECT s.f_id, s.f_foreign_id, s.f_tenant_id,
               s.bumen_id, s.wenjian_leibie, s.fenshu
        FROM dms_wenjian_xiuding_shenhe a
        JOIN dms_wenjian_xiuding_shenhe_fenfa_bumen s
          ON s.f_tenant_id = a.f_tenant_id
         AND s.f_foreign_id = a.f_id
         AND s.f_delete_mark IS NULL
        WHERE a.f_tenant_id = #{tenantId}
          AND a.f_id = #{auditId}
          AND a.f_delete_mark IS NULL
        ORDER BY s.f_id
    </select>
 
    <select id="selectAuditPrintSnapshot" resultMap="PrintTaskResultMap">
        SELECT r.wenjian_bianhao, r.wenjian_mingcheng,
               v.banbenhao,
               COALESCE(receipt.approved_at, v.shengxiao_shijian) AS shenhe_wancheng_riqi,
               a.peixun_wancheng_riqi,
               COALESCE(a.banben_shengxiao_shijian, v.shengxiao_shijian) AS shengxiao_riqi
        FROM dms_wenjian_xinzeng_shenhe a
        LEFT JOIN public.dms_approved_file_batches receipt
          ON receipt.tenant_id = a.f_tenant_id AND receipt.task_id = a.f_flow_task_id
         AND receipt.audit_type = 'xinzeng_shenhe'
        JOIN dms_wenjian_ziyuan r
          ON r.f_tenant_id = a.f_tenant_id
         AND r.f_id = a.wenjian_id
         AND r.biz_status = 'effective'
         AND r.f_delete_mark IS NULL
        JOIN dms_wenjian_banben v
          ON v.f_tenant_id = a.f_tenant_id
         AND v.f_id = a.shengxiao_wenjian_banben_id
         AND v.wenjian_id = r.f_id
         AND v.f_delete_mark IS NULL
        WHERE #{auditType} = 'xinzeng'
          AND a.f_tenant_id = #{tenantId} AND a.f_id = #{auditId}
          AND a.f_delete_mark IS NULL
        UNION ALL
        SELECT r.wenjian_bianhao, r.wenjian_mingcheng,
               v.banbenhao,
               COALESCE(receipt.approved_at, v.shengxiao_shijian),
               a.peixun_wancheng_riqi,
               COALESCE(a.banben_shengxiao_shijian, v.shengxiao_shijian)
        FROM dms_wenjian_xiuding_shenhe a
        LEFT JOIN public.dms_approved_file_batches receipt
          ON receipt.tenant_id = a.f_tenant_id AND receipt.task_id = a.f_flow_task_id
         AND receipt.audit_type = 'xiuding_shenhe'
        JOIN dms_wenjian_ziyuan r
          ON r.f_tenant_id = a.f_tenant_id
         AND r.f_id = a.wenjian_id
         AND r.biz_status = 'effective'
         AND r.f_delete_mark IS NULL
        JOIN dms_wenjian_banben v
          ON v.f_tenant_id = a.f_tenant_id
         AND v.f_id = a.shengxiao_wenjian_banben_id
         AND v.wenjian_id = r.f_id
         AND v.f_delete_mark IS NULL
        WHERE #{auditType} = 'xiuding'
          AND a.f_tenant_id = #{tenantId} AND a.f_id = #{auditId}
          AND a.f_delete_mark IS NULL
    </select>
 
    <insert id="insertPrintTaskIfAbsent">
        INSERT INTO dms_wenjian_dayin_renwu (
            f_id, f_tenant_id, laiyuan_leixing, laiyuan_danju_id,
            laiyuan_mingxi_id, wenjian_id, wenjian_bianhao, wenjian_mingcheng,
            banbenhao, shenhe_wancheng_riqi, peixun_wancheng_riqi,
            shengxiao_riqi, jieshou_bumen_id,
            wenjian_leibie, dayin_fenshu, beizhu_shuoming, biz_status, f_creator_user_id,
            f_creator_time, f_delete_mark, shifou_shoukong
        )
        SELECT #{id}, #{tenantId}, #{laiyuanLeixing}, #{laiyuanDanjuId},
               #{laiyuanMingxiId}, #{wenjianId}, #{wenjianBianhao},
               #{wenjianMingcheng}, #{banbenhao}, #{shenheWanchengRiqi},
               #{peixunWanchengRiqi}, #{shengxiaoRiqi}, #{jieshouBumenId},
               #{wenjianLeibie}, #{dayinFenshu},
               #{beizhuShuoming}, #{bizStatus}, #{creatorUserId}, #{creatorTime}, NULL, #{shifouShoukong}
        WHERE NOT EXISTS (
            SELECT 1 FROM dms_wenjian_dayin_renwu
            WHERE f_tenant_id = #{tenantId}
              AND laiyuan_leixing = #{laiyuanLeixing}
              AND laiyuan_danju_id = #{laiyuanDanjuId}
              AND COALESCE(laiyuan_mingxi_id, '') = COALESCE(#{laiyuanMingxiId}, '')
              AND f_delete_mark IS NULL
        )
        ON CONFLICT DO NOTHING
    </insert>
 
    <insert id="insertDistributionTaskIfAbsent">
        INSERT INTO dms_wenjian_fenfa_renwu (
            f_id, f_tenant_id, dayin_renwu_id, fenfa_bianhao, fafang_leixing, biz_status,
            f_creator_user_id, f_creator_time, f_delete_mark
        )
        SELECT #{id}, #{tenantId}, #{dayinRenwuId}, #{fenfaBianhao}, #{fafangLeixing}, #{bizStatus},
               #{creatorUserId}, #{creatorTime}, NULL
        FROM dms_wenjian_dayin_renwu p
        WHERE p.f_tenant_id = #{tenantId}
          AND p.f_id = #{dayinRenwuId}
          AND p.biz_status IN ('bufendayin', 'yidayin')
          AND p.f_delete_mark IS NULL
          AND NOT EXISTS (
              SELECT 1 FROM dms_wenjian_fenfa_renwu d
              WHERE d.f_tenant_id = #{tenantId}
                AND d.dayin_renwu_id = #{dayinRenwuId}
                AND d.f_delete_mark IS NULL
          )
        ON CONFLICT DO NOTHING
    </insert>
 
    <select id="selectPrintTaskForUpdate" resultMap="PrintTaskResultMap">
        SELECT <include refid="PrintTaskColumns"/>
        FROM dms_wenjian_dayin_renwu
        WHERE f_tenant_id = #{tenantId} AND f_id = #{id}
          AND f_delete_mark IS NULL
        FOR UPDATE
    </select>
 
    <!-- 成功记账即消耗打印额度;作废、回收和逻辑删除均不返还。 -->
    <select id="countConsumedPrints" resultType="int">
        SELECT COUNT(*) FROM dms_wenjian_dayin_jilu
        WHERE f_tenant_id = #{tenantId} AND dayin_renwu_id = #{taskId}
    </select>
 
    <select id="countActivePrints" resultType="int">
        SELECT COUNT(*) FROM dms_wenjian_dayin_jilu
        WHERE f_tenant_id = #{tenantId} AND dayin_renwu_id = #{taskId}
          AND biz_status &lt;&gt; 'yizuofei'
          AND f_delete_mark IS NULL
    </select>
 
    <select id="maxPrintSequence" resultType="int">
        SELECT COALESCE(MAX(dayin_xuhao), 0) FROM dms_wenjian_dayin_jilu
        WHERE f_tenant_id = #{tenantId} AND dayin_renwu_id = #{taskId}
    </select>
 
    <insert id="insertPrintRecord">
        INSERT INTO dms_wenjian_dayin_jilu (
            f_id, f_tenant_id, dayin_renwu_id, dayin_xuhao, dayin_liushuihao, dayin_jishu_id, dayin_jishu_xuhao, dayin_jishu_liushuihao,
            dayin_ren_id, dayin_bumen_id, dayin_shijian, biz_status,
            f_creator_user_id, f_creator_time, f_delete_mark
        ) VALUES (
            #{id}, #{tenantId}, #{dayinRenwuId}, #{dayinXuhao}, #{dayinLiushuihao}, #{dayinJishuId}, #{dayinJishuXuhao}, #{dayinJishuLiushuihao},
            #{dayinRenId}, #{dayinBumenId}, #{dayinShijian}, #{bizStatus},
            #{creatorUserId}, #{creatorTime}, NULL
        )
    </insert>
 
    <update id="archivePrintRecords">
        UPDATE dms_wenjian_dayin_jilu
        SET biz_status = 'yiguidang'
        WHERE f_delete_mark IS NULL
          AND dayin_liushuihao IN
        <foreach collection="serialNumbers" item="serialNumber" open="(" separator="," close=")">
            #{serialNumber}
        </foreach>
    </update>
 
    <update id="updateTaskStatus">
        UPDATE dms_wenjian_dayin_renwu
        SET biz_status = #{bizStatus}, f_last_modify_user_id = #{userId},
            f_last_modify_time = #{now}
        WHERE f_tenant_id = #{tenantId} AND f_id = #{id}
          AND biz_status &lt;&gt; 'yiquxiao'
          AND f_delete_mark IS NULL
    </update>
 
    <select id="selectPrintForUpdate" resultMap="PrintResultMap">
        SELECT p.f_id, p.f_tenant_id, p.dayin_renwu_id, p.dayin_xuhao,
               p.dayin_liushuihao, p.dayin_jishu_liushuihao, p.dayin_ren_id, p.dayin_bumen_id, p.dayin_shijian,
               p.fafang_ren_id, p.fafang_bumen_id, p.fafang_shijian,
               p.qianshou_ren_id, p.qianshou_bumen_id, p.qianshou_shijian,
               p.biz_user_id, p.biz_review_user, p.biz_sign, p.biz_review_sign,
               p.beizhu_shuoming, p.biz_status,
               p.f_creator_user_id, p.f_creator_time,
               p.f_last_modify_user_id, p.f_last_modify_time, p.f_delete_mark
        FROM dms_wenjian_dayin_jilu p
        JOIN dms_wenjian_fenfa_renwu d
          ON d.f_tenant_id = p.f_tenant_id
         AND d.dayin_renwu_id = p.dayin_renwu_id
         AND d.biz_status &lt;&gt; 'yiquxiao'
         AND d.f_delete_mark IS NULL
        WHERE p.f_tenant_id = #{tenantId}
          <choose>
            <when test="recordId != null and recordId != ''">AND p.f_id = #{recordId}</when>
            <otherwise>AND p.dayin_liushuihao = #{serialNumber}</otherwise>
          </choose>
          AND p.f_delete_mark IS NULL
        FOR UPDATE OF p
    </select>
 
    <select id="selectHandoverDetail" resultMap="HandoverDetailResultMap">
        SELECT p.f_id, p.dayin_renwu_id, d.fenfa_bianhao,
               r.wenjian_mingcheng, r.wenjian_bianhao, r.banbenhao,
               p.dayin_liushuihao, p.dayin_jishu_liushuihao, 1 AS dayin_fenshu, p.dayin_ren_id,
               printer.f_real_name AS dayin_ren, p.dayin_shijian,
               r.jieshou_bumen_id,
               receiving_org.f_full_name AS jieshou_bumen,
               p.qianshou_bumen_id,
               signer_org.f_full_name AS qianshou_bumen,
               p.biz_user_id, issuer.f_real_name AS biz_user,
               p.biz_review_user, receiver.f_real_name AS biz_review_user_name,
               p.beizhu_shuoming, p.biz_status
        FROM dms_wenjian_dayin_jilu p
        JOIN dms_wenjian_dayin_renwu r
          ON r.f_tenant_id = p.f_tenant_id
         AND r.f_id = p.dayin_renwu_id
         AND r.f_delete_mark IS NULL
        JOIN dms_wenjian_fenfa_renwu d
          ON d.f_tenant_id = p.f_tenant_id
         AND d.dayin_renwu_id = p.dayin_renwu_id
         AND d.f_delete_mark IS NULL
        LEFT JOIN base_user printer
          ON printer.f_tenant_id = p.f_tenant_id
         AND printer.f_id = p.dayin_ren_id
         AND printer.f_delete_mark IS NULL
        LEFT JOIN base_organize receiving_org
          ON receiving_org.f_tenant_id = r.f_tenant_id
         AND receiving_org.f_id = r.jieshou_bumen_id
         AND receiving_org.f_delete_mark IS NULL
        LEFT JOIN base_organize signer_org
          ON signer_org.f_tenant_id = p.f_tenant_id
         AND signer_org.f_id = p.qianshou_bumen_id
         AND signer_org.f_delete_mark IS NULL
        LEFT JOIN base_user issuer
          ON issuer.f_tenant_id = p.f_tenant_id
         AND issuer.f_id = p.biz_user_id
         AND issuer.f_delete_mark IS NULL
        LEFT JOIN base_user receiver
          ON receiver.f_tenant_id = p.f_tenant_id
         AND receiver.f_id = p.biz_review_user
         AND receiver.f_delete_mark IS NULL
        WHERE p.f_tenant_id = #{tenantId}
          AND p.f_id = #{recordId}
          AND p.f_delete_mark IS NULL
    </select>
 
    <select id="selectDistributionTaskByPrintTaskForUpdate" resultMap="TaskResultMap">
        SELECT <include refid="TaskColumns"/>
        FROM dms_wenjian_fenfa_renwu
        WHERE f_tenant_id = #{tenantId} AND dayin_renwu_id = #{printTaskId}
          AND biz_status &lt;&gt; 'yiquxiao'
          AND f_delete_mark IS NULL
        FOR UPDATE
    </select>
 
    <update id="handoverPrintRecord">
        UPDATE dms_wenjian_dayin_jilu
        SET fafang_ren_id = #{issuerId}, fafang_bumen_id = #{issuerDepartmentId},
            fafang_shijian = #{now}, beizhu_shuoming = #{remark},
            qianshou_ren_id = #{receiverId}, qianshou_bumen_id = #{signerDepartmentId},
            qianshou_shijian = #{now}, biz_user_id = #{issuerId},
            biz_review_user = #{receiverId}, biz_sign = #{bizSign},
            biz_review_sign = #{bizReviewSign}, biz_status = 'daihuishou',
            f_last_modify_user_id = #{issuerId}, f_last_modify_time = #{now}
        WHERE f_tenant_id = #{tenantId} AND f_id = #{recordId}
          AND biz_status IN ('daifafang', 'daiqianshou')
          AND f_delete_mark IS NULL
    </update>
 
    <update id="completeDistributionTaskHandoverIfFinished">
        UPDATE dms_wenjian_fenfa_renwu d
        SET jieshou_ren_id = #{receiverId}, fafang_ren_id = #{issuerId},
            fafang_bumen_id = #{issuerDepartmentId}, fafang_shijian = #{now},
            beizhu_shuoming = #{remark}, biz_status = 'yiwancheng',
            f_last_modify_user_id = #{issuerId}, f_last_modify_time = #{now}
        WHERE d.f_tenant_id = #{tenantId} AND d.f_id = #{id}
          AND d.biz_status IN ('daifafang', 'daiqianshou')
          AND d.f_delete_mark IS NULL
          AND EXISTS (
              SELECT 1 FROM dms_wenjian_dayin_renwu t
              WHERE t.f_tenant_id = d.f_tenant_id AND t.f_id = #{printTaskId}
                AND t.f_id = d.dayin_renwu_id AND t.biz_status = 'yidayin'
                AND t.f_delete_mark IS NULL
          )
          AND NOT EXISTS (
              SELECT 1
              FROM dms_wenjian_dayin_jilu p
              WHERE p.f_tenant_id = d.f_tenant_id
                AND p.dayin_renwu_id = #{printTaskId}
                AND p.biz_status IN ('daifafang', 'daiqianshou')
                AND p.f_delete_mark IS NULL
          )
    </update>
 
    <insert id="insertImbalance">
        INSERT INTO dms_wenjian_fenfa_bupingheng (
            f_id, f_tenant_id, dayin_jilu_id, cuoshi, yuanyin, biz_status,
            f_creator_user_id, f_creator_time, f_delete_mark
        ) VALUES (
            #{id}, #{tenantId}, #{recordId}, #{measure}, #{reason}, 'effective',
            #{userId}, #{now}, NULL
        )
    </insert>
 
    <!-- p、t 为副本与原打印任务;成功回调须精确包含本副本,不能由任务状态推断。 -->
    <sql id="EarlyRecycleEligible">
        (p.biz_status IN ('daifafang', 'daiqianshou')
         AND t.laiyuan_leixing IN ('wenjian_xinzeng_shenhe', 'wenjian_xiuding_shenhe')
         AND EXISTS (
             SELECT 1 FROM dms_wenjian_dayin_queren q
             WHERE q.f_tenant_id = p.f_tenant_id AND q.dayin_renwu_id = p.dayin_renwu_id
               AND COALESCE(NULLIF(q.jieguo_neirong, ''), '[]')::jsonb
                   @> jsonb_build_array(jsonb_build_object('id', p.f_id))
         ))
    </sql>
    <sql id="RecycleEligible">
        (p.biz_status = 'daihuishou' OR <include refid="EarlyRecycleEligible"/>)
    </sql>
 
    <select id="selectRecycleCandidateBySerialNumber" resultMap="RecycleDetailResultMap">
        SELECT p.f_id AS dayin_jilu_id, p.dayin_liushuihao, p.dayin_jishu_liushuihao,
               t.wenjian_bianhao, t.wenjian_mingcheng, t.banbenhao,
               signer_org.f_full_name AS qianshou_bumen,
               signer.f_real_name AS qianshou_ren,
               p.qianshou_shijian, p.biz_status, p.dayin_renwu_id,
               <include refid="EarlyRecycleEligible"/> AS early_recycle_eligible
        FROM dms_wenjian_dayin_jilu p
        JOIN dms_wenjian_dayin_renwu t
          ON t.f_tenant_id = p.f_tenant_id
         AND t.f_id = p.dayin_renwu_id
         AND t.f_delete_mark IS NULL
        LEFT JOIN base_organize signer_org
          ON signer_org.f_tenant_id = p.f_tenant_id
         AND signer_org.f_id = p.qianshou_bumen_id
         AND signer_org.f_delete_mark IS NULL
        LEFT JOIN base_user signer
          ON signer.f_tenant_id = p.f_tenant_id
         AND signer.f_id = p.qianshou_ren_id
         AND signer.f_delete_mark IS NULL
        WHERE p.f_tenant_id = #{tenantId}
          AND p.dayin_liushuihao = #{serialNumber}
          AND <include refid="RecycleEligible"/>
          AND p.f_delete_mark IS NULL
          AND NOT EXISTS (SELECT 1 FROM dms_wenjian_chongda_fasong r
                          WHERE r.f_tenant_id = p.f_tenant_id AND r.dayin_jilu_id = p.f_id
                            AND r.fasong_zhuangtai IN ('OPEN', 'CONFIRMED'))
    </select>
 
    <insert id="insertRecycle">
        INSERT INTO dms_wenjian_huishou (
            f_id, f_tenant_id, biz_sign, chuli_fangshi, beizhu_shuoming, f_delete_mark
        ) VALUES (
            #{id}, #{tenantId}, #{bizSign}, #{chuliFangshi}, #{beizhuShuoming}, NULL
        )
    </insert>
 
    <select id="selectRecyclePrintsForUpdate" resultMap="RecycleDetailResultMap">
        SELECT p.f_id AS dayin_jilu_id, p.dayin_liushuihao, p.dayin_jishu_liushuihao,
               t.wenjian_bianhao, t.wenjian_mingcheng, t.banbenhao,
               signer_org.f_full_name AS qianshou_bumen,
               signer.f_real_name AS qianshou_ren,
               p.qianshou_shijian, p.biz_status, p.dayin_renwu_id,
               <include refid="EarlyRecycleEligible"/> AS early_recycle_eligible
        FROM dms_wenjian_dayin_jilu p
        JOIN dms_wenjian_dayin_renwu t
          ON t.f_tenant_id = p.f_tenant_id
         AND t.f_id = p.dayin_renwu_id
         AND t.f_delete_mark IS NULL
        LEFT JOIN base_organize signer_org
          ON signer_org.f_tenant_id = p.f_tenant_id
         AND signer_org.f_id = p.qianshou_bumen_id
         AND signer_org.f_delete_mark IS NULL
        LEFT JOIN base_user signer
          ON signer.f_tenant_id = p.f_tenant_id
         AND signer.f_id = p.qianshou_ren_id
         AND signer.f_delete_mark IS NULL
        WHERE p.f_tenant_id = #{tenantId}
          AND p.f_id IN
          <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
              #{recordId}
          </foreach>
          AND p.f_delete_mark IS NULL
        ORDER BY p.f_id
        FOR UPDATE OF p
    </select>
 
    <insert id="insertRecycleDetail">
        INSERT INTO dms_wenjian_huishou_zixiang (
            f_id, f_foreign_id, f_tenant_id,
            dayin_jilu_id, dayin_liushuihao, wenjian_bianhao, wenjian_mingcheng,
            banbenhao, qianshou_bumen, qianshou_ren, qianshou_shijian,
            huishou_ren_id, huishou_shijian,
            f_delete_mark, f_delete_time, f_delete_user_id
        ) VALUES (
            #{id}, #{foreignId}, #{tenantId},
            #{dayinJiluId}, #{dayinLiushuihao}, #{wenjianBianhao}, #{wenjianMingcheng},
            #{banbenhao}, #{qianshouBumen}, #{qianshouRen}, #{qianshouShijian},
            #{huishouRenId}, #{huishouShijian},
            NULL, NULL, NULL
        )
    </insert>
 
    <update id="markPrintsRecycled">
        UPDATE dms_wenjian_dayin_jilu p
        SET biz_status = 'daixiaohui',
            f_last_modify_user_id = #{userId}, f_last_modify_time = #{now}
        WHERE f_tenant_id = #{tenantId}
          AND f_id IN
          <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
              #{recordId}
          </foreach>
          AND EXISTS (SELECT 1 FROM dms_wenjian_dayin_renwu t
                      WHERE t.f_tenant_id = p.f_tenant_id AND t.f_id = p.dayin_renwu_id
                        AND t.f_delete_mark IS NULL AND <include refid="RecycleEligible"/>)
          AND f_delete_mark IS NULL
    </update>
 
    <!-- 提前回收只关闭已无待交接副本的完整打印任务,不生成发放/签收人或时间。 -->
    <update id="completeDistributionAfterRecycle">
        UPDATE dms_wenjian_fenfa_renwu d
        SET biz_status = 'yiwancheng', f_last_modify_user_id = #{userId}, f_last_modify_time = #{now}
        WHERE d.f_tenant_id = #{tenantId} AND d.dayin_renwu_id = #{taskId}
          AND d.f_delete_mark IS NULL AND d.biz_status IN ('daifafang', 'daiqianshou')
          AND EXISTS (SELECT 1 FROM dms_wenjian_dayin_renwu t
                      WHERE t.f_tenant_id = d.f_tenant_id AND t.f_id = d.dayin_renwu_id
                        AND t.f_delete_mark IS NULL AND t.biz_status = 'yidayin')
          AND NOT EXISTS (SELECT 1 FROM dms_wenjian_dayin_jilu p
                          WHERE p.f_tenant_id = d.f_tenant_id AND p.dayin_renwu_id = d.dayin_renwu_id
                            AND p.f_delete_mark IS NULL AND p.biz_status IN ('daifafang', 'daiqianshou'))
    </update>
 
    <select id="selectDestructionCandidateBySerialNumber"
            resultMap="DestructionDetailResultMap">
        SELECT p.f_id AS dayin_jilu_id, t.wenjian_id, p.dayin_liushuihao, p.dayin_jishu_liushuihao,
               t.wenjian_bianhao, t.wenjian_mingcheng, t.banbenhao,
               p.qianshou_bumen_id, p.biz_status
        FROM dms_wenjian_dayin_jilu p
        JOIN dms_wenjian_dayin_renwu t
          ON t.f_tenant_id = p.f_tenant_id
         AND t.f_id = p.dayin_renwu_id
         AND t.f_delete_mark IS NULL
        WHERE p.f_tenant_id = #{tenantId}
          AND p.dayin_liushuihao = #{serialNumber}
          AND p.biz_status = 'daixiaohui'
          AND p.f_delete_mark IS NULL
    </select>
 
    <select id="selectTaskPageXml" resultMap="TaskVoResultMap">
        SELECT t.f_id, t.laiyuan_leixing, t.laiyuan_danju_id, t.laiyuan_mingxi_id,
               t.wenjian_id, t.wenjian_mingcheng, t.wenjian_bianhao, t.banbenhao,
               t.shenhe_wancheng_riqi, t.peixun_wancheng_riqi, t.shengxiao_riqi,
               t.jieshou_bumen_id, t.dayin_fenshu,
               COUNT(p.f_id) FILTER (WHERE p.biz_status &lt;&gt; 'yizuofei')::INTEGER AS yidayin_fenshu,
               GREATEST(t.dayin_fenshu - COUNT(p.f_id) FILTER (
                   WHERE p.biz_status &lt;&gt; 'yizuofei')::INTEGER, 0) AS weidayin_fenshu,
               t.beizhu_shuoming,
               t.biz_status, t.f_creator_time
        FROM dms_wenjian_dayin_renwu t
        LEFT JOIN dms_wenjian_dayin_jilu p ON p.f_tenant_id = t.f_tenant_id
             AND p.dayin_renwu_id = t.f_id
             AND p.f_delete_mark IS NULL
        WHERE t.f_tenant_id = #{tenantId}
          AND t.f_delete_mark IS NULL
        <if test="bizStatus != null and bizStatus != ''">
          AND t.biz_status = #{bizStatus}
        </if>
        <if test="keyword != null and keyword != ''">
          AND (t.wenjian_mingcheng LIKE CONCAT('%', #{keyword}, '%')
               OR t.wenjian_bianhao LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        GROUP BY t.f_id, t.laiyuan_leixing, t.laiyuan_danju_id, t.laiyuan_mingxi_id,
                 t.wenjian_id, t.wenjian_mingcheng, t.wenjian_bianhao, t.banbenhao,
                 t.shenhe_wancheng_riqi, t.peixun_wancheng_riqi, t.shengxiao_riqi,
                 t.jieshou_bumen_id, t.dayin_fenshu,
                 t.beizhu_shuoming, t.biz_status, t.f_creator_time
        ORDER BY t.f_creator_time DESC, t.f_id
    </select>
 
    <sql id="DistributionLedgerColumns">
        d.f_id, d.dayin_renwu_id, d.fenfa_bianhao,
        t.wenjian_id, t.wenjian_bianhao, t.wenjian_mingcheng, t.banbenhao,
        d.fafang_leixing, t.jieshou_bumen_id,
        o.f_full_name AS jieshou_bumen, d.jieshou_ren_id,
        receiver.f_real_name AS jieshou_ren,
        t.dayin_fenshu AS ying_fafang_fenshu,
        stats.yi_fafang_fenshu,
        t.dayin_fenshu AS ying_qianshou_fenshu,
        stats.yi_qianshou_fenshu,
        d.fafang_ren_id, issuer.f_real_name AS fafang_ren,
        d.fafang_bumen_id, d.fafang_shijian,
        signed.qianshou_ren_id, signer.f_real_name AS qianshou_ren,
        signed.qianshou_shijian,
        (signed.biz_sign IS NOT NULL AND signed.biz_sign &lt;&gt; ''
         AND signed.biz_review_sign IS NOT NULL
         AND signed.biz_review_sign &lt;&gt; '') AS biz_sign_biaoshi,
        d.beizhu_shuoming, d.biz_status, d.f_creator_time
    </sql>
 
    <sql id="DistributionLedgerFrom">
        FROM dms_wenjian_fenfa_renwu d
        JOIN dms_wenjian_dayin_renwu t
          ON t.f_tenant_id = d.f_tenant_id AND t.f_id = d.dayin_renwu_id
         AND t.f_delete_mark IS NULL
        LEFT JOIN base_organize o
          ON o.f_tenant_id = t.f_tenant_id AND o.f_id = t.jieshou_bumen_id
         AND o.f_delete_mark IS NULL
        LEFT JOIN base_user receiver
          ON receiver.f_tenant_id = d.f_tenant_id AND receiver.f_id = d.jieshou_ren_id
         AND receiver.f_delete_mark IS NULL
        LEFT JOIN base_user issuer
          ON issuer.f_tenant_id = d.f_tenant_id AND issuer.f_id = d.fafang_ren_id
         AND issuer.f_delete_mark IS NULL
        LEFT JOIN LATERAL (
            SELECT COUNT(*) FILTER (WHERE p.fafang_shijian IS NOT NULL)::INTEGER
                       AS yi_fafang_fenshu,
                   COUNT(*) FILTER (WHERE p.qianshou_shijian IS NOT NULL)::INTEGER
                       AS yi_qianshou_fenshu
            FROM dms_wenjian_dayin_jilu p
            WHERE p.f_tenant_id = d.f_tenant_id
              AND p.dayin_renwu_id = d.dayin_renwu_id
              AND p.biz_status &lt;&gt; 'yizuofei'
              AND p.f_delete_mark IS NULL
        ) stats ON TRUE
        LEFT JOIN LATERAL (
            SELECT p.qianshou_ren_id, p.qianshou_shijian,
                   p.biz_sign, p.biz_review_sign
            FROM dms_wenjian_dayin_jilu p
            WHERE p.f_tenant_id = d.f_tenant_id
              AND p.dayin_renwu_id = d.dayin_renwu_id
              AND p.qianshou_shijian IS NOT NULL
              AND p.biz_status &lt;&gt; 'yizuofei'
              AND p.f_delete_mark IS NULL
            ORDER BY p.qianshou_shijian DESC, p.f_id DESC
            LIMIT 1
        ) signed ON TRUE
        LEFT JOIN base_user signer
          ON signer.f_tenant_id = d.f_tenant_id AND signer.f_id = signed.qianshou_ren_id
         AND signer.f_delete_mark IS NULL
    </sql>
 
    <sql id="DistributionKeywordFilter">
        <if test="keyword != null and keyword != ''">
          AND (d.fenfa_bianhao LIKE CONCAT('%', #{keyword}, '%')
               OR t.wenjian_bianhao LIKE CONCAT('%', #{keyword}, '%')
               OR t.wenjian_mingcheng LIKE CONCAT('%', #{keyword}, '%'))
        </if>
    </sql>
 
    <select id="selectDistributionPageXml" resultMap="DistributionVoResultMap">
        SELECT <include refid="DistributionLedgerColumns"/>
        <include refid="DistributionLedgerFrom"/>
        WHERE d.f_tenant_id = #{tenantId}
          AND d.f_delete_mark IS NULL
        <if test="bizStatus != null and bizStatus != ''">AND d.biz_status = #{bizStatus}</if>
        <include refid="DistributionKeywordFilter"/>
        ORDER BY d.f_creator_time DESC, d.f_id
    </select>
 
    <select id="selectIssueLedgerPageXml" resultMap="DistributionVoResultMap">
        SELECT <include refid="PrintHandoverLedgerColumns"/>
        <include refid="PrintHandoverLedgerFrom"/>
        WHERE d.f_tenant_id = #{tenantId}
          AND d.biz_status &lt;&gt; 'yiquxiao'
          AND p.biz_status &lt;&gt; 'yizuofei'
          AND d.f_delete_mark IS NULL
          AND p.f_delete_mark IS NULL
        <if test="issueStatus == 'unissued'">
          AND p.fafang_shijian IS NULL
          AND p.biz_status IN ('daifafang', 'daiqianshou')
        </if>
        <if test="issueStatus == 'issued'">AND p.fafang_shijian IS NOT NULL</if>
        <include refid="PrintHandoverKeywordFilter"/>
        ORDER BY COALESCE(p.fafang_shijian, p.f_creator_time) DESC,
                 p.dayin_xuhao, p.f_id
    </select>
 
    <select id="selectReceiptLedgerPageXml" resultMap="DistributionVoResultMap">
        SELECT <include refid="PrintHandoverLedgerColumns"/>
        <include refid="PrintHandoverLedgerFrom"/>
        WHERE d.f_tenant_id = #{tenantId}
          AND d.biz_status &lt;&gt; 'yiquxiao'
          AND p.biz_status &lt;&gt; 'yizuofei'
          AND d.f_delete_mark IS NULL
          AND p.f_delete_mark IS NULL
        <if test="receiptStatus == 'pending'">
          AND p.qianshou_shijian IS NULL
          AND p.biz_status IN ('daifafang', 'daiqianshou')
        </if>
        <if test="receiptStatus == 'received'">AND p.qianshou_shijian IS NOT NULL</if>
        <include refid="PrintHandoverKeywordFilter"/>
        ORDER BY COALESCE(p.qianshou_shijian, p.f_creator_time) DESC,
                 p.dayin_xuhao, p.f_id
    </select>
 
    <sql id="PrintHandoverLedgerColumns">
        p.f_id, d.f_id AS fenfa_renwu_id, d.dayin_renwu_id, d.fenfa_bianhao,
        t.wenjian_id, t.wenjian_bianhao, t.wenjian_mingcheng, t.banbenhao,
        p.dayin_liushuihao, p.dayin_jishu_liushuihao, p.dayin_xuhao, p.dayin_ren_id,
        printer.f_real_name AS dayin_ren, p.dayin_shijian,
        d.fafang_leixing, t.jieshou_bumen_id,
        target_org.f_full_name AS jieshou_bumen,
        COALESCE(p.biz_review_user, d.jieshou_ren_id) AS jieshou_ren_id,
        receiver.f_real_name AS jieshou_ren,
        1 AS ying_fafang_fenshu,
        CASE WHEN p.fafang_shijian IS NOT NULL THEN 1 ELSE 0 END AS yi_fafang_fenshu,
        1 AS ying_qianshou_fenshu,
        CASE WHEN p.qianshou_shijian IS NOT NULL THEN 1 ELSE 0 END AS yi_qianshou_fenshu,
        p.fafang_ren_id, issuer.f_real_name AS fafang_ren,
        p.fafang_bumen_id, p.fafang_shijian,
        p.qianshou_ren_id, signer.f_real_name AS qianshou_ren,
        p.qianshou_shijian,
        (p.biz_sign IS NOT NULL AND p.biz_sign &lt;&gt; ''
         AND p.biz_review_sign IS NOT NULL
         AND p.biz_review_sign &lt;&gt; '') AS biz_sign_biaoshi,
        COALESCE(p.beizhu_shuoming, d.beizhu_shuoming) AS beizhu_shuoming,
        p.biz_status, p.f_creator_time
    </sql>
 
    <sql id="PrintHandoverLedgerFrom">
        FROM dms_wenjian_fenfa_renwu d
        JOIN dms_wenjian_dayin_renwu t
          ON t.f_tenant_id = d.f_tenant_id AND t.f_id = d.dayin_renwu_id
         AND t.f_delete_mark IS NULL
        JOIN dms_wenjian_dayin_jilu p
          ON p.f_tenant_id = d.f_tenant_id
         AND p.dayin_renwu_id = d.dayin_renwu_id
        LEFT JOIN base_organize target_org
          ON target_org.f_tenant_id = t.f_tenant_id
         AND target_org.f_id = t.jieshou_bumen_id
         AND target_org.f_delete_mark IS NULL
        LEFT JOIN base_user receiver
          ON receiver.f_tenant_id = p.f_tenant_id
         AND receiver.f_id = COALESCE(p.biz_review_user, d.jieshou_ren_id)
         AND receiver.f_delete_mark IS NULL
        LEFT JOIN base_user printer
          ON printer.f_tenant_id = p.f_tenant_id AND printer.f_id = p.dayin_ren_id
         AND printer.f_delete_mark IS NULL
        LEFT JOIN base_user issuer
          ON issuer.f_tenant_id = p.f_tenant_id AND issuer.f_id = p.fafang_ren_id
         AND issuer.f_delete_mark IS NULL
        LEFT JOIN base_user signer
          ON signer.f_tenant_id = p.f_tenant_id AND signer.f_id = p.qianshou_ren_id
         AND signer.f_delete_mark IS NULL
    </sql>
 
    <sql id="PrintHandoverKeywordFilter">
        <if test="keyword != null and keyword != ''">
          AND (d.fenfa_bianhao LIKE CONCAT('%', #{keyword}, '%')
               OR t.wenjian_bianhao LIKE CONCAT('%', #{keyword}, '%')
               OR t.wenjian_mingcheng LIKE CONCAT('%', #{keyword}, '%')
               OR p.dayin_liushuihao LIKE CONCAT('%', #{keyword}, '%')
               OR target_org.f_full_name LIKE CONCAT('%', #{keyword}, '%'))
        </if>
    </sql>
 
</mapper>