zjh
4 天以前 bd1618cb6bdae1da53fd21df7afbd4962278f9c8
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
package com.ltkj.web.controller.system;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltkj.common.annotation.Log;
import com.ltkj.common.annotation.RepeatSubmit;
import com.ltkj.common.enums.BusinessType;
import com.ltkj.common.enums.DataSourceType;
import com.ltkj.common.utils.SecurityUtils;
import com.ltkj.db.DataSourceContextHolder;
import com.ltkj.framework.config.MatchUtils;
import com.ltkj.framework.datasource.DynamicDataSourceContextHolder;
import com.ltkj.hosp.domain.HisApiConfig;
import com.ltkj.hosp.domain.TjCustomerBlack;
import com.ltkj.hosp.domain.TjReservation;
import com.ltkj.hosp.hisDto.OutpinregapplyDto;
import com.ltkj.hosp.mapper.TestMapper;
import com.ltkj.hosp.service.*;
import com.ltkj.hosp.sqlDomain.LtkjMiddleDetail;
import com.ltkj.hosp.sqlDomain.LtkjMiddleHead;
import com.ltkj.hosp.sqlDomain.LtkjTjPat;
import com.ltkj.system.service.ISysConfigService;
import com.ltkj.web.controller.his.HisApiGetMethodService;
import com.ltkj.web.controller.his.HisApiMethod;
import com.ltkj.web.controller.his.HisApiMethodService;
import com.ltkj.web.wxUtils.HttpClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import com.ltkj.common.core.controller.BaseController;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.hosp.domain.TjCustomer;
import com.ltkj.common.utils.poi.ExcelUtil;
import com.ltkj.common.core.page.TableDataInfo;
 
import java.util.*;
import java.util.stream.Collectors;
 
 
/**
 * 客户信息Controller
 *
 * @author ltkj
 * @date 2022-11-17
 */
@RestController
@RequestMapping("/hosp/customer")
@Api(tags = "客户管理接口")
@Slf4j
public class TjCustomerController extends BaseController {
    @Resource
    private ITjCustomerService tjCustomerService;
    @Autowired
    private HisApiGetMethodService hisApiGetMethodService;
    @Resource
    private ITjReservationService tjReservationService;
    @Autowired
    private HisApiConfigService hisApiConfigService;
    @Resource
    private ISysConfigService sysConfigService;
 
    @Resource
    private TestMapper testMapper;
    @Resource
    private TjvLtkjvtjpatService tjvLtkjvtjpatService;
    @Resource
    private LtkjMiddleHeadService headService;
    @Resource
    private LtkjMiddleDetailService middleDetailService;
    @Resource
    private ITjOrderService tjOrderService;
    @Resource
    private TjCustomerBlackService blackService;
    @Autowired
    private HisApiMethod hisApiMethod;
    @Autowired
    private HisApiMethodService controller;
 
 
    //将方法返回值解析成json格式
    public JSONObject getJSONObject(String builder) {
        String Response = JSONUtil.parseObj(builder).getStr("Response");
        return JSONUtil.parseObj(Response);
    }
 
    //获取方法返回值信息
    public String getAjaxResult(AjaxResult result) {
        return result.get("data").toString();
    }
 
