zjh
2024-02-28 fcc5d56c4fa7b269d96b8ef66a527fa6a7c90450
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
package com.ltkj.web.controller.system;
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltkj.common.annotation.RepeatSubmit;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.core.domain.entity.SysUser;
import com.ltkj.common.core.redis.RedisCache;
import com.ltkj.common.enums.DataSourceType;
import com.ltkj.common.utils.SecurityUtils;
import com.ltkj.common.utils.StringUtils;
import com.ltkj.framework.config.MatchUtils;
import com.ltkj.framework.config.UserHoder;
import com.ltkj.framework.datasource.DynamicDataSourceContextHolder;
import com.ltkj.hosp.domain.*;
import com.ltkj.hosp.mapper.TestMapper;
import com.ltkj.hosp.service.*;
import com.ltkj.hosp.sqlDomain.*;
import com.ltkj.hosp.vodomain.CsProVo;
import com.ltkj.hosp.vodomain.UpdateOrderRemarkVo;
import com.ltkj.mall.mallOrderUtils.TjConstants;
import com.ltkj.system.service.ISysConfigService;
import com.ltkj.system.service.ISysDeptService;
import com.ltkj.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.AjAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
import static org.apache.poi.hemf.record.emfplus.HemfPlusRecordType.save;
import static org.apache.poi.hemf.record.emfplus.HemfPlusRecordType.setClipRect;
 
/**
 * @Author: 西安路泰科技有限公司/赵佳豪
 * @Date: 2022/12/6 14:29
 */
