yxh
9 天以前 9df73858bc61e0e268113f5f79a8934e970a4f74
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
package com.smartor.service.impl;
 
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysUserDept;
import com.ruoyi.common.core.domain.entity.SysUserRole;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import com.smartor.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
 
/**
 * 患者档案Service业务层处理
 *
 * @author smartor
 * @date 2023-03-04
 */
@Slf4j
@Service
public class ExternalInfoServiceImpl implements ExternalInfoService {
    @Autowired
    private ExternalInfoMapper externalInfoMapper;
    @Autowired
    private PatArchiveMapper patArchiveMapper;
    @Autowired
    private PatMedOuthospMapper patMedOuthospMapper;
    @Autowired
    private PatMedInhospMapper patMedInhospMapper;
    @Autowired
    private WuxiSysUserMapper userMapper;
    @Autowired
    private WuxiSysDeptMapper deptMapper;
    @Autowired
    private SysUserDeptMapper sudMapper;
    @Autowired
    private WuxiSysUserRoleMapper sysUserRoleMapper;
 
    @Override
    public PatArchive getExternalPatientInfo(Long brid) {
        log.error("getExternalPatientInfo的数据入参为:{}", brid);
        List<ExternalPatientInfo> externalPatientInfo = externalInfoMapper.getExternalPatientInfo(brid.toString());
 
        List<ExternalInHospPatientInfo> externalInHospPatientInfos = externalInfoMapper.getExternalInHospPatientInfo(null, null, brid);
 
        log.error("externalPatientInfo的数据为:{}", externalPatientInfo.size());
        if (externalPatientInfo.size() == 0) {
            return null;
        }
        ExternalPatientInfo externalPatientInfo1 = externalPatientInfo.get(0);
        PatArchive patArchive = new PatArchive();
        patArchive.setPatientno(externalPatientInfo1.getJzkh());
        patArchive.setNotrequiredFlag("0");
        patArchive.setSourcefrom(1);
        patArchive.setPatidHis(externalPatientInfo1.getBrid());
        patArchive.setPattype("2");
        patArchive.setName(externalPatientInfo1.getBrxm());
        if (externalPatientInfo1.getBrxb().equals("男")) patArchive.setSex(1L);
        else if (externalPatientInfo1.getBrxb().equals("女")) patArchive.setSex(2L);
        //计算年龄
        if (ObjectUtils.isNotEmpty(externalPatientInfo1.getCsny())) {
            Map<String, String> map = calculateAge(externalPatientInfo1.getCsny().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now());
            patArchive.setAge(ObjectUtils.isNotEmpty(map.get("age")) ? Long.valueOf(map.get("age")) : null);
            patArchive.setAgeUnit(map.get("ageUnit"));
            patArchive.setAge2(ObjectUtils.isNotEmpty(map.get("age2")) ? Long.valueOf(map.get("age2")) : null);
            patArchive.setAgeUnit2(map.get("ageUnit2"));
        }
 
        patArchive.setNation(externalPatientInfo1.getMz());
        patArchive.setBirthplace(externalPatientInfo1.getCsd_x());
        patArchive.setNativePlace(externalPatientInfo1.getGj());
        patArchive.setPlaceOfResidence(externalPatientInfo1.getLxdz());
        patArchive.setIdcardno(externalPatientInfo1.getSfzh());
        if (CollectionUtils.isNotEmpty(externalInHospPatientInfos)) {
            patArchive.setIdcardno(externalInHospPatientInfos.get(0).getSfzh());
            if (StringUtils.isEmpty(externalInHospPatientInfos.get(0).getSfzh())) {
                patArchive.setIdcardno(externalPatientInfo1.getJzkh());
            }
        }
        patArchive.setBirthdate(externalPatientInfo1.getCsny());
        patArchive.setCarePerson(externalPatientInfo1.getLxrm());
        patArchive.setRelativetelcode(externalPatientInfo1.getLxdh());
        patArchive.setArchiveby(externalPatientInfo1.getJdr());
        patArchive.setTelcode(externalPatientInfo1.getJtdh());
        return patArchive;
    }
 
