郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.safeCheck.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountGroupService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountUserService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.*;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.safeCheck.common.RocketMQTemplateHelper;
import com.gkhy.safePlatform.safeCheck.entity.*;
import com.gkhy.safePlatform.safeCheck.model.dto.req.*;
import com.gkhy.safePlatform.safeCheck.model.dto.resp.*;
import com.gkhy.safePlatform.safeCheck.model.query.MobileTaskDataDBQuery;
import com.gkhy.safePlatform.safeCheck.mq.msg.SafeCheckSmartScreenDataPushMsg;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckMinioAccessService;
import com.gkhy.safePlatform.safeCheck.service.baseService.*;
import com.gkhy.safePlatform.safeCheck.enums.*;
import com.gkhy.safePlatform.safeCheck.model.query.MobileUserTaskDBQuery;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckSmartScreenService;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckTaskMobileManagerService;
import com.gkhy.safePlatform.safeCheck.util.SendMessageUtil;
import com.gkhy.safePlatform.safeCheck.util.UserInfoUtil;
import javafx.scene.input.DataFormat;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
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
public class SafeCheckTaskMobileManagerServiceImpl implements SafeCheckTaskMobileManagerService {
 
 
    @DubboReference(check = false)
    private AccountAuthService accountAuthService;
 
    @DubboReference(check = false)
    private AccountGroupService accountGroupService;
 
    @DubboReference(check = false)
    private AccountUserService accountUserService;
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private RocketMQTemplateHelper rocketMQTemplateHelper;
 
    @Value("${rocketmq.topic.safeCheckSmartScreenDataPushTopic}")
    private String safeCheckSmartScreenDataPushTopic;
 
    @Autowired
    private SafeCheckTaskService safeCheckTaskService;
 
    @Autowired
    private SafeCheckRfidService safeCheckRfidService;
 
//    @Autowired
//    private SafeCheckWebSocketServer webSocketServer;
 
    @Autowired
    private AbnormalWorkOrderService abnormalWorkOrderService;
 
    @Autowired
    private SendMessageUtil sendMessageUtil;
 
    @Autowired
    private AbnormalWorkOrderImagesService abnormalWorkOrderImagesService;
 
    @Autowired
    private SafeCheckSmartScreenService smartScreenService;
 
    @Autowired
    private SafeCheckMinioAccessService minioAccessService;
 
    @Autowired
    private SafeCheckTaskAndQuotaService safeCheckTaskAndQuotaService;
 
 
    /**
     * @description 查询用户所属的班组,用户上班时间的任务信息
     */
    @Transactional
    @Override
    public IPage listUserTaskByPage(ContextCacheUser currentUser,Page pageInfo, SafeCheckMobilePageReqDTO safeCheckMobilePageReqDTO) {
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
        Long uid = currentUser.getUid();
        ResultVO<List<GroupRPCRespDTO>> listResultVO = accountGroupService.listGroupInfoByUid(uid);
        List<GroupRPCRespDTO> groupInfo = (List<GroupRPCRespDTO>) listResultVO.getData();
        if (CollectionUtils.isEmpty(groupInfo)){
            return null;
        }
        //String groupName = groupInfo.getGroupName();
        List<Long> execClassgroupids = groupInfo.stream().map(e -> e.getGroupId()).collect(Collectors.toList());
        Map<Long, String> stringMap = groupInfo.stream().collect(Collectors.toMap(GroupRPCRespDTO::getGroupId, GroupRPCRespDTO::getGroupName));
        //LocalDate now = LocalDate.now();
        //ResultVO<UserGroupTimTableRPCRespDTO> timeTable = accountUserService.getUserGroupWorkTimeTable(uid, now, now);
        //UserGroupTimTableRPCRespDTO data = (UserGroupTimTableRPCRespDTO) timeTable.getData();
        //List<UserTimeTableRPCRespDTO> timeDetails = data.getTimeDetails();
        //
        //if (timeDetails == null || timeDetails.size()==0){
        //    return null;
        //}
        ////todo 获取当天的第一个排班
        //UserTimeTableRPCRespDTO userTimeTable = timeDetails.get(0);
        //LocalDateTime startTime = userTimeTable.getStartTime();
        //LocalDateTime endTime = userTimeTable.getEndTime();
 
        MobileUserTaskDBQuery taskDBQuery = new MobileUserTaskDBQuery();
        taskDBQuery.setExecClassgroupId(execClassgroupids);
        //taskDBQuery.setStartTime(startTime);
        //taskDBQuery.setEndTime(endTime);
        taskDBQuery.setTaskStatus(safeCheckMobilePageReqDTO.getTaskStatus());
        taskDBQuery.setTaskClaim(safeCheckMobilePageReqDTO.getTaskClaim());
        taskDBQuery.setTaskType(safeCheckMobilePageReqDTO.getTaskType());
        IPage taskIPage = safeCheckTaskService.listUserTaskByPage(pageInfo, taskDBQuery);
        List<SafeCheckTaskMobileDO> records = taskIPage.getRecords();
        if (records == null || records.size() == 0 ){
            return null;
        }
        List<SafeCheckTaskMobilePageRespDTO> taskMobilePages = records.stream().map((record)->{
            SafeCheckTaskMobilePageRespDTO pageRespDTO = new SafeCheckTaskMobilePageRespDTO();
            BeanUtils.copyProperties(record,pageRespDTO);
            List<SafeCheckTaskAndQuota> points = record.getPoints();
            if (points != null && points.size() > 0){
                List<SafeCheckTaskAndQuotaMobileRespDTO> mobileRespDTOS = points.stream().map((point)->{
                    SafeCheckTaskAndQuotaMobileRespDTO mobileRespDTO = new SafeCheckTaskAndQuotaMobileRespDTO();
                    BeanUtils.copyProperties(point,mobileRespDTO);
                    return mobileRespDTO;
                }).collect(Collectors.toList());
                pageRespDTO.setPoints(mobileRespDTOS);
            }
            pageRespDTO.setExecClassgroup(stringMap.get(record.getExecClassgroupId()));
            return pageRespDTO;
        }).collect(Collectors.toList());
        taskIPage.setRecords(taskMobilePages);
        return taskIPage;
    }
 