@RestController
@RequestMapping("/check")
@Api(tags = "总检医师相关接口")
public class TjCheckController {
    @Resource
    private ITjOrderService orderService;
    @Resource
    private ITjOrderDetailService detailService;
    @Resource
    private ITjCustomerService customerService;
    @Resource
    private ITjProjectService projectService;
    @Resource
    private IDictCompService compService;
    @Resource
    private ITjStandardService tjStandardService;
    @Resource
    private ITjAdviceService tjAdviceService;
    @Resource
    private ITjOrderRemarkService orderRemarkService;
    @Resource
    private ITbTransitionService transitionService;
    @Resource
    private ISysUserService userService;
    @Resource
    private RedisCache redisCache;
    @Resource
    private TjAsyncService asyncService;
    @Resource
    private ITjConfirmLogService confirmLogService;
    @Autowired
    private ISysConfigService configService;
    @Resource
    private ISysDeptService deptService;
    @Resource
    private TestMapper testMapper;
    @Resource
    private TjvLtkjvtjpatService tjvLtkjvtjpatService;
    @Resource
    private LtkjMiddleDetailService middleDetailService;
    @Resource
    private LtkjMiddleHeadService headService;
    @Resource
    private LtkjExamJcbgdService ltkjExamJcbgdService;
    @Resource
    private LtkjExamJcsqdService ltkjExamJcsqdService;
    @Resource
    private LtkjHybgdService ltkjHybgdService;
    @Resource
    private LtkjHysqdService ltkjHysqdService;
 
 
    @GetMapping("/getOperationPermissionsByTjNum")
    @ApiOperation(value = "(初审页面  和 医生页面)根据体检号获取是否可操作权限接口  true可操作  反之不可")
    public AjaxResult getOperationPermissionsByTjNum(@ApiParam(value = "体检号") @RequestParam String tjNumber) {
        TjOrder order = orderService.getOrderByTjNum(tjNumber);
        if (null != order) {
            Integer status = order.getStatus();
            if (status < 300) {
                return AjaxResult.success(true);
            }
            if (status > 300) {
                return AjaxResult.success(false);
            }
        }
        return AjaxResult.success(false);
    }
 
 
    @GetMapping("/getCsList")
    @ApiOperation(value = "初审查询体检记录客户列表")//0待1已
    @Transactional
    public AjaxResult getCsList(@ApiParam(value = "审核状态  0待审核  1已审核") @RequestParam(required = false) Integer checkStatus,
                                @ApiParam(value = "页码数(默认1)") @RequestParam(defaultValue = "1") Integer page,
                                @ApiParam(value = "显示条数(默认10)") @RequestParam(defaultValue = "10") Integer pageSize,
                                @ApiParam(value = "体检单号") @RequestParam(required = false) String tjNumber,
                                @ApiParam(value = "单位") @RequestParam(required = false) Long compId,
                                @ApiParam(value = "姓名)") @RequestParam(required = false) String name,
                                @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                                @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime) {
 
//        DateTime beginTimes = null;
//        DateTime endTimes = null;
//        if (null != beginTime && null != endTime) {
//            beginTimes = DateUtil.beginOfDay(DateUtil.parse(beginTime));
//            endTimes = DateUtil.endOfDay(DateUtil.parse(endTime));
//        }
//        //初始化体检订单表
//        List<TjOrder> orderList = null;
//        List<TjCustomer> customerList = new ArrayList<>();
//        Map<String, Object> map = new HashMap<>();
//
//        if (checkStatus == 0)
//            asyncService.wCScheckSetCustomerLisByRedis(orderService.getCsTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes));
//        if (checkStatus == 1)
//            asyncService.yCScheckSetCustomerLisByRedis(orderService.getCsTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes));
//
//        //根据姓名查询
//        if (null != name && !"".equals(name)) {
//            List<TjCustomer> list = customerService.getTjCustomerList(name);
//            if (null != list && list.size() > 0) {
//                List<TjCustomer> lists = new ArrayList<>();
//                for (TjCustomer customer : list) {
//                    orderList = orderService.getCsTjOrderListByCusId(customer.getCusId());
//                    if (null != orderList && orderList.size() > 0) {
//                        for (TjOrder tjOrder : orderList) {
//                            customer.setTjNumber(tjOrder.getTjNumber());
//                            customer.setTjStatus(Long.valueOf(tjOrder.getCheckStatus()));
//                            customer.setOrderId(tjOrder.getOrderId());
//                            customer.setTjTime(tjOrder.getCreateTime());
//                            customer.setFinishTime(tjOrder.getFinishTime());
//                            customer.setCusName(MatchUtils.hideCusName(customer.getCusName()));
//                            customer.setCusPhone(MatchUtils.hidePhoneNum(customer.getCusPhone()));
//                            customer.setCusIdcard(MatchUtils.hideIdCardNum(customer.getCusIdcard()));
//                            customer.setConfirmStatus(String.valueOf(tjOrder.getStatus()));
//                            if (tjOrder.getFirmId().equals("0")) {
//                                customer.setTjCompName(null);
//                            } else {
//                                customer.setTjCompName(compService.selectDictCompByDrugManufacturerId(tjOrder.getFirmId()).getCnName());
//                            }
//                            lists.add(customer);
//                        }
//                    }
//                }
//                List<TjCustomer> customers = null;
//                if (lists.size() > 0) {
//                    customers = lists.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
//                }
//                map.put("customers", customers);
//                map.put("total", lists.size());
//                return AjaxResult.success(map);
//            }
//            return AjaxResult.success("暂无数据");
//        }
//
//        //条件查询
//        if (null != tjNumber || null != compId || (null != beginTime && null != endTime)) {
//            orderList = orderService.getCsTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            return getResult(page, pageSize, orderList, customerList, map);
//        }
//
//        List<TjCustomer> customers = null;
//        if (checkStatus == 0) {
//            customers = redisCache.getCacheMapValue("cScheck", "ws");
//        }
//        if (checkStatus == 1) {
//            customers = redisCache.getCacheMapValue("cScheck", "ys");
//        }
//        if (customers != null && customers.size() > 0) {
//            List<TjCustomer> customerLists = customers.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
//            map.put("customers", customerLists);
//            map.put("total", customers.size());
//            return AjaxResult.success(map);
//        } else {
//            orderList = orderService.getCsTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            return getResult(page, pageSize, orderList, customerList, map);
//        }
        String value = String.valueOf(compId);
        if (tjNumber == null) tjNumber = "";
        if (compId == null) value = "";
        if (name == null) name = "";
        if (beginTime == null) beginTime = "";
        if (endTime == null) endTime = "";
        return AjaxResult.success(orderService.getCsCunChuGuoChengCustomerList(tjNumber, page, pageSize, checkStatus, value, beginTime, endTime, name));
    }
 
 
    /**
     * 初审
     */
    @GetMapping("/cSWebGetProByTjNumAndOrderId")
    @ApiOperation(value = "(初审页面)点击体检人员展示体检项目接口")
    public AjaxResult cSWebGetProByTjNumAndOrderId(@ApiParam(value = "体检号") @RequestParam String tjNumber) {
//        List<TjOrderRemark> orderRemarkList = orderRemarkService.getTjOrderRemarkListByTjNum(tjNumber);
//        List<CsProVo> list = new ArrayList<>();
//        if (null != orderRemarkList && orderRemarkList.size() > 0) {
//            for (TjOrderRemark remark : orderRemarkList) {
//                CsProVo vo = new CsProVo();
//                vo.setDeptName(deptService.getById(remark.getDeptId()).getDeptName());
//                vo.setProName(projectService.getById(remark.getProId()).getProName());
//                vo.setType(remark.getType());
//                if (null != remark.getDoctorName()) {
//                    vo.setDoctorName(userService.getById(remark.getDoctorName()).getNickName());
//                    vo.setBcdoctorName(userService.getById(remark.getDoctorName()).getNickName());
//                }
//                vo.setSffs("统收");
//                vo.setIsPay("已收费");
//                vo.setQdcreateTime(remark.getCreateTime());
//                vo.setBcupdateTime(remark.getUpdateTime());
//                vo.setZhupdateTime(remark.getUpdateTime());
//                list.add(vo);
//            }
//            List<CsProVo> list1 = orderRemarkService.cSWebGetProByTjNumAndOrderId(tjNumber);
//            Collections.sort(list, new Comparator<CsProVo>() {
//                @Override
//                public int compare(CsProVo o1, CsProVo o2) {
//                    return o1.getType() - o2.getType();
//                }
//            });
//        }
 
        List<CsProVo> list = orderRemarkService.cSWebGetProByTjNumAndOrderId(tjNumber);
        if (null != list && list.size() > 0) {
            list.sort(new Comparator<CsProVo>() {
                @Override
                public int compare(CsProVo o1, CsProVo o2) {
                    return o1.getType() - o2.getType();
                }
            });
        }
        return AjaxResult.success(list);
    }
 
 
    /**
     * 初审
     */
    @GetMapping("/confirmOrder")
    @ApiOperation(value = "初审(并修改状态)接口")
    @Transactional
    public AjaxResult confirmOrder(@ApiParam(value = "tjNumber") @RequestParam String tjNumber,
                                   @ApiParam(value = "状态 0同意 1拒绝 默认同意") @RequestParam(defaultValue = "0") Integer status,
                                   @ApiParam(value = "驳回理由") @RequestParam(required = false) String remark) {
        int num = orderRemarkService.getTjYqOrderRemarkByTjNum(tjNumber);
        if (num > 0) return AjaxResult.error("请先同步体检项目!");
        if (null != tjNumber) {
            Long userId = Long.valueOf(SecurityUtils.getLoginUser().getUserId());
            SysUser sysUser = userService.getById(userId);
            TjOrder tjOrder = orderService.getOrderByTjNum(tjNumber);
            if (null != tjOrder) {
                tjOrder.setConfirmTime(new DateTime());
                tjOrder.setConfirmDoctor(sysUser.getNickName());
                tjOrder.setConfirmStatus(String.valueOf(status));
                if (status == 0) {
                    tjOrder.setStatus(TjConstants.TJ_CHECK);
                    asyncService.updateCheckType(tjNumber);
                } else {
                    tjOrder.setStatus(TjConstants.TJ_REFUSED);
                }
                if (orderService.updateById(tjOrder)) {
                    TjConfirmLog confirmLog = new TjConfirmLog();
                    confirmLog.setOrderId(String.valueOf(tjOrder.getOrderId()));
                    confirmLog.setTjNum(tjNumber);
                    confirmLog.setStatus(status);
                    confirmLogService.save(confirmLog);
                    asyncService.wCScheckSetCustomerLisByRedis(orderService.getCsTjOrderList(tjNumber, 0, null, null, null));
                    asyncService.yCScheckSetCustomerLisByRedis(orderService.getCsTjOrderList(tjNumber, 1, null, null, null));
                    return AjaxResult.success("操作成功");
                }
                return AjaxResult.error("操作失败");
            }
            return AjaxResult.success("数据不存在");
        }
        return AjaxResult.error();
    }
 
 
    /**
     * 同步sql server数据到本地数据库数据接口
     */
    @GetMapping("/dataSynchronization")
    @ApiOperation(value = "同步sql server数据到本地数据库数据接口")
    @RepeatSubmit
    public AjaxResult dataSynchronization(@RequestParam String tjNumber) {
        //根据配置调取存储过程 将临时表数据存入预约表2023.12.12
        final String getInfoFromSqlData = configService.selectConfigByKey("getInfoFromSqlData");
        if ("Y".equals(getInfoFromSqlData)) {
            TjOrder order = orderService.getOrderByTjNum(tjNumber);
            if (null != order) {
                List<LtkjHysqd> one = testMapper.getHysqdByTmh(order.getCardId());
                if (one != null && one.size() > 0) {
                    for (LtkjHysqd ltkjHysqd : one) {
//                        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
                        LtkjHysqd hysqd = ltkjHysqdService.getLtkjHysqdByTjhAndTmh(ltkjHysqd.getTjh(), ltkjHysqd.getTmh());
                        if (hysqd == null) {
                            ltkjHysqdService.save(ltkjHysqd);
                            DynamicDataSourceContextHolder.clearDataSourceType();
                            List<LtkjHybgd> one1 = testMapper.getHybgdByTmh(ltkjHysqd.getTmh());
                            if (one1 != null && one1.size() > 0) {
                                DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
                                ltkjHybgdService.saveBatch(one1);
                                DynamicDataSourceContextHolder.clearDataSourceType();
                            }
                        }
                    }
                }
 
//                List<LtkjExamJcsqd> one2 = testMapper.getExamJcsqdTmh(tjNumber);
//                if (one2 != null && one2.size() > 0) {
//                    DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
//                    ltkjExamJcsqdService.saveBatch(one2);
//                    DynamicDataSourceContextHolder.clearDataSourceType();
//                    for (LtkjExamJcsqd ltkjExamJcsqd : one2) {
//                        List<LtkjExamJcbgd> one3 = testMapper.getExamJcbgdByTjh(ltkjExamJcsqd.getTmh());
//                        if (one3 != null && one3.size() > 0) {
//                            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
//                            ltkjExamJcbgdService.saveBatch(one3);
//                            DynamicDataSourceContextHolder.clearDataSourceType();
//                        }
//                    }
//                }
                //同步化验项目
                testMapper.updateDetailByCall(order.getCardId(), String.valueOf(order.getOrderId()), order.getTjNumber(), UserHoder.getLoginUser().getUserId());
                //同步检查项目
                testMapper.updateDetailByTjJcCall(order.getCardId(), String.valueOf(order.getOrderId()), order.getTjNumber(), UserHoder.getLoginUser().getUserId());
                asyncService.updateCheckType(order.getTjNumber());
            }
        }
        return AjaxResult.success("同步成功");
    }
 
 
    /**
     * 查询体检记录客户列表
     */
    @GetMapping("/getList")
    @ApiOperation(value = "查询体检记录客户列表")//0待1已
//    @PreAuthorize("@ss.hasPermi('check:check:getList')")
    public AjaxResult getList(@ApiParam(value = "审核状态0待审核1已审核") @RequestParam(required = false) Integer checkStatus,
                              @ApiParam(value = "页码数(默认1)") @RequestParam(defaultValue = "1") Integer page,
                              @ApiParam(value = "显示条数(默认10)") @RequestParam(defaultValue = "10") Integer pageSize,
                              @ApiParam(value = "体检单号") @RequestParam(required = false) String tjNumber,
                              @ApiParam(value = "单位") @RequestParam(required = false) Long compId,
                              @ApiParam(value = "姓名)") @RequestParam(required = false) String name,
                              @ApiParam(value = "开始时间") @RequestParam(required = false) String beginTime,
                              @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime) {
        //        String config = configService.selectConfigByKey("tj_confirm");
//        DateTime beginTimes = null;
//        DateTime endTimes = null;
//        if (null != beginTime && null != endTime) {
//            beginTimes = DateUtil.beginOfDay(DateUtil.parse(beginTime));
//            endTimes = DateUtil.endOfDay(DateUtil.parse(endTime));
//        }
//        asyncService.checkSetCustomerLisByRedis(config);
//        //初始化体检订单表
//        List<TjOrder> orderList = null;
//        List<TjCustomer> customerList = new ArrayList<>();
//        Map<String, Object> map = new HashMap<>();
//
//        //姓名查询
//        if (null != name && !"".equals(name)) {
//            List<TjCustomer> list = customerService.getTjCustomerList(name);
//            if (null != list && list.size() > 0) {
//                List<TjCustomer> lists = new ArrayList<>();
//                for (TjCustomer customer : list) {
//                    if ("Y".equals(config)) {
//                        orderList = orderService.getCsTjOrderListByCusId1(customer.getCusId());
//                    } else {
//                        orderList = orderService.getTjOrderListByCusId(customer.getCusId());
//                    }
//                    if (null != orderList && orderList.size() > 0) {
//                        for (TjOrder tjOrder : orderList) {
//                            customer.setTjNumber(tjOrder.getTjNumber());
//                            customer.setTjStatus(Long.valueOf(tjOrder.getCheckStatus()));
//                            customer.setOrderId(tjOrder.getOrderId());
//                            customer.setTjTime(tjOrder.getFinishTime());
//                            customer.setFinishTime(tjOrder.getFinishTime());
//                            customer.setCusName(MatchUtils.hideCusName(customer.getCusName()));
//                            customer.setCusPhone(MatchUtils.hidePhoneNum(customer.getCusPhone()));
//                            customer.setCusIdcard(MatchUtils.hideIdCardNum(customer.getCusIdcard()));
//                            customer.setConfirmStatus(String.valueOf(tjOrder.getStatus()));
//                            customer.setTjCategory(tjOrder.getTjCategory());
//                            if (tjOrder.getFirmId().equals("0")) {
//                                customer.setTjCompName(null);
//                            } else {
//                                customer.setTjCompName(compService.selectDictCompByDrugManufacturerId(tjOrder.getFirmId()).getCnName());
//                            }
//                            lists.add(customer);
//                        }
//                    }
//                }
//                List<TjCustomer> customers = null;
//                if (lists.size() > 0) {
//                    customers = lists.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
//                }
//                map.put("customers", customers);
//                map.put("total", lists.size());
//                return AjaxResult.success(map);
//            }
//            return AjaxResult.success("暂无数据");
//        }
//
//        //条件查询
//        if (null != tjNumber || null != compId || (null != beginTime && null != endTime)) {
//            //判断是否开启初审
//            if ("Y".equals(config)) {
//                orderList = orderService.getCsTjOrderList1(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            } else {
//                orderList = orderService.getTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            }
//            return getResult(page, pageSize, orderList, customerList, map);
//        }
//
//        List<TjCustomer> customers = null;
//        if (null != checkStatus && checkStatus == 0) {
//            customers = redisCache.getCacheMapValue("check", "ws");
//        }
//        if (null != checkStatus && checkStatus == 1) {
//            customers = redisCache.getCacheMapValue("check", "ys");
//        }
//        if (customers != null && customers.size() > 0) {
////            asyncService.addRedis(customers);
//            List<TjCustomer> customerLists = customers.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
//            map.put("customers", customerLists);
//            map.put("total", customers.size());
//            return AjaxResult.success(map);
//        } else {
//            //判断是否开启初审
//            if ("Y".equals(config)) {
//                orderList = orderService.getCsTjOrderList1(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            } else {
//                orderList = orderService.getTjOrderList(tjNumber, checkStatus, compId, beginTimes, endTimes);
//            }
//            return getResult(page, pageSize, orderList, customerList, map);
//        }
        String value = String.valueOf(compId);
        if (tjNumber == null) tjNumber = "";
        if (compId == null) value = "";
        if (name == null) name = "";
        if (beginTime == null) beginTime = "";
        if (endTime == null) endTime = "";
        return AjaxResult.success(orderService.getCunChuGuoChengCustomerList(tjNumber, page, pageSize, checkStatus, value, beginTime, endTime, name));
    }
 
