陈昶聿
2 天以前 3d047130b059efd46f56be37465a751ab16ee8cb
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
package com.smartor.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.entity.PatArchive;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inhospvisit.api.InhospVisitFacade;
import com.ruoyi.inhospvisit.domain.PatMedInhosp;
import com.ruoyi.outpatientvisit.domain.PatMedOuthosp;
import com.ruoyi.patarchive.domain.PatSpecificArchiveGenerateConfig;
import com.ruoyi.patarchive.mapper.PatArchiveMapper;
import com.ruoyi.patarchive.service.IPatSpecificArchiveGenerateConfigService;
import com.ruoyi.task.api.TaskSubtaskApi;
import com.ruoyi.task.domain.ServiceSubtask;
import com.ruoyi.task.service.IServiceSubtaskService;
import com.smartor.common.SpecificArchiveConstants;
import com.smartor.domain.PatChildArchive;
import com.smartor.domain.PatChildArchiveDetail;
import com.smartor.domain.PatMaternityArchive;
import com.smartor.domain.PatMaternityArchiveDetail;
import com.smartor.domain.PatSpecificArchiveGenerateLog;
import com.smartor.service.IPatChildArchiveService;
import com.smartor.service.IPatMaternityArchiveService;
import com.smartor.service.IPatSpecificArchiveGenerateLogService;
import com.smartor.service.IPatSpecificArchiveGenerateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * 专科档案生成Service业务层处理。
 *
 * <p>只处理丽水市妇幼保健院(orgid = 47226079133110211G1001):</p>
 * <ul>
 *     <li>门诊接诊完成:按科室/诊断匹配配置后生成妇科/儿科档案;</li>
 *     <li>住院随访完成:按出院/在院科室、病区、诊断匹配配置后生成妇科/儿科档案,
 *     生成前按“患者 + 专科 + 档案类型”判断是否已有对应档案,已有则跳过。</li>
 * </ul>
 */
@Slf4j
@Service
public class PatSpecificArchiveGenerateServiceImpl implements IPatSpecificArchiveGenerateService {
 
    /** 部门类型:科室 */
    private static final String DEPT_TYPE_DEPARTMENT = "1";
    /** 部门类型:病区 */
    private static final String DEPT_TYPE_WARD = "2";
    /** 就诊类型:出院 */
    private static final String HOSP_TYPE_DISCHARGE = "2";
    /** 就诊类型:住院/入院 */
    private static final String HOSP_TYPE_INHOSP = "6";
    /** 住院随访完成状态 */
    private static final Long SEND_STATE_FINISHED = 6L;
    /** 住院随访档案生成的最大重试次数 */
    private static final int MAX_RETRY = 3;
    /** Redis 锁前缀 */
    private static final String LOCK_PREFIX = "lock:archive:inhosp:";
 
    @Autowired
    private IPatSpecificArchiveGenerateConfigService patSpecificArchiveGenerateConfigService;
 
    @Autowired
    private IPatMaternityArchiveService patMaternityArchiveService;
 
    @Autowired
    private IPatChildArchiveService patChildArchiveService;
 
    @Autowired
    private IPatSpecificArchiveGenerateLogService patSpecificArchiveGenerateLogService;
 
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
    @Autowired
    private IServiceSubtaskService serviceSubtaskService;
 
    @Autowired
    private InhospVisitFacade inhospVisitFacade;
 
    @Autowired
    private TaskSubtaskApi taskSubtaskApi;
 
    @Autowired
    private RedisCache redisCache;
 
    /**
     * 住院随访服务类型白名单,默认出院随访、专科随访、入院随访、手术随访。
     */
    @Value("${archive.inhosp.followup.service-types:2,13,18,19}")
    private String inhospFollowupServiceTypes;
 
    /** 住院随访生成专科档案开关,默认开启,便于按机构灰度。 */
    @Value("${archive.inhosp.followup.enabled:true}")
    private boolean inhospFollowupEnabled;
 
    @Override
    public int generateByOuthosp(PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
        if (patMedOuthosp == null) {
            return 0;
        }
        // 仅丽水市妇幼保健院生效,避免影响其他机构
        if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(patMedOuthosp.getOrgid())) {
            return 0;
        }
        if (StringUtils.isBlank(patMedOuthosp.getOuthospno())) {
            log.warn("生成专科档案跳过:门诊编号为空,serialnum={}", patMedOuthosp.getSerialnum());
            return 0;
        }
 
        PatArchive targetArchive = resolvePatArchive(patMedOuthosp, patArchive);
        if (targetArchive == null && patMedOuthosp.getPatid() != null) {
            targetArchive = patArchiveMapper.selectPatArchiveByPatid(patMedOuthosp.getPatid());
        }
        Long patid = targetArchive != null ? targetArchive.getId() : patMedOuthosp.getPatid();
        if (patid == null) {
            log.warn("生成专科档案跳过:未找到患者档案,outhospno={},patno={}",
                    patMedOuthosp.getOuthospno(), patMedOuthosp.getPatno());
            return 0;
        }
 