    /**
     * @description 用户认领任务
     */
    @Override
    public void updateTaskClaimById(ContextCacheUser currentUser, Long taskId) {
        if (taskId == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务id不能为空");
        }
 
        //0、加分布式锁
        String lockName = "SAFECHECK_CLAIM_TASK_"+taskId;
        RLock claimTaskLock = redissonClient.getLock(lockName);
        claimTaskLock.lock(3, TimeUnit.SECONDS);
 
        SafeCheckTask task = safeCheckTaskService.getTaskById(taskId);
        if (task == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"任务不存在");
        }
        if (task.getTaskClaim().intValue() == 2){
            throw new AusinessException(E.DATA_DATABASE_EXIST,"任务已被认领");
        }
        task.setTaskClaimTime(new Date());
        task.setExecUserId(currentUser.getUid());
        task.setExecUserName(currentUser.getRealName());
        task.setTaskClaim(TaskClaimStatusEnum.TASK_CLAIM_STATUS_YES.getCode());
        task.setEnterpriseId(currentUser.getDepId());
        safeCheckTaskService.updateTaskClaimById(task);
 
        //1、释放分布式锁
        claimTaskLock.unlock();
    }
 
    /**
     * @description 用户单个提交巡检点巡检结果
     */
    @Transactional
    @Override
    public void updateTaskAndQuotaResultById(ContextCacheUser currentUser, SafeCheckTaskAndQuotaSubmitReqDTO safeCheckTaskAndQuotaSubmitReqDTO) {
        if (safeCheckTaskAndQuotaSubmitReqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        SafeCheckTaskAndQuota safeCheckTaskAndQuota = new SafeCheckTaskAndQuota();
        BeanUtils.copyProperties(safeCheckTaskAndQuotaSubmitReqDTO,safeCheckTaskAndQuota);
        List<String> images = safeCheckTaskAndQuotaSubmitReqDTO.getImages();
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
        Long uid = currentUser.getUid();
        this.taskAndQuotaResultParamCheck(safeCheckTaskAndQuota);
 
        int taskAndQuotaId = safeCheckTaskAndQuota.getId();
        SafeCheckTaskAndQuota referenceTaskAndQuota = safeCheckTaskAndQuotaService.getTaskAndQuotaById(taskAndQuotaId);
        if (referenceTaskAndQuota == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"数据库无此巡检任务生成的巡检点信息");
        }
 
        //提交用户必须是执行用户
        SafeCheckTask task = safeCheckTaskService.getTaskById(safeCheckTaskAndQuota.getTaskId());
        if (task == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"任务不存在");
        }
        if (!task.getExecUserId().equals(uid)){
            throw new AusinessException(E.UPDATE_FAIL,"不是任务认领人,无法提交巡检点巡检结果");
        }
 
        Byte reportResult = referenceTaskAndQuota.getReportResult();
        if (reportResult != null){
            throw new AusinessException(E.UPDATE_FAIL,"巡检点巡检结果已经提交,请勿重复提交");
        }
        Byte dataReportType = safeCheckTaskAndQuota.getDataReportType();
 
        SafeCheckTaskAndQuota resultTaskAndQuota = null;
        if (dataReportType.intValue() == 1){
            resultTaskAndQuota = this.optResultIsAbnormal(safeCheckTaskAndQuota, referenceTaskAndQuota);
        }
        if (dataReportType.intValue() == 2){
            resultTaskAndQuota = this.fillBlankResultIsAbnormal(safeCheckTaskAndQuota, referenceTaskAndQuota);
        }
        if (dataReportType.intValue() == 3){
            resultTaskAndQuota = this.optResultIsAbnormal(safeCheckTaskAndQuota, referenceTaskAndQuota);
            //选择的结果(0-正常;1-异常;3-备)
            Byte optResult = resultTaskAndQuota.getReportResult();
            //填空的结果(0-正常;1-异常)
            resultTaskAndQuota = this.fillBlankResultIsAbnormal(safeCheckTaskAndQuota, referenceTaskAndQuota);
            Byte blankResult = resultTaskAndQuota.getReportResult();
            if (TaskResultEnum.TASK_RESULT_BEI.getCode().equals(optResult)){
                if (TaskResultEnum.TASK_RESULT_NORMAL.getCode().equals(blankResult)){
                    resultTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                }else {
                    resultTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                }
 
            }else {
                if (optResult.intValue() != TaskResultEnum.TASK_RESULT_NORMAL.getCode().intValue()
                        || blankResult.intValue() != TaskResultEnum.TASK_RESULT_NORMAL.getCode().intValue()) {
                    resultTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                } else {
                    resultTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                }
            }
        }
        Byte rfidPosition = resultTaskAndQuota.getRfidPosition();
        if (rfidPosition == RfidPositionEnum.RFID_POSITION_YES.getCode()){
            String rfid = resultTaskAndQuota.getRfid();
            Long taskId = resultTaskAndQuota.getTaskId();
            //将任务关联的巡检点rfid相同的数据rfid定位状态都改为已定位
            int countResult = safeCheckTaskAndQuotaService.countRfidSameByTaskId(taskId, rfid);
            int updateResult = safeCheckTaskAndQuotaService.updateRfidPositionStatusByTaskIdAndRfid(taskId, rfid, RfidPositionEnum.RFID_POSITION_YES.getCode());
            if (countResult != updateResult){
                throw new AusinessException(E.UPDATE_FAIL,"部分巡检点定位状态修改失败");
            }
        }
        resultTaskAndQuota.setPointCheckStatus(PointCheckStatusEnum.POINT_CHECK_STATUS_FINISHED.getCode());
        //最后将巡检点检查结果信息进行修改
        safeCheckTaskAndQuotaService.updatePointCheckResultByIdAndTaskId(resultTaskAndQuota);
        if (TaskResultEnum.TASK_RESULT_UNUSUAL.getCode().equals(resultTaskAndQuota.getReportResult())){
            //如果结果异常走异常处理流程
            workOrderExceptionHandlingProcess(uid,resultTaskAndQuota,images);
            //异常发送短信
            workOrderExceptionSendMesProcess(task,resultTaskAndQuota);
        }
 
 
//        整个巡检链数据发过去
//        Long taskId = safeCheckTaskAndQuota.getTaskId();
//        List<SafeCheckTaskAndQuota> taskAndQuotas = safeCheckTaskAndQuotaService.listTaskAndQuotaByTaskId(taskId);
//        if (taskAndQuotas == null && taskAndQuotas.size() ==0){
//            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"当前任务无关联巡检链");
//        }
//        List<SafeCheckSmartScreenRepsDTO>  smartScreenData = taskAndQuotas.stream().map((taskAndQuota)->{
//            SafeCheckSmartScreenRepsDTO smartScreenRepsDTO = new SafeCheckSmartScreenRepsDTO();
//            BeanUtils.copyProperties(taskAndQuota,smartScreenRepsDTO);
//            return smartScreenRepsDTO;
//        }).collect(Collectors.toList());
 
