liusheng
2024-10-17 723d38375c45d24737bfef6f33a9686254abf99b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
package com.smartor.service.impl;
 
import afu.org.checkerframework.checker.oigj.qual.O;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.http.HttpUtils;
import com.smartor.config.PhoneUtils;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import com.smartor.service.IIvrTaskTemplateScriptService;
import com.smartor.service.IIvrTaskTemplateService;
import com.smartor.service.IServiceSubtaskService;
import com.smartor.service.IServiceTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * 单一任务(随访)Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-02
 */
@Slf4j
@Service
public class ServiceSubtaskServiceImpl implements IServiceSubtaskService {
    @Autowired
    private ServiceSubtaskMapper serviceSubtaskMapper;
 
    @Autowired
    private ServiceSubtaskDetailMapper serviceSubtaskDetailMapper;
 
    @Autowired
    private IServiceTaskService serviceTaskService;
 
    @Autowired
    private IvrTaskTemplateTargetoptionMapper serviceTaskScriptTargetoptionMapper;
 
    @Autowired
    private IvrTaskVisitResultMapper serviceTaskVisitResultMapper;
 
    @Autowired
    private RedisCache redisCache;
 
    @Autowired
    private IvrLibaExtemplatescriptMapper ivrLibaExtemplatescriptMapper;
 
    @Autowired
    private IIvrTaskTemplateService ivrTaskTemplateService;
    @Autowired
    private IIvrTaskTemplateScriptService iIvrTaskTemplateScriptService;
 
    @Value("${pri_key}")
    private String pri_key;
 
    @Value("${ASRCallBackPath}")
    private String ASRCallBackPath;
 
    @Value("${hangup}")
    private String hangup;
 
 
    /**
     * 查询单一任务(随访)
     *
     * @param id 单一任务(随访)主键
     * @return 单一任务(随访)
     */
    @Override
    public ServiceSubtask selectServiceSubtaskById(Long id) {
        return serviceSubtaskMapper.selectServiceSubtaskById(id);
    }
 