    public Map<String, String> calculateAge(LocalDate birthdate, LocalDate today) {
        if (birthdate == null || today.isBefore(birthdate)) {
            return null;
        }
        Map<String, String> ageMap = new HashMap<>();
 
        Period period = Period.between(birthdate, today);
        long totalDays = ChronoUnit.DAYS.between(birthdate, today);
        long totalMonths = ChronoUnit.MONTHS.between(birthdate, today);
 
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
 
        String ageUnit;
        Integer age;
        String ageUnit2 = null;
        Integer age2 = null;
 
        if (totalDays < 30) {
            // 小于 1 个月,按天计算
            ageUnit = "天";
            age = (int) totalDays;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", null);
            ageMap.put("ageUnit2", null);
        } else if (totalMonths < 12) {
            // 小于 一年,按月 + 天计算
            ageUnit = "月";
            age = (int) totalMonths;
            ageUnit2 = "天";
            age2 = days;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", age2 != null ? age2.toString() : null);
            ageMap.put("ageUnit2", ageUnit2);
        } else {
            // 大于等于 一年,按年 + 月计算
            ageUnit = "岁";
            age = years;
            ageUnit2 = "月";
            age2 = months;
            ageMap.put("age", age != null ? age.toString() : null);
            ageMap.put("ageUnit", ageUnit);
            ageMap.put("age2", age2 != null ? age2.toString() : null);
            ageMap.put("ageUnit2", ageUnit2);
        }
 
        return ageMap;
    }
 