    /**
     * 根据体检订单号获取体检详情信息接口
     *
     * @param tjNumber
     * @param advice
     * @param checkStatus
     * @return
     */
    @PutMapping("/getTjdetailList")
    @ApiOperation(value = "根据体检订单号获取体检详情信息(并修改状态)接口")
//    @PreAuthorize("@ss.hasPermi('check:check:getTjdetailList')")
    public AjaxResult getTjdetailList(@ApiParam(value = "体检tjNumber") @RequestParam String tjNumber,
                                      @ApiParam(value = "总检建议") @RequestParam(required = false) String advice,
                                      @ApiParam(value = "审核状态0待审核1已审核") @RequestParam Integer checkStatus) {
 
        int num = orderRemarkService.getTjYqOrderRemarkByTjNum(tjNumber);
        if (num > 0) return AjaxResult.error("有延期项目暂不能生成报告!!!");
        if (null != tjNumber) {
            Long userId = Long.valueOf(SecurityUtils.getLoginUser().getUserId());
            SysUser sysUser = userService.getById(userId);
            LambdaQueryWrapper<TjOrder> wq1 = new LambdaQueryWrapper<>();
            wq1.eq(TjOrder::getTjNumber, tjNumber);
            TjOrder tjOrder = orderService.getOne(wq1);
            if (null != tjOrder) {
                tjOrder.setCheckAdvice(advice);
                tjOrder.setCheckStatus(checkStatus);
                tjOrder.setStatus(TjConstants.TJ_CHECK);
                tjOrder.setCheckDoctor(sysUser.getNickName());
                tjOrder.setCheckTime(new Date());
                tjOrder.setStatus(401);
                if (orderService.updateById(tjOrder)) {
                    TjCustomer customer = customerService.getById(tjOrder.getUserId());
                    if (null != customer) {
                        customer.setCusNumber(customer.getCusNumber() + 1);
                        if (customerService.updateById(customer)) {
                            LambdaQueryWrapper<TbTransition> wqq = new LambdaQueryWrapper<>();
                            wqq.eq(TbTransition::getCusId, customer.getCusIdcard());
                            wqq.eq(TbTransition::getCardId, customer.getCardId());
                            transitionService.remove(wqq);
                            asyncService.updateCheckType(tjNumber);
                            return AjaxResult.success("操作成功");
                        }
                        return AjaxResult.error("操作失败");
                    }
                }
                return AjaxResult.error("操作失败");
            }
            return AjaxResult.success("数据不存在");
        }
        return AjaxResult.error();
    }
 
 
    /**
     * 总检点击体检信息详情————非化验项目
     *
     * @param tjNumber
     * @return
     */
    @GetMapping("/updateCheckType")
    @ApiOperation(value = "总检点击体检信息详情————非化验项目")
    public AjaxResult updateCheckType(@ApiParam(value = "客户体检号") @RequestParam String tjNumber) {
//        if (redisCache.hasKey("updateCheckType" + tjNumber)) {
//            List<Map<String, Object>> cacheMapValue = redisCache.getCacheMapValue("updateCheckType" + tjNumber, tjNumber);
//            return AjaxResult.success(cacheMapValue);
//        }
        return getAjaxResult(tjNumber);
    }
 