    /**
     * 查询单一任务(随访)列表
     *
     * @param serviceSubtaskVO 单一任务(随访)
     * @return 单一任务(随访)
     */
    @Override
    public List<ServiceSubtask> selectServiceSubtaskList(ServiceSubtaskVO serviceSubtaskVO) {
        return serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskVO);
    }
 
    @Override
    public ServiceTaskVO queryTaskByCondition(ServiceSubtask serviceSubtask) {
        //定义患者与单一任务关联表集合
        List<PatTaskRelevance> patTaskRelevances = new ArrayList<>();
        ServiceSubtaskVO serviceSubtaskVO = DtoConversionUtils.sourceToTarget(serviceSubtask, ServiceSubtaskVO.class);
        List<ServiceSubtask> list = selectServiceSubtaskList(serviceSubtaskVO);
 
        ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
        if (CollectionUtils.isEmpty(list) || list.size() == 0) {
            return DtoConversionUtils.sourceToTarget(serviceTask, ServiceTaskVO.class);
        }
 
 
        //将查出来的数据倒入ServiceSubtaskVO中
        ServiceTaskVO serviceTaskVO = DtoConversionUtils.sourceToTarget(serviceTask, ServiceTaskVO.class);
        serviceTaskVO.setShowDate(serviceTask.getShowDate());
        serviceTaskVO.setShowTimeMorn(serviceTask.getShowTimeMorn());
        serviceTaskVO.setShowTimeNoon(serviceTask.getShowTimeNoon());
        serviceTaskVO.setShowTimeNight(serviceTask.getShowTimeNight());
        serviceTaskVO.setPreachform(serviceTask.getPreachform());
        String sendTimeSlot = serviceTask.getSendTimeSlot();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //获取到发送时间的集合
            if (com.ruoyi.common.utils.StringUtils.isNotEmpty(sendTimeSlot)) {
                List<TaskSendTimeVO> taskSendTimeVOList = objectMapper.readValue(sendTimeSlot, List.class);
                serviceTaskVO.setSendTimeslot(taskSendTimeVOList);
                serviceTaskVO.setSendType(serviceTask.getSendType());
            }
            //文本变量参数
            if (com.ruoyi.common.utils.StringUtils.isNotEmpty(serviceTask.getTextParam())) {
                Map<String, Map<String, String>> textParam = objectMapper.readValue(serviceTask.getTextParam(), Map.class);
                serviceTaskVO.setTextParam(textParam);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
 
        for (ServiceSubtask serviceSubtask1 : list) {
            PatTaskRelevance patTaskRelevance = new PatTaskRelevance();
            if (!serviceSubtask1.getHospType().equals("2")) {
                log.info("随访查询不为出院,{}", serviceSubtask1.getHospType());
                //获取到患者信息,并放入到集合中
                patTaskRelevance.setName(serviceSubtask1.getSendname());
                patTaskRelevance.setAge(serviceSubtask1.getAge());
                patTaskRelevance.setFinishtime(serviceSubtask1.getFinishtime());
                patTaskRelevance.setSfzh(serviceSubtask1.getSfzh());
                patTaskRelevance.setPhone(serviceSubtask1.getPhone());
                patTaskRelevance.setAddr(serviceSubtask1.getAddr());
                patTaskRelevance.setDiagname(serviceSubtask1.getDiagname());
                patTaskRelevance.setPatid(serviceSubtask1.getPatid());
                patTaskRelevance.setSendStatus(serviceSubtask1.getSendstate());
                patTaskRelevance.setDeptCode(serviceSubtask1.getDeptcode());
                patTaskRelevance.setDeptName(serviceSubtask1.getDeptname());
                patTaskRelevance.setLeavehospitaldistrictcode(serviceSubtask1.getLeavehospitaldistrictcode());
                patTaskRelevance.setLeavehospitaldistrictname(serviceSubtask1.getLeavehospitaldistrictname());
                patTaskRelevance.setInhosptime(serviceSubtask1.getInhosptime());
                patTaskRelevance.setDrname(serviceSubtask1.getDrname());
                patTaskRelevance.setDrcode(serviceSubtask1.getDrcode());
                patTaskRelevance.setEndtime(serviceSubtask1.getEndtime());
                patTaskRelevance.setNurseId(serviceSubtask1.getNurseId());
                patTaskRelevance.setNurseName(serviceSubtask1.getNurseName());
                patTaskRelevances.add(patTaskRelevance);
            }
            if (serviceSubtask1.getHospType().equals("2")) {
                log.info("随访查询为出院,{}", serviceSubtask1.getHospType());
                patTaskRelevance.setName(serviceSubtask1.getSendname());
                patTaskRelevance.setAge(serviceSubtask1.getAge());
                patTaskRelevance.setSfzh(serviceSubtask1.getSfzh());
                patTaskRelevance.setPhone(serviceSubtask1.getPhone());
                patTaskRelevance.setFinishtime(serviceSubtask1.getFinishtime());
                patTaskRelevance.setAddr(serviceSubtask1.getAddr());
                patTaskRelevance.setDeptName(serviceSubtask1.getDeptname());
                patTaskRelevance.setDeptCode(serviceSubtask1.getDeptcode());
                patTaskRelevance.setBedNo(serviceSubtask1.getBedNo());
                patTaskRelevance.setDiagname(serviceSubtask1.getDiagname());
                patTaskRelevance.setPatid(serviceSubtask1.getPatid());
                patTaskRelevance.setSendStatus(serviceSubtask1.getSendstate());
                patTaskRelevance.setLeavehospitaldistrictcode(serviceSubtask1.getLeavehospitaldistrictcode());
                patTaskRelevance.setLeavehospitaldistrictname(serviceSubtask1.getLeavehospitaldistrictname());
                patTaskRelevance.setInhosptime(serviceSubtask1.getInhosptime());
                patTaskRelevance.setStarttime(serviceSubtask1.getStarttime());
                patTaskRelevance.setDrname(serviceSubtask1.getDrname());
                patTaskRelevance.setDrcode(serviceSubtask1.getDrcode());
                patTaskRelevance.setEndtime(serviceSubtask1.getEndtime());
                patTaskRelevance.setStarttime(serviceSubtask1.getStarttime());
                patTaskRelevance.setNurseId(serviceSubtask1.getNurseId());
                patTaskRelevance.setNurseName(serviceSubtask1.getNurseName());
                patTaskRelevances.add(patTaskRelevance);
            }
        }
 
        serviceTaskVO.setPatTaskRelevances(patTaskRelevances);
        return serviceTaskVO;
    }
 
    @Override
    public List<ServiceSubtask> patItem(ServiceSubtaskVO serviceSubtaskVO) {
        List<ServiceSubtask> selectServiceSubtaskList = this.selectServiceSubtaskList(serviceSubtaskVO);
        //根据出院 时间倒序
//        List<ServiceSubtask> sortedServiceSubtaskList = selectServiceSubtaskList.stream().sorted(Comparator.comparing(ServiceSubtask::getEndtime).reversed()).collect(Collectors.toList());
        return selectServiceSubtaskList;
    }
 
    @Override
    public Map<String, Object> patItemCount(ServiceSubtaskVO serviceSubtaskVO) {
        serviceSubtaskVO.setPageSize(99999999);
        serviceSubtaskVO.setPageNum(1);
        List<ServiceSubtask> selectServiceSubtaskList = this.selectServiceSubtaskList(serviceSubtaskVO);
        Map<String, Object> map = new HashMap<>();
        Integer wzx = 0;
        Integer ysf = 0;
        Integer yc = 0;
        Integer fssb = 0;
        Integer yfs = 0;
        Integer blq = 0;
        for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
            if (serviceSubtask.getSendstate() == 4L) wzx = wzx + 1;
            else if (serviceSubtask.getSendstate() != 4L) ysf = ysf + 1;
            if (serviceSubtask.getSendstate() == 5L) fssb = fssb + 1;
            if (serviceSubtask.getSendstate() == 3L) yfs = yfs + 1;
            if (serviceSubtask.getSendstate() == 1L) blq = blq + 1;
            if (serviceSubtask.getExcep().equals("1")) yc = yc + 1;
        }
        map.put("wzx", wzx);
        map.put("ysf", ysf);
        map.put("yc", yc);
        map.put("fssb", fssb);
        map.put("yfs", yfs);
        map.put("blq", blq);
 
        return map;
    }
 
    /**
     * 新增单一任务(随访)
     *
     * @param serviceSubtask 单一任务(随访)
     * @return 结果
     */
    @Override
    public int insertServiceSubtask(ServiceSubtask serviceSubtask) {
        serviceSubtask.setCreateTime(DateUtils.getNowDate());
        return serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
    }
 
    /**
     * 修改单一任务(随访)
     *
     * @param serviceSubtask 单一任务(随访)
     * @return 结果
     */
    @Override
    public Boolean updateServiceSubtask(ServiceSubtask serviceSubtask) {
        serviceSubtask.setUpdateTime(DateUtils.getNowDate());
        return serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
    }
 
    /**
     * 批量删除单一任务(随访)
     *
     * @return 结果
     */
    @Override
    public int deleteServiceSubtaskByIds(Long[] ids) {
        Integer i = 0;
        for (Long id : ids) {
            i = serviceSubtaskMapper.deleteServiceSubtaskById(id);
        }
        return i;
    }
 
    /**
     * 单一任务
     *
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Integer> insertOrUpdateTask(ServiceTaskVO serviceTaskVO) {
        if (ObjectUtils.isEmpty(serviceTaskVO)) {
            log.info("任务入参为空,请检查入参");
            throw new BaseException("任务入参为空,请检查入参");
        }
        Integer integer = null;
        ServiceTask serviceTask = DtoConversionUtils.sourceToTarget(serviceTaskVO, ServiceTask.class);
        serviceTask.setTextParam(JSON.toJSONString(serviceTaskVO.getTextParam()));
        if (serviceTaskVO.getIsoperation() != null && serviceTaskVO.getIsoperation() == 1) {
            //往任务表中,新增任务
            if (ObjectUtils.isNotEmpty(serviceTaskVO.getSendTimeslot()))
                serviceTask.setSendTimeSlot(JSON.toJSONString(serviceTaskVO.getSendTimeslot()));
            if (serviceTask.getSendState() == null) {
                serviceTask.setSendState(1L);
                serviceTaskVO.setSendState(1L);
            }
            if (StringUtils.isNotEmpty(serviceTaskVO.getLibtemplateid())) {
                serviceTask.setLibtemplateid(serviceTaskVO.getLibtemplateid());
            }
            serviceTask.setTemplateid(serviceTaskVO.getTemplateid());
 
            serviceTask.setLeaveldeptcodes(serviceTaskVO.getLeaveldeptcodes());
            serviceTask.setLeavehospitaldistrictcode(serviceTask.getLeavehospitaldistrictcode());
            serviceTaskService.insertServiceTask(serviceTask);
            if (serviceTaskVO.getLongTask() != null && serviceTaskVO.getLongTask() == 1) {
                Map<String, Integer> map = new HashMap<>();
                map.put("subTaskId", null);
                map.put("taskId", serviceTask.getTaskid().intValue());
                return map;
            }
 
            //将任务信息放到服务表中
            ServiceSubtask serviceSubtask = DtoConversionUtils.sourceToTarget(serviceTaskVO, ServiceSubtask.class);
            serviceSubtask.setTaskid(serviceTask.getTaskid().longValue());
            serviceSubtask.setTemplatename(serviceTaskVO.getTemplatename());
            //新增
            if (CollectionUtils.isNotEmpty(serviceTaskVO.getPatTaskRelevances())) {
                for (PatTaskRelevance patTaskRelevance : serviceTaskVO.getPatTaskRelevances()) {
                    //将任务信息新增到随访服务表中
                    serviceSubtask.setSendname(patTaskRelevance.getName());
                    serviceSubtask.setAge(patTaskRelevance.getAge());
                    serviceSubtask.setSfzh(patTaskRelevance.getIdcardno());
                    serviceSubtask.setPhone(patTaskRelevance.getPhone());
                    serviceSubtask.setAddr(patTaskRelevance.getAddr());
                    serviceSubtask.setPatid(patTaskRelevance.getPatid());
                    serviceSubtask.setCreateTime(DateUtils.getNowDate());
                    serviceSubtask.setSendstate(1L);
                    serviceSubtask.setDeptcode(patTaskRelevance.getDeptCode());
                    serviceSubtask.setDeptname(patTaskRelevance.getDeptName());
                    serviceSubtask.setLeavehospitaldistrictcode(patTaskRelevance.getLeavehospitaldistrictcode());
                    serviceSubtask.setLeavehospitaldistrictname(patTaskRelevance.getLeavehospitaldistrictname());
                    serviceSubtask.setType(serviceTaskVO.getHospType());
                    serviceSubtask.setHospType(patTaskRelevance.getHospType());
                    serviceSubtask.setOpenid(patTaskRelevance.getOpenid());
                    serviceSubtask.setDrname(patTaskRelevance.getDrname());
                    serviceSubtask.setDrcode(patTaskRelevance.getDrcode());
                    serviceSubtask.setInhosptime(patTaskRelevance.getInhosptime());
                    serviceSubtask.setEndtime(patTaskRelevance.getEndtime());
                    serviceSubtask.setNurseId(patTaskRelevance.getNurseId());
                    serviceSubtask.setNurseName(patTaskRelevance.getNurseName());
                    serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
                    integer = serviceSubtask.getId().intValue();
                }
            }
 
        } else if (serviceTaskVO.getIsoperation() != null && serviceTaskVO.getIsoperation() == 2) {
            //任务修改
            if (ObjectUtils.isNotEmpty(serviceTaskVO.getSendTimeslot()))
                serviceTask.setSendTimeSlot(JSON.toJSONString(serviceTaskVO.getSendTimeslot()));
            //修改操作,需要将stopState状态+1
            ServiceTask serviceTask1 = serviceTaskService.selectServiceTaskByTaskid(serviceTask.getTaskid());
            long l = serviceTask1.getStopState() + 1;
            serviceTask.setStopState(l);
            if (serviceTaskVO.getLibtemplateid() != null)
                serviceTask.setLibtemplateid(serviceTaskVO.getLibtemplateid().toString());
            serviceTask.setTemplateid(serviceTaskVO.getTemplateid());
            serviceTaskService.updateServiceTask(serviceTask);
            if (CollectionUtils.isNotEmpty(serviceTaskVO.getPatTaskRelevances())) {
                for (PatTaskRelevance patTaskRelevance : serviceTaskVO.getPatTaskRelevances()) {
                    ServiceSubtask serviceSubtask = DtoConversionUtils.sourceToTarget(serviceTaskVO, ServiceSubtask.class);
                    serviceSubtask.setSendname(patTaskRelevance.getName());
                    serviceSubtask.setAge(patTaskRelevance.getAge());
                    serviceSubtask.setSfzh(patTaskRelevance.getSfzh());
                    serviceSubtask.setPhone(patTaskRelevance.getPhone());
                    serviceSubtask.setAddr(patTaskRelevance.getAddr());
                    serviceSubtask.setPatid(patTaskRelevance.getPatid());
                    serviceSubtask.setOpenid(patTaskRelevance.getOpenid());
                    serviceSubtask.setDeptcode(patTaskRelevance.getDeptCode());
                    serviceSubtask.setLeavehospitaldistrictname(patTaskRelevance.getLeavehospitaldistrictname());
                    serviceSubtask.setLeavehospitaldistrictcode(patTaskRelevance.getLeavehospitaldistrictcode());
                    serviceSubtask.setDeptname(patTaskRelevance.getDeptName());
                    serviceSubtask.setType(serviceTaskVO.getHospType());
                    serviceSubtask.setCreateTime(DateUtils.getNowDate());
                    serviceSubtask.setDrname(patTaskRelevance.getDrname());
                    serviceSubtask.setDrcode(patTaskRelevance.getDrcode());
                    serviceSubtask.setInhosptime(patTaskRelevance.getInhosptime());
                    serviceSubtask.setHospType(patTaskRelevance.getHospType());
                    serviceSubtask.setEndtime(patTaskRelevance.getEndtime());
                    serviceSubtask.setNurseId(patTaskRelevance.getNurseId());
                    serviceSubtask.setNurseName(patTaskRelevance.getNurseName());
                    serviceSubtask.setTextParam(new Gson().toJson(serviceTaskVO.getTextParam()));
                    if (patTaskRelevance.getIsoperation() != null) {
                        if (patTaskRelevance.getIsoperation() == 2)
                            serviceSubtaskMapper.updateServiceSubtaskByCondition(serviceSubtask);
                        if (patTaskRelevance.getIsoperation() == 1) {
                            serviceSubtask.setSendstate(1L);
                            serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
                        }
                        if (patTaskRelevance.getIsoperation() == 3)
                            //  通过taskid和patid去删除该条数据
                            serviceSubtaskMapper.deleteServiceSubtaskByCondition(serviceTaskVO.getTaskid(), patTaskRelevance.getPatid());
                    }
                    integer = serviceSubtask.getTaskid().intValue();
                }
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("subTaskId", integer);
        map.put("taskId", serviceTask.getTaskid().intValue());
        return map;
    }
 
 
    @Override
    public void phoneCallBack(PhoneCallBackVO phoneCallBackVO) {
        phoneCallBackVO.setTextResult(phoneCallBackVO.getTextResult().substring(0, phoneCallBackVO.getTextResult().length() - 1));
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
 
        //获取数据
        Boolean aBoolean = redisCache.hasKey(phoneCallBackVO.getUuid());
        if (!aBoolean) {
            throw new BaseException("该uuid不存在");
        }
        Integer hangupValue = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "hangup");
        PhoneUtils phoneUtils = new PhoneUtils();
        if (hangupValue != null && hangupValue == 1) {
            String date = simpleDateFormat1.format(new Date());
            log.info("电话要挂断了: {}", date);
            //hangupValue == 1  随访结束,直接可以挂电话
            phoneUtils.hangup("", "", "", "", "", "", "", phoneCallBackVO.getUuid());
            return;
        }
 
        Map<String, Object> map = redisCache.getCacheObject(phoneCallBackVO.getUuid());
        ServiceSubtask serviceSubtask = (ServiceSubtask) map.get("ServiceSubtask");
        List<IvrTaskTemplateScriptVO> IvrTaskTemplateScriptVOs = (List<IvrTaskTemplateScriptVO>) map.get("IvrTaskTemplateScriptVO");
        //将uuid更新到数据库中
        serviceSubtask.setSenduuid(phoneCallBackVO.getUuid());
        serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
 
        //获取模板信息
        IvrTaskTemplateVO ivrTaskTemplateVO = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "IvrTaskTemplateVO");
 
        //语音识别结果上报接口: 3
        Integer noVoice = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "noVoice");
        QuestionMessage returnQues = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "returnQues");
        //将传回的结果放到回复对象中
        returnQues.setContent(phoneCallBackVO.getTextResult());
 
        IvrTaskTemplateScriptVO nowQuestion = returnQues.getNowQuestion();
 
        if (StringUtils.isEmpty(returnQues.getContent())) {
            //无回话
            //判断noVoice是否已经到了最大值
            if (noVoice == ivrTaskTemplateVO.getNoVoiceNum().intValue()) {
                //已经问了对应的遍数,就判断是否还有下一题
                if (nowQuestion.getTargetid() == IvrTaskTemplateScriptVOs.size()) {
                    //没有下一题了,就挂断电话,播放结束语
                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "hangup", 1, 120, TimeUnit.MINUTES);
                    phoneUtils.ttsPlayback(ivrTaskTemplateVO.getRevisitAfter(), phoneCallBackVO.getUuid());
                    return;
                } else {
                    //有下一题
                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", 0, 120, TimeUnit.MINUTES);
                    IvrTaskTemplateScriptVO nextQuestion = getNextQuestion(IvrTaskTemplateScriptVOs, nowQuestion);
                    // 问题,  去调用“tts合成和播放”接口
                    String date = simpleDateFormat1.format(new Date());
                    log.info("去调用tts合成和播放接口: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                    phoneUtils.ttsPlayback(nowQuestion.getScriptContent(), phoneCallBackVO.getUuid());
                }
            } else {
                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", noVoice + 1, 120, TimeUnit.MINUTES);
                //调用ivrLibaTemplateScriptVO中的slienceText(静默话术)
                String slienceText = nowQuestion.getSlienceText();
                //静默话术  + 问题,  去调用“tts合成和播放”接口
                String date = simpleDateFormat1.format(new Date());
                log.info("静默话术  + 问题,去调用tts合成和播放接口: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                phoneUtils.ttsPlayback(slienceText + nowQuestion.getScriptContent(), phoneCallBackVO.getUuid());
                return;
            }
 
        } else {
            //isppd用来记录是否匹配到
            Boolean isppd = false;
            //有回话,对回答的问题,进行正则匹配(这里只针对选择题,其它题型不行)
            for (int j = 0; j < nowQuestion.getIvrTaskScriptTargetoptionList().size(); j++) {
                //包含
                Matcher matcher = null;
                if (StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex())) {
                    Pattern pattern = Pattern.compile(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex());
                    matcher = pattern.matcher(phoneCallBackVO.getTextResult());
                }
                //不包含
                Matcher matcher2 = null;
                if (StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2())) {
                    Pattern pattern2 = Pattern.compile(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2());
                    matcher2 = pattern2.matcher(phoneCallBackVO.getTextResult());
                }
                log.error("PCB--getQuestionText问题为:{},UUID:{}", nowQuestion.getScriptContent(), phoneCallBackVO.getUuid());
                if (StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() || StringUtils.isEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
                    //说明匹配正确了
                    //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
                    nowQuestion.getIvrTaskScriptTargetoptionList().get(j).setIsUserOperation(1);
                    serviceTaskScriptTargetoptionMapper.updateIvrTaskTemplateTargetoption(nowQuestion.getIvrTaskScriptTargetoptionList().get(j));
 
                    //将静默置为0
                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", 0, 120, TimeUnit.MINUTES);
                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", 0, 120, TimeUnit.MINUTES);
 
                    //将患者的回签写进表里
                    IvrTaskVisitResult serviceTaskVisitResult = DtoConversionUtils.sourceToTarget(serviceSubtask, IvrTaskVisitResult.class);
                    serviceTaskVisitResult.setId(null);
                    serviceTaskVisitResult.setQuestion(nowQuestion.getScriptContent());
                    serviceTaskVisitResult.setPatientAnswer(phoneCallBackVO.getTextResult());
                    serviceTaskVisitResult.setCreateTime(new Date());
                    serviceTaskVisitResult.setOptionResult(nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getTargetvalue());
                    serviceTaskVisitResultMapper.insertIvrTaskVisitResult(serviceTaskVisitResult);
 
                    //将匹配到的标识改成true
                    isppd = true;
 
                    //获取下一题
                    Long nextQuestion = nowQuestion.getIvrTaskScriptTargetoptionList().get(j).getNextQuestion();
                    for (IvrTaskTemplateScriptVO script : IvrTaskTemplateScriptVOs) {
                        if (script.getTargetid() == nextQuestion) {
                            QuestionMessage questionMessage = new QuestionMessage();
                            questionMessage.setNowQuestion(script);
                            questionMessage.setQuestionList(IvrTaskTemplateScriptVOs);
                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
                            phoneUtils.ttsPlayback(script.getScriptContent(), phoneCallBackVO.getUuid());
                            return;
                        } else if (nextQuestion > IvrTaskTemplateScriptVOs.size()) {
                            //没有下一题了,就结束了
                            String date = simpleDateFormat1.format(new Date());
                            log.error("没有下一题了,就结束了: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "hangup", 1, 120, TimeUnit.MINUTES);
                            phoneUtils.ttsPlayback(ivrTaskTemplateVO.getRevisitAfter(), phoneCallBackVO.getUuid());
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            phoneUtils.hangup("", "", ivrTaskTemplateVO.getRevisitAfter(), "", "", "", "", phoneCallBackVO.getUuid());
                            return;
                        }
                    }
                    return;
                } else {
                    //没有匹配上当前option
                    //Targetregex2 为false,表示在Targetregex2中存在  语句中的关键字,这个option就不用再继续匹配了,直接匹配下一个option
                    continue;
                }
            }
 
            if (isppd != true) {
                //没有匹配到
                Integer mateNum = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "mateNum");
                if (mateNum == null) mateNum = 0;
                //无匹配次数去判断是否到最大询问次数,并且所有的选项都匹配完了
                if (mateNum == ivrTaskTemplateVO.getMateNum().intValue()) {
                    //如果下一题为空.则新的数据返回,并加上感谢语
                    if (nowQuestion.getTargetid() < IvrTaskTemplateScriptVOs.size()) {
                        QuestionMessage questionMessage = new QuestionMessage();
                        IvrTaskTemplateScriptVO nextQuestion = getNextQuestion(IvrTaskTemplateScriptVOs, nowQuestion);
                        questionMessage.setQuestionList(IvrTaskTemplateScriptVOs);
                        questionMessage.setNowQuestion(nextQuestion);
                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", 0, 120, TimeUnit.MINUTES);
                        String date = simpleDateFormat1.format(new Date());
                        log.info("如果下一题为空.则新的数据返回,并加上感谢语: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                        phoneUtils.ttsPlayback(nextQuestion.getScriptContent(), phoneCallBackVO.getUuid());
                        return;
                    } else {
                        //就可以挂断电话了
                        String date = simpleDateFormat1.format(new Date());
                        log.info("就可以挂断电话了------: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "hangup", 1, 120, TimeUnit.MINUTES);
                        phoneUtils.ttsPlayback(ivrTaskTemplateVO.getRevisitAfter(), phoneCallBackVO.getUuid());
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        phoneUtils.hangup("", "", ivrTaskTemplateVO.getRevisitAfter(), "", "", "", "", phoneCallBackVO.getUuid());
                        return;
                    }
                } else if (mateNum < ivrTaskTemplateVO.getMateNum().intValue()) {
                    //没有问到规定次数
                    mateNum = mateNum + 1;
                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", mateNum, 120, TimeUnit.MINUTES);
                }
            }
            //选项匹配完成后,需要再去通过库再进行匹配一次
            String extemplateID = ivrTaskTemplateVO.getSubmoduleID();
            String[] split = extemplateID.split(",");
            List<String> list = Arrays.asList(split);
            List<Long> list1 = new ArrayList<>();
            if (StringUtils.isNotEmpty(extemplateID)) {
                for (String str : list) {
                    list1.add(Long.valueOf(str));
                }
                List<IvrLibaExtemplatescript> ivrLibaExtemplatescripts = ivrLibaExtemplatescriptMapper.queryIvrLibaExtemplatescriptList(list1);
                for (IvrLibaExtemplatescript ivrLibaExtemplatescript : ivrLibaExtemplatescripts) {
                    Matcher matcher = null;
                    if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex())) {
                        Pattern pattern = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex());
                        matcher = pattern.matcher(returnQues.getContent());
                    }
 
                    Matcher matcher2 = null;
                    if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2())) {
                        Pattern pattern2 = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex2());
                        matcher2 = pattern2.matcher(returnQues.getContent());
                    }
                    log.info("++++++++++++++++++++++++++通用库是否为空:selfRegex : {} , selfRegex2 : {}", ivrLibaExtemplatescript.getSelfRegex(), ivrLibaExtemplatescript.getSelfRegex2());
                    if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches() && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches()) {
                        QuestionMessage questionMessage = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "returnQues");
                        IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO = returnQues.getNowQuestion();
                        ivrTaskTemplateScriptVO.setSubmoduleText(ivrLibaExtemplatescript.getSwitchText());
                        ivrTaskTemplateScriptVO.setSubmoduleVoice(ivrLibaExtemplatescript.getSwitchWav());
                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
                        if (ivrLibaExtemplatescript.getIsEnd() == 1) {
                            //将问题置空
                            IvrTaskTemplateScriptVO nowQuestion1 = questionMessage.getNowQuestion();
                            nowQuestion1.setScriptContent(null);
                            nowQuestion1.setScriptVoice(null);
                            questionMessage.setNowQuestion(nowQuestion1);
                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "isOver", 1, 120, TimeUnit.MINUTES);
                        }
                        //调用“15、tts合成和播放, tts_playback”将结果传回
                        String date = simpleDateFormat1.format(new Date());
                        log.info("调用“15、tts合成和播放------: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                        phoneUtils.ttsPlayback(nowQuestion.getScriptContent() + ivrTaskTemplateScriptVO.getSubmoduleText(), phoneCallBackVO.getUuid());
                    }
                    break;
                }
                String date = simpleDateFormat1.format(new Date());
                log.info("最后的信息回复-: {},uuid为:{}", date, phoneCallBackVO.getUuid());
                phoneUtils.ttsPlayback(nowQuestion.getScriptContent(), phoneCallBackVO.getUuid());
            }
        }
    }
 
    /**
     * 电话ASR通话回调(雨绮)
     *
     * @param phoneCallReqYQVO
     */
    @Override
    public PhoneCallBackYQVO phoneCallBackYQ(PhoneCallReqYQVO phoneCallReqYQVO) {
        //定义一个分数的变量
        Boolean aBoolean1 = redisCache.hasKey(phoneCallReqYQVO.getUuid() + "SCORE");
        if (!aBoolean1) redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", 0.0, 120, TimeUnit.MINUTES);
        PhoneCallBackYQVO phoneCallBackYQVO = new PhoneCallBackYQVO();
        //channel_create 通道创建的时候,可以执行一些其它操作,譬如发个短信之类的;  我们的业务可以不用管    PlayEventCallback 这个是播放语音的,暂时用不到     End_time()= -1或null表示当前的asrtext不是一句完整的话
        if (phoneCallReqYQVO.getOperate().equals("channel_create")) {
            return phoneCallBackYQVO;
        }
        //PlayEventCallback 这个是播放语音的    playstart:放音开始(问题播报开始)    playstop: 放音结束(问题播报结束)
        if (phoneCallReqYQVO.getOperate().equals("PlayEventCallback")) {
            String cacheJSY = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "- jsy");
            if (phoneCallReqYQVO.getOperate().equals("PlayEventCallback") && phoneCallReqYQVO.getPlaystop() == false) {
                //判断redis中是否有结束语
                if (StringUtils.isEmpty(cacheJSY)) {
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop", false, 120, TimeUnit.MINUTES);
                    log.error("PlayEventCallbackPlaystop的值为-------:{}", false);
                }
            } else {
                //如果结束语不为空,则要挂电话了
                if (StringUtils.isNotEmpty(cacheJSY)) {
                    Map<String, String> req = new HashMap<>();
                    req.put("uuid", phoneCallReqYQVO.getUuid());
                    req.put("caller", phoneCallReqYQVO.getPhone());
                    HttpUtils.sendPost(hangup, new Gson().toJson(req));
                    //删除结束语的患存
                    redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "- jsy");
                } else {
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop", true, 120, TimeUnit.MINUTES);
                    log.error("-------PlayEventCallbackPlaystop的值为:{}", true);
                }
            }
            return phoneCallBackYQVO;
        }
        //获取放音是否结束
        boolean isPlayEventOver = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
        if (!isPlayEventOver) {
            return phoneCallBackYQVO;
        }
 
        //通过子任务ID获取到模板信息
        ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(Long.valueOf(phoneCallReqYQVO.getTaskid()));
        IvrTaskTemplate ivrTaskTemplate = ivrTaskTemplateService.selectIvrTaskTemplateByID(serviceSubtask.getTemplateid());
        //获取模板问题
        IvrTaskTemplateScript ivrTaskTemplateScript = new IvrTaskTemplateScript();
        ivrTaskTemplateScript.setTemplateID(serviceSubtask.getTemplateid());
        List<IvrTaskTemplateScript> ivrTaskTemplateScripts = iIvrTaskTemplateScriptService.selectIvrTaskTemplateScriptList(ivrTaskTemplateScript);
        //获取问题ID 和 序号
        String scriptId = redisCache.getCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim());
        log.error("scriptId是多少:{}", scriptId);
        //当前题的信息
        IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO = iIvrTaskTemplateScriptService.getTaskTempScriptInfoByid(Long.valueOf(scriptId));
        //判断UUID是否存在
        Boolean aBoolean = redisCache.hasKey(phoneCallReqYQVO.getUuid());
        if (!aBoolean) {
            //给静默设置一个默认次数在redis中
            redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "noVoice", 0, 120, TimeUnit.MINUTES);
            //如果不存在,就把当前的UUID做为key,放到对象中去
            redisCache.setCacheObject(phoneCallReqYQVO.getUuid(), phoneCallReqYQVO, 120, TimeUnit.MINUTES);
        }
 
        if ("SilentCallback".equals(phoneCallReqYQVO.getOperate())) {
            //如果是静默回调
            Integer num = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "noVoice");
            //判断静默回调次数是否小与模板规定的次数
            if (num < ivrTaskTemplate.getNoVoiceNum()) {
                //小与的话,就继续问患者
                phoneCallBackYQVO.setType("text");
                phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                String scriptContent = ivrTaskTemplateScriptVO.getScriptContent();
                log.error("SilentCallback的问题内容scriptContent:{}", scriptContent);
                phoneCallBackYQVO.setValue(getObject(serviceSubtask, scriptContent));
                //将静默次数加1
                Integer noVoiceNum = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "noVoice");
                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "noVoice", noVoiceNum + 1, 120, TimeUnit.MINUTES);
                return phoneCallBackYQVO;
            } else {
                log.error("静默次数达到,挂掉电话:{}", num);
                //大与等于的话,直接挂断
                phoneCallBackYQVO.setType("text");
                phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                phoneCallBackYQVO.setValue(ivrTaskTemplate.getRevisitAfter());
                //将结果写到detail中
                ServiceSubTaskDetailReq serviceSubTaskDetailReq = new ServiceSubTaskDetailReq();
                List<ServiceSubtaskDetail> serviceSubtaskDetailList = new ArrayList<>();
                serviceSubtaskDetailList.add(getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate));
                serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList);
                saveQuestionAnswerPhone(serviceSubTaskDetailReq);
                //去redis中,把该子任务ID删除
                Long id = serviceSubtask.getId();
                //先更新一下分数
                double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                serviceSubtask.setScore(BigDecimal.valueOf(score));
                serviceSubtask.setFinishtime(new Date());
                serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
                Map<String, String> map = delRedisValue(null, id.toString());
                redisCache.setCacheObject(map.get("cacheName"), map.get("val"));
                redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
                //在redis中保存一下结束语,在调用挂电话的方法时删除
                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", ivrTaskTemplate.getRevisitAfter(), 120, TimeUnit.MINUTES);
            }
            return phoneCallBackYQVO;
        } else if ("AsrCallback".equals(phoneCallReqYQVO.getOperate()) && phoneCallReqYQVO.getEnd_time() != null && phoneCallReqYQVO.getEnd_time() != -1) {
            //播报第一题
            Integer integer = redisCache.getCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim() + "-firstSort");