//        根据任务id以及巡检链中对应的巡检点id,推送一条巡检记录
//        SafeCheckTaskAndQuota taskAndQuota = safeCheckTaskAndQuotaService.getTaskAndQuotaByIdAndTaskId(resultTaskAndQuota.getId(),referenceTaskAndQuota.getTaskId());
//        SafeCheckSmartScreenRepsDTO smartScreenRepsDTO = new SafeCheckSmartScreenRepsDTO();
//        BeanUtils.copyProperties(taskAndQuota,smartScreenRepsDTO);
//        String msg = JsonUtils.toJson(smartScreenRepsDTO);
 
 
//        首先判断当前的任务id是否已经连接到websocket todo 之前使用的直推
//        String taskId = Long.toString(referenceTaskAndQuota.getTaskId());
//        if (SafeCheckWebSocketServer.taskIsconnectByTaskId(taskId)){
//            List<SafeCheckTaskAndQuota> taskAndQuotas = safeCheckTaskAndQuotaService.listTaskAndQuotaByTaskId(referenceTaskAndQuota.getTaskId());
//            List<SafeCheckSmartScreenRepsDTO> screenDataByTaskId = SafeCheckSmartScreenServiceImpl.getSmartScreenDataByTaskId(taskAndQuotas);
//            String msg = JsonUtils.toJson(screenDataByTaskId);
//            //调用websocket发送消息
//            try {
//                webSocketServer.sendInfo(msg,taskId);
//            }catch (Exception e){
//                throw new AusinessException(E.DATA_STATUS_NOT_EXIST,"websocket消息fa发送失败");
//            }
 
//        todo 现在直接是把消息发给mq 由wenscoket上的消费者监听到消息后推送消息
        //1、mq发送生成推送消息
        SafeCheckSmartScreenDataPushMsg screenDataPushMsg = new SafeCheckSmartScreenDataPushMsg();
        screenDataPushMsg.setTaskId(referenceTaskAndQuota.getTaskId());
        //先提交后再推送消息
        rocketMQTemplateHelper.syncSend(safeCheckSmartScreenDataPushTopic,screenDataPushMsg);
//            Runnable runnable = new Runnable(){
//                @Override
//                public void run() {
//                    while (true) {
//                        System.out.println("【websocket】"+new Date()+ "定时发送消息中.......");
//                        try {
//                            webSocketServer.sendInfo(msg,taskId);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                        try {
//                            Thread.sleep(10000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            };
//            Thread thread = new Thread(runnable);
//            thread.start();
//        }
    }
 
    /**
     * @description 异常工单发送短信
     */
    private void workOrderExceptionSendMesProcess(SafeCheckTask task,SafeCheckTaskAndQuota safeCheckTaskAndQuota){
        String taskName = task.getTaskName();
        SafeCheckRfid rfid = safeCheckRfidService.getRfidById(safeCheckTaskAndQuota.getRfidId(), DelectStatusEnum.DELECT_NO.getStatus());
        if (rfid != null){
            ResultVO<UserInfoRPCRespDTO> info = accountUserService.getUserInfoByUid(rfid.getExceptionHandlerId());
            UserInfoRPCRespDTO data = (UserInfoRPCRespDTO) info.getData();
            if (data != null && StringUtils.isNotBlank(data.getPhone())){
                String[] phone = {data.getPhone()};
                Map<String, String> map = new HashMap<>();
                map.put("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒")));
                map.put("task", taskName);
                map.put("rfid", safeCheckTaskAndQuota.getRfidName());
                map.put("point", safeCheckTaskAndQuota.getPoint());
                sendMessageUtil.sendMessageCheck(phone,map);
            }
        }
    }
 
    /**
     * 工单异常处理流程
     */
    private void workOrderExceptionHandlingProcess(Long uid,SafeCheckTaskAndQuota resultTaskAndQuota,List<String> images) {
        ResultVO<UserInfoRPCRespDTO> userInfoByUid = accountUserService.getUserInfoByUid(uid);
        UserInfoRPCRespDTO userInfo = (UserInfoRPCRespDTO) userInfoByUid.getData();
        AbnormalWorkOrder order = new AbnormalWorkOrder();
        SafeCheckTaskAndQuota taskAndQuotaById = safeCheckTaskAndQuotaService.getTaskAndQuotaById(resultTaskAndQuota.getId());
        if (taskAndQuotaById == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"巡检点记录不存在");
        }
        Date date = new Date();
        WorkOrderRelatedDataDO workOrderRelatedDataDO = safeCheckTaskAndQuotaService.getWorkOrderRelatedData(resultTaskAndQuota.getId());
        BeanUtils.copyProperties(workOrderRelatedDataDO,order,"id");
        order.setFirstReferenceResult(resultTaskAndQuota.getFirstReferenceResult());
        order.setSecondReferenceResult(resultTaskAndQuota.getSecondReferenceResult());
        order.setTaskAndQuotaId(workOrderRelatedDataDO.getId());
        order.setWorkOrderTime(date);
 
 
        //0、加分布式锁
        String lockName = "SAFECHECK_WORK_ORDER_EXCEPTION";
        RLock workOrderExceptionLock = redissonClient.getLock(lockName);
        workOrderExceptionLock.lock(3, TimeUnit.SECONDS);
 
        GetLastWorkOrderSortDO getLastWorkOrderSortDO = abnormalWorkOrderService.getLastWorkOrderSort();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        if (getLastWorkOrderSortDO == null){
            order.setWorkOrderSort(1L);
        }else {
            //todo: 增加了非空判断,但空值处理未实现
            if(date != null && getLastWorkOrderSortDO != null && getLastWorkOrderSortDO.getWorkOrderTime() != null){
                if (fmt.format(date).equals(fmt.format(getLastWorkOrderSortDO.getWorkOrderTime()))){
                    order.setWorkOrderSort(getLastWorkOrderSortDO.getWorkOrderSort()+1);
                }else {
                    order.setWorkOrderSort(1L);
                }
            }
        }
        order.setOccurrenceTime(date);
        SafeCheckTask task = safeCheckTaskService.getTaskById(workOrderRelatedDataDO.getTaskId());
        if (task == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"任务不存在");
        }
        order.setTaskName(task.getTaskName());
        order.setExecDepId(task.getExecDepId());
        order.setExecClassgroupId(task.getExecClassgroupId());
        ResultVO<UserInfoRPCRespDTO> infoByUid = accountUserService.getUserInfoByUid(workOrderRelatedDataDO.getExceptionHandlerId());
        UserInfoRPCRespDTO userInfoRPCRespDTO = (UserInfoRPCRespDTO) infoByUid.getData();
        if (userInfoRPCRespDTO != null){
            order.setHiddenDangerHandlerId(userInfoRPCRespDTO.getUid());
            order.setHiddenDangerHandlerName(userInfoRPCRespDTO.getRealName());
            order.setHiddenDangerHandlerPhone(userInfoRPCRespDTO.getPhone());
        }
        order.setHandlerStatus(ExceptionHandleStatusEnum.TO_BE_RESPONDED.getStatus());
        order.setGmtCreate(date);
        order.setGmtModitify(date);
        order.setCreateUserName(userInfo.getRealName());
        order.setLastEditUserName(userInfo.getRealName());
        abnormalWorkOrderService.save(order);
        Long id = order.getId();
        if (CollectionUtils.isNotEmpty(images)){
            List<AbnormalWorkOrderImages> workOrderImages = new ArrayList<>();
            for (String image : images) {
                AbnormalWorkOrderImages orderImages = new AbnormalWorkOrderImages();
                orderImages.setAbnormalImage(image);
                orderImages.setAbnormalWorkOrderId(id);
                orderImages.setTaskAndQuotaId(resultTaskAndQuota.getId());
                orderImages.setGmtCreate(date);
                orderImages.setCreateUserName(userInfo.getRealName());
                orderImages.setImageTimeStatus(ImageTimeStatusEnum.BEFORE_PROCESSING.getStatus());
                orderImages.setCreateUserId(uid);
                workOrderImages.add(orderImages);
            }
            abnormalWorkOrderImagesService.saveBatch(workOrderImages);
        }
        //1、释放分布式锁
        workOrderExceptionLock.unlock();
    }
 
    /**
     * @description 最终任务提交:是否还有rfid未定位 是否还有巡检点没有提交
     */
    @Override
    public void updateTaskResultById(ContextCacheUser currentUser, Long taskId) {
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
        Long uid = currentUser.getUid();
 
        //提交用户必须是执行用户
        SafeCheckTask task = safeCheckTaskService.getTaskById(taskId);
        if (task == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"任务不存在");
        }
        if (!task.getExecUserId().equals(uid)){
            throw new AusinessException(E.UPDATE_FAIL,"不是任务认领人,无法提交");
        }
 
        //查询该任务下巡检点是否还有没有定位的