    private AjaxResult getAjaxResult(String tjNumber) {
 
        asyncService.updateCheckType(tjNumber);
        List<Map<String, Object>> list = new ArrayList<>();
        TjOrder one = orderService.getOrderByTjNum(tjNumber);
 
        if (one == null) {
            return AjaxResult.error("暂无数据!!");
        }
 
        TjCustomer customer = customerService.getById(one.getUserId());
        List<TjOrderRemark> remarkList = orderRemarkService.getRemarkListByTjNumAndType(tjNumber);
 
        if (null != remarkList && remarkList.size() > 0) {
            for (TjOrderRemark remark : remarkList) {
                Map<String, Object> parent = new HashMap<>();
                parent.put("checkAdvice", one.getCheckAdvice());
                TjProject project = projectService.selectTjProjectByProId(remark.getProId());
                //判断该项目是否需要打印报告
                if ("N".equals(project.getNeedReport())) {
                    continue;
                }
 
 
                //判断该项目是否检验科项目  2024/2/28g
                if (project.getDeptId() == 241) {
                    continue;
                }
 
 
                if (null != project) {
                    parent.put("parent", project.getProName());
                    parent.put("parentId", project.getProId().toString());
                }
                List<TjOrderDetail> tjOrderDetails = detailService.getDetailByOrderIdAndParId(one.getOrderId(), remark.getProId());
                if (null != tjOrderDetails && tjOrderDetails.size() > 0) {
                    for (TjOrderDetail tjOrderDetail : tjOrderDetails) {
                        final TjProject byId = projectService.selectTjProjectByProId(tjOrderDetail.getProId());
                        if (null == byId) {
                            detailService.removeById(tjOrderDetail.getOrderDetailId());
                        } else {
                            tjOrderDetail.setProject(byId);
                        }
                        List<TjStandard> list2 = tjStandardService.getTjStandardListByProId(String.valueOf(tjOrderDetail.getProId()));
                        if (list2.size() == 0) {
                            tjOrderDetail.setStandard(new TjStandard());
                        } else if (list2.size() == 1) {
                            tjOrderDetail.setStandard(list2.get(0));
                        } else {
                            final int ageType = StringUtils.getAgeType(DateUtil.ageOfNow(customer.getCusBrithday()));
                            TjStandard tjStandard1 = tjStandardService.getListBySexAndType(customer.getCusSex(), ageType, String.valueOf(tjOrderDetail.getProId()));
                            tjOrderDetail.setStandard(tjStandard1);
                        }
                    }
                    String summary = remark.getSummary();
                    if (null != summary) {
                        String[] split = summary.split(";");
//                        List<Long> longList = new ArrayList<>();
//                        for (String str : split) {
//                            if(!"".equals(str)){
//                                longList.add(Long.valueOf(str));
//                            }
//                        }
                        parent.put("parentAdvice", tjAdviceService.getAdviceStringByIds(split));
 
                    } else {
                        parent.put("parentAdvice", null);
                    }
                    parent.put("sons", tjOrderDetails);
                    parent.put("remark", remark.getRemark());
                    if (remark.getDoctorName() != null && null != userService.getById(remark.getDoctorName())) {
                        parent.put("doctorName", userService.getById(remark.getDoctorName()).getNickName());
                    }
                    list.add(parent);
                } else {
                    return AjaxResult.success("该客户没有体检项目数据");
                }
            }
            return AjaxResult.success(list);
        }
        return AjaxResult.success("该客户没有体检项目数据!");
    }
 
 
    /**
     * 总检点击体检信息详情————化验项目
     *
     * @param tjNumber
     * @return
     */
    @GetMapping("/updateCheckTypeHuaYan")
    @ApiOperation(value = "总检点击体检信息详情————化验项目")
    public AjaxResult updateCheckTypeHuaYan(@ApiParam(value = "客户体检号") @RequestParam String tjNumber) {
//        asyncService.updateCheckType(tjNumber);
        List<Map<String, Object>> list = new ArrayList<>();
        TjOrder one = orderService.getOrderByTjNum(tjNumber);
 
        if (one == null) {
            return AjaxResult.error("暂无数据!!");
        }
        TjCustomer customer = customerService.getById(one.getUserId());
        List<TjOrderRemark> remarkList = orderRemarkService.addHuaYanTable(tjNumber);
        List<TjOrderDetail> tjOrderDetails = detailService.addHuaYanTable(tjNumber);
 
 
        if (null != remarkList && remarkList.size() > 0) {
            for (TjOrderRemark remark : remarkList) {
                Map<String, Object> parent = new HashMap<>();
                parent.put("checkAdvice", one.getCheckAdvice());
                parent.put("parent", remark.getProName());
                parent.put("parentId", remark.getProId().toString());
 
                if (null != tjOrderDetails && tjOrderDetails.size() > 0) {
                    for (TjOrderDetail tjOrderDetail : tjOrderDetails) {
                        final TjProject byId = projectService.selectTjProjectByProId(tjOrderDetail.getProId());
                        if (null == byId) {
                            detailService.removeById(tjOrderDetail.getOrderDetailId());
                        } else {
                            tjOrderDetail.setProject(byId);
                        }
                        List<TjStandard> list2 = tjStandardService.getTjStandardListByProId(String.valueOf(tjOrderDetail.getProId()));
                        if (list2.size() == 0) {
                            tjOrderDetail.setStandard(new TjStandard());
                        } else if (list2.size() == 1) {
                            tjOrderDetail.setStandard(list2.get(0));
                        } else {
                            final int ageType = StringUtils.getAgeType(DateUtil.ageOfNow(customer.getCusBrithday()));
                            TjStandard tjStandard1 = tjStandardService.getListBySexAndType(customer.getCusSex(), ageType, String.valueOf(tjOrderDetail.getProId()));
                            tjOrderDetail.setStandard(tjStandard1);
                        }
                    }
 
                    String summary = remark.getSummary();
                    if (null != summary) {
                        String[] split = summary.split(";");
                        parent.put("parentAdvice", tjAdviceService.getAdviceStringByIds(split));
                    } else {
                        parent.put("parentAdvice", null);
                    }
                    parent.put("sons", tjOrderDetails);
                    parent.put("remark", remark.getRemark());
                    if (remark.getDoctorName() != null && null != userService.getById(remark.getDoctorName())) {
                        parent.put("doctorName", userService.getById(remark.getDoctorName()).getNickName());
                    }
 
                    list.add(parent);
                } else {
                    return AjaxResult.success("该客户没有体检项目数据");
                }
            }
            return AjaxResult.success(list);
        }
        return AjaxResult.success("该客户没有体检项目数据!");
    }
 
 
    @GetMapping("/getTm")
    @ApiOperation(value = "获取条码")
//    @PreAuthorize("@ss.hasPermi('check:check:getTm')")
    public AjaxResult getTm(@ApiParam(value = "客户体检号") @RequestParam String tjNumber) {
        return AjaxResult.success(tjNumber);
    }
 