//            if(integer==0){
//                //integer==0,表示开场白刚播报完,需要停顿一下,等患者说话
//                redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim() + "-firstSort", 1, 120, TimeUnit.MINUTES);
//                return phoneCallBackYQVO;
//            }
            if (ivrTaskTemplateScriptVO.getSort() == 1 && integer == 1) {
                phoneCallBackYQVO.setType("text");
                phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                String scriptContent = ivrTaskTemplateScriptVO.getScriptContent();
                phoneCallBackYQVO.setValue(getObject(serviceSubtask, scriptContent));
                redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim() + "-firstSort", 2, 120, TimeUnit.MINUTES);
                return phoneCallBackYQVO;
            }
 
            //如果是文本回调
            //根据问题ID获取该问题的类型
            if (ivrTaskTemplateScriptVO.getScriptType().equals("1")) {
                //是选择题
                for (int j = 0; j < ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().size(); j++) {
                    log.error("phoneCallReqYQVO.getAsrtext()的值为:{}", phoneCallReqYQVO.getAsrtext());
                    if (StringUtils.isEmpty(phoneCallReqYQVO.getAsrtext())) {
                        continue;
                    }
                    //包含
                    Matcher matcher = null;
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex())) {
                        Pattern pattern = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex());
                        matcher = pattern.matcher(phoneCallReqYQVO.getAsrtext());
                    }
                    //不包含
                    Matcher matcher2 = null;
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2())) {
                        Pattern pattern2 = Pattern.compile(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2());
                        matcher2 = pattern2.matcher(phoneCallReqYQVO.getAsrtext());
                    }
                    if (StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
                        //说明匹配正确了
                        //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
                        ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).setIsUserOperation(1);
                        serviceTaskScriptTargetoptionMapper.updateIvrTaskTemplateTargetoption(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j));
 
                        //将患者的回签写进service_subtask_detail中
                        ServiceSubTaskDetailReq serviceSubTaskDetailReq = new ServiceSubTaskDetailReq();
                        List<ServiceSubtaskDetail> serviceSubtaskDetailList = new ArrayList<>();
                        ivrTaskTemplateScriptVO.setQuestionResult(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getOptiondesc());
                        serviceSubtaskDetailList.add(getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate));
                        serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList);
                        saveQuestionAnswerPhone(serviceSubTaskDetailReq);
                        //将当前前的播报状态删除,给下一题让位
                        redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