//        List<String> points = safeCheckTaskAndQuotaService.selectNoRfidPositionByTaskId(taskId);
//        if (points != null && points.size()>0){
//            String pointNoPosition = "";
//            for (String point : points) {
//                pointNoPosition = pointNoPosition + point + " ";
//            }
//            throw new AusinessException(E.UPDATE_FAIL,"【"+pointNoPosition+"】等巡检点未完成定位,请定位后提交");
//        }
        //查询该任务下巡检点是否还有没提交的
        List<String> pointsNoReports = safeCheckTaskAndQuotaService.selectRfidNoReportByTaskId(taskId);
        if (pointsNoReports != null && pointsNoReports.size() > 0){
            String pointNoReport = "";
            for (String point : pointsNoReports) {
                pointNoReport = pointNoReport + point + " ";
            }
            throw new AusinessException(E.UPDATE_FAIL,"【"+pointNoReport+"】等巡检点未提交,请提交后重试");
        }
        //统计巡检点中是否存在巡检异常的数据
        int abnormalPoint = safeCheckTaskAndQuotaService.countRfidReportIsAbnormalByTaskId(taskId,TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
        if (abnormalPoint > 0){
            safeCheckTaskService.updateTaskCheckResultByTaskId(taskId, TaskStatusEnum.TASK_CHECK_FINISHED.getStatus(),TaskResultEnum.TASK_RESULT_UNUSUAL.getCode(),new Date());
        }else {
            safeCheckTaskService.updateTaskCheckResultByTaskId(taskId, TaskStatusEnum.TASK_CHECK_FINISHED.getStatus(), TaskResultEnum.TASK_RESULT_NORMAL.getCode(), new Date());
        }
    }
 
    /**
     * @description 查询用户工单列表
     */
    @Override
    public IPage listUserExcepOrderByPage(ContextCacheUser currentUser, Page pageInfo) {
        Long uid = currentUser.getUid();
        IPage orderIPage = abnormalWorkOrderService.listUserExcepOrderByPage(pageInfo,uid);
        List<AbnormalWorkOrder> records = orderIPage.getRecords();
        if (CollectionUtils.isEmpty(records)){
            return orderIPage;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd分");
 
        List<ListUserExcepOrderByPageRespDTO> dtos = records.stream().map((record) -> {
            ListUserExcepOrderByPageRespDTO dto = new ListUserExcepOrderByPageRespDTO();
            String dateTime = dateFormat.format(record.getWorkOrderTime());
            dto.setWorkOrderTime(dateTime);
            BeanUtils.copyProperties(record, dto);
            return dto;
        }).collect(Collectors.toList());
        orderIPage.setRecords(dtos);
        return orderIPage;
    }
 
    /**
     * 响应回执
     */
    @Override
    public void updateExcepOrderhandleStatusById(ContextCacheUser currentUser, ExcepOrderhandleStatusByIdReqDTO reqDTO) {
        if (reqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        if (reqDTO.getId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"工单id不能为空");
        }
        AbnormalWorkOrder workOrder = abnormalWorkOrderService.getById(reqDTO.getId());
        if (workOrder == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"工单不存在");
        }
        if (!currentUser.getUid().equals(workOrder.getHiddenDangerHandlerId())){
            throw new AusinessException(E.DATA_OPERATION_NO_PERMISSION,"提交人必须为异常处理人");
        }
        AbnormalWorkOrder order = new AbnormalWorkOrder();
        order.setId(reqDTO.getId());
        order.setHandlerStatus(reqDTO.getHandlerStatus());
        Byte handlerStatus = reqDTO.getHandlerStatus();
        if (ExceptionHandleStatusEnum.SUBMITTED.getStatus().equals(handlerStatus)){
            Long hiddenDangerTransferHandlerId = reqDTO.getHiddenDangerTransferHandlerId();
            if (hiddenDangerTransferHandlerId == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"移交用户id不能为空");
            }
            ResultVO<UserInfoRPCRespDTO> userInfoByUid = accountUserService.getUserInfoByUid(hiddenDangerTransferHandlerId);
            UserInfoRPCRespDTO userInfoRPCRespDTO = (UserInfoRPCRespDTO) userInfoByUid.getData();
            if (userInfoRPCRespDTO == null){
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"移交用户不存在");
            }else {
                order.setHiddenDangerTransferHandlerId(hiddenDangerTransferHandlerId);
                order.setHiddenDangerTransferHandlerName(userInfoRPCRespDTO.getRealName());
                order.setHiddenDangerTransferHandlerPhone(userInfoRPCRespDTO.getPhone());
            }
        }
        Date date = new Date();
        if (ExceptionHandleStatusEnum.MARK_FALSE_ALARM.getStatus().equals(handlerStatus)){
            order.setAccepterStatus(ExceptionAccepterStatusEnum.MARK_FALSE_ALARM.getStatus());
            order.setHiddenDangerAccepterId(currentUser.getUid());
            order.setHiddenDangerAccepterName(currentUser.getRealName());
            order.setHiddenDangerAccepterPhone(currentUser.getPhone());
            order.setGmtHiddenDangerAccept(date);
        }
        order.setHandlerStatusSubmiter(currentUser.getRealName());
        order.setGmtHandlerStatusSubmit(date);
        order.setGmtModitify(date);
        order.setLastEditUserName(currentUser.getRealName());
        LambdaQueryWrapper<AbnormalWorkOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AbnormalWorkOrder::getId,reqDTO.getId()).
                eq(AbnormalWorkOrder::getHandlerStatus,ExceptionHandleStatusEnum.TO_BE_RESPONDED.getStatus());
        boolean update = abnormalWorkOrderService.update(order, wrapper);
        if (!update){
            throw new AusinessException(E.UPDATE_FAIL,"数据修改失败");
        }
    }
 
    /**
     * @description 处理后填报
     */
    @Override
    @Transactional
    public void updateExcepOrderHandledAfterStatusById(ContextCacheUser currentUser, ExcepOrderHandledAfterStatusByIdReqDTO reqDTO) {
        if (reqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        if (reqDTO.getId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"工单id不能为空");
        }
        AbnormalWorkOrder workOrder = abnormalWorkOrderService.getById(reqDTO.getId());
        if (workOrder == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"工单不存在");
        }
        if (!currentUser.getUid().equals(workOrder.getHiddenDangerHandlerId())){
            throw new AusinessException(E.DATA_OPERATION_NO_PERMISSION,"提交人必须为异常处理人");
        }
        if (StringUtils.isBlank(reqDTO.getHandlerDesc())){
            throw new AusinessException(E.DATA_PARAM_NULL,"处置反馈不能为空");
        }
        Date date = new Date();
        AbnormalWorkOrder order = new AbnormalWorkOrder();
        order.setId(reqDTO.getId());
        order.setHandlerDesc(reqDTO.getHandlerDesc());
        order.setGmtModitify(date);
        order.setLastEditUserName(currentUser.getRealName());
        order.setHandlerCompletedTime(date);
        order.setHandlerStatus(ExceptionHandleStatusEnum.TO_BE_ACCEPTED.getStatus());
        LambdaQueryWrapper<AbnormalWorkOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AbnormalWorkOrder::getId,reqDTO.getId())
                .eq(AbnormalWorkOrder::getHandlerStatus,ExceptionHandleStatusEnum.SELF_INSPECTION_PROCESSING.getStatus())
                        .or()
                .eq(AbnormalWorkOrder::getHandlerStatus,ExceptionHandleStatusEnum.SUBMITTED.getStatus());
        abnormalWorkOrderService.update(order,wrapper);
        Integer taskAndQuotaId = workOrder.getTaskAndQuotaId();
        List<String> images = reqDTO.getImages();
        LambdaQueryWrapper<AbnormalWorkOrderImages> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AbnormalWorkOrderImages::getAbnormalWorkOrderId,reqDTO.getId())
                .eq(AbnormalWorkOrderImages::getImageTimeStatus,ImageTimeStatusEnum.AFTER_PROCESSING.getStatus());
        abnormalWorkOrderImagesService.remove(lambdaQueryWrapper);
        //List<AbnormalWorkOrderImages> oldImages = abnormalWorkOrderImagesService.list(lambdaQueryWrapper);
        //if (CollectionUtils.isNotEmpty(oldImages)) {
        //    for (AbnormalWorkOrderImages image : oldImages) {
        //        LambdaQueryWrapper<AbnormalWorkOrderImages> queryWrapper = new LambdaQueryWrapper<>();
        //        queryWrapper.eq(AbnormalWorkOrderImages::getAbnormalImage, image);
        //        abnormalWorkOrderImagesService.remove(queryWrapper);
        //    }
        //}
        String pattern = "\\w{4}[-]\\w{2}[-]\\w{2}[_]\\w{8}[.]\\w{3}";
        if (CollectionUtils.isNotEmpty(images)){
            //如果反馈时间为空的话 说明没有提交过
            if (workOrder.getHandlerCompletedTime() == null){
                List<AbnormalWorkOrderImages> list = images.stream().map((image) -> {
                    AbnormalWorkOrderImages orderImages = new AbnormalWorkOrderImages();
                    orderImages.setAbnormalWorkOrderId(reqDTO.getId());
                    orderImages.setTaskAndQuotaId(taskAndQuotaId);
                    orderImages.setAbnormalImage(image);
                    orderImages.setImageTimeStatus(ImageTimeStatusEnum.AFTER_PROCESSING.getStatus());
                    orderImages.setCreateUserId(currentUser.getUid());
                    orderImages.setCreateUserName(currentUser.getRealName());
                    orderImages.setGmtCreate(date);
                    return orderImages;
                }).collect(Collectors.toList());
                abnormalWorkOrderImagesService.saveBatch(list);
            }else {
                List<String> newImages = new ArrayList<>();
                for (String image : images) {
                    if (image.startsWith("http")){
                        Pattern r = Pattern.compile(pattern);
                        Matcher m = r.matcher(image);
                        m.find();
                        newImages.add(m.group());
                    }else {
                        newImages.add(image);
                    }
                }
                List<AbnormalWorkOrderImages> list = newImages.stream().map((image) -> {
                    AbnormalWorkOrderImages orderImages = new AbnormalWorkOrderImages();
                    orderImages.setAbnormalWorkOrderId(reqDTO.getId());
                    orderImages.setTaskAndQuotaId(taskAndQuotaId);
                    orderImages.setAbnormalImage(image);
                    orderImages.setImageTimeStatus(ImageTimeStatusEnum.AFTER_PROCESSING.getStatus());
                    orderImages.setCreateUserId(currentUser.getUid());
                    orderImages.setCreateUserName(currentUser.getRealName());
                    orderImages.setGmtCreate(date);
                    return orderImages;
                }).collect(Collectors.toList());
                abnormalWorkOrderImagesService.saveBatch(list);
            }
        }
    }
 
    /**
     * @description 根据工单id查询现场照片
     */
    @Override
    public ListImagesByIdRespDTO listImagesById(Long id) {
        if (id == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        LambdaQueryWrapper<AbnormalWorkOrderImages> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AbnormalWorkOrderImages::getAbnormalWorkOrderId,id);
        List<AbnormalWorkOrderImages> images = abnormalWorkOrderImagesService.list(queryWrapper);
        ListImagesByIdRespDTO dto = new ListImagesByIdRespDTO();
        if (CollectionUtils.isNotEmpty(images)){
            List<String> beforeImages = new ArrayList<>();
            List<String> afterImages = new ArrayList<>();
            dto.setId(id);
            for (AbnormalWorkOrderImages image : images) {
                String file = minioAccessService.viewExceFile(image.getAbnormalImage());
                if (ImageTimeStatusEnum.BEFORE_PROCESSING.getStatus().equals(image.getImageTimeStatus())){
                    beforeImages.add(file);
                }
                if (ImageTimeStatusEnum.AFTER_PROCESSING.getStatus().equals(image.getImageTimeStatus())){
                    afterImages.add(file);
                }
            }
            dto.setBeforeImages(beforeImages);
            dto.setAfterImages(afterImages);
        }
        return dto;
    }
 
    /**
     * @description 将状态改为标记误报
     */
    @Override
    public void updateFalseAlarmStatusById(ContextCacheUser currentUser, FalseAlarmStatusByIdReqDTO reqDTO) {
        if (reqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        if (reqDTO.getId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"工单id不能为空");
        }
        AbnormalWorkOrder workOrder = abnormalWorkOrderService.getById(reqDTO.getId());
        if (workOrder == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"工单不存在");
        }
        Date date = new Date();
        AbnormalWorkOrder order = new AbnormalWorkOrder();
        order.setHandlerStatus(ExceptionHandleStatusEnum.MARK_FALSE_ALARM.getStatus());
        order.setHandlerStatusSubmiter(currentUser.getRealName());
        order.setGmtHandlerStatusSubmit(date);
        order.setGmtHiddenDangerAccept(date);
        order.setAccepterStatus(ExceptionAccepterStatusEnum.MARK_FALSE_ALARM.getStatus());
        order.setLastEditUserName(currentUser.getRealName());
        order.setGmtModitify(date);
        LambdaQueryWrapper<AbnormalWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AbnormalWorkOrder::getId,reqDTO.getId())
                .eq(AbnormalWorkOrder::getHandlerStatus,ExceptionHandleStatusEnum.TO_BE_RESPONDED.getStatus());
        boolean update = abnormalWorkOrderService.update(order, queryWrapper);
        if (!update){
            throw new AusinessException(E.UPDATE_FAIL,"状态更改失败");
        }
    }
 
    /**
     * @description 将状态改为已验收
     */
    @Override
    public void updateAcceptedStatusById(ContextCacheUser currentUser, UpdateAcceptedStatusByIdReqDTO reqDTO) {
        if (reqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        if (reqDTO.getId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"工单id不能为空");
        }
        AbnormalWorkOrder workOrder = abnormalWorkOrderService.getById(reqDTO.getId());
        if (workOrder == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"工单不存在");
        }
        Date date = new Date();
        AbnormalWorkOrder order = new AbnormalWorkOrder();
        order.setHandlerStatus(ExceptionHandleStatusEnum.COMPLETED.getStatus());
        order.setHandlerStatusSubmiter(currentUser.getRealName());
        order.setGmtHandlerStatusSubmit(date);
        order.setHiddenDangerAccepterId(currentUser.getUid());
        order.setHiddenDangerAccepterName(currentUser.getRealName());
        order.setHiddenDangerAccepterPhone(currentUser.getPhone());
        order.setGmtHiddenDangerAccept(date);
        order.setAccepterStatus(ExceptionAccepterStatusEnum.YES_ACCEPTED.getStatus());
        order.setLastEditUserName(currentUser.getRealName());
        order.setGmtModitify(date);
        LambdaQueryWrapper<AbnormalWorkOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AbnormalWorkOrder::getId,reqDTO.getId())
                .eq(AbnormalWorkOrder::getHandlerStatus,ExceptionHandleStatusEnum.TO_BE_ACCEPTED.getStatus());
        boolean update = abnormalWorkOrderService.update(order, queryWrapper);
        if (!update){
            throw new AusinessException(E.UPDATE_FAIL,"状态更改失败");
        }
    }
 
 
    /**
     * @description 查询巡检异常清单
     */
    @Override
    public IPage listExcepOrderByPage(ContextCacheUser currentUser, Page pageInfo) {
        List<Long> depIds = null;
        if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){
            ResultVO<List<Long>> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId());
            depIds = (List<Long>) resultVO.getData();
        }
        IPage orderIPage = abnormalWorkOrderService.listExcepOrderByPage(pageInfo,depIds);
        List<AbnormalWorkOrder> records = orderIPage.getRecords();
        if (CollectionUtils.isEmpty(records)){
            return orderIPage;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        List<ListExcepOrderByPageRespDTO> dtos = records.stream().map((record) -> {
            ListExcepOrderByPageRespDTO dto = new ListExcepOrderByPageRespDTO();
            BeanUtils.copyProperties(record, dto);
            String time = dateFormat.format(record.getWorkOrderTime());
            dto.setWorkOrderNum(time + new DecimalFormat("0000").format(record.getWorkOrderSort()));
            return dto;
        }).collect(Collectors.toList());
        orderIPage.setRecords(dtos);
        return orderIPage;
    }
 
 
    /**
     * @description 根据id获取反馈填报信息内容
     */
    @Override
    public ExcepOrderHandledDataByIdRespDTO getExcepOrderHandledDataById(ExcepOrderHandledDataByIdReqDTO reqDTO) {
        if (reqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"关键参数不能为空");
        }
        if (reqDTO.getId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"工单id不能为空");
        }
        AbnormalWorkOrder workOrder = abnormalWorkOrderService.getById(reqDTO.getId());
        if (workOrder == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"工单不存在");
        }
        ExcepOrderHandledDataByIdRespDTO dto = new ExcepOrderHandledDataByIdRespDTO();
        BeanUtils.copyProperties(workOrder,dto);
        LambdaQueryWrapper<AbnormalWorkOrderImages> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AbnormalWorkOrderImages::getAbnormalWorkOrderId,reqDTO.getId())
                .eq(AbnormalWorkOrderImages::getImageTimeStatus,ImageTimeStatusEnum.AFTER_PROCESSING.getStatus());
        List<AbnormalWorkOrderImages> images = abnormalWorkOrderImagesService.list(wrapper);
        if (CollectionUtils.isEmpty(images)){
            return dto;
        }
        List<String> collect = images.stream().map((image) -> {
            String abnormalImage = image.getAbnormalImage();
            String file = minioAccessService.viewExceFile(abnormalImage);
            return file;
        }).collect(Collectors.toList());
        dto.setImages(collect);
        return dto;
    }
 
    /**
     * @description 根据部门,班组和状态作为条件查询任务以及任务相关的巡检点
     */
    @Override
    public IPage listTaskDataByCondition(ContextCacheUser currentUser, Page pageInfo, ListTaskDataByConditionReqDTO reqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
 
        MobileTaskDataDBQuery taskDBQuery = new MobileTaskDataDBQuery();
        taskDBQuery.setExecClassgroupId(reqDTO.getExecClassgroupId());
        taskDBQuery.setTaskStatus(reqDTO.getTaskStatus());
        taskDBQuery.setExcDepId(reqDTO.getExcDepId());
        IPage taskIPage = safeCheckTaskService.listTaskDataByCondition(pageInfo, taskDBQuery);
 
        List<SafeCheckTaskDataMobileDO> records = taskIPage.getRecords();
        if (records == null || records.size() == 0 ){
            return null;
        }
 
        List<Long> execClassgroupids = records.stream().map(e -> e.getExecClassgroupId()).collect(Collectors.toList());
        ResultVO<Map<Long, GroupRPCRespDTO>> mapResultVO = accountGroupService.listGroupMapByGroupIds(execClassgroupids);
        Map<Long, GroupRPCRespDTO> groupInfos = (Map<Long, GroupRPCRespDTO>) mapResultVO.getData();
        List<SafeCheckTaskDataByConditionMobileRespDTO> taskMobilePages = records.stream().map((record)->{
            SafeCheckTaskDataByConditionMobileRespDTO pageRespDTO = new SafeCheckTaskDataByConditionMobileRespDTO();
            BeanUtils.copyProperties(record,pageRespDTO);
            List<SafeCheckTaskAndQuota> points = record.getPoints();
            if (points != null && points.size() > 0){
                List<SafeCheckTaskDataAndQuotaRespDTO> mobileRespDTOS = points.stream().map((point)->{
                    SafeCheckTaskDataAndQuotaRespDTO mobileRespDTO = new SafeCheckTaskDataAndQuotaRespDTO();
                    BeanUtils.copyProperties(point,mobileRespDTO);
                    return mobileRespDTO;
                }).collect(Collectors.toList());
                pageRespDTO.setPoints(mobileRespDTOS);
            }
            if (groupInfos != null){
                GroupRPCRespDTO dto = groupInfos.get(record.getExecClassgroupId());
                if (dto != null){
                    pageRespDTO.setExecClassgroup(dto.getGroupName());
                }
            }
            return pageRespDTO;
        }).collect(Collectors.toList());
        taskIPage.setRecords(taskMobilePages);
        return taskIPage;
    }
 
 