    @Override
    public Boolean getExternalOutHospInfo() {
        //先去门急诊表获取最大的离院时间 admitdate
        String startTime = null;
        String endTime = null;
        Date date = patMedOuthospMapper.selectAdmitdateMax();
        if (ObjectUtils.isNotEmpty(date)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            startTime = formatter.format(date);
            endTime = formatter.format(new Date());
        }
        log.error("门急诊获取数据的开始时间:{},结束时间:{}", startTime, endTime);
        //获取门急诊的数据
        List<ExternalOutHospInfo> externalOutHospInfo = externalInfoMapper.getExternalOutHospInfo(startTime, endTime);
        log.error("门急诊获取数据的数量:{}", externalOutHospInfo.size());
        if (CollectionUtils.isNotEmpty(externalOutHospInfo)) {
            for (ExternalOutHospInfo externalOutHospInfo1 : externalOutHospInfo) {
                //先对患者基本信息进行处理
                Long patid = dealPatArchive(externalOutHospInfo1.getBrid());
                log.error("门急诊获取患者ID:{}", patid);
                //新增门急诊信息
                PatMedOuthosp patMedOuthosp = new PatMedOuthosp();
                patMedOuthosp.setOuthospno(externalOutHospInfo1.getJzkh());
                patMedOuthosp.setSerialnum(externalOutHospInfo1.getJzkh());
                patMedOuthosp.setPatid(patid);
                PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
                patMedOuthosp.setPatname(patArchive.getName());
                patMedOuthosp.setDeptcode("" + externalOutHospInfo1.getKsdm());
                patMedOuthosp.setDeptname(externalOutHospInfo1.getKsmc());
                patMedOuthosp.setDiagname(externalOutHospInfo1.getZddm());
                patMedOuthosp.setIcd10code(externalOutHospInfo1.getZdmc());
                patMedOuthosp.setDrcode(externalOutHospInfo1.getYsdm().toString());
                patMedOuthosp.setDrname(externalOutHospInfo1.getYsmc());
                patMedOuthosp.setAdmitdate(externalOutHospInfo1.getJzrq());
                patMedOuthosp.setSchemestatus(externalOutHospInfo1.getQzbz());
                patMedOuthosp.setMainsuit(externalOutHospInfo1.getZs());
                patMedOuthosp.setHpi(externalOutHospInfo1.getXbs());
                patMedOuthosp.setCategory(externalOutHospInfo1.getLb());
                patMedOuthosp.setCreateTime(new Date());
                patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp);
            }
        }
        return true;
    }
 
    /**
     * 这个方法没用了
     *
     * @return
     */
    @Override
    public Boolean getExternalOutHospPatientInfo() {
        return null;
    }
 
    /**
     * 获取入院数据
     *
     * @return
     */
    @Override
    public Boolean getExternalInHospPatientInfo() {
        String startTime = null;
        String endTime = null;
        Date date = patMedInhospMapper.selectStartTimeMax();
        if (ObjectUtils.isNotEmpty(date)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            startTime = formatter.format(date);
            endTime = formatter.format(new Date());
        }
        log.error("入院获取数据的开始时间:{},结束时间:{}", startTime, endTime);
        //获取入院患者信息数据
        List<ExternalInHospPatientInfo> externalInHospPatientInfo1 = externalInfoMapper.getExternalInHospPatientInfo(startTime, endTime, null);
        log.error("入院获取数据数量:{}", externalInHospPatientInfo1.size());
        for (ExternalInHospPatientInfo externalInHospPatientInfo : externalInHospPatientInfo1) {
            //先对患者基本信息进行处理
            Long patid = dealPatArchive(externalInHospPatientInfo.getBrid());
            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
            //新增入院数据
            PatMedInhosp patMedInhosp = new PatMedInhosp();
            patMedInhosp.setInhospno(externalInHospPatientInfo.getZyhm());
            List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList2(patMedInhosp);
            if (CollectionUtils.isNotEmpty(patMedInhospList) && patMedInhospList.size() > 0) {
                continue;
            }
 
            patMedInhosp.setSerialnum(externalInHospPatientInfo.getZyhm());
            patMedInhosp.setNurseId(externalInHospPatientInfo.getZrhsbm());
            patMedInhosp.setNurseName(externalInHospPatientInfo.getZrhs());
            patMedInhosp.setStarttime(externalInHospPatientInfo.getRyrq());
            patMedInhosp.setDiagname(externalInHospPatientInfo.getRyqk());
            patMedInhosp.setDeptcode(externalInHospPatientInfo.getBrks());
            patMedInhosp.setDeptname(externalInHospPatientInfo.getBrksmc());
            patMedInhosp.setDrname(externalInHospPatientInfo.getZzys());
            patMedInhosp.setDrcode(externalInHospPatientInfo.getZzysbm());
            patMedInhosp.setHospitaldistrictcode("" + externalInHospPatientInfo.getBrbqdm());
            patMedInhosp.setHospitaldistrictname("" + externalInHospPatientInfo.getBrbq());
            patMedInhosp.setBedNo("" + externalInHospPatientInfo.getBrch());
            patMedInhosp.setPatid(patid);
            patMedInhosp.setSchemestatus(1L);
            patMedInhosp.setInhospstate("0");
            patMedInhosp.setPatname(patArchive.getName());
            patMedInhosp.setTelcode(patArchive.getTelcode());
            patMedInhosp.setPatno(patArchive.getPatientno());
            patMedInhosp.setCreateTime(new Date());
            patMedInhospMapper.insertPatMedInhosp(patMedInhosp);
        }
 
        return true;
    }
 
    /**
     * 获取入院数据
     *
     * @return
     */
    @Override
    public Boolean getExternalInHospPatientInfoBY250325() {
        String startTime = null;
        String endTime = null;
        Date date = patMedInhospMapper.selectStartTimeMax();
        if (ObjectUtils.isNotEmpty(date)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            startTime = "2025-03-25";
            endTime = formatter.format(new Date());
        }
        log.error("入院获取数据的开始时间:{},结束时间:{}", startTime, endTime);
        //获取入院患者信息数据
        List<ExternalInHospPatientInfo> externalInHospPatientInfo1 = externalInfoMapper.getExternalInHospPatientInfo(startTime, endTime, null);
        log.error("入院获取数据数量:{}", externalInHospPatientInfo1.size());
        for (ExternalInHospPatientInfo externalInHospPatientInfo : externalInHospPatientInfo1) {
            //先对患者基本信息进行处理
            Long patid = dealPatArchive(externalInHospPatientInfo.getBrid());
            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
            //新增入院数据
            PatMedInhosp patMedInhosp = new PatMedInhosp();
            patMedInhosp.setInhospno(externalInHospPatientInfo.getZyhm());
            List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList2(patMedInhosp);
            if (CollectionUtils.isNotEmpty(patMedInhospList) && patMedInhospList.size() > 0) {
                patMedInhospList.get(0).setDeptcode(externalInHospPatientInfo.getBrks());
                patMedInhospList.get(0).setDeptname(externalInHospPatientInfo.getBrksmc());
                patMedInhospList.get(0).setDrname(externalInHospPatientInfo.getZzys());
                patMedInhospList.get(0).setDrcode(externalInHospPatientInfo.getZzysbm());
                patMedInhospList.get(0).setHospitaldistrictcode("" + externalInHospPatientInfo.getBrbqdm());
                patMedInhospList.get(0).setHospitaldistrictname("" + externalInHospPatientInfo.getBrbq());
                patMedInhospList.get(0).setBedNo("" + externalInHospPatientInfo.getBrch());
                patMedInhospList.get(0).setUpdateTime(new Date());
                patMedInhospMapper.updatePatMedInhosp(patMedInhospList.get(0));
                continue;
            }
 
            patMedInhosp.setSerialnum(externalInHospPatientInfo.getZyhm());
            patMedInhosp.setNurseId(externalInHospPatientInfo.getZrhsbm());
            patMedInhosp.setNurseName(externalInHospPatientInfo.getZrhs());
            patMedInhosp.setStarttime(externalInHospPatientInfo.getRyrq());
            patMedInhosp.setDiagname(externalInHospPatientInfo.getRyqk());
            patMedInhosp.setDeptcode(externalInHospPatientInfo.getBrks());
            patMedInhosp.setDeptname(externalInHospPatientInfo.getBrksmc());
            patMedInhosp.setDrname(externalInHospPatientInfo.getZzys());
            patMedInhosp.setDrcode(externalInHospPatientInfo.getZzysbm());
            patMedInhosp.setHospitaldistrictcode("" + externalInHospPatientInfo.getBrbqdm());
            patMedInhosp.setHospitaldistrictname("" + externalInHospPatientInfo.getBrbq());
            patMedInhosp.setBedNo("" + externalInHospPatientInfo.getBrch());
            patMedInhosp.setPatid(patid);
            patMedInhosp.setSchemestatus(1L);
            patMedInhosp.setInhospstate("0");
            patMedInhosp.setPatname(patArchive.getName());
            patMedInhosp.setTelcode(patArchive.getTelcode());
            patMedInhosp.setPatno(patArchive.getPatientno());
            patMedInhosp.setCreateTime(new Date());
            patMedInhospMapper.insertPatMedInhosp(patMedInhosp);
        }
 
        return true;
    }
 
    @Override
    public Boolean getExternalUserAndDeptInfo() {
        //获取用户信息
        String startTime = null;
        String endTime = null;
        Date date = null;
//      date = userMapper.selectStartTimeMax();
//        if (ObjectUtils.isNotEmpty(date)) {
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            startTime = formatter.format(date);
//            endTime = formatter.format(new Date());
//        }
//        log.error("externalUserInfoList用户的时间入参:{},{}", startTime, endTime);
        List<ExternalUserInfo> externalUserInfoList = externalInfoMapper.getExternalUserInfo(startTime, endTime);
        log.error("externalUserInfoList用户的数量:{}", externalUserInfoList.size());
        if (CollectionUtils.isNotEmpty(externalUserInfoList)) {
            for (ExternalUserInfo externalUserInfo : externalUserInfoList) {
                log.error("externalUserInfo的数据为:{}", externalUserInfo);
                SysUser sysUser = new SysUser();
                sysUser.setHisUserId(externalUserInfo.getHisUserId());
                sysUser.setGuid(externalUserInfo.getOrgCode());
                sysUser.setDeptCode(externalUserInfo.getDeptCode());
                sysUser.setDeptName(externalUserInfo.getDeptName());
                sysUser.setSearchscope("1");
                if (externalUserInfo.getDeptName().contains("病区")) sysUser.setSearchscope("2");
                sysUser.setNickName(externalUserInfo.getUserName());
                sysUser.setUserName(externalUserInfo.getLoginName());
                sysUser.setPhonenumber(externalUserInfo.getTel());
                sysUser.setTitle(externalUserInfo.getZc());
                sysUser.setUserType(externalUserInfo.getGzlb());
                sysUser.setSex(externalUserInfo.getSex());
                sysUser.setIdCard(externalUserInfo.getIdCard());
                // wxrt@0230
                String mm = externalUserInfo.getMm();
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                sysUser.setPassword(passwordEncoder.encode(mm));
                //先判断该用户是否存在
                SysUser sysUser1 = new SysUser();
                sysUser1.setUserName(externalUserInfo.getLoginName());
                List<SysUser> sysUserInfo = userMapper.getSysUserInfo(sysUser1);
                if (CollectionUtils.isNotEmpty(sysUserInfo) && sysUserInfo.size() > 0) {
                    log.error("sysUser的进入了修改");
                    sysUser.setUserId(sysUserInfo.get(0).getUserId());
                    log.error("sysUser的添加值为:{}", sysUser);
                    userMapper.updateUser(sysUser);
                } else {
                    log.error("sysUser的进入了新增");
                    userMapper.insertUser(sysUser);
                }
            }
        }
        //部门、部门与用户关联、与角色关联的代码,先不更新
//        String sTime = null;
//        String eTime = null;
//        date = deptMapper.selectStartTimeMax();
//        if (ObjectUtils.isNotEmpty(date)) {
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            sTime = formatter.format(date);
//            eTime = formatter.format(new Date());
//        }
//        //新增科室
//        List<ExternalDeptInfo> externalDeptInfoList = externalInfoMapper.getExternalDeptInfo(sTime, eTime);
//        if (CollectionUtils.isNotEmpty(externalDeptInfoList)) {
//            for (ExternalDeptInfo externalDeptInfo : externalDeptInfoList) {
//                SysDept sysDept = new SysDept();
//
//                if (StringUtils.isNotEmpty(externalDeptInfo.getDeptCode())) {
//                    sysDept.setDeptCode(externalDeptInfo.getDeptCode());
//                    sysDept.setDeptName(externalDeptInfo.getDeptName());
//                    sysDept.setDeptType("1");
//                    if (externalDeptInfo.getDeptName().contains("病区")) sysDept.setDeptType("2");
//                    if (StringUtils.isNotEmpty(externalDeptInfo.getSjksdm()))
//                        sysDept.setParentCode(externalDeptInfo.getSjksdm().toString());
//                    sysDept.setStatus("0");
//                    //查询一下,该科室或病区是否存在如果不存在则新增
//                    SysDept sd = new SysDept();
//                    if (StringUtils.isNotEmpty(sysDept.getDeptCode())) {
//                        sd.setDeptCode(sysDept.getDeptCode());
//                    }
//                    List<SysDept> sysDepts = deptMapper.selectDeptList(sd);
//                    if (CollectionUtils.isNotEmpty(sysDepts)) {
//                        sysDept.setDeptId(sysDepts.get(0).getDeptId());
//                        log.error("sysDept的修改数据为:{}", sysDept);
//                        deptMapper.updateDept(sysDept);
//                    } else {
//                        log.error("sysDept的新增数据为:{}", sysDept);
//                        deptMapper.insertDept(sysDept);
//                    }
//                }
//                if (StringUtils.isNotEmpty(externalDeptInfo.getBqdm())) {
//                    sysDept.setDeptCode(externalDeptInfo.getBqdm());
//                    sysDept.setDeptName(externalDeptInfo.getBqmc());
//                    sysDept.setDeptType("2");
//                    if (StringUtils.isNotEmpty(externalDeptInfo.getSjksdm()))
//                        sysDept.setParentCode(externalDeptInfo.getSjksdm().toString());
//                    sysDept.setStatus("0");
//                    //查询一下,该科室或病区是否存在如果不存在则新增
//                    SysDept sd = new SysDept();
//                    if (StringUtils.isNotEmpty(sysDept.getDeptCode())) {
//                        sd.setDeptCode(sysDept.getDeptCode());
//                    }
//                    List<SysDept> sysDepts = deptMapper.selectDeptList(sd);
//                    if (CollectionUtils.isNotEmpty(sysDepts)) {
//                        sysDept.setDeptId(sysDepts.get(0).getDeptId());
//                        deptMapper.updateDept(sysDept);
//                    } else {
//                        deptMapper.insertDept(sysDept);
//                    }
//                }
//
//            }
//        }
 
//        //新增用户与科室的关系
//        List<SysUser> sysUserList = userMapper.getSysUserInfo(null);
//        for (SysUser sysUser : sysUserList) {
//            //先通过userId和deptCode查一遍是否已经关联
//            SysUserDept sud = new SysUserDept();
//            sud.setDeptCode(sysUser.getDeptCode());
//            sud.setUserId(sysUser.getUserId());
//            List<SysUserDept> sysUserDepts = sudMapper.selectSysUserDeptList(sud);
//            if (CollectionUtils.isNotEmpty(sysUserDepts)) continue;
//            log.error("sysUser.getDeptCode()的值为:{}", sysUser.getDeptCode());
//            //管理员不需要
//            if (sysUser.getDeptCode().equals("0108")) continue;
//            SysDept sysDept = deptMapper.selectDeptByCode(sysUser.getDeptCode());
//            SysUserDept sysUserDept = new SysUserDept();
//            sysUserDept.setUserId(sysUser.getUserId());
//            sysUserDept.setDeptType(sysDept.getDeptType());
//            sysUserDept.setDeptCode(sysDept.getDeptCode());
//            sysUserDept.setDeptName(sysDept.getDeptName());
//            sysUserDept.setDeptId(sysDept.getDeptId());
//            sudMapper.insertSysUserDept(sysUserDept);
//            //新增sys_user_role
//            if (StringUtils.isNotEmpty(sysUser.getUserType())) {
//                SysUserRole sysUserRole = new SysUserRole();
//                sysUserRole.setUserId(sysUser.getUserId());
//                if (sysUser.getUserType().equals("护士")) {
//                    sysUserRole.setRoleId(4L);
//                } else if (sysUser.getUserType().equals("医生")) {
//                    sysUserRole.setRoleId(3L);
//                } else if (sysUser.getUserType().equals("管理")) {
//                    sysUserRole.setRoleId(2L);
//                } else {
//                    sysUserRole.setRoleId(2L);
//                }
//                sysUserRoleMapper.insertUserRole(sysUserRole);
//            }
//        }
        return true;
    }
 
    /**
     * 获取出数院信息
     *
     * @return
     */
    @Override
    public Boolean getExternalLeaveHospPatientInfo() {
        String startTime = null;
        String endTime = null;
        Date date = patMedInhospMapper.selectEndtimeMax();
        if (ObjectUtils.isNotEmpty(date)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            startTime = formatter.format(date);
            endTime = formatter.format(new Date());
        }
        List<ExternalLeaveHospPatientInfo> externalLeaveHospPatientInfo = externalInfoMapper.getExternalLeaveHospPatientInfo(startTime, endTime);
        for (ExternalLeaveHospPatientInfo externalLeaveHospPatientInfo1 : externalLeaveHospPatientInfo) {
            //先对患者基本信息进行处理
            Long patid = dealPatArchive(externalLeaveHospPatientInfo1.getBrid());
            //新增出院数据
            //根据住院号查询入院信息
            PatMedInhosp pmi = new PatMedInhosp();
            pmi.setInhospno(externalLeaveHospPatientInfo1.getZybm());
            List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList2(pmi);
            log.error("通过住院号:{},查询患者入院的信息为:{}", externalLeaveHospPatientInfo1.getZybm(), patMedInhospList);
//            log.error("externalLeaveHospPatientInfo1的结果为:{}", externalLeaveHospPatientInfo1);
            if (CollectionUtils.isEmpty(patMedInhospList)) {
                //如果不存在入院信息,是不是需要获取一下入院信息,则进行新增()
                List<ExternalInHospPatientInfo> externalInHospPatientInfo1 = externalInfoMapper.getExternalInHospPatientInfo(null, null, externalLeaveHospPatientInfo1.getBrid());
                if (CollectionUtils.isNotEmpty(externalInHospPatientInfo1)) {
                    //先对患者基本信息进行处理
                    Long pid = dealPatArchive(externalInHospPatientInfo1.get(0).getBrid());
                    PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
                    //新增入院数据
                    PatMedInhosp inhosp = new PatMedInhosp();
                    inhosp.setInhospno(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setSerialnum(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setNurseId(externalInHospPatientInfo1.get(0).getZrhsbm());
                    inhosp.setNurseName(externalInHospPatientInfo1.get(0).getZrhs());
                    inhosp.setStarttime(externalInHospPatientInfo1.get(0).getRyrq());
                    inhosp.setDiagname(externalInHospPatientInfo1.get(0).getRyqk());
                    inhosp.setDeptcode(externalLeaveHospPatientInfo1.getCyks());
                    inhosp.setDeptname(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setDrname(externalLeaveHospPatientInfo1.getZrys());
                    inhosp.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                    inhosp.setHospitaldistrictid("" + externalInHospPatientInfo1.get(0).getBrbqdm());
                    inhosp.setBedNo("" + externalInHospPatientInfo1.get(0).getBrch());
                    inhosp.setPatid(pid);
                    inhosp.setPatname(patArchive.getName());
                    inhosp.setTelcode(patArchive.getTelcode());
                    inhosp.setPatno(patArchive.getPatientno());
                    inhosp.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                    inhosp.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                    inhosp.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                    inhosp.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                    inhosp.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
                    inhosp.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
 
                    inhosp.setSchemestatus(2L);
                    inhosp.setInhospstate("1");
                    inhosp.setCreateTime(new Date());
//                    log.error("新增的patMedInhosp为:{}", inhosp);
                    int i = patMedInhospMapper.insertPatMedInhosp(inhosp);
                    log.error("新增的patMedInhosp结果为:{}", i);
 
                } else {
                    log.error("该病人不存在入院信息,病人ID:{}", externalLeaveHospPatientInfo1.getBrid());
                }
 
            } else if (CollectionUtils.isNotEmpty(patMedInhospList)) {
                //如果存在入院信息,则进行修改
                pmi = patMedInhospList.get(0);
                pmi.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                pmi.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                pmi.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                pmi.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                pmi.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
                pmi.setSchemestatus(2L);
                pmi.setInhospstate("1");
                pmi.setUpdateTime(new Date());
                pmi.setNurseId(externalLeaveHospPatientInfo1.getZrhsbm());
                pmi.setNurseName(externalLeaveHospPatientInfo1.getZrhs());
                pmi.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                pmi.setDrname(externalLeaveHospPatientInfo1.getZrys());
 
                log.error("修改的pmi结果为:{}", pmi);
                patMedInhospMapper.updatePatMedInhosp(pmi);
            }
 
        }
        return null;
    }
 
 
    /**
     * 获取出数院信息从2025-03-25开始
     *
     * @return
     */
    @Override
    public Boolean getExternalLeaveHospPatientInfo30Day() {
        String startTime = null;
        String endTime = null;
        Date date = patMedInhospMapper.selectEndtimeMax();
        if (ObjectUtils.isNotEmpty(date)) {
            Date now = new Date();
            // 使用Calendar计算30天前
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DAY_OF_MONTH, -30);
            Date before30Days = cal.getTime();
 
 
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            startTime = formatter.format(date);
//            startTime = formatter.format(before30Days);
            startTime = "2025-03-25";
            endTime = formatter.format(new Date());
        }
        List<ExternalLeaveHospPatientInfo> externalLeaveHospPatientInfo = externalInfoMapper.getExternalLeaveHospPatientInfo(startTime, endTime);
        for (ExternalLeaveHospPatientInfo externalLeaveHospPatientInfo1 : externalLeaveHospPatientInfo) {
            //先对患者基本信息进行处理
            Long patid = dealPatArchive(externalLeaveHospPatientInfo1.getBrid());
            //新增出院数据
            //根据住院号查询入院信息
            PatMedInhosp pmi = new PatMedInhosp();
            pmi.setInhospno(externalLeaveHospPatientInfo1.getZybm());
            List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList2(pmi);
            log.error("通过住院号:{},查询患者入院的信息为:{}", externalLeaveHospPatientInfo1.getZybm(), patMedInhospList);
//            log.error("externalLeaveHospPatientInfo1的结果为:{}", externalLeaveHospPatientInfo1);
            if (CollectionUtils.isEmpty(patMedInhospList)) {
                //如果不存在入院信息,是不是需要获取一下入院信息,则进行新增()
                List<ExternalInHospPatientInfo> externalInHospPatientInfo1 = externalInfoMapper.getExternalInHospPatientInfo(null, null, externalLeaveHospPatientInfo1.getBrid());
                if (CollectionUtils.isNotEmpty(externalInHospPatientInfo1)) {
                    //先对患者基本信息进行处理
                    Long pid = dealPatArchive(externalInHospPatientInfo1.get(0).getBrid());
                    PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
                    //新增入院数据
                    PatMedInhosp inhosp = new PatMedInhosp();
                    inhosp.setInhospno(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setSerialnum(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setNurseId(externalInHospPatientInfo1.get(0).getZrhsbm());
                    inhosp.setNurseName(externalInHospPatientInfo1.get(0).getZrhs());
                    inhosp.setStarttime(externalInHospPatientInfo1.get(0).getRyrq());
                    inhosp.setDiagname(externalInHospPatientInfo1.get(0).getRyqk());
                    inhosp.setDeptcode(externalLeaveHospPatientInfo1.getCyks());
                    inhosp.setDeptname(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setDrname(externalLeaveHospPatientInfo1.getZrys());
                    inhosp.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                    inhosp.setHospitaldistrictid("" + externalInHospPatientInfo1.get(0).getBrbqdm());
                    inhosp.setBedNo("" + externalInHospPatientInfo1.get(0).getBrch());
                    inhosp.setPatid(pid);
                    inhosp.setPatname(patArchive.getName());
                    inhosp.setTelcode(patArchive.getTelcode());
                    inhosp.setPatno(patArchive.getPatientno());
                    inhosp.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                    inhosp.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                    inhosp.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                    inhosp.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                    inhosp.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
                    inhosp.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
 
                    inhosp.setSchemestatus(2L);
                    inhosp.setInhospstate("1");
                    inhosp.setCreateTime(new Date());
//                    log.error("新增的patMedInhosp为:{}", inhosp);
                    int i = patMedInhospMapper.insertPatMedInhosp(inhosp);
                    log.error("新增的patMedInhosp结果为:{}", i);
 
                } else {
                    log.error("该病人不存在入院信息,病人ID:{}", externalLeaveHospPatientInfo1.getBrid());
                }
 
            } else if (CollectionUtils.isNotEmpty(patMedInhospList)) {
                //如果存在入院信息,则进行修改
                pmi = patMedInhospList.get(0);
                pmi.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                pmi.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                pmi.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                pmi.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                pmi.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
//                pmi.setSchemestatus(2L);
                pmi.setInhospstate("1");
//                pmi.setUpdateTime(new Date());
                pmi.setNurseId(externalLeaveHospPatientInfo1.getZrhsbm());
                pmi.setNurseName(externalLeaveHospPatientInfo1.getZrhs());
                pmi.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                pmi.setDrname(externalLeaveHospPatientInfo1.getZrys());
 
                log.error("修改的pmi结果为:{}", pmi);
                patMedInhospMapper.updatePatMedInhosp(pmi);
            }
 
        }
        return null;
    }
 
 
    /**
     * 患者基本信息进行处理
     *
     * @param brid
     * @return
     */
    @Override
    public Long dealPatArchive(Long brid) {
        PatArchive patArchive = new PatArchive();
        patArchive.setPatidHis(brid);
        List<PatArchive> patArchives = patArchiveMapper.selectPatArchiveList(patArchive);
        PatArchive patientInfo = getExternalPatientInfo(brid);
        if (ObjectUtils.isNotEmpty(patientInfo)) {
            if (CollectionUtils.isNotEmpty(patArchives)) {
                //如果不为空,则走更新操作
                patientInfo.setId(patArchives.get(0).getId());
                patientInfo.setUpdateTime(new Date());
                patArchiveMapper.updatePatArchive(patientInfo);
            } else {
                patientInfo.setCreateTime(new Date());
                patArchiveMapper.insertPatArchiveSingle(patientInfo);
            }
            return patientInfo.getId();
        }
        return null;
    }
}