//                        //获取下一题
                        log.error("获取下一题的信息:{}", ivrTaskTemplateScriptVO);
                        if (ivrTaskTemplateScriptVO.getBranchFlag().equals("1") || ivrTaskTemplateScriptVO.getBranchFlag().equals("0") && ivrTaskTemplateScriptVO.getNextScriptno() != null && ivrTaskTemplateScriptVO.getNextScriptno() != 0) {
                            Long nextQuestion = null;
                            if (ivrTaskTemplateScriptVO.getBranchFlag().equals("1")) {
                                nextQuestion = ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getNextQuestion();
                                //更新分数
                                double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                                score = BigDecimal.valueOf(score).add(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getScore()).doubleValue();
                                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score);
                            } else {
                                nextQuestion = ivrTaskTemplateScriptVO.getNextScriptno();
                                //更新分数
                                Object obj = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                                Double score = (obj == null ? new Double(0.00) : new Double(((Double) obj).doubleValue()));
                                score = BigDecimal.valueOf(score).add(ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList().get(j).getScore()).doubleValue();
                                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score);
                            }
 
                            for (IvrTaskTemplateScript script : ivrTaskTemplateScripts) {
                                if (script.getSort() == nextQuestion.intValue()) {
                                    phoneCallBackYQVO.setType("text");
                                    phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                                    String scriptContent = script.getScriptContent();
                                    log.error("下一题问题:{}", scriptContent);
                                    phoneCallBackYQVO.setValue(getObject(serviceSubtask, scriptContent));
                                    //将该患者的Redis中的题目ID,进行修改
                                    redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim(), script.getId().toString(), 120, TimeUnit.MINUTES);
                                    //删除无响应
                                    redisCache.deleteObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                                }
                            }
                        } else if (ivrTaskTemplateScriptVO.getBranchFlag().equals("0")) {
                            if (ivrTaskTemplateScriptVO.getNextScriptno() == null || ivrTaskTemplateScriptVO.getNextScriptno() == 0) {
                                phoneCallBackYQVO.setType("text");
                                phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                                //更新一下分数
                                double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                                serviceSubtask.setScore(BigDecimal.valueOf(score));
                                serviceSubtask.setFinishtime(new Date());
                                serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
                                //设置结束语
                                phoneCallBackYQVO.setValue(ivrTaskTemplate.getRevisitAfter());
                                Long id = serviceSubtask.getId();
                                Map<String, String> map = delRedisValue(null, id.toString());
                                redisCache.setCacheObject(map.get("cacheName"), map.get("val"));
                                redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                                redisCache.deleteObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                                redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "PlayEventCallbackPlaystop");
                                //在redis中保存一下结束语,在调用挂电话的方法时删除
                                redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", ivrTaskTemplate.getRevisitAfter(), 120, TimeUnit.MINUTES);
//                                return phoneCallBackYQVO;
                            }
                        }
                    } else {
                        continue;
                    }
                }
                //都没有匹配到
                if (StringUtils.isEmpty(phoneCallBackYQVO.getValue())) {
                    Integer count = redisCache.getCacheObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                    if (count != null && count >= ivrTaskTemplate.getMateNum()) {
                        //如果count已经大于或等于没有匹配次数
                        if (ivrTaskTemplateScriptVO.getBranchFlag().equals("0") && ivrTaskTemplateScriptVO.getNextScriptno() == null || ivrTaskTemplateScriptVO.getBranchFlag().equals("0") && ivrTaskTemplateScriptVO.getNextScriptno() == 0 || ivrTaskTemplateScriptVO.getBranchFlag().equals("1") && ivrTaskTemplateScriptVO.getNextScriptno() == null || ivrTaskTemplateScriptVO.getBranchFlag().equals("1") && ivrTaskTemplateScriptVO.getNextScriptno() == 0) {
                            //如果是最后一道题,或者没有下一题了,就直接挂机
                            phoneCallBackYQVO.setType("text");
                            phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                            if (StringUtils.isNotEmpty(phoneCallBackYQVO.getValue()))
                                phoneCallBackYQVO.setValue(phoneCallBackYQVO.getValue() + ivrTaskTemplate.getRevisitAfter());
                            else phoneCallBackYQVO.setValue(ivrTaskTemplate.getRevisitAfter());
 
                            //更新一下分数
                            double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                            serviceSubtask.setScore(BigDecimal.valueOf(score));
                            serviceSubtask.setFinishtime(new Date());
                            serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
 
                            //在redis中保存一下结束语,在调用挂电话的方法时删除
                            redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", ivrTaskTemplate.getRevisitAfter(), 120, TimeUnit.MINUTES);
                            //去redis中,把该子任务ID删除
                            Long id = serviceSubtask.getId();
                            Map<String, String> map = delRedisValue(null, id.toString());
                            redisCache.setCacheObject(map.get("cacheName"), map.get("val"));
                            redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                            redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
//                            return phoneCallBackYQVO;
                        } else {
                            //根据ivrTaskTemplateScriptVO.getNextScriptno()获取下一题进行提问
                            for (IvrTaskTemplateScript script : ivrTaskTemplateScripts) {
                                if (script.getSort() == ivrTaskTemplateScriptVO.getNextScriptno().intValue()) {
                                    phoneCallBackYQVO.setType("text");
                                    phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                                    String scriptContent = script.getScriptContent();
                                    phoneCallBackYQVO.setValue(getObject(serviceSubtask, scriptContent));
                                    //将该患者的Redis中的题目ID,进行修改
                                    redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "-" + phoneCallReqYQVO.getPhone().trim(), script.getId().toString(), 120, TimeUnit.MINUTES);
 
                                    //更新一下分数
                                    double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                                    score = BigDecimal.valueOf(score).add(script.getScore()).doubleValue();
 
                                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score);
                                }
                            }
                        }
                    } else {
                        if (count == null)
                            redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid(), 1, 120, TimeUnit.MINUTES);
                        else
                            redisCache.setCacheObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid(), count + 1, 120, TimeUnit.MINUTES);
 
                        phoneCallBackYQVO.setType("text");
                        phoneCallBackYQVO.setValue(ivrTaskTemplateScriptVO.getNoMatchText() + getObject(serviceSubtask, ivrTaskTemplateScriptVO.getScriptContent()));
                    }
                }
 
            } else {
                //不是选择题,直接记录答案,将结果写到detail中
                ServiceSubTaskDetailReq serviceSubTaskDetailReq = new ServiceSubTaskDetailReq();
                List<ServiceSubtaskDetail> serviceSubtaskDetailList = new ArrayList<>();
                serviceSubtaskDetailList.add(getServiceSubtaskDetail(phoneCallReqYQVO, ivrTaskTemplateScriptVO, serviceSubtask, ivrTaskTemplate));
                serviceSubTaskDetailReq.setServiceSubtaskDetailList(serviceSubtaskDetailList);
                saveQuestionAnswerPhone(serviceSubTaskDetailReq);
//                String xh = idSort.split("-")[1];
 
                //如果选项分支为1的话,则需要根据问题上的nextScriptno进行跳转
                //问答题没有跳转
                if (ivrTaskTemplateScriptVO.getNextScriptno() != null && ivrTaskTemplateScriptVO.getNextScriptno() != 0) {
                    for (IvrTaskTemplateScript ivrTaskTemplateScript1 : ivrTaskTemplateScripts) {
                        if (ivrTaskTemplateScriptVO.getNextScriptno().intValue() == ivrTaskTemplateScript1.getSort()) {
                            phoneCallBackYQVO.setType("text");
                            phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                            String scriptContent = ivrTaskTemplateScript1.getScriptContent();
                            phoneCallBackYQVO.setValue(getObject(serviceSubtask, scriptContent));
                            redisCache.deleteObject(phoneCallReqYQVO.getTaskid().trim() + "&&" + "mate" + "&&" + phoneCallReqYQVO.getUuid());
                            redisCache.setCacheObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone(), ivrTaskTemplateScript1.getId().toString());
 
                            //更新一下分数
                            double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                            score = BigDecimal.valueOf(score).add(ivrTaskTemplateScriptVO.getScore()).doubleValue();
                            redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "SCORE", score);
 
//                            return phoneCallBackYQVO;
                        }
                    }
                } else if (ivrTaskTemplateScriptVO.getNextScriptno() == null || ivrTaskTemplateScriptVO.getNextScriptno() == 0) {
                    //没有下一题了,就结束了
                    phoneCallBackYQVO.setType("text");
                    phoneCallBackYQVO.setSilent_interval(ivrTaskTemplate.getSilencetime().intValue());
                    phoneCallBackYQVO.setValue(ivrTaskTemplate.getRevisitAfter());
 
                    //更新一下分数
                    double score = redisCache.getCacheObject(phoneCallReqYQVO.getUuid() + "SCORE");
                    serviceSubtask.setScore(BigDecimal.valueOf(score));
                    serviceSubtask.setFinishtime(new Date());
                    serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
 
                    //去redis中,把该子任务ID删除
                    Long id = serviceSubtask.getId();
                    Map<String, String> map = delRedisValue(null, id.toString());
                    redisCache.setCacheObject(map.get("cacheName"), map.get("val"));
                    //在redis中保存一下结束语,在调用挂电话的方法时删除
                    redisCache.setCacheObject(phoneCallReqYQVO.getUuid() + "- jsy", ivrTaskTemplate.getRevisitAfter(), 120, TimeUnit.MINUTES);
                    redisCache.deleteObject(serviceSubtask.getId() + "-" + serviceSubtask.getPhone());
                    redisCache.deleteObject(phoneCallReqYQVO.getUuid() + "SCORE");
                }
 
                //选项匹配完成后,需要再去通过库再进行匹配一次
                String extemplateID = ivrTaskTemplate.getSubmoduleID();
                if (StringUtils.isNotEmpty(extemplateID)) {
                    String[] split = extemplateID.split(",");
                    List<String> list = Arrays.asList(split);
                    List<Long> list1 = new ArrayList<>();
                    for (String str : list) {
                        list1.add(Long.valueOf(str));
                    }
                    List<IvrLibaExtemplatescript> ivrLibaExtemplatescripts = ivrLibaExtemplatescriptMapper.queryIvrLibaExtemplatescriptList(list1);
                    for (IvrLibaExtemplatescript ivrLibaExtemplatescript : ivrLibaExtemplatescripts) {
                        Matcher matcher = null;
                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex())) {
                            Pattern pattern = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex());
                            matcher = pattern.matcher(phoneCallReqYQVO.getAsrtext());
                        }
 
                        Matcher matcher2 = null;
                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2())) {
                            Pattern pattern2 = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex2());
                            matcher2 = pattern2.matcher(phoneCallReqYQVO.getAsrtext());
                        }
                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches() && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches()) {
                            //将通用库匹配的,放到返回值中
                            phoneCallBackYQVO.setValue(phoneCallBackYQVO.getValue() + ivrLibaExtemplatescript.getSwitchText());
                        }
                        break;
                    }
                }
            }
        }
        return phoneCallBackYQVO;
    }
 
 
    private String getObject(ServiceSubtask serviceSubtask, String scriptContent) {
        Map<String, Map<String, String>> param = getParam(serviceSubtask.getTaskid());
        for (Map<String, String> map : param.values()) {
            for (String key : map.keySet()) {
                scriptContent = scriptContent.replace(key, StringUtils.isNotEmpty(map.get(key)) ? map.get(key) : "");
            }
        }
        scriptContent.replace("${name}", StringUtils.isNotEmpty(serviceSubtask.getName()) ? serviceSubtask.getName() : "");
        scriptContent.replace("${dzz}", StringUtils.isNotEmpty(serviceSubtask.getPlaceOfResidence()) ? serviceSubtask.getPlaceOfResidence() : "");
        scriptContent.replace("${phone}", StringUtils.isNotEmpty(serviceSubtask.getTelcode()) ? serviceSubtask.getTelcode() : "");
 
        return scriptContent;
    }
 
 
    /**
     * 获取任务里的通配符
     *
     * @param taskId
     * @return
     */
    private Map<String, Map<String, String>> getParam(Long taskId) {
        ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(taskId);
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Map<String, String>> serviceTaskMap = null;
        try {
            serviceTaskMap = objectMapper.readValue(serviceTask.getTextParam(), Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return serviceTaskMap;
    }
 
    private Map<String, String> delRedisValue(String cache, String id) {
        id = "," + id + ",";
        String val = "";
        String cacheName = "";
        Map<String, String> map = new HashMap<>();
 
        if (StringUtils.isNotEmpty(cache)) {
            val = redisCache.getCacheObject(cache);
            if (!StringUtils.isEmpty(val)) {
                if (val.contains(id)) {
                    val = val.replaceAll(id, "");
                }
            }
            map.put("val", val);
            map.put("cacheName", cache);
            return map;
        }
 
        for (int i = 0; i < 6; i++) {
            val = redisCache.getCacheObject("cache-0" + i);
            if (!StringUtils.isEmpty(val)) {
                if (val.contains(id)) {
                    val = val.replaceAll(id, "");
                    map.put("val", val);
                    map.put("cacheName", "cache-0" + i);
                }
            }
        }
        return map;
    }
 
 
    /**
     * 雨绮任务拉取
     *
     * @return
     */
    @Override
    public List<PullTaskVO> taskPull() {
        //pullTaskVOList用于数据返回
        List<PullTaskVO> pullTaskVOList = new ArrayList<>();
        String value0 = redisCache.getCacheObject("cache-0");
        //  cache-0为立即发起的,其它的先推迟
        if (!StringUtils.isEmpty(value0)) {
            pullTaskVOList = getPullTaskList(value0, "cache-0");
            //将cache-0的数据,转移不对劲cache-00中
            redisCache.deleteObject("cache-00");
            String cache00 = redisCache.getCacheObject("cache-00");
            if (!StringUtils.isEmpty(cache00))
                redisCache.setCacheObject("cache-00", cache00 + "," + value0, 120, TimeUnit.MINUTES);
            else redisCache.setCacheObject("cache-00", value0, 120, TimeUnit.MINUTES);
            redisCache.deleteObject("cache-0");
        } else {
            for (int i = 1; i < 6; i++) {
                //取出从cache-1 到 cache-5的第一个子任务信息
                String value = redisCache.getCacheObject("cache-" + i);
                if (StringUtils.isEmpty(value)) continue;
                List<PullTaskVO> pullTaskVOList2 = getPullTaskList(value, "cache-" + i);
                if (CollectionUtils.isNotEmpty(pullTaskVOList2) && pullTaskVOList2.size() > 0) {
                    pullTaskVOList.addAll(pullTaskVOList2);
                }
                //将cache-i的数据,转移不对劲cache-0i中
                String cache0i = redisCache.getCacheObject("cache-0" + i);
                if (!StringUtils.isEmpty(cache0i)) redisCache.setCacheObject("cache-0" + i, cache0i + "," + value);
                else redisCache.setCacheObject("cache-0" + i, value);
                redisCache.deleteObject("cache-" + i);
            }
        }
        if (CollectionUtils.isNotEmpty(pullTaskVOList)) {
            //给回调参数赋值
            for (PullTaskVO pullTaskVO : pullTaskVOList) {
                pullTaskVO.setAsrcallback(ASRCallBackPath);
            }
        }
        return pullTaskVOList;
    }
 
 
    private List<PullTaskVO> getPullTaskList(String subIds, String cacheName) {
        //pullTaskVOList用于数据返回
        List<PullTaskVO> pullTaskVOList = new ArrayList<>();
        //newValue0用于保存没有处理的子 任务
        String newValue0 = "";
        //根据,获取子任务的ID
        String[] split = subIds.split(",,");
        for (int i = 0; i < split.length; i++) {
            if (cacheName.equals("cache-0") && i < 5 || !cacheName.equals("cache-0") && i < 1) {
                PullTaskVO pullTaskVO = new PullTaskVO();
                String subId = split[i].trim().replace(",", "");
 
                ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(Long.valueOf(subId));
                IvrTaskTemplate ivrTaskTemplate = ivrTaskTemplateService.selectIvrTaskTemplateByID(serviceSubtask.getTemplateid());
                //通过任务模板中的"第一次问题编号"获取第一个问题;
                IvrTaskTemplateScript ivrTaskTemplateScript = null;
                IvrTaskTemplateScript ivrTaskTemplateScript1 = new IvrTaskTemplateScript();
                ivrTaskTemplateScript1.setTemplateID(ivrTaskTemplate.getId());
                List<IvrTaskTemplateScript> ivrTaskTemplateScripts = iIvrTaskTemplateScriptService.selectIvrTaskTemplateScriptList(ivrTaskTemplateScript1);
                aa:
                for (IvrTaskTemplateScript ivrTaskTemplateScript2 : ivrTaskTemplateScripts) {
                    if (ivrTaskTemplate.getFirstQuestionNum() == Long.valueOf(ivrTaskTemplateScript2.getSort())) {
                        ivrTaskTemplateScript = ivrTaskTemplateScript2;
                        break aa;
                    }
                }
                //如果ivrTaskTemplateScript为空,也就没有往下执行的必要了
                if (ObjectUtils.isEmpty(ivrTaskTemplateScript)) return null;
                //获取通配符匹配过后的问题
//                String scrContent = getObject(serviceSubtask, ivrTaskTemplateScript.getScriptContent());
//                String kcb = ivrTaskTemplate.getRevisitBefore() + "," + scrContent;
                String kcb = ivrTaskTemplate.getRevisitBefore();
 
                //封装返回数据
                //taskId = 子任务ID + 问题ID +问题序号
                pullTaskVO.setTaskid(subId);
                pullTaskVO.setAppkey("ZurNHpaQLq6P55YS");
                pullTaskVO.setSections(LocalTime.now().format(DateTimeFormatter.ofPattern("hh:mm")) + "-" + LocalTime.now().plusMinutes(1).format(DateTimeFormatter.ofPattern("hh:mm")));
                pullTaskVO.setPhones(serviceSubtask.getPhone());
                pullTaskVO.setPrologue(kcb);
                pullTaskVO.setDisplayNo("85129866");
                pullTaskVOList.add(pullTaskVO);
                redisCache.setCacheObject(subId.trim() + "-" + serviceSubtask.getPhone().trim(), ivrTaskTemplateScript.getId().toString());
                redisCache.setCacheObject(subId.trim() + "-" + serviceSubtask.getPhone().trim() + "-firstSort", 1, 120, TimeUnit.MINUTES);
            } else {
                if (StringUtils.isEmpty(newValue0)) {
                    newValue0 = "," + split[i].trim() + ",";
                } else {
                    newValue0 = newValue0 + "," + split[i].trim() + ",";
                }
                redisCache.setCacheObject(cacheName, newValue0);
            }
        }
 
        return pullTaskVOList;
    }
 
 
    //下面的代码不能删除,上面的方法只是配合电话端联调用的,
//    @Override
//    public PhoneCallBackVO phoneCallBack(PhoneCallBackVO phoneCallBackVO) {
//        log.error("phoneCallBackVO的入参:{},{},{},{},{},{},{}", phoneCallBackVO.getResultType(), phoneCallBackVO.getUuid(), phoneCallBackVO.getErrResult(), phoneCallBackVO.getTextResult(), phoneCallBackVO.getHangUpResult(), phoneCallBackVO.getEnumState(), phoneCallBackVO.getUint8());
//        //获取数据
//        Boolean aBoolean = redisCache.hasKey(phoneCallBackVO.getUuid());
//        if (!aBoolean) {
//            throw new BaseException("该uuid不存在");
//        }
//        Integer hangupValue = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "hangup");
//        if (hangupValue != null && hangupValue == 1) {
//            //hangupValue == 1  随访结束,直接可以挂电话
//            PhoneUtils phoneUtils = new PhoneUtils();
//            phoneUtils.hangup(phoneCallBackVO.getUuid(), null, null, null, null, null, null, null);
//
//        }
//
//        Map<String, Object> map = redisCache.getCacheObject(phoneCallBackVO.getUuid());
////        ObjectMapper objectMapper = new ObjectMapper();
////        Map<String, Object> map = null;
////        try {
////            map = objectMapper.readValue(cacheObject, Map.class);
////        } catch (JsonProcessingException e) {
////            e.printStackTrace();
////        }
//
//        ServiceSubtask ServiceSubtask = (ServiceSubtask) map.get("ServiceSubtask");
//        List<IvrLibaTemplateScriptVO> ivrLibaTemplateScriptVOs = (List<IvrLibaTemplateScriptVO>) map.get("ivrLibaTemplateScriptVO");
//        //将uuid更新到数据库中
//        ServiceSubtask.setSenduuid(phoneCallBackVO.getUuid());
//        ServiceSubtaskMapper.updateServiceSubtask(ServiceSubtask);
//
//        //获取模板信息
//        IvrLibaTemplateVO ivrLibaTemplateVO = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "ivrLibaTemplateVO");
//
//
//        //首先判断resultType
//        if (phoneCallBackVO.getResultType() == 1) {
//            //呼叫结果接口: 1
//            if (phoneCallBackVO.getUint8() == 1) {
//                //呼叫失败,去redis中记录一下失败次数,进行再次呼叫
//                Integer integer = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "uint8");
//                if (integer != null) {
//                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "uint8", integer + 1, 120, TimeUnit.MINUTES);
//                } else {
//                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "uint8", 1, 120, TimeUnit.MINUTES);
//                }
//
//                if (integer != null && integer == ServiceSubtask.getRecallcount().intValue()) {
//                    log.info("无人接听:{},   {}", phoneCallBackVO.getErrResult(), phoneCallBackVO.getUuid());
//                    //连续打规定次,如果要没人接,那就结束
//                    ServiceSubtask.setResult(phoneCallBackVO.getErrResult());
//                    ServiceSubtaskMapper.updateServiceSubtask(ServiceSubtask);
//                    redisCache.deleteObject(phoneCallBackVO.getUuid() + "uint8");
//                } else if (integer != null && integer < ServiceSubtask.getRecallcount().intValue()) {
//                    //进行重拨
//                    PhoneUtils phoneUtils = new PhoneUtils();
//                    phoneUtils.ob(null, null, null, null, null, null, null, ServiceSubtask.getPhone(), phoneCallBackVO.getUuid(), true);
//                }
//            }
//
//        } else if (phoneCallBackVO.getResultType() == 2) {
//            //通话状态更新接口: 2
//            if (phoneCallBackVO.getEnumState() == 0) {
//                // 0-振铃
//                Integer integer = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "enumState");
//
//                if (integer != null && integer < ServiceSubtask.getRecallcount().intValue()) {
//                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "enumState", integer + 1, 120, TimeUnit.MINUTES);
//                } else if (integer == null) {
//                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "enumState", 1, 120, TimeUnit.MINUTES);
//                } else if (integer != null && integer == ServiceSubtask.getRecallcount().intValue()) {
//                    ServiceSubtask.setResult("无人接听");
//                    ServiceSubtaskMapper.updateServiceSubtask(ServiceSubtask);
//                    redisCache.deleteObject(phoneCallBackVO.getUuid() + "enumState");
//                }
//            } else if (phoneCallBackVO.getEnumState() == 2) {
//                //患者挂断电话
//                log.info("患者挂断电话:{}", phoneCallBackVO.getUuid());
//                ServiceSubtask.setResult(phoneCallBackVO.getHangUpResult());
//                ServiceSubtaskMapper.updateServiceSubtask(ServiceSubtask);
//                redisCache.deleteObject(phoneCallBackVO.getUuid() + "enumState");
//            }
//
//
//        } else if (phoneCallBackVO.getResultType() == 3) {
//            //语音识别结果上报接口: 3
//            Integer noVoice = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "noVoice");
//            QuestionMessage returnQues = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "returnQues");
//            IvrLibaTemplateScriptVO nowQuestion = returnQues.getNowQuestion();
//            PhoneUtils phoneUtils = new PhoneUtils();
//
//            if (StringUtils.isEmpty(phoneCallBackVO.getTextResult())) {
//                //无回话
//                //判断noVoice是否已经到了最大值
//                if (noVoice == ivrLibaTemplateVO.getNoVoiceNum().intValue()) {
//                    //已经问了对应的遍数,就判断是否还有下一题
//                    if (nowQuestion.getTargetid() == ivrLibaTemplateScriptVOs.size()) {
//                        //没有下一题了,就挂断电话,播放结束语
//                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "hangup", 1, 120, TimeUnit.MINUTES);
//                        phoneUtils.ttsPlayback(ivrLibaTemplateVO.getRevisitAfter(), phoneCallBackVO.getUuid());
//                    } else {
//                        //有下一题
//                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", 0, 120, TimeUnit.MINUTES);
//                        IvrLibaTemplateScriptVO nextQuestion = getNextQuestion(ivrLibaTemplateScriptVOs, nowQuestion);
//                        // 问题,  去调用“tts合成和播放”接口
//                        phoneUtils.ttsPlayback(nowQuestion.getQuestionText(), phoneCallBackVO.getUuid());
//                    }
//                } else {
//                    redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", noVoice + 1, 120, TimeUnit.MINUTES);
//                    //调用ivrLibaTemplateScriptVO中的slienceText(静默话术)
//                    String slienceText = nowQuestion.getSlienceText();
//                    //静默话术  + 问题,  去调用“tts合成和播放”接口
//                    phoneUtils.ttsPlayback(slienceText + nowQuestion.getQuestionText(), phoneCallBackVO.getUuid());
//                    return new PhoneCallBackVO();
//                }
//
//            } else {
//                //有回话,对回答的问题,进行正则匹配(这里只针对选择题,其它题型不行)
//                for (int j = 0; j < nowQuestion.getIvrLibaScriptTargetoptionList().size(); j++) {
//                    //包含
//                    Matcher matcher = null;
//                    if (StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex())) {
//                        Pattern pattern = Pattern.compile(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex());
//                        matcher = pattern.matcher(phoneCallBackVO.getTextResult());
//                    }
//                    //不包含
//                    Matcher matcher2 = null;
//                    if (StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2())) {
//                        Pattern pattern2 = Pattern.compile(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2());
//                        matcher2 = pattern2.matcher(phoneCallBackVO.getTextResult());
//                    }
//                    log.info("phoneCallBack--Targetregex的值为:{}, phoneCallBack--Targetregex2的值为:{}", nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex(), nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2());
//                    if (StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches() && StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex()) && StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2()) && matcher2.matches() || StringUtils.isEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex2()) && StringUtils.isNotEmpty(nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getTargetregex()) && matcher.matches()) {
//                        //说明匹配正确了
//                        //这里应该先判断类型,去再修改,设置IsUserOperation是单选题的改法
//                        nowQuestion.getIvrLibaScriptTargetoptionList().get(j).setIsUserOperation(true);
//                        ivrLibaScriptTargetoptionMapper.updateIvrLibaTemplateTargetoption(nowQuestion.getIvrLibaScriptTargetoptionList().get(j));
//
//
//                        //将静默置为0
//                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "noVoice", 0, 120, TimeUnit.MINUTES);
//                        redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", 0, 120, TimeUnit.MINUTES);
//                        //获取下一题
//                        Integer nextQuestion = nowQuestion.getIvrLibaScriptTargetoptionList().get(j).getNextQuestion();
//                        for (IvrLibaTemplateScriptVO script : ivrLibaTemplateScriptVOs) {
//                            if (script.getTargetid() == nextQuestion) {
//                                QuestionMessage questionMessage = new QuestionMessage();
//                                questionMessage.setNowQuestion(script);
//                                questionMessage.setQuestionList(ivrLibaTemplateScriptVOs);
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
//                                break;
//                            }
//                        }
//                        break;
//                    } else {
//                        //没有匹配到
//                        Integer mateNum = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "mateNum");
//                        //无匹配次数去判断是否到最大询问次数,并且所有的选项都匹配完了
//                        if (mateNum == ivrLibaTemplateVO.getMateNum().intValue() && j == nowQuestion.getIvrLibaScriptTargetoptionList().size() - 1) {
//                            //如果下一题为空.则新的数据返回,并加上感谢语
//                            if (nowQuestion.getTargetid() < ivrLibaTemplateScriptVOs.size()) {
//                                QuestionMessage questionMessage = new QuestionMessage();
//                                IvrLibaTemplateScriptVO nextQuestion = getNextQuestion(ivrLibaTemplateScriptVOs, nowQuestion);
//                                questionMessage.setQuestionList(ivrLibaTemplateScriptVOs);
//                                questionMessage.setNowQuestion(nextQuestion);
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", 0, 120, TimeUnit.MINUTES);
//                            } else {
//                                //就可以挂断电话了
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "hangup", 1, 120, TimeUnit.MINUTES);
//                                phoneUtils.ttsPlayback(ivrLibaTemplateVO.getRevisitAfter(), phoneCallBackVO.getUuid());
//                                break;
//                            }
//                        } else if (mateNum < ivrLibaTemplateVO.getMateNum().intValue() && j == nowQuestion.getIvrLibaScriptTargetoptionList().size() - 1) {
//                            //没有问到规定次数
//                            mateNum = mateNum + 1;
//                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "mateNum", mateNum, 120, TimeUnit.MINUTES);
//                        }
//                    }
//
//                }
//                //选项匹配完成后,需要再去通过库再进行匹配一次
//                String extemplateID = ivrLibaTemplateVO.getSubmoduleID();
//                String[] split = extemplateID.split(",");
//                List<String> list = Arrays.asList(split);
//                List<Long> list1 = new ArrayList<>();
//                if (StringUtils.isNotEmpty(extemplateID)) {
//                    for (String str : list) {
//                        list1.add(Long.valueOf(str));
//                    }
//                    List<IvrLibaExtemplatescript> ivrLibaExtemplatescripts = ivrLibaExtemplatescriptMapper.queryIvrLibaExtemplatescriptList(list1);
//                    for (IvrLibaExtemplatescript ivrLibaExtemplatescript : ivrLibaExtemplatescripts) {
//                        Matcher matcher = null;
//                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex())) {
//                            Pattern pattern = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex());
//                            matcher = pattern.matcher(returnQues.getContent());
//                        }
//
//                        Matcher matcher2 = null;
//                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2())) {
//                            Pattern pattern2 = Pattern.compile(ivrLibaExtemplatescript.getSelfRegex2());
//                            matcher2 = pattern2.matcher(returnQues.getContent());
//                        }
//                        log.info("++++++++++++++++++++++++++通用库是否为空:selfRegex : {} , selfRegex2 : {}", ivrLibaExtemplatescript.getSelfRegex(), ivrLibaExtemplatescript.getSelfRegex2());
//                        if (StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches() && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && matcher2.matches() || StringUtils.isEmpty(ivrLibaExtemplatescript.getSelfRegex2()) && StringUtils.isNotEmpty(ivrLibaExtemplatescript.getSelfRegex()) && matcher.matches()) {
//                            QuestionMessage questionMessage = redisCache.getCacheObject(phoneCallBackVO.getUuid() + "returnQues");
//                            IvrLibaTemplateScriptVO ivrLibaTemplateScriptVO = returnQues.getNowQuestion();
//                            ivrLibaTemplateScriptVO.setSubmoduleText(ivrLibaExtemplatescript.getSwitchText());
//                            ivrLibaTemplateScriptVO.setSubmoduleVoice(ivrLibaExtemplatescript.getSwitchWav());
//                            redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
//                            if (ivrLibaExtemplatescript.getIsEnd() == 1) {
//                                //将问题置空
//                                IvrLibaTemplateScriptVO nowQuestion1 = questionMessage.getNowQuestion();
//                                nowQuestion1.setQuestionText(null);
//                                nowQuestion1.setQuestionVoice(null);
//                                questionMessage.setNowQuestion(nowQuestion1);
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "returnQues", questionMessage, 120, TimeUnit.MINUTES);
//
//                                redisCache.setCacheObject(phoneCallBackVO.getUuid() + "isOver", 1, 120, TimeUnit.MINUTES);
//                            }
//
//                            //调用“15、tts合成和播放, tts_playback”将结果传回
//
//
//                        }
//                        break;
//                    }
//                }
//
//            }
//        }
//        return phoneCallBackVO;
//    }
 
    @Override
    public Integer saveQuestionAnswerPhone(ServiceSubTaskDetailReq serviceSubTaskDetailReq) {
        int i = 0;
        if (StringUtils.isNotEmpty(serviceSubTaskDetailReq.getParam1())) {
            RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
            Long tid = Long.valueOf(rsaPublicKeyExample.decryptedData(serviceSubTaskDetailReq.getParam1(), pri_key));
            Long pid = Long.valueOf(rsaPublicKeyExample.decryptedData(serviceSubTaskDetailReq.getParam2(), pri_key));
            ServiceSubtaskVO ivrTaskSingle = new ServiceSubtaskVO();
            ivrTaskSingle.setTaskid(tid);
            ivrTaskSingle.setPatid(pid);
            List<ServiceSubtask> selectServiceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(ivrTaskSingle);
            if (CollectionUtils.isEmpty(selectServiceSubtaskList) || selectServiceSubtaskList.size() == 0) {
                log.error("报错了,selectServiceSubtaskList数据为空了:{}", ivrTaskSingle);
                return 0;
            }
            //随访
            for (ServiceSubtaskDetail serviceSubtaskDetail : serviceSubTaskDetailReq.getServiceSubtaskDetailList()) {
                serviceSubtaskDetail.setSubId(selectServiceSubtaskList.get(0).getId());
                serviceSubtaskDetail.setId(UUID.randomUUID().toString());
                serviceSubtaskDetail.setCreateTime(new Date());
                i = serviceSubtaskDetailMapper.insertServiceSubtaskDetail(serviceSubtaskDetail);
            }
        } else {
            for (ServiceSubtaskDetail serviceSubtaskDetail : serviceSubTaskDetailReq.getServiceSubtaskDetailList()) {
                serviceSubtaskDetail.setCreateTime(new Date());
                i = serviceSubtaskDetailMapper.insertServiceSubtaskDetail(serviceSubtaskDetail);
            }
        }
        return i;
    }
 
    @Override
    public List<ServiceSubtaskCount> getSfFzInfoEveryMonth(ServiceSubtaskCountReq serviceSubtaskCountReq) {
        if (serviceSubtaskCountReq.getUserId() == null) {
            Long userId = SecurityUtils.getUserId();
            serviceSubtaskCountReq.setUserId(userId);
        }
        if (serviceSubtaskCountReq.getStartTime() == null) {
            LocalDate of = LocalDate.of(LocalDate.now().getYear(), 1, 1);
            serviceSubtaskCountReq.setStartTime(Date.from(of.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        }
        if (serviceSubtaskCountReq.getEndTime() == null) {
            serviceSubtaskCountReq.setEndTime(new Date());
        }
        List<ServiceSubtaskCount> sfFzInfoEveryMonth = serviceSubtaskMapper.getSfFzInfoEveryMonth(serviceSubtaskCountReq);
        List<ServiceSubtaskCount> result = sfFzInfoEveryMonth.stream().collect(Collectors.groupingBy(ServiceSubtaskCount::getMonth, Collectors.groupingBy(ServiceSubtaskCount::getServiceType, Collectors.summingLong(ServiceSubtaskCount::getCount)))).entrySet().stream().flatMap(monthEntry -> monthEntry.getValue().entrySet().stream().map(typeEntry -> new ServiceSubtaskCount(monthEntry.getKey(), typeEntry.getKey(), typeEntry.getValue()))).collect(Collectors.toList());
 
        List<ServiceSubtaskCount> result2 = new ArrayList<>();
        //根据服务类型进行筛选
        if (CollectionUtils.isNotEmpty(serviceSubtaskCountReq.getServiceType())) {
            for (ServiceSubtaskCount serviceSubtaskCount : result) {
                for (Long type : serviceSubtaskCountReq.getServiceType()) {
                    if (type == serviceSubtaskCount.getServiceType()) {
                        result2.add(serviceSubtaskCount);
                    }
                }
            }
        } else {
            result2 = result;
        }
        return result2;
    }
 
    private IvrTaskTemplateScriptVO getNextQuestion(List<IvrTaskTemplateScriptVO> IvrTaskTemplateScriptVOList, IvrTaskTemplateScriptVO IvrTaskTemplateScriptVO) {
 
        for (int j = 0; j < IvrTaskTemplateScriptVOList.size(); j++) {
            if (IvrTaskTemplateScriptVOList.get(j).getTargetid() == IvrTaskTemplateScriptVO.getTargetid() + 1) {
                // 对该条templateScriptVO进行处理
                return IvrTaskTemplateScriptVOList.get(j);
            }
        }
        return null;
    }
 
 
    private ServiceSubtaskDetail getServiceSubtaskDetail(PhoneCallReqYQVO phoneCallReqYQVO, IvrTaskTemplateScriptVO ivrTaskTemplateScriptVO, ServiceSubtask serviceSubtask, IvrTaskTemplate ivrTaskTemplate) {
        ServiceSubtaskDetail serviceSubtaskDetail = new ServiceSubtaskDetail();
        serviceSubtaskDetail.setSubId(Long.valueOf(phoneCallReqYQVO.getTaskid()));
        serviceSubtaskDetail.setUuid(phoneCallReqYQVO.getUuid());
        serviceSubtaskDetail.setPhone(phoneCallReqYQVO.getPhone());
        serviceSubtaskDetail.setOperate(serviceSubtask.getCreateBy());
        serviceSubtaskDetail.setDisplayno(phoneCallReqYQVO.getPhone());
        serviceSubtaskDetail.setAssigntime(System.currentTimeMillis());
        serviceSubtaskDetail.setStarttime(System.currentTimeMillis());
        serviceSubtaskDetail.setAnswertime(System.currentTimeMillis());
        serviceSubtaskDetail.setAsrtext("无应答");
        if (StringUtils.isNotEmpty(phoneCallReqYQVO.getAsrtext()))
            serviceSubtaskDetail.setAsrtext(phoneCallReqYQVO.getAsrtext());
        serviceSubtaskDetail.setBeginTime(System.currentTimeMillis());
        serviceSubtaskDetail.setEndTime(System.currentTimeMillis());
        serviceSubtaskDetail.setSentEnd(1L);
        serviceSubtaskDetail.setTemplateid(ivrTaskTemplate.getId().toString());
        serviceSubtaskDetail.setTemplatequestionnum(ivrTaskTemplateScriptVO.getId());
        serviceSubtaskDetail.setQuestiontext(ivrTaskTemplateScriptVO.getScriptContent());
        serviceSubtaskDetail.setCategoryname(ivrTaskTemplateScriptVO.getScriptType());
        serviceSubtaskDetail.setTargetoptions(ivrTaskTemplateScriptVO.getTargetOptions());
 
        int i = 1;
        for (IvrTaskTemplateTargetoption ivrTaskTemplateTargetoption : ivrTaskTemplateScriptVO.getIvrTaskScriptTargetoptionList()) {
            if (ivrTaskTemplateTargetoption.getIsUserOperation() == 1) {
                serviceSubtaskDetail.setMatchedtext(ivrTaskTemplateTargetoption.getTargetvalue());
            }
            serviceSubtaskDetail.setTargetvalue(StringUtils.isEmpty(serviceSubtaskDetail.getTargetvalue()) ? i + ivrTaskTemplateTargetoption.getTargetvalue() : serviceSubtaskDetail.getTargetvalue() + "  " + (i + 1) + ivrTaskTemplateTargetoption.getTargetvalue());
        }
 
        serviceSubtaskDetail.setAddtime(new Date());
        serviceSubtaskDetail.setIsupload(0L);
        serviceSubtaskDetail.setUploadTime(new Date());
        serviceSubtaskDetail.setDelFlag("0");
        serviceSubtaskDetail.setValueType(ivrTaskTemplateScriptVO.getScriptType());
        return serviceSubtaskDetail;
    }
 
}