//    /**
//     * @description 判断巡检结果是否正常
//     */
//    private SafeCheckTaskAndQuota taskAndQuotaResultIsAbnormal(SafeCheckTaskAndQuota safeCheckTaskAndQuota){
//        int taskAndQuotaId = safeCheckTaskAndQuota.getId();
//        SafeCheckTaskAndQuota referenceTaskAndQuota = safeCheckTaskAndQuotaService.getTaskAndQuotaById(taskAndQuotaId);
//        if (referenceTaskAndQuota == null){
//            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"数据库无此巡检任务生成的巡检点信息");
//        }
//
//        Byte dataReportType = safeCheckTaskAndQuota.getDataReportType();
//        if (dataReportType.intValue() == 1){
//            if (safeCheckTaskAndQuota.getFirstReferenceResult() != referenceTaskAndQuota.getFirstReferenceValue()){
//                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//            }else {
//                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//            }
//            safeCheckTaskAndQuota.setReportTime(new Date());
//            return safeCheckTaskAndQuota;
//        }
//
//        if (dataReportType.intValue() == 2){
//            Byte secondReferenceSign = referenceTaskAndQuota.getSecondReferenceSign();
//            BigDecimal secondReferenceValue = referenceTaskAndQuota.getSecondReferenceValue();
//            Byte thirdReferenceSign = referenceTaskAndQuota.getThirdReferenceSign();
//            BigDecimal thirdReferenceValue = referenceTaskAndQuota.getThirdReferenceValue();
//            BigDecimal result = safeCheckTaskAndQuota.getSecondReferenceResult();
//
//            //如果第二参考值为为空,那肯定只有第三参考值了
//            if (secondReferenceValue == null){
//                //第三参考值的话需要 实际值< 或者 <= 参考值
//                if (thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
//                    int flag = result.compareTo(thirdReferenceValue);
//                    if (flag < 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                    }else {
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    }
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//                }
//
//                int flag = result.compareTo(thirdReferenceValue);
//                if (flag <= 0 ){
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                }else {
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                }
//                safeCheckTaskAndQuota.setReportTime(new Date());
//                return safeCheckTaskAndQuota;
//            }
//            //如果第三参考值为为空,第二参考值不为空
//            if (thirdReferenceValue == null){
//                //第二参考值的话需要 实际值> 或者 >= 参考值 这里是>
//                if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue()){
//                    int flag = result.compareTo(secondReferenceValue);
//                    if (flag > 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                    }else {
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    }
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//                }
//                int flag = result.compareTo(secondReferenceValue);
//                if (flag >= 0 ){
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                }else {
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                }
//                safeCheckTaskAndQuota.setReportTime(new Date());
//                return safeCheckTaskAndQuota;
//            }
//            //第二参考值和第三参考值
//            if (secondReferenceValue == null && thirdReferenceValue == null){
//                //参考值 < 数据 < 参考值
//                if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue() &&
//                    thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
//                    if (result.compareTo(secondReferenceValue) > 0 && result.compareTo(thirdReferenceValue) < 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                        safeCheckTaskAndQuota.setReportTime(new Date());
//                        return safeCheckTaskAndQuota;
//                    }
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//
//                }
//                //参考值 < 数据 <= 参考值
//                if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue() &&
//                        thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LE.getCode().intValue()){
//                    if (result.compareTo(secondReferenceValue) > 0 && result.compareTo(thirdReferenceValue) <= 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                        safeCheckTaskAndQuota.setReportTime(new Date());
//                        return safeCheckTaskAndQuota;
//                    }
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//
//                }
//                //参考值 <= 数据 < 参考值
//                if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GE.getCode().intValue() &&
//                        thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
//                    if (result.compareTo(secondReferenceValue) >= 0 && result.compareTo(thirdReferenceValue) < 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                        safeCheckTaskAndQuota.setReportTime(new Date());
//                        return safeCheckTaskAndQuota;
//                    }
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//
//                }
//                //参考值 <= 数据 <= 参考值
//                if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GE.getCode().intValue() &&
//                        thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LE.getCode().intValue()){
//                    if (result.compareTo(secondReferenceValue) >= 0 && result.compareTo(thirdReferenceValue) <= 0 ){
//                        safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
//                        safeCheckTaskAndQuota.setReportTime(new Date());
//                        return safeCheckTaskAndQuota;
//                    }
//                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
//                    safeCheckTaskAndQuota.setReportTime(new Date());
//                    return safeCheckTaskAndQuota;
//
//                }
//            }
//        }
//        return null;
//    }
 
    /**
     * @description 判断选择结果是否正常
     */
    private SafeCheckTaskAndQuota optResultIsAbnormal(SafeCheckTaskAndQuota safeCheckTaskAndQuota,SafeCheckTaskAndQuota referenceTaskAndQuota){
        safeCheckTaskAndQuota.setReportTime(new Date());
        if (referenceTaskAndQuota.getFirstReferenceValue().equals(TaskResultEnum.TASK_RESULT_BEI.getCode())){
            safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_BEI.getCode());
            return safeCheckTaskAndQuota;
        }
        if (safeCheckTaskAndQuota.getFirstReferenceResult() != referenceTaskAndQuota.getFirstReferenceValue()){
            safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
        }else {
            safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
        }
        return safeCheckTaskAndQuota;
    }
 
    /**
     * @description 判断填空结果是否正常
     */
    private SafeCheckTaskAndQuota fillBlankResultIsAbnormal(SafeCheckTaskAndQuota safeCheckTaskAndQuota,SafeCheckTaskAndQuota referenceTaskAndQuota){
        Byte secondReferenceSign = referenceTaskAndQuota.getSecondReferenceSign();
        BigDecimal secondReferenceValue = referenceTaskAndQuota.getSecondReferenceValue();
        Byte thirdReferenceSign = referenceTaskAndQuota.getThirdReferenceSign();
        BigDecimal thirdReferenceValue = referenceTaskAndQuota.getThirdReferenceValue();
        BigDecimal result = safeCheckTaskAndQuota.getSecondReferenceResult();
 
        //如果第二参考值为为空,那肯定只有第三参考值了
        if (secondReferenceValue == null){
            //第三参考值的话需要 实际值< 或者 <= 参考值
            if (thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
                int flag = result.compareTo(thirdReferenceValue);
                if (flag < 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                }else {
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                }
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
            }
 
            int flag = result.compareTo(thirdReferenceValue);
            if (flag <= 0 ){
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
            }else {
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
            }
            safeCheckTaskAndQuota.setReportTime(new Date());
            return safeCheckTaskAndQuota;
        }
        //如果第三参考值为为空,第二参考值不为空
        if (thirdReferenceValue == null){
            //第二参考值的话需要 实际值> 或者 >= 参考值 这里是>
            if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue()){
                int flag = result.compareTo(secondReferenceValue);
                if (flag > 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                }else {
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                }
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
            }
            int flag = result.compareTo(secondReferenceValue);
            if (flag >= 0 ){
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
            }else {
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
            }
            safeCheckTaskAndQuota.setReportTime(new Date());
            return safeCheckTaskAndQuota;
        }
        //第二参考值和第三参考值
        if (secondReferenceValue != null && thirdReferenceValue != null){
            //参考值 < 数据 < 参考值
            if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue() &&
                    thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
                if (result.compareTo(secondReferenceValue) > 0 && result.compareTo(thirdReferenceValue) < 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                    safeCheckTaskAndQuota.setReportTime(new Date());
                    return safeCheckTaskAndQuota;
                }
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
 
            }
            //参考值 < 数据 <= 参考值
            if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GT.getCode().intValue() &&
                    thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LE.getCode().intValue()){
                if (result.compareTo(secondReferenceValue) > 0 && result.compareTo(thirdReferenceValue) <= 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                    safeCheckTaskAndQuota.setReportTime(new Date());
                    return safeCheckTaskAndQuota;
                }
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
 
            }
            //参考值 <= 数据 < 参考值
            if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GE.getCode().intValue() &&
                    thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getCode().intValue()){
                if (result.compareTo(secondReferenceValue) >= 0 && result.compareTo(thirdReferenceValue) < 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                    safeCheckTaskAndQuota.setReportTime(new Date());
                    return safeCheckTaskAndQuota;
                }
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
 
            }
            //参考值 <= 数据 <= 参考值
            if (secondReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_GE.getCode().intValue() &&
                    thirdReferenceSign.intValue() == ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LE.getCode().intValue()){
                if (result.compareTo(secondReferenceValue) >= 0 && result.compareTo(thirdReferenceValue) <= 0 ){
                    safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_NORMAL.getCode());
                    safeCheckTaskAndQuota.setReportTime(new Date());
                    return safeCheckTaskAndQuota;
                }
                safeCheckTaskAndQuota.setReportResult(TaskResultEnum.TASK_RESULT_UNUSUAL.getCode());
                safeCheckTaskAndQuota.setReportTime(new Date());
                return safeCheckTaskAndQuota;
            }
        }
        return null;
    }
 
    /**
     * @description 巡检点结果数据校验
     */
    private void taskAndQuotaResultParamCheck(SafeCheckTaskAndQuota safeCheckTaskAndQuota){
        if (safeCheckTaskAndQuota == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点巡检结果不能为空");
        }
        if (safeCheckTaskAndQuota.getId() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务关联的巡检点主键id不能为空");
        }
        if (safeCheckTaskAndQuota.getTaskId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务id不能为空");
        }
        if (safeCheckTaskAndQuota.getDataReportType() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型不能为空");
        }
        int dataReportType = safeCheckTaskAndQuota.getDataReportType().intValue();
        switch (dataReportType){
            case 1:
                if (safeCheckTaskAndQuota.getFirstReferenceResult() == null){
                    throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型【选择结果】不能为空");
                }
                break;
            case 2:
                if (safeCheckTaskAndQuota.getSecondReferenceResult() == null){
                    throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型【填空结果】不能为空");
                }
                break;
            case 3:
                if (safeCheckTaskAndQuota.getSecondReferenceResult() == null || safeCheckTaskAndQuota.getFirstReferenceResult() == null){
                    throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型【选择或填空结果】不能为空");
                }
                break;
        }
    }
}