        String campusid = StringUtils.isNotBlank(patMedOuthosp.getCampusid())
                ? patMedOuthosp.getCampusid() : SpecificArchiveConstants.DEFAULT_CAMPUS_ID;
        List<PatSpecificArchiveGenerateConfig> configs =
                patSpecificArchiveGenerateConfigService.selectEffectiveConfigList(patMedOuthosp.getOrgid(), campusid);
        if (CollectionUtils.isEmpty(configs)) {
            log.debug("生成专科档案跳过:机构未配置档案生成规则,orgid={},campusid={}",
                    patMedOuthosp.getOrgid(), campusid);
            return 0;
        }
 
        // 同一条门诊记录、同一部门分类、同一档案类型只生成一次
        Set<String> generatedKeys = new HashSet<>();
        int generatedCount = 0;
        for (PatSpecificArchiveGenerateConfig config : configs) {
            if (!matchConfig(config, patMedOuthosp.getDeptcode(), null,
                    patMedOuthosp.getIcd10code(), patMedOuthosp.getDiagname())) {
                continue;
            }
            String archiveType = config.getArchiveType();
            String deptCategory = config.getDeptCategory();
            if (StringUtils.isBlank(archiveType) || StringUtils.isBlank(deptCategory)) {
                log.warn("生成专科档案跳过:配置不完整,configId={},deptCategory={},archiveType={}",
                        config.getId(), deptCategory, archiveType);
                continue;
            }
 
            String generatedKey = deptCategory + "|" + archiveType;
            if (!generatedKeys.add(generatedKey)) {
                continue;
            }
 
            try {
                if (SpecificArchiveConstants.DEPT_CATEGORY_GYNECOLOGY.equals(deptCategory)) {
                    PatMaternityArchiveDetail detail =
                            buildMaternityDetail(patMedOuthosp, targetArchive, config, campusid);
                    patMaternityArchiveService.insertOrUpdateMaternityArchive(detail);
                    generatedCount++;
                } else if (SpecificArchiveConstants.DEPT_CATEGORY_PEDIATRICS.equals(deptCategory)) {
                    PatChildArchiveDetail detail =
                            buildChildDetail(patMedOuthosp, targetArchive, config, campusid);
                    patChildArchiveService.insertOrUpdateChildArchive(detail);
                    generatedCount++;
                } else {
                    log.warn("生成专科档案跳过:未知部门分类,configId={},deptCategory={}",
                            config.getId(), deptCategory);
                }
            } catch (Exception e) {
                log.error("生成专科档案失败,configId={},outhospno={},deptCategory={},archiveType={}",
                        config.getId(), patMedOuthosp.getOuthospno(), deptCategory, archiveType, e);
            }
        }
        return generatedCount;
    }
 
    @Override
    public int generateByInhospFollowup(Long subId) {
        if (!inhospFollowupEnabled || subId == null) {
            return 0;
        }
        ServiceSubtask subtask = serviceSubtaskService.selectServiceSubtaskById(subId);
        if (subtask == null || !SEND_STATE_FINISHED.equals(subtask.getSendstate())) {
            return 0;
        }
        if (!isInhospFollowup(subtask)) {
            return 0;
        }
 
        // 仅限丽水妇幼,机构为空时用住院记录/患者档案兜底
        PatMedInhosp inhosp = resolveInhosp(subtask);
        PatArchive patArchive = resolvePatArchive(subtask.getPatid(), inhosp);
        String orgid = firstNotBlank(subtask.getOrgid(),
                inhosp == null ? null : inhosp.getOrgid(),
                patArchive == null ? null : patArchive.getOrgid());
        if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(orgid)) {
            log.debug("住院随访生成专科档案跳过:非丽水妇幼机构,subId={},orgid={}", subId, orgid);
            return 0;
        }
 
        Long patid = firstNonNull(subtask.getPatid(),
                inhosp == null ? null : inhosp.getPatid(),
                patArchive == null ? null : patArchive.getId());
        if (patid == null) {
            log.warn("住院随访生成专科档案跳过:未找到患者,subId={},inhospid={}", subId, subtask.getInhospid());
            return 0;
        }
        if (patArchive == null) {
            patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
        }
 
        String campusid = firstNotBlank(subtask.getCampusid(),
                inhosp == null ? null : inhosp.getCampusid(),
                patArchive == null ? null : patArchive.getCampusid(),
                SpecificArchiveConstants.DEFAULT_CAMPUS_ID);
 
        boolean admission = HOSP_TYPE_INHOSP.equals(subtask.getHospType());
        String deptCode = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getDeptcode(), subtask.getDeptcode(),
                subtask.getVisitDeptCode(), subtask.getLeaveldeptcode())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeaveldeptcode(), subtask.getLeaveldeptcode(),
                subtask.getVisitDeptCode(), inhosp == null ? null : inhosp.getDeptcode());
        String deptName = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getDeptname(), subtask.getDeptname(),
                subtask.getVisitDeptName(), subtask.getLeaveldeptname())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeaveldeptname(), subtask.getLeaveldeptname(),
                subtask.getVisitDeptName(), inhosp == null ? null : inhosp.getDeptname());
        String wardCode = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getHospitaldistrictcode(),
                subtask.getHospitaldistrictcode(), subtask.getLeavehospitaldistrictcode())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeavehospitaldistrictcode(),
                subtask.getLeavehospitaldistrictcode(), subtask.getHospitaldistrictcode());
        String wardName = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getHospitaldistrictname(),
                subtask.getHospitaldistrictname(), subtask.getLeavehospitaldistrictname())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeavehospitaldistrictname(),
                subtask.getLeavehospitaldistrictname(), subtask.getHospitaldistrictname());
        String icd10Code = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getIcd10code(), subtask.getLeaveicd10code())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeaveicd10code(),
                subtask.getLeaveicd10code(), inhosp == null ? null : inhosp.getIcd10code());
        String diagName = admission
                ? firstNotBlank(inhosp == null ? null : inhosp.getDiagname(), subtask.getDiagname())
                : firstNotBlank(inhosp == null ? null : inhosp.getLeavediagname(),
                subtask.getLeavediagname(), inhosp == null ? null : inhosp.getDiagname());
 
        InhospArchiveContext context = new InhospArchiveContext();
        context.subtask = subtask;
        context.inhosp = inhosp;
        context.patArchive = patArchive;
        context.patid = patid;
        context.orgid = orgid;
        context.campusid = campusid;
        context.deptCode = deptCode;
        context.deptName = deptName;
        context.wardCode = wardCode;
        context.wardName = wardName;
        context.icd10Code = icd10Code;
        context.diagName = diagName;
        context.diagTime = firstDate(subtask.getFinishtime(), inhosp == null ? null : inhosp.getEndtime());
 
        List<PatSpecificArchiveGenerateConfig> configs =
                patSpecificArchiveGenerateConfigService.selectEffectiveConfigList(orgid, campusid);
        if (CollectionUtils.isEmpty(configs)) {
            recordLog(context, null, null, null, SpecificArchiveConstants.RESULT_NO_MATCH, null,
                    "机构未配置档案生成规则");
            return 0;
        }
 
        Set<String> generatedKeys = new HashSet<>();
        int generatedCount = 0;
        boolean anyMatched = false;
        for (PatSpecificArchiveGenerateConfig config : configs) {
            String archiveType = config.getArchiveType();
            String deptCategory = config.getDeptCategory();
            if (StringUtils.isBlank(archiveType) || StringUtils.isBlank(deptCategory)) {
                log.warn("住院随访生成专科档案跳过:配置不完整,configId={},deptCategory={},archiveType={}",
                        config.getId(), deptCategory, archiveType);
                continue;
            }
            if (!matchConfig(config, deptCode, wardCode, icd10Code, diagName)) {
                continue;
            }
            anyMatched = true;
            String generatedKey = deptCategory + "|" + archiveType;
            if (!generatedKeys.add(generatedKey)) {
                continue;
            }
            if (alreadyProcessed(subId, config.getId())) {
                continue;
            }
 
            String lockKey = LOCK_PREFIX + orgid + ":" + patid + ":" + deptCategory + ":" + archiveType;
            boolean locked = false;
            try {
                locked = redisCache.setCacheObjectIfAbsent(lockKey, "1", 30, TimeUnit.SECONDS);
            } catch (Exception e) {
                // Redis 不可用时 fail-open,依赖生成日志和数据库查询兜底,避免档案生成整体停摆
                locked = true;
                log.warn("住院随访生成专科档案获取Redis锁异常,降级为无锁执行,subId={},lockKey={}", subId, lockKey, e);
            }
            if (!locked) {
                // 未拿到锁时由补偿任务重试,避免并发重复生成
                log.info("住院随访生成专科档案未获取到锁,等待补偿重试,subId={},lockKey={}", subId, lockKey);
                recordLog(context, config, deptCategory, archiveType, SpecificArchiveConstants.RESULT_FAILED,
                        null, "获取生成锁失败");
                continue;
            }
 
            try {
                Long existingArchiveId = findExistingArchiveId(orgid, patid, deptCategory, archiveType);
                if (existingArchiveId != null) {
                    recordLog(context, config, deptCategory, archiveType,
                            SpecificArchiveConstants.RESULT_EXISTS, existingArchiveId, "患者已有对应档案,跳过生成");
                    continue;
                }
 
                Long archiveId;
                if (SpecificArchiveConstants.DEPT_CATEGORY_GYNECOLOGY.equals(deptCategory)) {
                    PatMaternityArchiveDetail detail = buildInhospMaternityDetail(context, config);
                    archiveId = (long) patMaternityArchiveService.insertOrUpdateMaternityArchive(detail);
                } else if (SpecificArchiveConstants.DEPT_CATEGORY_PEDIATRICS.equals(deptCategory)) {
                    PatChildArchiveDetail detail = buildInhospChildDetail(context, config);
                    archiveId = (long) patChildArchiveService.insertOrUpdateChildArchive(detail);
                } else {
                    log.warn("住院随访生成专科档案跳过:未知部门分类,configId={},deptCategory={}",
                            config.getId(), deptCategory);
                    continue;
                }
                recordLog(context, config, deptCategory, archiveType,
                        SpecificArchiveConstants.RESULT_GENERATED, archiveId, "生成成功");
                generatedCount++;
            } catch (Exception e) {
                log.error("住院随访生成专科档案失败,subId={},configId={},deptCategory={},archiveType={}",
                        subId, config.getId(), deptCategory, archiveType, e);
                recordLog(context, config, deptCategory, archiveType,
                        SpecificArchiveConstants.RESULT_FAILED, null, truncateMsg(e.getMessage()));
            } finally {
                if (locked) {
                    try {
                        redisCache.deleteObject(lockKey);
                    } catch (Exception e) {
                        log.warn("住院随访生成专科档案释放Redis锁异常,lockKey={}", lockKey, e);
                    }
                }
            }
        }
 
        if (!anyMatched) {
            recordLog(context, null, null, null, SpecificArchiveConstants.RESULT_NO_MATCH, null,
                    "未命中科室/病区/诊断配置");
        }
        return generatedCount;
    }
 
    @Override
    public int compensateInhospFollowups() {
        if (!inhospFollowupEnabled) {
            return 0;
        }
        List<String> serviceTypes = parseCsv(inhospFollowupServiceTypes);
        if (CollectionUtils.isEmpty(serviceTypes)) {
            return 0;
        }
        Date startTime = new Date(System.currentTimeMillis() - 7L * 24 * 60 * 60 * 1000);
        List<ServiceSubtask> subtasks = taskSubtaskApi.findCompletedInhospFollowups(
                SpecificArchiveConstants.LISHUI_MCH_ORG_ID, serviceTypes, startTime, 200);
        if (CollectionUtils.isEmpty(subtasks)) {
            return 0;
        }
        int generated = 0;
        for (ServiceSubtask subtask : subtasks) {
            if (subtask.getId() == null
                    || patSpecificArchiveGenerateLogService.existsProcessedBySourceSub(
                    SpecificArchiveConstants.SOURCE_TYPE_INHOSP_FOLLOWUP, subtask.getId())) {
                continue;
            }
            try {
                generated += generateByInhospFollowup(subtask.getId());
            } catch (Exception e) {
                log.error("补偿生成住院随访专科档案失败,subId={}", subtask.getId(), e);
            }
        }
        return generated;
    }
 
    /**
     * 判断随访子任务是否属于需要生成档案的住院随访。
     */
    private boolean isInhospFollowup(ServiceSubtask subtask) {
        if (!HOSP_TYPE_DISCHARGE.equals(subtask.getHospType())
                && !HOSP_TYPE_INHOSP.equals(subtask.getHospType())) {
            return false;
        }
        if (StringUtils.isBlank(subtask.getServiceType())) {
            return false;
        }
        return parseCsv(inhospFollowupServiceTypes).contains(subtask.getServiceType().trim());
    }
 
    private boolean alreadyProcessed(Long subId, Long configId) {
        int cid = configId == null ? SpecificArchiveConstants.NO_CONFIG_ID : configId.intValue();
        PatSpecificArchiveGenerateLog old = patSpecificArchiveGenerateLogService
                .selectBySourceSubAndConfig(SpecificArchiveConstants.SOURCE_TYPE_INHOSP_FOLLOWUP, subId, cid);
        if (old == null) {
            return false;
        }
        if (SpecificArchiveConstants.RESULT_FAILED.equals(old.getProcessResult())) {
            return old.getRetryCount() != null && old.getRetryCount() >= MAX_RETRY;
        }
        return true;
    }
 
    /**
     * 查询患者是否已有对应专科档案。
     *
     * <p>用户确认口径:按“专科 + 档案类型”判断。妇科、儿科分表存储,因此妇科查妇科表,
     * 儿科查儿科表,再按档案类型区分。</p>
     */
    private Long findExistingArchiveId(String orgid, Long patid, String deptCategory, String archiveType) {
        if (SpecificArchiveConstants.DEPT_CATEGORY_GYNECOLOGY.equals(deptCategory)) {
            PatMaternityArchive query = new PatMaternityArchive();
            query.setOrgid(orgid);
            query.setPatid(patid);
            query.setArchiveType(archiveType);
            List<PatMaternityArchive> list = patMaternityArchiveService.selectPatMaternityArchiveList(query);
            return CollectionUtils.isNotEmpty(list) ? list.get(0).getId() : null;
        }
        if (SpecificArchiveConstants.DEPT_CATEGORY_PEDIATRICS.equals(deptCategory)) {
            PatChildArchive query = new PatChildArchive();
            query.setOrgid(orgid);
            query.setPatid(patid);
            query.setArchiveType(archiveType);
            List<PatChildArchive> list = patChildArchiveService.selectPatChildArchiveList(query);
            return CollectionUtils.isNotEmpty(list) ? list.get(0).getId() : null;
        }
        return null;
    }
 
    /**
     * 判断科室/病区、诊断是否命中配置。
     *
     * <p>部门条件与诊断条件为 OR 关系:任一条件命中即生成;两个集合都为空时配置无效。</p>
     */
    private boolean matchConfig(PatSpecificArchiveGenerateConfig config, String deptCode, String wardCode,
                                String icd10Code, String diagName) {
        List<String> deptCodes = parseCodes(config.getDeptCodes());
        List<String> icd10Codes = parseCodes(config.getIcd10Codes());
        if (CollectionUtils.isEmpty(deptCodes) && CollectionUtils.isEmpty(icd10Codes)) {
            return false;
        }
 
        boolean deptMatched = false;
        if (CollectionUtils.isNotEmpty(deptCodes)) {
            if (DEPT_TYPE_DEPARTMENT.equals(config.getDeptType())) {
                deptMatched = containsIgnoreCase(deptCodes, deptCode);
            } else if (DEPT_TYPE_WARD.equals(config.getDeptType())) {
                deptMatched = containsIgnoreCase(deptCodes, wardCode);
            }
        }
 
        boolean diagnosisMatched = false;
        if (CollectionUtils.isNotEmpty(icd10Codes)) {
            diagnosisMatched = icd10Codes.stream().anyMatch(code -> {
                if (equalsIgnoreCase(code, icd10Code)) {
                    return true;
                }
                // 部分 HIS 数据只有诊断名称没有诊断码,此时允许按诊断名称兜底匹配
                return StringUtils.isBlank(icd10Code) && equalsIgnoreCase(code, diagName);
            });
        }
 
        return deptMatched || diagnosisMatched;
    }
 
    private PatMaternityArchiveDetail buildMaternityDetail(PatMedOuthosp patMedOuthosp, PatArchive patArchive,
                                                           PatSpecificArchiveGenerateConfig config, String campusid) {
        PatMaternityArchiveDetail detail = new PatMaternityArchiveDetail();
        detail.setArchiveType(config.getArchiveType());
        detail.setFollowupPeriod(config.getFollowupPeriod());
        detail.setSourceType(SpecificArchiveConstants.SOURCE_TYPE_OUTHOSP);
        detail.setSourceBizId(patMedOuthosp.getId());
        detail.setSourceBizNo(patMedOuthosp.getOuthospno());
        fillCommonDetail(detail, patMedOuthosp, patArchive, campusid);
        if (patArchive != null) {
            detail.setAddress(patArchive.getPlaceOfResidence());
        }
        return detail;
    }
 
    private PatChildArchiveDetail buildChildDetail(PatMedOuthosp patMedOuthosp, PatArchive patArchive,
                                                   PatSpecificArchiveGenerateConfig config, String campusid) {
        PatChildArchiveDetail detail = new PatChildArchiveDetail();
        detail.setArchiveType(config.getArchiveType());
        detail.setFollowupPeriod(config.getFollowupPeriod());
        detail.setSourceType(SpecificArchiveConstants.SOURCE_TYPE_OUTHOSP);
        detail.setSourceBizId(patMedOuthosp.getId());
        detail.setSourceBizNo(patMedOuthosp.getOuthospno());
        fillCommonChildDetail(detail, patMedOuthosp, patArchive, campusid);
        detail.setParentPhone(patArchive != null
                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode())
                : patMedOuthosp.getTelcode());
        if (patArchive != null) {
            detail.setParentAddress(patArchive.getPlaceOfResidence());
        }
        // 营养性疾病档案同步诊断信息,方便列表直接展示
        if ("1".equals(config.getArchiveType())) {
            detail.setNutritionIcd10Code(patMedOuthosp.getIcd10code());
            detail.setNutritionDiagName(patMedOuthosp.getDiagname());
        }
        return detail;
    }
 
    private PatMaternityArchiveDetail buildInhospMaternityDetail(InhospArchiveContext context,
                                                                 PatSpecificArchiveGenerateConfig config) {
        PatMaternityArchiveDetail detail = new PatMaternityArchiveDetail();
        detail.setArchiveType(config.getArchiveType());
        detail.setFollowupPeriod(config.getFollowupPeriod());
        detail.setSourceType(SpecificArchiveConstants.SOURCE_TYPE_INHOSP_FOLLOWUP);
        detail.setSourceBizId(inInhospid(context));
        detail.setSourceBizNo(inInhospno(context));
        detail.setSourceSubId(context.subtask.getId());
        detail.setOuthospno(null);
        detail.setInhospno(inInhospno(context));
        detail.setSerialnum(inSerialnum(context));
        detail.setPatid(context.patid);
        detail.setPatname(firstNotBlank(context.patArchive == null ? null : context.patArchive.getName(),
                context.inhosp == null ? null : context.inhosp.getPatname()));
        detail.setBirthdate(context.patArchive == null ? null : context.patArchive.getBirthdate());
        detail.setIdcardno(firstNotBlank(context.patArchive == null ? null : context.patArchive.getIdcardno(),
                context.inhosp == null ? null : context.inhosp.getIdcardno()));
        detail.setPhone(firstNotBlank(context.patArchive == null ? null : context.patArchive.getTelcode(),
                context.inhosp == null ? null : context.inhosp.getTelcode()));
        detail.setSex(firstNonNull(context.patArchive == null ? null : context.patArchive.getSex(),
                context.inhosp == null ? null : context.inhosp.getSex(),
                context.subtask.getSex()));
        detail.setDiagTime(context.diagTime);
        detail.setArchiveTime(new Date());
        detail.setDoctorCode(inDrcode(context));
        detail.setDoctorName(inDrname(context));
        detail.setManagementDoctorCode(firstNotBlank(context.inhosp == null ? null : context.inhosp.getManagementDoctorCode(),
                inDrcode(context)));
        detail.setManagementDoctorName(firstNotBlank(context.inhosp == null ? null : context.inhosp.getManagementDoctor(),
                inDrname(context)));
        detail.setFirstDoctorCode(inDrcode(context));
        detail.setFirstDoctorName(inDrname(context));
        detail.setDeptcode(context.deptCode);
        detail.setDeptname(context.deptName);
        detail.setWardcode(context.wardCode);
        detail.setWardname(context.wardName);
        detail.setIcd10code(context.icd10Code);
        detail.setDiagname(context.diagName);
        detail.setAddress(context.patArchive == null ? null : context.patArchive.getPlaceOfResidence());
        detail.setRegisterUser("住院随访");
        detail.setOrgid(context.orgid);
        detail.setCampusid(context.campusid);
        detail.setDelFlag("0");
        return detail;
    }
 
    private PatChildArchiveDetail buildInhospChildDetail(InhospArchiveContext context,
                                                         PatSpecificArchiveGenerateConfig config) {
        PatChildArchiveDetail detail = new PatChildArchiveDetail();
        detail.setArchiveType(config.getArchiveType());
        detail.setFollowupPeriod(config.getFollowupPeriod());
        detail.setSourceType(SpecificArchiveConstants.SOURCE_TYPE_INHOSP_FOLLOWUP);
        detail.setSourceBizId(inInhospid(context));
        detail.setSourceBizNo(inInhospno(context));
        detail.setSourceSubId(context.subtask.getId());
        detail.setOuthospno(null);
        detail.setInhospno(inInhospno(context));
        detail.setSerialnum(inSerialnum(context));
        detail.setPatid(context.patid);
        detail.setPatname(firstNotBlank(context.patArchive == null ? null : context.patArchive.getName(),
                context.inhosp == null ? null : context.inhosp.getPatname()));
        detail.setBirthdate(context.patArchive == null ? null : context.patArchive.getBirthdate());
        detail.setIdcardno(firstNotBlank(context.patArchive == null ? null : context.patArchive.getIdcardno(),
                context.inhosp == null ? null : context.inhosp.getIdcardno()));
        detail.setPhone(firstNotBlank(context.patArchive == null ? null : context.patArchive.getTelcode(),
                context.inhosp == null ? null : context.inhosp.getTelcode()));
        detail.setSex(firstNonNull(context.patArchive == null ? null : context.patArchive.getSex(),
                context.inhosp == null ? null : context.inhosp.getSex(),
                context.subtask.getSex()));
        detail.setDiagTime(context.diagTime);
        detail.setArchiveTime(new Date());
        detail.setDoctorCode(inDrcode(context));
        detail.setDoctorName(inDrname(context));
        detail.setManagementDoctorCode(firstNotBlank(context.inhosp == null ? null : context.inhosp.getManagementDoctorCode(),
                inDrcode(context)));
        detail.setManagementDoctorName(firstNotBlank(context.inhosp == null ? null : context.inhosp.getManagementDoctor(),
                inDrname(context)));
        detail.setDeptcode(context.deptCode);
        detail.setDeptname(context.deptName);
        detail.setWardcode(context.wardCode);
        detail.setWardname(context.wardName);
        detail.setIcd10code(context.icd10Code);
        detail.setDiagname(context.diagName);
        detail.setParentPhone(firstNotBlank(context.patArchive == null ? null : context.patArchive.getTelcode(),
                context.inhosp == null ? null : context.inhosp.getTelcode()));
        detail.setParentAddress(context.patArchive == null ? null : context.patArchive.getPlaceOfResidence());
        if ("1".equals(config.getArchiveType())) {
            detail.setNutritionIcd10Code(context.icd10Code);
            detail.setNutritionDiagName(context.diagName);
        }
        detail.setRegisterUser("住院随访");
        detail.setOrgid(context.orgid);
        detail.setCampusid(context.campusid);
        detail.setDelFlag("0");
        return detail;
    }
 
    private void fillCommonDetail(PatMaternityArchiveDetail detail, PatMedOuthosp patMedOuthosp,
                                  PatArchive patArchive, String campusid) {
        detail.setOuthospno(patMedOuthosp.getOuthospno());
        detail.setSerialnum(patMedOuthosp.getSerialnum());
        detail.setPatid(patArchive != null ? patArchive.getId() : patMedOuthosp.getPatid());
        detail.setPatname(patArchive != null
                ? firstNotBlank(patArchive.getName(), patMedOuthosp.getPatname()) : patMedOuthosp.getPatname());
        detail.setBirthdate(patArchive != null ? patArchive.getBirthdate() : null);
        detail.setIdcardno(patArchive != null
                ? firstNotBlank(patArchive.getIdcardno(), patMedOuthosp.getIdcardno()) : patMedOuthosp.getIdcardno());
        detail.setPhone(patArchive != null
                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode()) : patMedOuthosp.getTelcode());
        detail.setSex(patArchive != null && patArchive.getSex() != null
                ? patArchive.getSex() : patMedOuthosp.getSex());
        detail.setDiagTime(patMedOuthosp.getAdmitdate());
        detail.setArchiveTime(new Date());
        detail.setDoctorCode(patMedOuthosp.getDrcode());
        detail.setDoctorName(patMedOuthosp.getDrname());
        detail.setManagementDoctorCode(patMedOuthosp.getDrcode());
        detail.setManagementDoctorName(patMedOuthosp.getDrname());
        detail.setFirstDoctorCode(patMedOuthosp.getDrcode());
        detail.setFirstDoctorName(patMedOuthosp.getDrname());
        detail.setDeptcode(patMedOuthosp.getDeptcode());
        detail.setDeptname(patMedOuthosp.getDeptname());
        detail.setIcd10code(patMedOuthosp.getIcd10code());
        detail.setDiagname(patMedOuthosp.getDiagname());
        detail.setRegisterUser(patMedOuthosp.getDrname());
        detail.setOrgid(patMedOuthosp.getOrgid());
        detail.setCampusid(campusid);
        detail.setDelFlag("0");
    }
 
    private void fillCommonChildDetail(PatChildArchiveDetail detail, PatMedOuthosp patMedOuthosp,
                                       PatArchive patArchive, String campusid) {
        detail.setOuthospno(patMedOuthosp.getOuthospno());
        detail.setSerialnum(patMedOuthosp.getSerialnum());
        detail.setPatid(patArchive != null ? patArchive.getId() : patMedOuthosp.getPatid());
        detail.setPatname(patArchive != null
                ? firstNotBlank(patArchive.getName(), patMedOuthosp.getPatname()) : patMedOuthosp.getPatname());
        detail.setBirthdate(patArchive != null ? patArchive.getBirthdate() : null);
        detail.setIdcardno(patArchive != null
                ? firstNotBlank(patArchive.getIdcardno(), patMedOuthosp.getIdcardno()) : patMedOuthosp.getIdcardno());
        detail.setPhone(patArchive != null
                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode()) : patMedOuthosp.getTelcode());
        detail.setSex(patArchive != null && patArchive.getSex() != null
                ? patArchive.getSex() : patMedOuthosp.getSex());
        detail.setDiagTime(patMedOuthosp.getAdmitdate());
        detail.setArchiveTime(new Date());
        detail.setDoctorCode(patMedOuthosp.getDrcode());
        detail.setDoctorName(patMedOuthosp.getDrname());
        detail.setManagementDoctorCode(patMedOuthosp.getDrcode());
        detail.setManagementDoctorName(patMedOuthosp.getDrname());
        detail.setDeptcode(patMedOuthosp.getDeptcode());
        detail.setDeptname(patMedOuthosp.getDeptname());
        detail.setIcd10code(patMedOuthosp.getIcd10code());
        detail.setDiagname(patMedOuthosp.getDiagname());
        detail.setRegisterUser(patMedOuthosp.getDrname());
        detail.setOrgid(patMedOuthosp.getOrgid());
        detail.setCampusid(campusid);
        detail.setDelFlag("0");
    }
 
    private PatArchive resolvePatArchive(PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
        if (patArchive != null && patArchive.getId() != null) {
            return patArchive;
        }
 
        PatArchive query = new PatArchive();
        if (StringUtils.isNotBlank(patMedOuthosp.getPatno())) {
            query.setPatientno(patMedOuthosp.getPatno());
        } else if (StringUtils.isNotBlank(patMedOuthosp.getIdcardno())) {
            query.setIdcardno(patMedOuthosp.getIdcardno());
        } else {
            return null;
        }
        query.setOrgid(patMedOuthosp.getOrgid());
 
        List<PatArchive> archives = patArchiveMapper.selectPatArchiveList(query);
        return CollectionUtils.isNotEmpty(archives) ? archives.get(0) : null;
    }
 
    private PatArchive resolvePatArchive(Long patid, PatMedInhosp inhosp) {
        if (patid != null) {
            PatArchive archive = patArchiveMapper.selectPatArchiveByPatid(patid);
            if (archive != null) {
                return archive;
            }
        }
        Long inhospPatid = inhosp == null ? null : inhosp.getPatid();
        return inhospPatid == null ? null : patArchiveMapper.selectPatArchiveByPatid(inhospPatid);
    }
 
    private PatMedInhosp resolveInhosp(ServiceSubtask subtask) {
        if (subtask.getInhospid() != null) {
            PatMedInhosp inhosp = inhospVisitFacade.selectPatMedInhospByInhospid(subtask.getInhospid());
            if (inhosp != null) {
                return inhosp;
            }
        }
        if (StringUtils.isNotBlank(subtask.getHospno())) {
            PatMedInhosp query = new PatMedInhosp();
            query.setInhospno(subtask.getHospno());
            query.setOrgid(subtask.getOrgid());
            List<PatMedInhosp> list = inhospVisitFacade.selectPatMedInhosp(query);
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        }
        return null;
    }
 
    private void recordLog(InhospArchiveContext context, PatSpecificArchiveGenerateConfig config,
                           String deptCategory, String archiveType, String result, Long archiveId, String msg) {
        PatSpecificArchiveGenerateLog log = new PatSpecificArchiveGenerateLog();
        log.setSourceType(SpecificArchiveConstants.SOURCE_TYPE_INHOSP_FOLLOWUP);
        log.setSourceBizId(inInhospid(context));
        log.setSourceBizNo(inInhospno(context));
        log.setSourceSubId(context.subtask.getId());
        log.setPatid(context.patid);
        log.setOrgid(context.orgid);
        log.setCampusid(context.campusid);
        log.setDeptCategory(deptCategory);
        log.setArchiveType(archiveType);
        log.setConfigId(config == null || config.getId() == null
                ? SpecificArchiveConstants.NO_CONFIG_ID : config.getId().intValue());
        log.setArchiveId(archiveId);
        log.setProcessResult(result);
        log.setProcessMsg(msg);
        PatSpecificArchiveGenerateLog old = patSpecificArchiveGenerateLogService.selectBySourceSubAndConfig(
                log.getSourceType(), log.getSourceSubId(), log.getConfigId());
        int retry = old == null || old.getRetryCount() == null ? 0 : old.getRetryCount();
        if (SpecificArchiveConstants.RESULT_FAILED.equals(result)) {
            retry++;
        }
        log.setRetryCount(retry);
        patSpecificArchiveGenerateLogService.saveOrUpdate(log);
    }
 
    private Long inInhospid(InhospArchiveContext context) {
        return firstNonNull(context.inhosp == null ? null : context.inhosp.getInhospid(),
                context.subtask.getInhospid());
    }
 
    private String inInhospno(InhospArchiveContext context) {
        return firstNotBlank(context.inhosp == null ? null : context.inhosp.getInhospno(),
                context.subtask.getHospno());
    }
 
    private String inSerialnum(InhospArchiveContext context) {
        return context.inhosp == null ? null : context.inhosp.getSerialnum();
    }
 
    private String inDrcode(InhospArchiveContext context) {
        return firstNotBlank(context.inhosp == null ? null : context.inhosp.getDrcode(), context.subtask.getDrcode());
    }
 
    private String inDrname(InhospArchiveContext context) {
        return firstNotBlank(context.inhosp == null ? null : context.inhosp.getDrname(), context.subtask.getDrname());
    }
 
    private List<String> parseCodes(String raw) {
        if (StringUtils.isBlank(raw)) {
            return Collections.emptyList();
        }
        String value = raw.trim();
        try {
            JSONArray array = JSON.parseArray(value);
            if (array != null) {
                List<String> result = new ArrayList<>();
                for (Object item : array) {
                    if (item != null && StringUtils.isNotBlank(String.valueOf(item))) {
                        result.add(String.valueOf(item).trim());
                    }
                }
                return result;
            }
        } catch (Exception e) {
            log.warn("解析档案生成配置集合失败,按逗号分隔处理:{}", raw);
        }
        return Arrays.stream(value.split(","))
                .map(String::trim)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());
    }
 
    private List<String> parseCsv(String raw) {
        if (StringUtils.isBlank(raw)) {
            return Collections.emptyList();
        }
        return Arrays.stream(raw.split(","))
                .map(String::trim)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toList());
    }
 
    private String firstNotBlank(String... values) {
        if (values == null) {
            return null;
        }
        for (String value : values) {
            if (StringUtils.isNotBlank(value)) {
                return value;
            }
        }
        return null;
    }
 
    private <T> T firstNonNull(T... values) {
        if (values == null) {
            return null;
        }
        for (T value : values) {
            if (value != null) {
                return value;
            }
        }
        return null;
    }
 
    private Date firstDate(Date first, Date second) {
        return first != null ? first : second;
    }
 
    private boolean containsIgnoreCase(List<String> source, String target) {
        if (StringUtils.isBlank(target)) {
            return false;
        }
        return source.stream().anyMatch(item -> equalsIgnoreCase(item, target));
    }
 
    private boolean equalsIgnoreCase(String first, String second) {
        return first != null && second != null && first.trim().equalsIgnoreCase(second.trim());
    }
 
    private String truncateMsg(String msg) {
        if (msg == null) {
            return null;
        }
        return msg.length() <= 900 ? msg : msg.substring(0, 900);
    }
 
    /**
     * 住院随访生成档案时用到的上下文。
     */
    private static class InhospArchiveContext {
        private ServiceSubtask subtask;
        private PatMedInhosp inhosp;
        private PatArchive patArchive;
        private Long patid;
        private String orgid;
        private String campusid;
        private String deptCode;
        private String deptName;
        private String wardCode;
        private String wardName;
        private String icd10Code;
        private String diagName;
        private Date diagTime;
    }
}