    @GetMapping("/newGetTjPat")
    @ApiOperation(value = "查询his数据库返回新的数据")
    public AjaxResult newGetTjPat(@RequestParam(required = false) String pacCode,
                                  @RequestParam(required = false) String pacName,
                                  @RequestParam(required = false) String pacRemark) {
        List<Map<String, Object>> maps = testMapper.newGetTjPat(pacCode, pacName, pacRemark);
        DynamicDataSourceContextHolder.clearDataSourceType();
//        List<Map<String, Object>> list=new ArrayList<>();
//        if(null !=maps && maps.size()>0){
//            for (Map<String, Object> map : maps) {
//                String pacCode1 = map.get("pacCode").toString();
//                List<String> s = tjOrderService.selectTjOrderByCardId1();
//                if(null !=s && s.size()>0){
//                    if(s.contains(pacCode1)){
////                        maps.remove(map);
//                        continue;
//                    }
//                    list.add(map);
//                }
//            }
//        }
        List<String> strings = tjOrderService.selectTjOrderByCardId1();
        if (null != strings && !strings.isEmpty()) {
            List<Map<String, Object>> collect = maps.stream()
                    .filter(item -> !strings.contains(item.get("pacCode").toString())).collect(Collectors.toList());
            return AjaxResult.success(collect);
        }
        return AjaxResult.success(maps);
//        return AjaxResult.success(maps);
    }
 
 
    @GetMapping("/vcustomerlist")
    @ApiOperation(value = "签到登记页面姓名栏弹框接口")
    public AjaxResult vcustomerlist(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String sjh,
                                  @RequestParam(required = false) String sfzh) {
        List<Map<String, Object>> maps = tjCustomerService.vcustomerlist(name, sjh, sfzh);
        return AjaxResult.success(maps);
    }
 
 
    /**
     * 根据身份证号获取用户信息
     */
    @PostMapping(value = "/cusIdcard")
    @ApiOperation(value = "根据身份证号获取用户信息")
//    @Log(title = "客户身份信息", businessType = BusinessType.EXPORT)
    public AjaxResult getInfoByIdCard(@RequestParam @ApiParam(value = "客户身份证号") String cusIdcard) {
        if (!"".equals(cusIdcard) && cusIdcard != null) {
 
            String sfjysfzh = sysConfigService.selectConfigByKey("sfjysfzh");
//            String sfjysjh = configService.selectConfigByKey("sfjysjh");
 
            //判断身份证号格式是否正确
            if(sfjysfzh.equalsIgnoreCase("Y")){
                if(cusIdcard.length()==18){
                    if (! MatchUtils.isIdCard(cusIdcard)) {
                        return AjaxResult.error("身份证号码错误");
                    }
                }
//                if(cusIdcard.length()==9){
//                    if (!MatchUtils.cardValidates(cusIdcard)) {
//                        return AjaxResult.error("身份证号码错误");
//                    }
//                }
            }
 
 
            final String getInfoFromSqlData = sysConfigService.selectConfigByKey("getInfoFromSqlData");
            if ("Y".equals(getInfoFromSqlData)) {
                //根据身份证号从his查信息
                LtkjTjPat tjPatByIdCard = testMapper.getTjPatByIdCard(cusIdcard);
                if (null != tjPatByIdCard) {
                    String fcardNo = tjPatByIdCard.getFcardNo();
                    if (null != fcardNo && !fcardNo.isEmpty()) {
                        fcardNo = fcardNo.trim();
                        tjPatByIdCard.setFcardNo(fcardNo);
                    } else {
                        fcardNo = cusIdcard.trim();
                        tjPatByIdCard.setFcardNo(fcardNo);
                    }
                    //根据身份证号从数据库拿信息 没有的话保存
                    LtkjTjPat ltkjTjPat = tjvLtkjvtjpatService.getLtkjTjPatByCusIdCard(cusIdcard);
                    if (null == ltkjTjPat) {
                        tjvLtkjvtjpatService.save(tjPatByIdCard);
                    }
                    DynamicDataSourceContextHolder.clearDataSourceType();
                    //再根据身份证号写customer表
                    TjCustomer tjCustomer = tjCustomerService.getTjCustomerByCusIdCard(tjPatByIdCard.getFcardNo());
                    if (null == tjCustomer) {
                        //为空的话掉存储过程保存到客户表
                        testMapper.getTjPatByCusId(cusIdcard);
                        LambdaQueryWrapper<TjCustomer> qw = new LambdaQueryWrapper<>();
                        qw.eq(TjCustomer::getCusIdcard, tjPatByIdCard.getFcardNo());
                        TjCustomer customer = tjCustomerService.getOne(qw);
                        customer.setReservationId(customer.getCardId());
                        extracted(customer);
                        return success(customer);
                    } else {
                        String cardId = tjPatByIdCard.getFcardId();
                        if (null != cardId && !cardId.equals("0")) {
                            int i = tjOrderService.selectTjOrderByCardId(cardId);
                            if (i > 0) {
                                return AjaxResult.error("不可重复签到");
                            }
 
                        }
                        tjCustomer.setReservationId(tjCustomer.getCardId());
                        tjCustomer.setCardId(tjPatByIdCard.getFcardId());
                        tjCustomerService.updateById(tjCustomer);
                        extracted(tjCustomer);
                        return success(tjCustomer);
                    }
 
                }
            }
 
            String key = sysConfigService.selectConfigByKey("sfkqdyhis");
            String s = sysConfigService.selectConfigByKey("is_request_common_his_api");
            LambdaQueryWrapper<TjReservation> wq = new LambdaQueryWrapper<>();
            wq.eq(TjReservation::getIdCard, cusIdcard);
            wq.eq(TjReservation::getIsExpire, 2);
            TjReservation tjReservation = tjReservationService.getOne(wq);
            if (tjReservation != null) {
 
                if (null != tjReservation.getReservationTime() && DateUtil.endOfDay(DateUtil.offsetDay(tjReservation.getReservationTime(),
                        tjReservation.getYxts())).before(new Date())) {
                    tjReservation.setIsExpire(1);
                    tjReservationService.updateById(tjReservation);
                    return AjaxResult.error("对不起您的预约已超时请重新预约");
                }
                LambdaQueryWrapper<TjCustomer> qw = new LambdaQueryWrapper<>();
                qw.eq(TjCustomer::getCusIdcard, cusIdcard);
                TjCustomer tjCustomer1 = tjCustomerService.getOne(qw);
                if (tjCustomer1 != null) {
//                    if (null != tjReservation.getCompanyId()) tjCustomer1.setDictCompId(Long.valueOf(tjReservation.getCompanyId()));
//                    if (null != tjReservation.getCompany()) tjCustomer1.setCompName(tjReservation.getCompany());
//                    tjCustomerService.updateById(tjCustomer1);
                    tjCustomer1.setTjType(tjReservation.getTjType());
                    tjCustomer1.setCusName(tjReservation.getName());
                    tjCustomer1.setCusSex(Long.valueOf(tjReservation.getSex()));
                    try {
                        tjCustomer1.setCusBrithday(MatchUtils.getBirthDayByIdCard(tjReservation.getIdCard().replaceAll(" ", "")));
                    } catch (Exception e) {
                        return AjaxResult.error("身份证号不正确");
                    }
                    tjCustomer1.setCusPhone(tjReservation.getPhoe());
                    tjCustomer1.setCusEmail(tjReservation.getEmail());
                    tjCustomer1.setCusAddr(tjReservation.getAddress());
                    tjCustomer1.setCusMarryStatus(String.valueOf(tjReservation.getMarriage()));
                    tjCustomer1.setCusNational(String.valueOf(tjReservation.getNation()));
 
                    tjCustomer1.setIdType(tjReservation.getIdType());
                    tjCustomer1.setAge(tjReservation.getAge());
                    tjCustomer1.setAgeUnit(tjReservation.getAgeUnit());
                    tjCustomer1.setCareer(tjReservation.getCareer());
                    tjCustomer1.setPym(PinyinUtil.getFirstLetter(tjReservation.getName(),""));
 
                    tjCustomer1.setTeamNo(tjReservation.getTeamNo());
                    tjCustomer1.setCompId(tjReservation.getCompanyId());
                    tjCustomer1.setReservationId(tjReservation.getId());
                    tjCustomer1.setDiscount(tjReservation.getDiscount());
                    tjCustomer1.setGroupingId(tjReservation.getGroupingId());
 
//                    if(null !=tjReservation.getCompanyId())tjCustomer1.setDictCompId(Long.valueOf(tjReservation.getCompanyId()));
                    if(null !=tjReservation.getCompany())tjCustomer1.setCompName(tjReservation.getCompany());
 
//                    tjCustomerService.updateById(tjCustomer1);
 
                    return success(tjCustomer1);
                }else {
                    TjCustomer tjCustomer = new TjCustomer();
                    tjCustomer.setCusIdcard(tjReservation.getIdCard());
                    tjCustomer.setCusName(tjReservation.getName());
                    tjCustomer.setCusSex(Long.valueOf(tjReservation.getSex()));
                    try {
                        tjCustomer.setCusBrithday(MatchUtils.getBirthDayByIdCard(tjReservation.getIdCard()));
                    } catch (Exception e) {
                        return AjaxResult.error("身份证号不正确");
                    }
                    tjCustomer.setCusPhone(tjReservation.getPhoe());
                    tjCustomer.setCusEmail(tjReservation.getEmail());
                    tjCustomer.setCusAddr(tjReservation.getAddress());
                    tjCustomer.setCusMarryStatus(String.valueOf(tjReservation.getMarriage()));
                    tjCustomer.setCusNational(String.valueOf(tjReservation.getNation()));
 
                    tjCustomer.setIdType(tjReservation.getIdType());
                    tjCustomer.setAge(tjReservation.getAge());
                    tjCustomer.setAgeUnit(tjReservation.getAgeUnit());
                    tjCustomer.setCareer(tjReservation.getCareer());
                    tjCustomer.setDwPhone(tjReservation.getDwPhone());
//                    tjCustomer.setCardId(tjReservation.getCardId());
                    tjCustomer.setIndexCard(tjReservation.getIndexCard());
                    //截取密码自动生成set进去
                    String substring = cusIdcard.substring(cusIdcard.length() - 6);
                    substring = DigestUtils.md5DigestAsHex(substring.getBytes());
                    tjCustomer.setCusPassword(substring);
                    tjCustomer.setTjType(tjReservation.getTjType());
                    tjCustomer.setPym(PinyinUtil.getFirstLetter(tjReservation.getName(),""));
                    if(null !=tjReservation.getCompanyId())tjCustomer.setCompId(tjReservation.getCompanyId());
                    if(null !=tjReservation.getCompany())tjCustomer.setCompName(tjReservation.getCompany());
 
                    if (null != key && key.equals("Y")) {
                        AjaxResult result = controller.Outpincreateapply(tjCustomer);
                        String result1 = getAjaxResult(result);
                        JSONObject object = getJSONObject(result1);
                        String code = object.getStr("ResultCode");
                        if (code.equals("0")) {
                            JSONArray resultDatass = object.getJSONArray("ResultData");
                            Map<String, Object> resultData = (Map<String, Object>) resultDatass.get(0);
                            if (null != resultData && !resultData.isEmpty()) {
                                String pationid = resultData.get("PationId").toString();
                                if (null != pationid) {
                                    tjCustomer.setPationId(pationid);
                                    tjCustomerService.save(tjCustomer);
                                    //保存注册入参出参
                                    resultData.put("cardId",pationid);
                                    JSONObject object4 = JSONUtil.parseObj(resultData);
                                    LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                                    lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, "Outpincreateapply");
                                    HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper);
                                    hisApiGetMethodService.save(object4, "Outpincreateapply", hisApiConfig, JSONUtil.toJsonStr(BeanUtil.beanToMap(tjCustomer)));
                                }
                            }
                        }
                    } else if(null != s && s.equalsIgnoreCase("Y")) {
                        TjCustomer requestCommonHisApi = isRequestCommonHisApi(tjCustomer);
                        if (null !=requestCommonHisApi){
                            tjCustomerService.save(requestCommonHisApi);
                        }
                    }else {
                        tjCustomerService.save(tjCustomer);
                    }
                    tjCustomer.setTeamNo(tjReservation.getTeamNo());
                    tjCustomer.setCompId(tjReservation.getCompanyId());
                    tjCustomer.setReservationId(tjReservation.getId());
                    tjCustomer.setDiscount(tjReservation.getDiscount());
                    tjCustomer.setGroupingId(tjReservation.getGroupingId());
                    return success(tjCustomer);
                }
 
            }
 