    /**
     * 总检修改备注
     *
     * @return
     */
    @PostMapping("/checkUpdateOrderRemark")
    @ApiOperation(value = "总检修改备注接口")
    public AjaxResult checkUpdateOrderRemark(@RequestBody List<UpdateOrderRemarkVo> updateOrderRemarkVos) {
        if (null != updateOrderRemarkVos && updateOrderRemarkVos.size() > 0) {
            for (UpdateOrderRemarkVo remarkVo : updateOrderRemarkVos) {
                LambdaQueryWrapper<TjOrderRemark> wqm = new LambdaQueryWrapper<>();
                wqm.eq(TjOrderRemark::getTjNumber, remarkVo.getTjNumber());
                wqm.eq(TjOrderRemark::getProId, remarkVo.getProId());
                TjOrderRemark remark = orderRemarkService.getOne(wqm);
                if (null != remark) {
                    remark.setRemark(remarkVo.getRemarks());
                    orderRemarkService.updateById(remark);
                }
            }
        }
        return AjaxResult.success();
    }
 
 
    private AjaxResult getResult(Integer page, Integer pageSize, List<TjOrder> orderList, List<TjCustomer> customerList, Map<String, Object> map) {
        for (TjOrder tjOrder : orderList) {
            if (null != tjOrder) {
                TjCustomer customer = customerService.selectTjCustomerByCusId(tjOrder.getUserId());
                if (customer == null) {
                    continue;
                }
                customer.setCusName(MatchUtils.hideCusName(customer.getCusName()));
                customer.setCusPhone(MatchUtils.hidePhoneNum(customer.getCusPhone()));
                customer.setCusIdcard(MatchUtils.hideIdCardNum(customer.getCusIdcard()));
                customer.setTjNumber(tjOrder.getTjNumber());
                customer.setTjStatus(Long.valueOf(tjOrder.getCheckStatus()));
                customer.setOrderId(tjOrder.getOrderId());
                customer.setTjTime(tjOrder.getCreateTime());
                customer.setFinishTime(tjOrder.getFinishTime());
                customer.setConfirmStatus(String.valueOf(tjOrder.getStatus()));
                customer.setTjCategory(tjOrder.getTjCategory());
                if (tjOrder.getFirmId() == null) {
                    customer.setTjCompName("无");
                } else {
                    DictComp byId = compService.selectDictCompByDrugManufacturerId(tjOrder.getFirmId());
                    if (byId != null) {
                        customer.setTjCompName(byId.getCnName());
                    }
                }
                customerList.add(customer);
            }
        }
        List<TjCustomer> customers = customerList.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        map.put("customers", customers);
        map.put("total", customerList.size());
        return AjaxResult.success(map);
    }
 
}