            //根据配置调取存储过程 将临时表数据存入预约表2023.12.12
            LambdaQueryWrapper<TjCustomer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            customerLambdaQueryWrapper.eq(TjCustomer::getCusIdcard, cusIdcard);
            TjCustomer tjCustomerServiceOne = tjCustomerService.getOne(customerLambdaQueryWrapper);
            if (null == tjCustomerServiceOne) {
                return AjaxResult.error("该人员信息不存在请先进行登记!!!");
            }
//            if (null != key && key.equals("Y")) {
                String cardId = tjCustomerServiceOne.getCardId();
                if (null != cardId && !cardId.equals("0")) {
//                    int i = tjOrderService.selectTjOrderByCardId(cardId);
//                    if (i > 0) {
//                        return AjaxResult.error("不可重复签到");
//                    }
                }
 
//            }
            if(!tjCustomerServiceOne.getAge().equals(String.valueOf(DateUtil.ageOfNow(tjCustomerServiceOne.getCusBrithday())))){
                tjCustomerServiceOne.setAge(String.valueOf(DateUtil.ageOfNow(tjCustomerServiceOne.getCusBrithday())));
                tjCustomerService.updateById(tjCustomerServiceOne);
            }
            return AjaxResult.success(tjCustomerServiceOne);
 
        }
        return error("身份证号不存在");
    }
 
    private void extracted(TjCustomer customer) {
        //从his表抓数据
        LtkjMiddleHead pat = testMapper.getMiddleHeadByPartId(customer.getCardId());
        if (null != pat) {
            LambdaQueryWrapper<LtkjMiddleHead> wq1 = new LambdaQueryWrapper<>();
            wq1.eq(LtkjMiddleHead::getFeadId, customer.getCardId());
            headService.remove(wq1);
            LambdaQueryWrapper<LtkjMiddleDetail> wq0 = new LambdaQueryWrapper<>();
            wq0.eq(LtkjMiddleDetail::getFheadId, pat.getFeadId());
            middleDetailService.remove(wq0);
            List<LtkjMiddleDetail> list = testMapper.getMiddleDetailByFeadId(pat.getFeadId());
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
            LtkjMiddleHead o = headService.getMiddleHeadByPartId(customer.getCardId());
            if (o == null) {
                headService.save(pat);
                for (LtkjMiddleDetail ltkjMiddleDetail : list) {
                    middleDetailService.save(ltkjMiddleDetail);
                }
            }
            testMapper.saveTjProByCusId(customer.getCardId(), customer.getCusIdcard());
        }
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
 
 
    @GetMapping("/getcusList")
    @ApiOperation(value = "档案管理接口")
    public TableDataInfo getcusList(TjCustomer tjCustomer) {
        startPage();
        List<TjCustomer> list = tjCustomerService.getcusList(tjCustomer);
        if (null != list && !list.isEmpty()) {
            for (TjCustomer customer : list) {
                customer.setCusPhone(MatchUtils.hidePhoneNum(customer.getCusPhone()));
                customer.setCusIdcard(MatchUtils.hideIdCardNum(customer.getCusIdcard()));
                TjCustomerBlack tjCustomerBlack = blackService.isTjCustomerBlack(String.valueOf(customer.getCusId()));
                if (null != tjCustomerBlack) {
                    customer.setIsBlack("Y");
                } else {
                    customer.setIsBlack("N");
                }
            }
        }
        return getDataTable(list);
    }
 
 
    /**
     * 查询客户信息列表
     */
//    @PreAuthorize("@ss.hasPermi('hosp:customer:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查看客户列表")
    public TableDataInfo list(TjCustomer tjCustomer) {
        startPage();
        List<TjCustomer> list = tjCustomerService.selectTjCustomerList(tjCustomer);
        if (null != list && !list.isEmpty()) {
            for (TjCustomer customer : list) {
                customer.setCusPhone(MatchUtils.hidePhoneNum(customer.getCusPhone()));
                customer.setCusIdcard(MatchUtils.hideIdCardNum(customer.getCusIdcard()));
                TjCustomerBlack tjCustomerBlack = blackService.isTjCustomerBlack(String.valueOf(customer.getCusId()));
                if (null != tjCustomerBlack) {
                    customer.setIsBlack("Y");
                } else {
                    customer.setIsBlack("N");
                }
            }
        }
        return getDataTable(list);
    }
 
    /**
     * 导出客户信息列表
     */
    //@PreAuthorize("@ss.hasPermi('hosp:customer:export')")
    //@Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TjCustomer tjCustomer) {
        List<TjCustomer> list = tjCustomerService.selectTjCustomerList(tjCustomer);
        ExcelUtil<TjCustomer> util = new ExcelUtil<TjCustomer>(TjCustomer.class);
        util.exportExcel(response, list, "客户信息数据");
    }
 
    /**
     * 获取客户信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('hosp:customer:query')")
    @GetMapping(value = "/{cusId}")
    @ApiOperation(value = "根据客户id获取客户详情")
    public AjaxResult getInfo(@PathVariable("cusId") @ApiParam(value = "客户id") Long cusId) {
        return success(tjCustomerService.selectTjCustomerByCusId(cusId));
    }
 
    /**
     * 新增客户信息
     */
    @PostMapping
    @ApiOperation(value = "新增客户")
    @Transactional
    @RepeatSubmit
    @Log(title = "新增客户信息", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @ApiParam(value = "客户对象信息") TjCustomer tjCustomer) {
        if (null == tjCustomer.getCusIdcard() || null == tjCustomer.getCusPhone()) {
            return AjaxResult.error("请输入身份证号或手机号");
        }
 
        Date dates = new Date();
        final String date = DateUtil.format(dates, "yyyy-MM-dd HH:mm:ss");
 
        String cusIdcard = tjCustomer.getCusIdcard();
 
        boolean b = true;
        String sfjysfzh = sysConfigService.selectConfigByKey("sfjysfzh");
        String sfjysjh = sysConfigService.selectConfigByKey("sfjysjh");
        if (null != tjCustomer.getIdType()) {
            if (tjCustomer.getIdType().equals("1")) {
                //判断身份证号格式是否正确
                if(sfjysfzh.equalsIgnoreCase("Y")){
                    if(cusIdcard.length()==18){
                        b = MatchUtils.isIdCard(cusIdcard);
                    }
                }
            }
//            else {
//                if(cusIdcard.length()==9){
//                    b = MatchUtils.cardValidate(cusIdcard, tjCustomer.getIdType());
//                }
//
//            }
        } else {
            return AjaxResult.error("请选择证件类型");
        }
        if (!b) return AjaxResult.error("证件号有误");
 
        //判断身份证号格式是否正确
        if(sfjysjh.equalsIgnoreCase("Y")){
            if (!(MatchUtils.isMobileNO(tjCustomer.getCusPhone())))
                return AjaxResult.error("手机号错误");
        }
 
        LambdaQueryWrapper<TjCustomer> wq = new LambdaQueryWrapper<>();
        wq.eq(TjCustomer::getCusIdcard, cusIdcard);
        TjCustomer customer = tjCustomerService.getOne(wq);
        if (customer != null) {
            return AjaxResult.error("该人员已存在");
        }
        String substring = cusIdcard.substring(cusIdcard.length() - 6);
        substring = DigestUtils.md5DigestAsHex(substring.getBytes());
        tjCustomer.setCusPassword(substring);
        if(cusIdcard.length()==18) {
            try {
                tjCustomer.setCusBrithday(MatchUtils.getBirthDayByIdCard(tjCustomer.getCusIdcard()));
            } catch (Exception e) {
                return AjaxResult.error("身份证号不正确");
            }
        }
        tjCustomer.setCusNumber(0L);
        tjCustomer.setPym(PinyinUtil.getFirstLetter(tjCustomer.getCusName(),""));
        String config = sysConfigService.selectConfigByKey("sfkqdyhis");
        logger.info("config ->{}",config);
        String dataSourceType = DataSourceContextHolder.getDataSourceKey();
        logger.info("登记当前数据源 ->{}",dataSourceType);
        if (null != config && config.equals("Y")) {
            AjaxResult tjCustomer1 = suijieHisXinXi(tjCustomer, date);
            if (tjCustomer1 != null) return tjCustomer1;
        } else {
            String s = sysConfigService.selectConfigByKey("is_request_common_his_api");
            logger.info("s ->{}",s);
            if (null != s && s.equals("Y")){
                TjCustomer requestCommonHisApi = isRequestCommonHisApi(tjCustomer);
                if (null !=requestCommonHisApi){
                    if (tjCustomerService.save(requestCommonHisApi)) {
                        return AjaxResult.success(requestCommonHisApi);
                    }
                }
            }else {
                if (tjCustomerService.save(tjCustomer)) {
                    return AjaxResult.success(tjCustomer);
                }
            }
        }
        return AjaxResult.error();
    }
 
    private TjCustomer isRequestCommonHisApi(TjCustomer tjCustomer) {
        String apiUrl = sysConfigService.selectConfigByKey("common_api_url");
        String hospbm = sysConfigService.selectConfigByKey("common_api_service_hospbm");
        HashMap<String, Object> isjd = new HashMap<>();
        isjd.put("cusIdCard", tjCustomer.getCusIdcard());
        isjd.put("compId","");
        String isCreat = HttpClientUtils.sendPost(apiUrl+"/api/his/"+hospbm+"/isCreat", isjd);
        JSONObject isCreatjsonObject = JSONUtil.parseObj(isCreat);
        if (isCreatjsonObject.getStr("code").equals("200")){
            JSONObject data = isCreatjsonObject.getJSONObject("data");
            tjCustomer.setPationId(data.getStr("pationId"));
            tjCustomer.setCardId(data.getStr("cardId"));
            tjCustomer.setHisJzkh(data.getStr("cardId"));
            return tjCustomer;
        }else {
            HashMap<String, Object> map = new HashMap<>();
            // 1是身份证
            // 根据凌云his接口只要不是国内身份证号 身份证字段不传 将号码传递到卡号字段并单位编号必填 单位编号随便写
            if (!tjCustomer.getIdType().equals("1")){
                map.put("cardId",tjCustomer.getCusIdcard());
                map.put("compId",tjCustomer.getCusIdcard());
                map.put("cusIdCard", "");
            }else {
                map.put("cardId","");
                map.put("cusIdCard", tjCustomer.getCusIdcard());
                map.put("compId","");
            }
            map.put("cusName", tjCustomer.getCusName());
            map.put("cusSex", tjCustomer.getCusSex());
            map.put("cusBrithday", tjCustomer.getCusBrithday() != null ? DateUtil.format(tjCustomer.getCusBrithday(), "yyyy-MM-dd") : "");
            map.put("cusAddr", tjCustomer.getCusAddr());
            map.put("cusPhone", tjCustomer.getCusPhone());
            map.put("compName","");
            String post = HttpClientUtils.sendPost(apiUrl+"/api/his/"+hospbm+"/creat", map);
            JSONObject jsonObject = JSONUtil.parseObj(post);
            if (jsonObject.getStr("code").equals("200")){
                JSONObject data = jsonObject.getJSONObject("data");
                tjCustomer.setPationId(data.getStr("pationId"));
                tjCustomer.setCardId(data.getStr("cardId"));
                tjCustomer.setHisJzkh(data.getStr("cardId"));
                return tjCustomer;
            }
        }
        return null;
    }
 
    private AjaxResult suijieHisXinXi(TjCustomer tjCustomer, String date) {
        AjaxResult result = controller.Outpincreateapply(tjCustomer);
        String result1 = getAjaxResult(result);
        JSONObject object = getJSONObject(result1);
        String code = object.getStr("ResultCode");
        if (code.equals("0")) {
            JSONArray resultDatass = object.getJSONArray("ResultData");
            Map<String, Object> resultData = (Map<String, Object>) resultDatass.get(0);
            if (null != resultData && !resultData.isEmpty()) {
                String pationid = resultData.get("PationId").toString();
                if (null != pationid) {
                    tjCustomer.setPationId(pationid);
//                    tjCustomerService.updateById(tjCustomer);
                    //保存注册入参出参
                    resultData.put("cardId",pationid);
                    JSONObject object4 = JSONUtil.parseObj(resultData);
                    LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, "Outpincreateapply");
                    HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper);
                    hisApiGetMethodService.save(object4, "Outpincreateapply", hisApiConfig, JSONUtil.toJsonStr(BeanUtil.beanToMap(tjCustomer)));
                }
 
            }
            if (tjCustomerService.save(tjCustomer)) {
                return AjaxResult.success(tjCustomer);
            }
        }
        return AjaxResult.error(object.getStr("ResultContent"));
    }
 
    /**
     * 修改客户信息
     */
    //@PreAuthorize("@ss.hasPermi('hosp:customer:edit')")
    @Log(title = "修改客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改客户信息")
    @Transactional
    @RepeatSubmit
    public AjaxResult edit(@RequestBody @ApiParam(value = "客户对象") TjCustomer tjCustomer) {
 
        TjCustomer customer = tjCustomerService.getById(tjCustomer.getCusId());
        if(null==customer){
            return AjaxResult.error("该人员不存在!");
        }
 
        String cusPhone = tjCustomer.getCusPhone();
        if (null == cusPhone) {
            return AjaxResult.error("手机号不能为空");
        }
        String sfjysfzh = sysConfigService.selectConfigByKey("sfjysfzh");
        String sfjysjh = sysConfigService.selectConfigByKey("sfjysjh");
 
            //判断身份证号格式是否正确
            if(sfjysfzh.equalsIgnoreCase("Y")){
                if(tjCustomer.getIdType().equals("1") ){
                    if (!(MatchUtils.isIdCard(tjCustomer.getCusIdcard())))
                        return AjaxResult.error("身份证号错误");
                }
 
//                if(tjCustomer.getCusIdcard().length()==9){
//                    if (!MatchUtils.cardValidates(tjCustomer.getCusIdcard())) {
//                        return AjaxResult.error("身份证号码错误");
//                    }
//                }
            }
 
 
        //判断身份证号格式是否正确
        if(sfjysjh.equalsIgnoreCase("Y")){
            if (!(MatchUtils.isMobileNO(tjCustomer.getCusPhone())))
                return AjaxResult.error("手机号错误");
        }
 
        tjCustomer.setCusPhone(cusPhone);
        tjCustomer.setPym(PinyinUtil.getFirstLetter(tjCustomer.getCusName(),""));
        if (tjCustomerService.updateById(tjCustomer)) {
            String config = sysConfigService.selectConfigByKey("sfkqdyhis");
            if (null != config && config.equals("Y")) {
                AjaxResult result = controller.Outpincreateapply(tjCustomer);
                String result1 = getAjaxResult(result);
                JSONObject object = getJSONObject(result1);
                String code = object.getStr("ResultCode");
                if (code.equals("0")) {
                    return AjaxResult.success();
                }
                return AjaxResult.error(object.getStr("ResultContent"));
            }else {
                String s = sysConfigService.selectConfigByKey("is_request_common_his_api");
                if (null != s && s.equals("Y")){
                    String apiUrl = sysConfigService.selectConfigByKey("common_api_url");
                    String hospbm = sysConfigService.selectConfigByKey("common_api_service_hospbm");
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("pationId",customer.getPationId());
                    map.put("cardId",customer.getHisJzkh());
                    map.put("cusName",tjCustomer.getCusName());
                    map.put("cusSex",tjCustomer.getCusSex());
                    map.put("cusIdCard",tjCustomer.getCusIdcard());
                    map.put("compId","");
                    map.put("cusBrithday",tjCustomer.getCusBrithday() != null ? DateUtil.format(tjCustomer.getCusBrithday(), "yyyy-MM-dd") : "");
                    map.put("cusAddr",tjCustomer.getCusAddr());
                    map.put("cusPhone",tjCustomer.getCusPhone());
                    map.put("compName","");
                    String isCreat = HttpClientUtils.sendPost(apiUrl+"/api/his/"+hospbm+"/update", map);
                    JSONObject isCreatjsonObject = JSONUtil.parseObj(isCreat);
                    if (isCreatjsonObject.getStr("code").equals("200")){
//                        JSONObject data = isCreatjsonObject.getJSONObject("data");
//                        tjCustomer.setPationId(data.getStr("pationId"));
//                        tjCustomer.setCardId(data.getStr("cardId"));
//                        tjCustomer.setHisJzkh(data.getStr("cardId"));
//                        if (tjCustomerService.updateById(tjCustomer)) {
                            return AjaxResult.success(tjCustomer);
//                        }
                    }else {
                        return AjaxResult.error();
                    }
                }
                return AjaxResult.success();
            }
        }
       return AjaxResult.error();
    }
 
 
    @Log(title = "注册客户信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/zhuceTjCustomer")
    @ApiOperation(value = "修改客户信息")
    @Transactional
    @RepeatSubmit
    public AjaxResult zhuceTjCustomer(@RequestBody @ApiParam(value = "客户对象") TjCustomer tjCustomer) {
 
        try {
            TjCustomer customer = tjCustomerService.getById(tjCustomer.getCusId());
            if(null==customer){
                return AjaxResult.error("该人员不存在!");
            }
 
            if(!customer.getPationId().equals("0")){
                return AjaxResult.error("该人员已经注册!");
            }
            String config = sysConfigService.selectConfigByKey("sfkqdyhis");
            if (null != config && config.equals("Y")) {
                AjaxResult result = controller.Outpincreateapply(customer);
                String result1 = getAjaxResult(result);
                JSONObject object = getJSONObject(result1);
                String code = object.getStr("ResultCode");
                if (code.equals("0")) {
                    JSONArray resultDatass = object.getJSONArray("ResultData");
                    Map<String, Object> resultData = (Map<String, Object>) resultDatass.get(0);
                    if (null != resultData && !resultData.isEmpty()) {
                        String pationid = resultData.get("PationId").toString();
                        if (null != pationid) {
                            customer.setPationId(pationid);
                            //保存注册入参出参
                            resultData.put("cardId",pationid);
                            JSONObject object4 = JSONUtil.parseObj(resultData);
                            LambdaQueryWrapper<HisApiConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper.eq(HisApiConfig::getApiMethod, "Outpincreateapply");
                            HisApiConfig hisApiConfig = hisApiConfigService.getOne(lambdaQueryWrapper);
                            hisApiGetMethodService.save(object4, "Outpincreateapply", hisApiConfig, JSONUtil.toJsonStr(BeanUtil.beanToMap(tjCustomer)));
                        }
 
                    }
                    if (tjCustomerService.updateById(customer)) {
                        return AjaxResult.success("注册成功");
                    }
                }else{
                    return AjaxResult.error("注册失败");
                }
            }
 
            String s = sysConfigService.selectConfigByKey("is_request_common_his_api");
            if (null != s && s.equals("Y")){
                TjCustomer requestCommonHisApi = isRequestCommonHisApi(customer);
                if (null !=requestCommonHisApi){
                    if (tjCustomerService.updateById(customer)) {
                        return AjaxResult.success("注册成功");
                    }
                }else {
                    return AjaxResult.error("注册失败");
                }
            }
            return AjaxResult.error("请先连接his服务");
        } catch (Exception e) {
            log.error(e.toString());
            throw new RuntimeException(e);
 
        }
 
    }
 
 
    /**
     * 删除客户信息
     */
    //@PreAuthorize("@ss.hasPermi('hosp:customer:remove')")
    //@Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cusIds}")
    @ApiOperation(value = "删除客户")
    public AjaxResult remove(@PathVariable @ApiParam(value = "所删除客户的id") Long[] cusIds) {
        return toAjax(tjCustomerService.removeByIds(Arrays.asList(cusIds)));
    }
}