郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
package com.gk.hotwork.doublePrevention.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Domain.DepartmentInfo;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.co.ContextCacheUser;
import com.gk.hotwork.Domain.Exception.E;
import com.gk.hotwork.Domain.Enum.ResultCodes;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.dto.UserRPCRespDTO;
import com.gk.hotwork.Service.DepartmentService;
import com.gk.hotwork.Service.Middle.AccountAuthService;
import com.gk.hotwork.Service.Middle.AccountDepartmentService;
import com.gk.hotwork.Service.UserService;
import com.gk.hotwork.doublePrevention.utils.BeanCopyUtils;
import com.gk.hotwork.doublePrevention.utils.SnowFlow;
import com.gk.hotwork.Domain.Vo.ResultVO;
import com.gk.hotwork.doublePrevention.entity.*;
import com.gk.hotwork.doublePrevention.entity.dto.CheckResultReportDO;
import com.gk.hotwork.doublePrevention.entity.dto.req.*;
import com.gk.hotwork.doublePrevention.entity.dto.resp.*;
import com.gk.hotwork.doublePrevention.enums.StatusEnum;
import com.gk.hotwork.doublePrevention.enums.SyncEnum;
import com.gk.hotwork.doublePrevention.enums.WorkStatusEnum;
import com.gk.hotwork.doublePrevention.mq.msg.PreventNoticeExecTaskMsg;
import com.gk.hotwork.doublePrevention.mq.msg.PreventTimeOutTaskMsg;
import com.gk.hotwork.doublePrevention.mq.msg.PreventWaitExecTaskMsg;
import com.gk.hotwork.doublePrevention.repository.param.*;
import com.gk.hotwork.doublePrevention.service.DangerService;
import com.gk.hotwork.doublePrevention.service.baseService.*;
import org.apache.commons.lang3.ObjectUtils;
 
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.xml.crypto.Data;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
@Service
public class DangerServiceImpl implements DangerService {
 
 
    @Autowired
    private AccountAuthService accountAuthService;
    @Autowired
    private UserService userService;
 
 
    @Autowired
    private AccountDepartmentService accountDepartmentService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private PreventDangerCheckTaskUnitService preventDangerCheckTaskUnitService;
    @Autowired
    private PreventRiskControlMeasureService preventRiskControlMeasureService;
    @Autowired
    private PreventDangerCheckWorkService preventDangerCheckWorkService;
    @Autowired
    private PreventDangerCheckTaskService preventDangerCheckTaskService;
    @Autowired
    private PreventDangerCheckContentService preventDangerCheckContentService;
    @Autowired
    private PreventReportConfigService preventReportConfigService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PreventTaskUnitAndMeasureService preventTaskUnitAndMeasureService;
 
    @Autowired
    private PreventTaskAndMeasureService preventTaskAndMeasureService;
    @Autowired
    private PreventWorkAndMeasureService preventWorkAndMeasureService;
 
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Value("${rocketmq.topic.preventCreateTaskTopic}")
    private String preventCreateTaskTopic;
    @Value("${rocketmq.topic.preventNoticeTaskTopic}")
    private String preventNoticeTaskTopic;
    @Value("${rocketmq.topic.preventTimeOutTaskTopic}")
    private String preventTimeOutTaskTopic;
    @Value("${rocketmq.topic.preventWaitWorkTopic}")
    private String preventWaitWorkTopic;
 
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 隐患排查任务单元-分页查询
     */
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> getTaskUnitPage(Long userId, PreventDangerCheckTaskUnitQueryReqDTO taskUnitQueryReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        List<String> measureList = new ArrayList<>();
        List<PreventDangerCheckTaskUnitQueryRespDTO> list = new ArrayList<>();
        Integer pageIndex = taskUnitQueryReqDTO.getPageIndex();
        Integer pageSize = taskUnitQueryReqDTO.getPageSize();
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //获取分页数据
        IPage<PreventDangerCheckTaskUnit> page =
                preventDangerCheckTaskUnitService.getTaskUnitPage(new Page<>(pageIndex, pageSize), taskUnitQueryReqDTO);
        if (ObjectUtils.isEmpty(page)){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
        //遍历结果集,封装数据
        for (PreventDangerCheckTaskUnit record : page.getRecords()) {
            //拷贝任务单元数据
            PreventDangerCheckTaskUnitQueryRespDTO respDTO = BeanCopyUtils.copyBean(record, PreventDangerCheckTaskUnitQueryRespDTO.class);
            //获取当前任务单元关联的所有管控措施
            List<PreventTaskUnitAndMeasure> measureLists = preventTaskUnitAndMeasureService.getListByUnitId(record.getId());
            //封装管控措施编码
            if (measureLists.size() > 0){
                List<PreventRiskControlMeasureDataRespDTO> measureContentList = new ArrayList<>();
                for (PreventTaskUnitAndMeasure controlMeasure : measureLists) {
                    PreventDangerCheckContent checkContent = preventDangerCheckContentService.getCheckContentByMeasureId(controlMeasure.getMeasureId());
                    PreventRiskControlMeasureDataRespDTO measure = BeanCopyUtils.copyBean(controlMeasure, PreventRiskControlMeasureDataRespDTO.class);
                    measure.setCheckContent(checkContent.getCheckContent());
                    measure.setId(controlMeasure.getMeasureId());
                    measureContentList.add(measure);
                }
                respDTO.setMeasureList(measureContentList);
            }
            respDTO.setPageIndex((int) page.getCurrent());
            respDTO.setPageSize((int) page.getSize());
            list.add(respDTO);
        }
 
        resultVO.setCount((int) page.getTotal());
        resultVO.setData(list);
 
        return resultVO;
    }
 
    /**
     * 隐患排查任务单元-新增
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> saveTaskUnit(Long userId, PreventDangerCheckTaskUnitSaveReqDTO taskUnitSaveReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("新增成功");
        PreventDangerCheckTaskUnit taskUnit = new PreventDangerCheckTaskUnit();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (taskUnitSaveReqDTO.getTaskUnitName() == null){
            throw new BusinessException(E.DATA_PARAM_NULL, "隐患排查任务单元名称不能为空");
        }
        //检查是否存在同名任务单元
        PreventDangerCheckTaskUnit taskUnitByName = preventDangerCheckTaskUnitService.getTaskUnitByName(taskUnitSaveReqDTO.getTaskUnitName());
        if (ObjectUtils.isNotEmpty(taskUnitByName)){
            throw new BusinessException(E.DATA_DATABASE_EXIST, "隐患排查任务单元名称已存在,请修改任务单元名称,或者添加编号");
        }
        //获取需要填充的信息
        SnowFlow snowFlow = new SnowFlow();//雪花算法生成器
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        long taskUnitId = snowFlow.nextId();
 
        //封装新增参数
        taskUnit.setId(taskUnitId);
        taskUnit.setUuid(uuid);
        taskUnit.setTaskUnitName(taskUnitSaveReqDTO.getTaskUnitName());
        taskUnit.setEnterpriseId((long) 1);
        taskUnit.setEnterpriseUuid("111");
        taskUnit.setGmtCreate(date);
        taskUnit.setGmtModitify(date);
        taskUnit.setCreateByUserName(userById.getRealName());
        taskUnit.setLastEditUserName(userById.getRealName());
        taskUnit.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
        if (ObjectUtils.isEmpty(taskUnitSaveReqDTO.getNote())){
            taskUnit.setNote(null);
        }
        taskUnit.setNote(taskUnitSaveReqDTO.getNote());
 
        //执行新增单元操作
        int result = preventDangerCheckTaskUnitService.saveTaskUnit(taskUnit);
 
        //保存管控措施与任务单元的关联
        if (taskUnitSaveReqDTO.getMeasureList().size() > 0){
            for (Long measureId : taskUnitSaveReqDTO.getMeasureList()) {
                //获取任务单元与措施关联对象
                PreventTaskUnitAndMeasure taskUnitAndMeasure = new PreventTaskUnitAndMeasure();
                //查询管控措施
                PreventRiskControlMeasure controlMeasureById = preventRiskControlMeasureService.getControlMeasureById(measureId);
                //封装参数
                taskUnitAndMeasure.setTaskUnitId(taskUnitId);
                taskUnitAndMeasure.setTaskUnitUuid(uuid);
                taskUnitAndMeasure.setMeasureId(measureId);
                taskUnitAndMeasure.setMeasureUuid(controlMeasureById.getUuid());
                taskUnitAndMeasure.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
                taskUnitAndMeasure.setGmtCreate(date);
                taskUnitAndMeasure.setCreateByUserName(userById.getRealName());
                taskUnitAndMeasure.setGmtModitify(date);
                taskUnitAndMeasure.setLastEditUserName(userById.getRealName());
                preventTaskUnitAndMeasureService.insert(taskUnitAndMeasure);
            }
        }
 
        resultVO.setCount(result);
 
        return resultVO;
    }
 
    /**
     * 隐患排查任务单元-修改
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> updateTaskUnit(Long userId, PreventDangerCheckTaskUnitUpdateReqDTO taskUnitUpdateReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("修改成功");
        PreventDangerCheckTaskUnitUpdateParams updateParams = new PreventDangerCheckTaskUnitUpdateParams();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (taskUnitUpdateReqDTO.getTaskUnitName() == null){
            throw new BusinessException(E.DATA_PARAM_NULL, "隐患排查任务单元名称不能为空");
        }
        //检查是否存在同名任务单元
        PreventDangerCheckTaskUnit taskUnitByName =
                preventDangerCheckTaskUnitService.getTaskUnitByName(taskUnitUpdateReqDTO.getTaskUnitName());
        //如果查询结果不为空,且入参Id与查询到的Id不相等,判断是存在同名任务单元
        if (ObjectUtils.isNotEmpty(taskUnitByName) && !taskUnitByName.getId().equals(taskUnitUpdateReqDTO.getId())){
            throw new BusinessException(E.DATA_DATABASE_EXIST, "隐患排查任务单元名称已存在,请修改任务单元名称,或者添加编号");
        }
        //获取需要填充的信息
        Date date = new Date();
        //封装参数
        updateParams.setId(taskUnitUpdateReqDTO.getId());
        updateParams.setTaskUnitName(taskUnitUpdateReqDTO.getTaskUnitName());
        updateParams.setGmtModitify(date);
        updateParams.setLastEditUserName(userById.getRealName());
        //填充非必填的参数
        updateParams.setNote(taskUnitUpdateReqDTO.getNote());
        //执行修改
        int result = preventDangerCheckTaskUnitService.updateTaskUnit(updateParams);
 
        //重置关联关系
        PreventTaskUnitAndMeasureParams taskUnitAndMeasureParams = new PreventTaskUnitAndMeasureParams();
        taskUnitAndMeasureParams.setTaskUnitId(taskUnitUpdateReqDTO.getId());
        taskUnitAndMeasureParams.setGmtModitify(date);
        taskUnitAndMeasureParams.setLastEditUserName(userById.getRealName());
        taskUnitAndMeasureParams.setDeleteStatus(StatusEnum.DELETE_STATUS_DISCARD.getCode());
        preventTaskUnitAndMeasureService.deleteTaskUnitAndMeasure(taskUnitAndMeasureParams);
 
        //便历措施Code集合,添加关联信息
        if (taskUnitUpdateReqDTO.getMeasureList().size() > 0){
            for (Long measureId : taskUnitUpdateReqDTO.getMeasureList()) {
                //获取任务单元与措施关联对象
                PreventTaskUnitAndMeasure taskUnitAndMeasure = new PreventTaskUnitAndMeasure();
                //查询管控措施
                PreventRiskControlMeasure controlMeasureById = preventRiskControlMeasureService.getControlMeasureById(measureId);
                PreventDangerCheckTaskUnit taskUnitById = preventDangerCheckTaskUnitService.getTaskUnitById(taskUnitUpdateReqDTO.getId());
                //封装参数
 
                taskUnitAndMeasure.setTaskUnitId(taskUnitUpdateReqDTO.getId());
                taskUnitAndMeasure.setTaskUnitUuid(taskUnitById.getUuid());
                taskUnitAndMeasure.setMeasureId(measureId);
                taskUnitAndMeasure.setMeasureUuid(controlMeasureById.getUuid());
                taskUnitAndMeasure.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
                taskUnitAndMeasure.setGmtCreate(date);
                taskUnitAndMeasure.setCreateByUserName(userById.getRealName());
                taskUnitAndMeasure.setGmtModitify(date);
                taskUnitAndMeasure.setLastEditUserName(userById.getRealName());
                preventTaskUnitAndMeasureService.insert(taskUnitAndMeasure);
            }
        }
 
        resultVO.setCount(result);
 
        return resultVO;
    }
 
    /**
     * 隐患排查任务单元-任务单元列表
     */
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> listTaskUnit(Long valueOf) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
        List<PreventDangerCheckTaskUnitListQueryRespDTO> list = new ArrayList<>();
 
        List<PreventDangerCheckTaskUnit> listTaskUnit  = preventDangerCheckTaskUnitService.listTaskUnit();
        for (PreventDangerCheckTaskUnit taskUnit : listTaskUnit) {
            PreventDangerCheckWork workByTaskUnitId = preventDangerCheckWorkService.getWorkByTaskUnitId(taskUnit.getId());
            if (ObjectUtils.isEmpty(workByTaskUnitId)){
                PreventDangerCheckTaskUnitListQueryRespDTO respDTO = BeanCopyUtils.copyBean(taskUnit, PreventDangerCheckTaskUnitListQueryRespDTO.class);
                list.add(respDTO);
            }
        }
 
        resultVO.setData(list);
        return resultVO;
    }
 
    /**
     * 隐患排查任务单元-删除
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> deleteTaskUnit(Long userId, PreventDangerCheckTaskUnitDeleteReqDTO taskUnitDeleteReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("删除成功");
        PreventDangerCheckTaskUnitDeleteParams deleteParams = new PreventDangerCheckTaskUnitDeleteParams();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (taskUnitDeleteReqDTO.getId() == null ){
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "请选择正确的删除内容");
        }
        //获取需要填充的信息
        Date date = new Date();
        //封装删除需要的参数
        deleteParams.setId(taskUnitDeleteReqDTO.getId());
        deleteParams.setGmtModitify(date);
        deleteParams.setLastEditUserName(userById.getRealName());
 
        //检查该单元是否有措施
        List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(taskUnitDeleteReqDTO.getId());
        //重置关联关系
        if (listByUnitId != null && listByUnitId.size() > 0){
            PreventTaskUnitAndMeasureParams taskUnitAndMeasureParams = new PreventTaskUnitAndMeasureParams();
            taskUnitAndMeasureParams.setTaskUnitId(taskUnitDeleteReqDTO.getId());
            taskUnitAndMeasureParams.setGmtModitify(date);
            taskUnitAndMeasureParams.setLastEditUserName(userById.getRealName());
            taskUnitAndMeasureParams.setDeleteStatus(StatusEnum.DELETE_STATUS_DISCARD.getCode());
            preventTaskUnitAndMeasureService.deleteTaskUnitAndMeasure(taskUnitAndMeasureParams);
        }
        //删除单元
        int result = preventDangerCheckTaskUnitService.deleteTaskUnit(deleteParams);
 
        resultVO.setCount(result);
 
        return resultVO;
    }
 
    //隐患排查作业
    /**
     * 隐患排查作业-分页查询
     */
    @Override
    public ResultVO<PreventDangerCheckWork> getCheckWorkPage(Long userId, PreventDangerCheckWorkQueryReqDTO workQueryReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        List<PreventDangerCheckWorkQueryRespDTO> list = new ArrayList<>();
        Integer pageIndex = workQueryReqDTO.getPageIndex();
        Integer pageSize = workQueryReqDTO.getPageSize();
 
        //获取用户信息
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //如果传空字符串,转换为null
        if (workQueryReqDTO.getCheckWorkName() == ""){
            workQueryReqDTO.setCheckWorkName(null);
        }
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_TASK_FROM_WORK.getCode());
        //获取到所有符合条件的作业
        IPage<PreventDangerCheckWork> page =
                preventDangerCheckWorkService.getCheckWorkPage(new Page<>(pageIndex, pageSize), workQueryReqDTO);
        if (ObjectUtils.isEmpty(page)){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
        //封装数据
        //List<PreventDangerCheckWorkQueryRespDTO> respList = BeanCopyUtils.copyBeanList(page.getRecords(), PreventDangerCheckWorkQueryRespDTO.class);
 
        for (PreventDangerCheckWork record : page.getRecords()) {
            PreventDangerCheckWorkQueryRespDTO respDTO = BeanCopyUtils.copyBean(record, PreventDangerCheckWorkQueryRespDTO.class);
            respDTO.setPageIndex((int) page.getCurrent());
            respDTO.setPageSize((int) page.getSize());
            if (record.getTaskUnitId() != null){
                PreventDangerCheckTaskUnit taskUnit = preventDangerCheckTaskUnitService.getTaskUnitById(record.getTaskUnitId());
                if (ObjectUtils.isNotEmpty(taskUnit)){
                    respDTO.setTaskUnitName(taskUnit.getTaskUnitName());
                }
            }
            respDTO.setReportState(reportConfigById.getReportState());
            respDTO.setReportType(reportConfigById.getReportType());
            respDTO.setDepId(userById.getDepartment().getDepId());
            list.add(respDTO);
        }
        resultVO.setCount((int) page.getTotal());
        resultVO.setData(list);
 
        return resultVO;
    }
 
    /**
     * 隐患排查作业-查看检查内容
     */
    @Override
    public ResultVO<PreventDangerCheckWork> getCheckWorkContent(Long userId, PreventDangerCheckWorkContentQueryReqDTO contentReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
        List<String> checkContentList = new ArrayList<>();
 
        if (contentReqDTO.getId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL,"");
        }
 
        //根据id,查询任务单元
        PreventDangerCheckTaskUnit taskUnitByWorkId = preventDangerCheckTaskUnitService.getTaskUnitByWorkId(contentReqDTO.getId());
        //如果结果不为空,查寻对应措施,得到检查内容并封装。
        if (ObjectUtils.isNotEmpty(taskUnitByWorkId)){
            //获取任务单元内的管控措施
            List<PreventRiskControlMeasure> measureListByUnit = preventRiskControlMeasureService.getlistByUnitId(taskUnitByWorkId.getId());
            //获取管控措施对应的内容
            for (PreventRiskControlMeasure controlMeasure : measureListByUnit) {
                PreventDangerCheckContent checkContents = preventDangerCheckContentService.getCheckContentByMeasureId(controlMeasure.getId());
                checkContentList.add(checkContents.getCheckContent());
            }
        }
        resultVO.setCount(checkContentList.size());
        resultVO.setData(checkContentList);
        return resultVO;
    }
 
    /**
     * 隐患排查作业-新增
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckWork> saveCheckWork(Long userId, PreventDangerCheckWorkSaveReqDTO workSaveReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("新增成功");
        PreventDangerCheckWork checkWork = new PreventDangerCheckWork();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
 
        //校验参数,并设置作业类型
        if (workSaveReqDTO.getCheckWorkType() == 1){
            checkWork.setCheckWorkType(workSaveReqDTO.getCheckWorkType());
        }else if(workSaveReqDTO.getCheckWorkType() == 2) {
            checkWork.setCheckWorkType(workSaveReqDTO.getCheckWorkType());
        }else {
            throw new BusinessException(E.DATA_PARAM_NULL, "作业类型选择错误");
        }
        //校验参数,并设置时间单位
        if (workSaveReqDTO.getCheckCycleUnit() == 1){
            checkWork.setCheckCycleUnit(workSaveReqDTO.getCheckCycleUnit());
        }else if (workSaveReqDTO.getCheckCycleUnit() == 2) {
            checkWork.setCheckCycleUnit(workSaveReqDTO.getCheckCycleUnit());
        }else if (workSaveReqDTO.getCheckCycleUnit() == 3) {
            checkWork.setCheckCycleUnit(workSaveReqDTO.getCheckCycleUnit());
        }else if (workSaveReqDTO.getCheckCycleUnit() == 4) {
            checkWork.setCheckCycleUnit(workSaveReqDTO.getCheckCycleUnit());
        }else if (workSaveReqDTO.getCheckCycleUnit() == 5) {
            checkWork.setCheckCycleUnit(workSaveReqDTO.getCheckCycleUnit());
        }else {
            throw new BusinessException(E.DATA_PARAM_NULL, "时间单位选择错误");
        }
        //校验普通参数
        if (workSaveReqDTO.getCheckWorkName() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "作业名称不能为空");
        }
        //根据workName获取作业信息
        PreventDangerCheckWork workByName = preventDangerCheckWorkService.getWorkByName(workSaveReqDTO.getCheckWorkName());
        if (ObjectUtils.isNotEmpty(workByName)){
            throw new BusinessException(E.DATA_DATABASE_EXIST, "作业名称已存在");
        }
        if (workSaveReqDTO.getCheckCycle() == null){
            throw new BusinessException(E.DATA_PARAM_NULL, "排查周期-数值不能为空");
        }
        if (workSaveReqDTO.getNoticeTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "提前通知时间-数值不能为空");
        }
        if (workSaveReqDTO.getValidTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "作业有效期不能为空");
        }
        if (workSaveReqDTO.getFirstStartTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "首次排查任务开始时间不能为空");
        }
        if (workSaveReqDTO.getExecDepId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "任务所属部门不能为空");
        }
        //校验部门是否存在
       DepartmentInfo departmentInfo = departmentService.getDepartmentInfoById(workSaveReqDTO.getExecDepId());
        if (ObjectUtils.isEmpty(departmentInfo)){
            throw new BusinessException(E.DATA_PARAM_NULL, "该部门不存在或已被删除,请选择正确的部门");
        }
        if (workSaveReqDTO.getTaskUnitId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "任务单元不能为空");
        }
        PreventDangerCheckTaskUnit taskUnit = preventDangerCheckTaskUnitService.getTaskUnitById(workSaveReqDTO.getTaskUnitId());
        if (ObjectUtils.isEmpty(taskUnit)){
            throw new BusinessException(E.DATA_PARAM_NULL, "该任务单元不存在或已被删除,请选择正确的任务单元");
        }
        //A方案 一个作业只能调度一个任务单元
        PreventDangerCheckWork workByTaskUnitId =
                preventDangerCheckWorkService.getWorkByTaskUnitId(workSaveReqDTO.getTaskUnitId());
        if (ObjectUtils.isNotEmpty(workByTaskUnitId)){
            throw new BusinessException(E.DATA_PARAM_NULL, "该任务单元已经被【" +workByTaskUnitId.getCheckWorkName() +"】作业调度");
        }
        if (workSaveReqDTO.getFirstStartTime().getTime() < new Date().getTime()){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "首次调度时间应该大于当前时间");
        }
 
        //获取巡检作业需要填充的信息
        SnowFlow snowFlow = new SnowFlow();//雪花算法生成器
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        long checkWorkId = snowFlow.nextId();
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_TASK_FROM_WORK.getCode());
        //封装参数
        PreventDangerCheckWork checkWorkParams = BeanCopyUtils.copyBean(workSaveReqDTO, PreventDangerCheckWork.class);
        checkWorkParams.setId(checkWorkId);
        checkWorkParams.setUuid(uuid);
        checkWorkParams.setCheckWorkStatus(WorkStatusEnum.WORK_OPEN.getCode());
        checkWorkParams.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
        checkWorkParams.setNextCheckTime(workSaveReqDTO.getFirstStartTime());
        checkWorkParams.setEnterpriseId((long) 1);
        checkWorkParams.setEnterpriseUuid("111");
        checkWorkParams.setExecDepUuid(null);
        checkWorkParams.setExecDep(departmentInfo.getDepartment());
        checkWorkParams.setGmtCreate(date);
        checkWorkParams.setGmtModitify(date);
        checkWorkParams.setCreateByUserName(userById.getRealName());
        checkWorkParams.setLastEditUserName(userById.getRealName());
        checkWorkParams.setTaskUnitUuid(taskUnit.getUuid());
        checkWorkParams.setLastCheckTime(null);
        checkWorkParams.setCreateByUserId(userId);
 
        checkWorkParams.setReportTime(null);
        //读取上报主配置,进行任务记录上报配置,如果开启上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()){
            //自动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){
                //设置上报状态为-等待上报
                checkWorkParams.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode());
                //设置本条数据上报开关为-开启
                checkWorkParams.setReportSwitch(SyncEnum.REPORT_ON.getCode());
                //设置本条数据上报更新时间
                checkWorkParams.setUpdateReportDataTime(date);
            }
            //手动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
                //设置上报状态为-不上报
                checkWorkParams.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
                //设置本条数据上报开关为-关闭
                checkWorkParams.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
                //设置本条数据上报更新时间
                checkWorkParams.setUpdateReportDataTime(date);
            }
        }
        //如果主配置关闭上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_OFF.getCode()){
            //设置上报状态为-不上报
            checkWorkParams.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
            //设置本条数据上报开关为-关闭
            checkWorkParams.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
            //设置本条数据上报更新时间
            checkWorkParams.setUpdateReportDataTime(date);
        }
 
        //插入作业
        int result = preventDangerCheckWorkService.saveCheckWork(checkWorkParams);
        if (result < 0){
            throw new BusinessException(E.ADD_FAIL, "作业插入失败");
        }
 
        //作业与措施的关联表,暂时未使用,只为了上报数据使用
        //获取措施list  todo 此处有问题
//        List<PreventRiskControlMeasure> measureLists = preventRiskControlMeasureService.getlistByUnitId(workSaveReqDTO.getTaskUnitId());
        List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(workSaveReqDTO.getTaskUnitId());
 
        PreventWorkAndMeasure workAndMeasure = new PreventWorkAndMeasure();
        for (PreventTaskUnitAndMeasure unitAndMeasure : listByUnitId) {
            String workAndMeasureUuid = UUID.randomUUID().toString();
            workAndMeasure.setId(workAndMeasureUuid);
            workAndMeasure.setWorkUuid(uuid);
            workAndMeasure.setWorkId(checkWorkId);
            workAndMeasure.setMeasureId(unitAndMeasure.getMeasureId());
            workAndMeasure.setMeasureUuid(unitAndMeasure.getMeasureUuid());
            int resultWorkAndMeasure = preventWorkAndMeasureService.insertWorkAndMeasure(workAndMeasure);
            if (resultWorkAndMeasure < 1){
                throw new BusinessException(E.ADD_FAIL, "保存作业与措施的关系失败");
            }
        }
 
        resultVO.setCount(result);
 
        return resultVO;
    }
 
    /**
     * 隐患排查作业-修改
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckWork> updateCheckWork(Long userId, PreventDangerCheckWorkUpdateReqDTO workUpdateReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("修改成功");
        PreventDangerCheckWork checkWork = new PreventDangerCheckWork();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数,并设置作业类型
        if (workUpdateReqDTO.getCheckWorkType() == 1){
            checkWork.setCheckWorkType(workUpdateReqDTO.getCheckWorkType());
        }else if(workUpdateReqDTO.getCheckWorkType() == 2) {
            checkWork.setCheckWorkType(workUpdateReqDTO.getCheckWorkType());
        }else {
            throw new BusinessException(E.DATA_PARAM_NULL, "作业类型选择错误");
        }
        //校验参数,并设置时间单位
        if (workUpdateReqDTO.getCheckCycleUnit() == 1){
            checkWork.setCheckCycleUnit(workUpdateReqDTO.getCheckCycleUnit());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 2) {
            checkWork.setCheckCycleUnit(workUpdateReqDTO.getCheckCycleUnit());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 3) {
            checkWork.setCheckCycleUnit(workUpdateReqDTO.getCheckCycleUnit());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 4) {
            checkWork.setCheckCycleUnit(workUpdateReqDTO.getCheckCycleUnit());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 5) {
            checkWork.setCheckCycleUnit(workUpdateReqDTO.getCheckCycleUnit());
        }else {
            throw new BusinessException(E.DATA_PARAM_NULL, "时间单位选择错误");
        }
        //校验参数,并设置作业状态
        if (workUpdateReqDTO.getCheckWorkType() == 1 ){
            checkWork.setCheckWorkStatus(workUpdateReqDTO.getCheckWorkStatus());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 2) {
            checkWork.setCheckWorkStatus(workUpdateReqDTO.getCheckWorkStatus());
        }else if (workUpdateReqDTO.getCheckCycleUnit() == 3) {
            checkWork.setCheckWorkStatus(workUpdateReqDTO.getCheckWorkStatus());
        }
        //校验普通参数
        if (workUpdateReqDTO.getId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL,"");
        }
        if (workUpdateReqDTO.getCheckWorkName() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "作业名称不能为空");
        }
        //根据workName获取作业信息
        PreventDangerCheckWork workByName = preventDangerCheckWorkService.getWorkByName(workUpdateReqDTO.getCheckWorkName());
        if (ObjectUtils.isNotEmpty(workByName) && !workByName.getId().equals(workUpdateReqDTO.getId())){
            throw new BusinessException(E.DATA_DATABASE_EXIST, "作业名称已存在");
        }
//        if (workUpdateReqDTO.getCheckWorkStatus() == null){
//            throw new BusinessException(E.DATA_DATABASE_EXIST, "作业状态不能为空");
//        }
        if (workUpdateReqDTO.getCheckCycle() == null){
            throw new BusinessException(E.DATA_PARAM_NULL, "排查周期-数值不能为空");
        }
        if (workUpdateReqDTO.getNoticeTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "提前通知时间-数值不能为空");
        }
        if (workUpdateReqDTO.getValidTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "作业有效期不能为空");
        }
 
        if (workUpdateReqDTO.getExecDepId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "执行部门不能为空");
        }
        //校验部门是否存在
//        ResultVO<DepInfoRPCRespDTO> depInfo = accountDepartmentService.getDepInfoByDepId(userId, workUpdateReqDTO.getExecDepId());
//        DepInfoRPCRespDTO execDep = (DepInfoRPCRespDTO)depInfo.getData();
        DepartmentInfo departmentInfo = departmentService.getDepartmentInfoById(workUpdateReqDTO.getExecDepId());
        if (ObjectUtils.isEmpty(departmentInfo)){
            throw new BusinessException(E.DATA_PARAM_NULL, "该部门不存在或已被删除,请选择正确的部门");
        }
        if (workUpdateReqDTO.getTaskUnitId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "任务单元不能为空");
        }
        PreventDangerCheckTaskUnit taskUnit = preventDangerCheckTaskUnitService.getTaskUnitById(workUpdateReqDTO.getTaskUnitId());
        if (ObjectUtils.isEmpty(taskUnit)){
            throw new BusinessException(E.DATA_PARAM_NULL, "该任务单元不存在或已被删除,请选择正确的任务单元");
        }
        //A方案 一个作业只能调度一个任务单元,或者若干个管控措施
        PreventDangerCheckWork workByTaskUnitId =
                preventDangerCheckWorkService.getWorkByTaskUnitId(workUpdateReqDTO.getTaskUnitId());
        if (ObjectUtils.isNotEmpty(workByTaskUnitId) && !workByTaskUnitId.getId().equals(workUpdateReqDTO.getId())){
            throw new BusinessException(E.DATA_PARAM_NULL, "该任务单元已经被" +workByTaskUnitId.getCheckWorkName() +"作业调度");
        }
        if (workUpdateReqDTO.getFirstStartTime().getTime() < new Date().getTime()){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "首次调度时间应该大于当前时间");
        }
 
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_TASK_FROM_WORK.getCode());
        Date date = new Date();
        //封装参数
        PreventDangerCheckWorkUpdateParams updateParams = BeanCopyUtils.copyBean(workUpdateReqDTO, PreventDangerCheckWorkUpdateParams.class);
 
        updateParams.setGmtModitify(date);
        updateParams.setLastEditUserName(userById.getRealName());
        updateParams.setExecDep(departmentInfo.getDepartment());
        updateParams.setTaskUnitUuid(taskUnit.getUuid());
        updateParams.setNextCheckTime(workUpdateReqDTO.getFirstStartTime());
        //读取上报主配置,进行任务记录上报配置,如果开启上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()){
            //自动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){
                //设置上报状态为-等待上报
                updateParams.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode());
                //设置本条数据上报开关为-开启
                updateParams.setReportSwitch(SyncEnum.REPORT_ON.getCode());
                //设置本条数据上报更新时间
                updateParams.setUpdateReportDataTime(date);
            }
            //手动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
                //设置上报状态为-不上报
                updateParams.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
                //设置本条数据上报开关为-关闭
                updateParams.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
                //设置本条数据上报更新时间
                updateParams.setUpdateReportDataTime(date);
            }
        }
        //如果主配置关闭上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_OFF.getCode()){
            //设置上报状态为-不上报
            updateParams.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
            //设置本条数据上报开关为-关闭
            updateParams.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
            //设置本条数据上报更新时间
            updateParams.setUpdateReportDataTime(date);
        }
 
        int result = preventDangerCheckWorkService.updateCheckWork(updateParams);
        //获取当前作业
        PreventDangerCheckWork workById = preventDangerCheckWorkService.getWorkById(workUpdateReqDTO.getId());
        if (!workUpdateReqDTO.getTaskUnitId().equals(workById.getTaskUnitId())){
            if (ObjectUtils.isNotEmpty(preventWorkAndMeasureService.getWorkAndMeasureByWorkUuid(workById.getUuid()))){
                //重置作业与措施的关联
                preventWorkAndMeasureService.updateWorkAndMeasure(workById.getUuid());
            }
        }
 
        //作业与措施的关联表,暂时未使用,只为了上报数据使用
        //获取措施list
//        List<PreventRiskControlMeasure> measureLists = preventRiskControlMeasureService.getlistByUnitId(workUpdateReqDTO.getTaskUnitId());
//        PreventWorkAndMeasure workAndMeasure = new PreventWorkAndMeasure();
//        for (PreventRiskControlMeasure measureList : measureLists) {
//            String workAndMeasureUuid = UUID.randomUUID().toString();
//            workAndMeasure.setId(workAndMeasureUuid);
//            workAndMeasure.setWorkUuid(workById.getUuid());
//            workAndMeasure.setMeasureUuid(measureList.getUuid());
//            int resultWorkAndMeasure = preventWorkAndMeasureService.insertWorkAndMeasure(workAndMeasure);
//            if (resultWorkAndMeasure < 0){
//                throw new BusinessException(E.ADD_FAIL, "保存作业与措施的关系失败");
//            }
//        }
        List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(workUpdateReqDTO.getTaskUnitId());
 
        PreventWorkAndMeasure workAndMeasure = new PreventWorkAndMeasure();
        for (PreventTaskUnitAndMeasure unitAndMeasure : listByUnitId) {
            String workAndMeasureUuid = UUID.randomUUID().toString();
            workAndMeasure.setId(workAndMeasureUuid);
            workAndMeasure.setWorkUuid(workById.getUuid());
            workAndMeasure.setWorkId(workById.getId());
            workAndMeasure.setMeasureId(unitAndMeasure.getMeasureId());
            workAndMeasure.setMeasureUuid(unitAndMeasure.getMeasureUuid());
            int resultWorkAndMeasure = preventWorkAndMeasureService.insertWorkAndMeasure(workAndMeasure);
            if (resultWorkAndMeasure < 1){
                throw new BusinessException(E.ADD_FAIL, "保存作业与措施的关系失败");
            }
        }
        resultVO.setCount(result);
 
        return resultVO;
    }
 
    /**
     * 隐患排查作业-列表
     */
    @Override
    public ResultVO<PreventDangerCheckWork> listCheckWork(Long valueOf) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        List<PreventDangerCheckWork> listCheckWork = preventDangerCheckWorkService.listCheckWork();
        List<PreventDangerCheckWorkListQueryRespDTO> respDTOS =
                BeanCopyUtils.copyBeanList(listCheckWork, PreventDangerCheckWorkListQueryRespDTO.class);
 
        resultVO.setData(respDTOS);
        return resultVO;
    }
    /**
     * 排查作业-手工上报-配置
     */
    @Override
    public ResultVO<PreventRiskAnaUnit> updateCheckWorkReport(Long userId, PreventHandReportConfigReqDTO preventHandReportConfigReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("修改成功");
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        if (preventHandReportConfigReqDTO.getId() == null){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "传参非法");
        }
        if (preventHandReportConfigReqDTO.getReportSwitch() == null){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "传参非法");
        }
        //查询当前隐患的配置
        PreventDangerCheckWork workById = preventDangerCheckWorkService.getWorkById(preventHandReportConfigReqDTO.getId());
        //配置相同,做处理
        if (workById.getReportSwitch() == preventHandReportConfigReqDTO.getReportSwitch()){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "与当前配置相同");
        }
        int result ;
        //读取主配置信息
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG_TASK_FROM_WORK.getCode());
        //只有当开启上报,且类型为手动上报时,可以修改
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()
                && reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
            result = preventDangerCheckWorkService.updateCheckWorkReport(preventHandReportConfigReqDTO);
 
        }else {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "当前上报配置,不支持对单条数据操作");
        }
 
        resultVO.setCount(result);
        return resultVO;
    }
 
    /**
     * 隐患排查作业-删除
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckWork> deleteCheckWork(Long userId, PreventDangerCheckWorkDeleteReqDTO workDeleteReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("删除成功");
        PreventDangerCheckWorkDeleteParams deleteParams = new PreventDangerCheckWorkDeleteParams();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (workDeleteReqDTO.getId() == null ){
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "请选择正确的删除内容");
        }
        //获取需要填充的信息
        Date date = new Date();
        //封装删除需要的参数
        deleteParams.setId(workDeleteReqDTO.getId());
        deleteParams.setGmtModitify(date);
        deleteParams.setLastEditUserName(userById.getRealName());
        deleteParams.setUpdateReportDataTime(date);
        //获取当前作业
        PreventDangerCheckWork workById = preventDangerCheckWorkService.getWorkById(workDeleteReqDTO.getId());
 
        if (ObjectUtils.isNotEmpty(preventWorkAndMeasureService.getWorkAndMeasureByWorkUuid(workById.getUuid()))){
            //重置作业与措施的关联
            preventWorkAndMeasureService.updateWorkAndMeasure(workById.getUuid());
        }
        //删除作业
        int result = preventDangerCheckWorkService.deleteCheckWork(deleteParams);
 
 
        resultVO.setCount(result);
        return resultVO;
    }
 
    //隐患排查任务
    /**
     * 隐患排查任务-分页查询
     */
    @Override
    public ResultVO<PreventDangerCheckTask> getTaskPage(Long userId, PreventDangerCheckTaskQueryReqDTO taskQueryReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        Integer pageIndex = taskQueryReqDTO.getPageIndex();
        Integer pageSize = taskQueryReqDTO.getPageSize();
        List<PreventDangerCheckTaskQueryRespDTO> list = new ArrayList<>();
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
 
        //获取到所有符合条件任务
        IPage<PreventDangerCheckTask> page =
                preventDangerCheckTaskService.getTaskPage(new Page<>(pageIndex, pageSize), taskQueryReqDTO);
        if (ObjectUtils.isEmpty(page)){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
 
        List<Long> taskIdLists = new ArrayList<>();
        //取出所有符合条件的任务的id
        for (PreventDangerCheckTask record : page.getRecords()) {
            taskIdLists.add(record.getId());
        }
 
        //取出所有符合条件的任务和措施的对应关系
        List<PreventTaskAndMeasure> taskAndMeasureList = preventTaskAndMeasureService.getListByTaskIdByIdList(taskIdLists);
        //取出所有符合条件的措施id
        List<Long> measureIdList = new ArrayList<>();
        for (PreventTaskAndMeasure preventTaskAndMeasure : taskAndMeasureList) {
            measureIdList.add(preventTaskAndMeasure.getControlMeasureId());
        }
 
        //取出所有符合条件措施
        List<PreventRiskControlMeasure> controlMeasureList = preventRiskControlMeasureService.getControlMeasureAndContent(measureIdList);
        //取出所有部门
        List<DepartmentInfo> departmentList = departmentService.listDepartmentInfoById();
 
        //遍历任务集合,匹配封装排查任务信息。
        //1、遍历所有符合条件的任务
        for (PreventDangerCheckTask task : page.getRecords()) {
            //封装task数据
            PreventDangerCheckTaskQueryRespDTO respDTO = BeanCopyUtils.copyBean(task, PreventDangerCheckTaskQueryRespDTO.class);
            //获取隐患排查内容的list
            List<PreventrCheckContentRespDTO> checkContentRespDTOS = new ArrayList<>();
            //2、遍历所有符合条件的对应关系
            for (PreventTaskAndMeasure taskAndMeasure : taskAndMeasureList) {
                if (taskAndMeasure.getCheckTaskId().equals(task.getId())){
                    //3、遍历所有符合条件的措施数据
                    for (PreventRiskControlMeasure measure : controlMeasureList) {
                        //4、匹配到任务对应的措施信息,封装检查内容
                        if (measure.getId().equals(taskAndMeasure.getControlMeasureId())){
                            PreventrCheckContentRespDTO checkInfo = BeanCopyUtils.copyBean(taskAndMeasure, PreventrCheckContentRespDTO.class);
                            checkInfo.setControlType(measure.getControlType());
                            checkInfo.setClassify1(measure.getClassify1());
                            checkInfo.setClassify2(measure.getClassify2());
                            checkInfo.setClassify3(measure.getClassify3());
                            checkInfo.setMeasureDesc(measure.getMeasureDesc());
                            checkInfo.setControlMeasureCode(measure.getControlMeasureCode());
                            checkContentRespDTOS.add(checkInfo);
                        }
                    }
                }
            }
            //4、遍历部门信息,封装任务执行部门
            for (DepartmentInfo department : departmentList) {
                if (department.getId().equals(task.getExecDepId())){
                    respDTO.setExecDep(department.getDepartment());
                }
            }
            respDTO.setCheckContent(checkContentRespDTOS);
            respDTO.setReportState(reportConfigById.getReportState());
            respDTO.setReportType(reportConfigById.getReportType());
            respDTO.setPageIndex((int) page.getCurrent());
            respDTO.setPageSize((int) page.getSize());
            list.add(respDTO);
        }
 
        resultVO.setCount((int) page.getTotal());
        resultVO.setData(list);
 
        return resultVO;
    }
    /**
     * 隐患排查任务-分页查询-手机端使用
     */
    @Override
    public ResultVO<PreventDangerCheckTask> getTaskPageForMobile(Long userId, PreventDangerCheckTaskQueryReqDTO taskQueryReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        Integer pageIndex = taskQueryReqDTO.getPageIndex();
        Integer pageSize = taskQueryReqDTO.getPageSize();
        List<PreventDangerCheckTaskQueryRespDTO> list = new ArrayList<>();
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
 
 
        //获取到所有符合条件任务
        IPage<PreventDangerCheckTask> page =
                preventDangerCheckTaskService.getTaskPageForMobile(new Page<>(pageIndex, pageSize), taskQueryReqDTO);
        if (ObjectUtils.isEmpty(page)){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
 
        List<PreventRiskControlMeasure> controlMeasureList = preventRiskControlMeasureService.listAllControlMeasure();
        //遍历任务集合,取出所有的排查任务信息。
        for (PreventDangerCheckTask record : page.getRecords()) {
            //取出任务和措施的对应关系
            List<PreventTaskAndMeasure> listByTaskId = preventTaskAndMeasureService.getListByTaskId(record.getId());
 
            PreventDangerCheckTaskQueryRespDTO respDTO = BeanCopyUtils.copyBean(record, PreventDangerCheckTaskQueryRespDTO.class);
            //获取隐患排查内容的list
            List<PreventrCheckContentRespDTO> preventCheckContentRespDTOS = new ArrayList<>();
            //封装措施数据
 
            for (PreventTaskAndMeasure taskAndMeasure : listByTaskId) {
                PreventrCheckContentRespDTO checkContentRespDTO = BeanCopyUtils.copyBean(taskAndMeasure, PreventrCheckContentRespDTO.class);
                for (PreventRiskControlMeasure measure : controlMeasureList) {
                    if (measure.getId().equals(taskAndMeasure.getControlMeasureId())){
                        checkContentRespDTO.setControlType(measure.getControlType());
                        checkContentRespDTO.setClassify1(measure.getClassify1());
                        checkContentRespDTO.setClassify2(measure.getClassify2());
                        checkContentRespDTO.setClassify3(measure.getClassify3());
                        checkContentRespDTO.setMeasureDesc(measure.getMeasureDesc());
                        checkContentRespDTO.setControlMeasureCode(measure.getControlMeasureCode());
                    }
                }
                preventCheckContentRespDTOS.add(checkContentRespDTO);
//
            }
 
            respDTO.setCheckContent(preventCheckContentRespDTOS);
 
            DepartmentInfo departmentInfo = departmentService.getDepartmentInfoById(record.getExecDepId());
            respDTO.setExecDep(departmentInfo.getDepartment());
            //封装任务数据
            respDTO.setReportState(reportConfigById.getReportState());
            respDTO.setReportType(reportConfigById.getReportType());
            respDTO.setPageIndex((int) page.getCurrent());
            respDTO.setPageSize((int) page.getSize());
            //封装排查内容数据
            list.add(respDTO);
 
        }
 
        resultVO.setCount((int) page.getTotal());
        resultVO.setData(list);
 
        return resultVO;
    }
    /**
     * 隐患排查任务-分页查询-手机端使用-test
     */
    @Override
    public ResultVO<PreventDangerCheckTask> getTaskPageForMobileTest(Long userId, PreventDangerCheckTaskQueryReqDTO taskQueryReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("查询成功");
 
        Integer pageIndex = taskQueryReqDTO.getPageIndex();
        Integer pageSize = taskQueryReqDTO.getPageSize();
        List<PreventDangerCheckTaskQueryRespDTO> list = new ArrayList<>();
 
//        System.out.println("进入方法");
 
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
 
 
        //获取到所有符合条件任务
        IPage<PreventDangerCheckTask> page =
                preventDangerCheckTaskService.getTaskPageForMobile(new Page<>(pageIndex, pageSize), taskQueryReqDTO);
        if (ObjectUtils.isEmpty(page)){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
        if (ObjectUtils.isEmpty(page.getRecords())){
            resultVO.setMsg("查询成功,无数据");
            return resultVO;
        }
        List<Long> taskIdLists = new ArrayList<>();
        //取出所有符合条件的任务的id
        for (PreventDangerCheckTask record : page.getRecords()) {
            taskIdLists.add(record.getId());
        }
 
        //取出所有符合条件的任务和措施的对应关系
        List<PreventTaskAndMeasure> taskAndMeasureList = preventTaskAndMeasureService.getListByTaskIdByIdList(taskIdLists);
        //取出所有符合条件的措施id
        List<Long> measureIdList = new ArrayList<>();
        for (PreventTaskAndMeasure preventTaskAndMeasure : taskAndMeasureList) {
            measureIdList.add(preventTaskAndMeasure.getControlMeasureId());
        }
 
        //取出所有符合条件措施
        List<PreventRiskControlMeasure> controlMeasureList = preventRiskControlMeasureService.getControlMeasureAndContent(measureIdList);
        //取出所有部门
        List<DepartmentInfo> departmentList = departmentService.listDepartmentInfoById();
 
        //遍历任务集合,匹配封装排查任务信息。
        //1、遍历所有符合条件的任务
        for (PreventDangerCheckTask task : page.getRecords()) {
            //封装task数据
            PreventDangerCheckTaskQueryRespDTO respDTO = BeanCopyUtils.copyBean(task, PreventDangerCheckTaskQueryRespDTO.class);
            //获取隐患排查内容的list
            List<PreventrCheckContentRespDTO> checkContentRespDTOS = new ArrayList<>();
            //2、遍历所有符合条件的对应关系
            for (PreventTaskAndMeasure taskAndMeasure : taskAndMeasureList) {
                if (taskAndMeasure.getCheckTaskId().equals(task.getId())){
                    //3、遍历所有符合条件的措施数据
                    for (PreventRiskControlMeasure measure : controlMeasureList) {
                        //4、匹配到任务对应的措施信息,封装检查内容
                        if (measure.getId().equals(taskAndMeasure.getControlMeasureId())){
                            PreventrCheckContentRespDTO checkInfo = BeanCopyUtils.copyBean(taskAndMeasure, PreventrCheckContentRespDTO.class);
                            checkInfo.setControlType(measure.getControlType());
                            checkInfo.setClassify1(measure.getClassify1());
                            checkInfo.setClassify2(measure.getClassify2());
                            checkInfo.setClassify3(measure.getClassify3());
                            checkInfo.setMeasureDesc(measure.getMeasureDesc());
                            checkInfo.setControlMeasureCode(measure.getControlMeasureCode());
                            checkContentRespDTOS.add(checkInfo);
                        }
                    }
                }
            }
            //4、遍历部门信息,封装任务执行部门
            for (DepartmentInfo department : departmentList) {
                if (department.getId().equals(task.getExecDepId())){
                    respDTO.setExecDep(department.getDepartment());
                }
            }
            respDTO.setCheckContent(checkContentRespDTOS);
            respDTO.setReportState(reportConfigById.getReportState());
            respDTO.setReportType(reportConfigById.getReportType());
            respDTO.setPageIndex((int) page.getCurrent());
            respDTO.setPageSize((int) page.getSize());
            list.add(respDTO);
        }
 
        resultVO.setCount((int) page.getTotal());
        resultVO.setData(list);
 
        return resultVO;
    }
    /**
     * 排查任务-认领任务
     */
    @Override
    public ResultVO<PreventDangerCheckTask> taskToUser(ContextCacheUser currentUser, PreventDangerCheckTaskDeleteReqDTO taskToUserDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("认领成功");
 
        //校验
        PreventDangerCheckTask taskById = preventDangerCheckTaskService.getTaskById(taskToUserDTO.getId());
 
        if (ObjectUtils.isEmpty(taskById)){
            throw new BusinessException(E.DATA_DATABASE_EXIST, "传参错误,未找到对应任务");
        }
        if (ObjectUtils.isNotEmpty(taskById)){
            if (taskById.getExecUserId() != null){
                throw new BusinessException(E.DATA_DATABASE_EXIST, "当前任务已经被人认领");
            }
            if (taskById.getTaskStatus() == 3){
                throw new BusinessException(E.DATA_DATABASE_EXIST, "当前任务已经超时,无法认领");
            }
            if (taskById.getTaskStatus() == 2){
                throw new BusinessException(E.DATA_DATABASE_EXIST, "任务已结束,无法认领");
            }
            Date date = new Date();
            PreventTaskToUserParams taskToUserParams = new PreventTaskToUserParams();
            //封装参数
            taskToUserParams.setId(taskToUserDTO.getId());
            taskToUserParams.setExecUserId(currentUser.getUid());
            taskToUserParams.setExecUserName(currentUser.getRealName());
            taskToUserParams.setLastEditUserName(currentUser.getRealName());
            taskToUserParams.setGmtModitify(date);
            taskToUserParams.setTaskBelong((byte) 2);
            taskToUserParams.setBelongTime(date);
            //执行修改,认领任务
            int result = preventDangerCheckTaskService.taskToUser(taskToUserParams);
            resultVO.setCount(result);
        }
 
        return resultVO;
    }
 
    /**
     * 排查任务-手工上报-配置
     */
    @Override
    public ResultVO<PreventRiskAnaUnit> updateCheckTaskReport(Long userId, PreventHandReportConfigReqDTO preventHandReportConfigReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("修改成功");
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        if (preventHandReportConfigReqDTO.getId() == null){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "传参非法");
        }
        if (preventHandReportConfigReqDTO.getReportSwitch() == null){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "传参非法");
        }
        //查询当前隐患的配置
        PreventDangerCheckTask taskById = preventDangerCheckTaskService.getTaskById(preventHandReportConfigReqDTO.getId());
        //配置相同,做处理
        if (taskById.getReportSwitch() == preventHandReportConfigReqDTO.getReportSwitch()){
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "与当前配置相同");
        }
        int result ;
        //读取主配置信息
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
        //只有当开启上报,且类型为手动上报时,可以修改
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()
                && reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
            result = preventDangerCheckTaskService.updateCheckTaskReport(preventHandReportConfigReqDTO);
 
        }else {
            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "当前上报配置,不支持对单条数据操作");
        }
        resultVO.setCount(result);
        return resultVO;
    }
 
    /**
     * 隐患排查任务-手动新增--暂时不提供
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckTask> saveTask(Long userId, PreventDangerCheckTaskSaveReqDTO taskSaveReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("新增成功");
        PreventDangerCheckTask checkTask = new PreventDangerCheckTask();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (taskSaveReqDTO.getStartTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "请填写任务开始时间");
        }
        if (taskSaveReqDTO.getValidTime() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "请填写任务有效时间");
        }
        if (taskSaveReqDTO.getExecUserId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "请填写执行人姓名");
        }
        //获取执行人信息
        ResultVO<UserRPCRespDTO> execUserResult = accountAuthService.getUserById(taskSaveReqDTO.getExecUserId());
        UserRPCRespDTO execUser = (UserRPCRespDTO)execUserResult.getData();
        if (ObjectUtils.isEmpty(execUser)){
            throw new BusinessException(E.DATA_PARAM_NULL, "请填写正确的执行人姓名");
        }
 
        //获取需要填充的信息
        SnowFlow snowFlow = new SnowFlow();//雪花算法生成器
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        long taskId = snowFlow.nextId();
 
        List<Long> measureIdList = new ArrayList<>();
        //判断任务单元是否为空,不为空,则取出措施id
        if (taskSaveReqDTO.getTaskUnitId() == null){
            throw new BusinessException(E.DATA_PARAM_NULL, "请选择任务单元");
        }
        //查找单元信息
        PreventDangerCheckTaskUnit taskUnitById = preventDangerCheckTaskUnitService.getTaskUnitById(taskSaveReqDTO.getTaskUnitId());
        if (ObjectUtils.isEmpty(taskUnitById)){
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "请选择正确的任务单元");
        }
 
        //根据单元id查找对应措施id列表-->measureList
        // List<PreventRiskControlMeasure> measureList = preventRiskControlMeasureService.getlistByUnitId(taskSaveReqDTO.getTaskUnitId());
        List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(taskSaveReqDTO.getTaskUnitId());
        //如果单元内措施列表不为空,遍历集合,取出措施id
        if (listByUnitId.size() > 0) {
            for (PreventTaskUnitAndMeasure taskUnitAndMeasure : listByUnitId) {
                measureIdList.add(taskUnitAndMeasure.getMeasureId());
            }
        }
 
        //设置任务参数
        checkTask.setCheckTaskUnitId(taskSaveReqDTO.getTaskUnitId());
        checkTask.setCheckTaskUnitUuid(taskUnitById.getUuid());
        //封装数据
        checkTask.setId(taskId);
        checkTask.setUuid(uuid);
        checkTask.setStartTime(taskSaveReqDTO.getStartTime());
        checkTask.setValidTime(taskSaveReqDTO.getValidTime());
        checkTask.setNoticeTime(date);//需要改为通知时间
        checkTask.setGmtCreate(date);
        checkTask.setGmtModitify(date);
        checkTask.setCreateUserName(userById.getRealName());
        checkTask.setLastEditUserName(userById.getRealName());
        checkTask.setResult(null);
        checkTask.setTaskStatus(WorkStatusEnum.TASK_WAIT.getCode());
        checkTask.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
        checkTask.setCheckWorkId(null);
        checkTask.setCheckWorkUuid(null);
        checkTask.setTaskType(WorkStatusEnum.TASK_TYPE_USER_ADD.getCode());
        checkTask.setTaskCode("task"+ date.toString());//TODO 暂定位task+时间
        checkTask.setExecUserId(taskSaveReqDTO.getExecUserId());
        checkTask.setExecUserName(execUser.getRealName());
        checkTask.setEnterpriseId((long) 1);
        checkTask.setEnterpriseUuid("111");
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
        checkTask.setReportTime(null);
 
        //读取上报主配置,进行任务记录上报配置,如果开启上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()){
            //自动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){
                //设置上报状态为-等待上报
                checkTask.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode());
                //设置本条数据上报开关为-开启
                checkTask.setReportSwitch(SyncEnum.REPORT_ON.getCode());
                //设置本条数据上报更新时间
                checkTask.setUpdateReportDataTime(date);
            }
            //手动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
                //设置上报状态为-不上报
                checkTask.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
                //设置本条数据上报开关为-关闭
                checkTask.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
                //设置本条数据上报更新时间
                checkTask.setUpdateReportDataTime(date);
            }
        }
        //如果主配置关闭上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_OFF.getCode()){
            //设置上报状态为-不上报
            checkTask.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
            //设置本条数据上报开关为-关闭
            checkTask.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
            //设置本条数据上报更新时间
            checkTask.setUpdateReportDataTime(date);
        }
 
        //任务插入
        int result = preventDangerCheckTaskService.saveTask(checkTask);
 
        //保存管控措施与任务关联,添加检查内容
        PreventTaskAndMeasure taskAndMeasure = new PreventTaskAndMeasure();
        for (Long measureId : measureIdList) {
            //得到管控措施对应的检查内容
            PreventDangerCheckContent checkContent = preventDangerCheckContentService.getCheckContentByMeasureId(measureId);
            //封装数据
            taskAndMeasure.setCheckTaskId(taskId);
            taskAndMeasure.setControlMeasureId(measureId);
            taskAndMeasure.setCheckContent(checkContent.getCheckContent());
            preventTaskAndMeasureService.saveTaskAndMeasure(taskAndMeasure);
        }
 
        resultVO.setCount(result);
        return resultVO;
    }
    /**
     * 隐患排查任务-调度
     */
    @Transactional
    @Override
    public  boolean createAutoTask(Long workId) {
 
        boolean createResult = false;
        //查询对应的作业,获取信息
        PreventDangerCheckWork workById = preventDangerCheckWorkService.getWorkById(workId);
        if (ObjectUtils.isEmpty(workById.getTaskUnitId())){
            logger.info("【双重预防】任务对应单元已被删除,停止调度");
            throw new BusinessException(E.DATA_STATUS_NOT_EXIST,"任务对应单元已被删除,停止调度");
        }
 
        System.out.println("\n【##】调度作业-检索信息");
        if (ObjectUtils.isEmpty(workById)){
            return createResult;
        }
 
 
 
        PreventDangerCheckTask checkTask = new PreventDangerCheckTask();
 
        //获取调度任务时,需要填充的信息
        SnowFlow snowFlow = new SnowFlow();//雪花算法生成器
        String uuid = UUID.randomUUID().toString();
        Date date = new Date();
        long taskId = snowFlow.nextId();
        Date validTime = null;
        Date noticeTime  = null;
        Date nextCheckTime  = null;
        Date checkTime  = null;
        //如果是第一次调度,以firstTime为标准;否则以nextTime为标准
        if (ObjectUtils.isNotEmpty(workById.getNextCheckTime())){
            //解析任务有效时间,以本次调度时的预期下次执行时间为基础计算
            if (workById.getValidTimeUnit() == 1) {
                //如果时间单位是分钟
                validTime = new Date(workById.getNextCheckTime().getTime() + workById.getValidTime() * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 2) {
                //如果时间单位是小时
                validTime = new Date(workById.getNextCheckTime().getTime() + workById.getValidTime() * 60 * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 3) {
                //如果时间单位是日
                validTime = new Date(workById.getNextCheckTime().getTime() + workById.getValidTime() * 24 * 60 * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 4) {
                //如果时间单位是月
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(workById.getNextCheckTime());//设置起时间
                calendar.add(Calendar.MONTH, workById.getValidTime());//增加N个月
                validTime = calendar.getTime();
            }
            //解析任务通知时间
            if (workById.getNoticeTimeUnit() == 1) {
                //如果时间单位是分钟
                noticeTime = new Date(workById.getNextCheckTime().getTime() - workById.getNoticeTime() * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 2) {
                //如果时间单位是小时
                noticeTime = new Date(workById.getNextCheckTime().getTime() - workById.getNoticeTime() * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 3) {
                //如果时间单位是日
                noticeTime = new Date(workById.getNextCheckTime().getTime() - workById.getNoticeTime() * 24 * 60 * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 4) {
                //如果时间单位是月
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(workById.getNextCheckTime());//设置起时间
                calendar.add(Calendar.MONTH, -workById.getNoticeTime());//前移N个月
                noticeTime = calendar.getTime();
            }
            checkTime = workById.getNextCheckTime();
        }else {
            //解析任务有效时间,以本次调度时的预期下次执行时间为基础计算
            if (workById.getValidTimeUnit() == 1) {
                //如果时间单位是分钟
                validTime = new Date(workById.getFirstStartTime().getTime() + workById.getValidTime() * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 2) {
                //如果时间单位是小时
                validTime = new Date(workById.getFirstStartTime().getTime() + workById.getValidTime() * 60 * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 3) {
                //如果时间单位是日
                validTime = new Date(workById.getFirstStartTime().getTime() + workById.getValidTime() * 24 * 60 * 60 * 1000);
            }else if (workById.getValidTimeUnit() == 4) {
                //如果时间单位是月
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(workById.getFirstStartTime());//设置起时间
                calendar.add(Calendar.MONTH, workById.getValidTime());//增加N个月
                validTime = calendar.getTime();
            }
            //解析任务通知时间
            if (workById.getNoticeTimeUnit() == 1) {
                //如果时间单位是分钟
                noticeTime = new Date(workById.getFirstStartTime().getTime() - workById.getNoticeTime() * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 2) {
                //如果时间单位是小时
                noticeTime = new Date(workById.getFirstStartTime().getTime() - workById.getNoticeTime() * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 3) {
                //如果时间单位是日
                noticeTime = new Date(workById.getFirstStartTime().getTime() - workById.getNoticeTime() * 24 * 60 * 60 * 1000);
            }else if (workById.getNoticeTimeUnit() == 4) {
                //如果时间单位是月
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(workById.getFirstStartTime());//设置起时间
                calendar.add(Calendar.MONTH, -workById.getNoticeTime());//前移N个月
                noticeTime = calendar.getTime();
            }
            checkTime = workById.getFirstStartTime();
        }
 
        //0、加分布式锁
        String lockName = "PREVENT_TASK_CREATE_"+workId;
        RLock createTaskLock = redissonClient.getLock(lockName);
        createTaskLock.lock(10, TimeUnit.SECONDS);
 
        //锁10秒期间,判断任务是否已经生成,如果已经生成,结束方法
        PreventDangerCheckTask taskByCheckWorkIdAndStartTime = preventDangerCheckTaskService.getTaskByCheckWorkIdAndStartTime(workId, checkTime);
        if (ObjectUtils.isNotEmpty(taskByCheckWorkIdAndStartTime)){
            logger.info("任务已被创建");
            return false;
        }
 
        //解析调度周期时间间隔,work的下次执行时间
        if (workById.getCheckCycleUnit() == 1) {
            //如果时间单位是分钟
            nextCheckTime = new Date(validTime.getTime() + workById.getCheckCycle() * 60 * 1000);
        }else if (workById.getCheckCycleUnit() == 2) {
            //如果时间单位是小时
            nextCheckTime = new Date(validTime.getTime()  + workById.getCheckCycle() * 60 * 60 * 1000);
        }else if (workById.getCheckCycleUnit() == 3) {
            //如果时间单位是日
            nextCheckTime = new Date(validTime.getTime()  + workById.getCheckCycle() * 24 * 60 * 60 * 1000);
        }else if (workById.getCheckCycleUnit() == 4) {
            //如果时间单位是月
            nextCheckTime = new Date(validTime.getTime() + workById.getCheckCycle() * 24 * 60 * 60 * 1000);
        }
 
 
        if (checkTime.getTime() > validTime.getTime()){
            logger.info("【双重预防】时间异常,停止调度,等待异常重置");
            throw new BusinessException(E.DATA_STATUS_NOT_EXIST,"时间异常,停止调度");
        }
 
        PreventReportConfig reportConfigById = preventReportConfigService.getReportConfigById(SyncEnum.REPORT_CONFIG__CHECK_RECORD.getCode());
        //封装任务数据
        checkTask.setId(taskId);
        checkTask.setUuid(uuid);
        checkTask.setStartTime(checkTime);
        checkTask.setValidTime(validTime);
        checkTask.setNoticeTime(noticeTime);
        checkTask.setGmtCreate(date);
        checkTask.setGmtModitify(date);
        checkTask.setCreateUserName(workById.getCreateByUserName());
        checkTask.setLastEditUserName(workById.getCreateByUserName());
        checkTask.setResult((byte) 3);//排查结果:1-正常,2-存在隐患,3-未处理
        checkTask.setTaskStatus(WorkStatusEnum.TASK_WAIT.getCode());
        checkTask.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
        checkTask.setCheckWorkId(workId);
        checkTask.setCheckWorkUuid(workById.getUuid());
        checkTask.setTaskCode(workById.getCheckWorkName());
        checkTask.setExecUserId(null);
        checkTask.setExecUserName(null);
        checkTask.setEnterpriseId((long) 1);
        checkTask.setEnterpriseUuid("111");
        checkTask.setDesc(null);
        checkTask.setExecDepId(workById.getExecDepId());
        checkTask.setExecDepUuid(null);
        checkTask.setTaskType(workById.getCheckWorkType());
        checkTask.setTaskBelong((byte) 1);
        checkTask.setBelongTime(null);
//        if (workById.getCheckWorkStatus().equals(WorkStatusEnum.TASK_TYPE_AUTO.getCode())){
//            checkTask.setTaskType((byte) 1);
//        }
//        if (workById.getCheckWorkStatus().equals(WorkStatusEnum.TASK_TYPE_USER_ADD.getCode())){
//            checkTask.setTaskType((byte) 2);
//        }
        //任务参数
        checkTask.setCheckTaskUnitId(workById.getId());
        checkTask.setCheckTaskUnitUuid(workById.getUuid());
        checkTask.setReportTime(null);
        //读取上报主配置,进行任务记录上报配置,如果开启上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_ON.getCode()){
            //自动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_HAND_EXEC_CONFIG.getCode()){
                //设置上报状态为-等待上报
                checkTask.setReportStatus(SyncEnum.SYNC_WAIT_EXEC.getCode());
                //设置本条数据上报开关为-开启
                checkTask.setReportSwitch(SyncEnum.REPORT_ON.getCode());
                //设置本条数据上报更新时间
                checkTask.setUpdateReportDataTime(date);
            }
            //手动上报
            if (reportConfigById.getReportType() == SyncEnum.REPORT_AUTO_EXEC_CONFIG.getCode()){
                //设置上报状态为-不上报
                checkTask.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
                //设置本条数据上报开关为-关闭
                checkTask.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
                //设置本条数据上报更新时间
                checkTask.setUpdateReportDataTime(date);
            }
        }
        //如果主配置关闭上报功能
        if (reportConfigById.getReportState() == SyncEnum.REPORT_OFF.getCode()){
            //设置上报状态为-不上报
            checkTask.setReportStatus(SyncEnum.SYNC_NOT_EXEC.getCode());
            //设置本条数据上报开关为-关闭
            checkTask.setReportSwitch(SyncEnum.REPORT_OFF.getCode());
            //设置本条数据上报更新时间
            checkTask.setUpdateReportDataTime(date);
        }
 
        int step = 1;
 
        /**写入任务信息*/
        int taskResult = preventDangerCheckTaskService.saveTask(checkTask);
 
        if (taskResult > 0) {
            PreventTaskAndMeasure taskAndMeasure = new PreventTaskAndMeasure();
            //查询任务单元对应的管控错措施列表
            List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(workById.getTaskUnitId());
            //如果单元内措施列表不为空,遍历集合,取出措施id
            if (listByUnitId != null && listByUnitId.size() > 0){
                /**保存管控措施与任务的关联 */
                for (PreventTaskUnitAndMeasure taskUnitAndMeasure : listByUnitId) {
                    //得到管控措施对应的检查内容
                    PreventDangerCheckContent checkContent = preventDangerCheckContentService.getCheckContentByMeasureId(taskUnitAndMeasure.getMeasureId());
                    //封装数据
                    taskAndMeasure.setCheckTaskId(taskId);
                    taskAndMeasure.setControlMeasureId(taskUnitAndMeasure.getMeasureId());
                    taskAndMeasure.setCheckContent(checkContent.getCheckContent());
                    taskAndMeasure.setCheckResult((byte) 3);//单项检查结果:1-正常,2-异常,3-未处理
                    taskAndMeasure.setDeleteStatus(StatusEnum.DELETE_STATUS_USE.getCode());
                    int result = preventTaskAndMeasureService.saveTaskAndMeasure(taskAndMeasure);
                    if (result == 0){
                        throw new BusinessException(E.ADD_FAIL, "保存对应检查内容失败");
                    }
                }
            }
            step = 2;
        }else {
            throw new BusinessException(E.ADD_FAIL, "保存任务信息失败");
        }
 
        /**修改作业信息*/
        if (step == 2){
            CheckWorkAutoUpdateParams updateWorkParams = new CheckWorkAutoUpdateParams();
            //封装修改参数
            updateWorkParams.setId(workById.getId());
            updateWorkParams.setLastCheckTime(checkTime);
            updateWorkParams.setNextCheckTime(nextCheckTime);
            if (workById.getCheckWorkType() == 2){
                updateWorkParams.setCheckWorkStatus((byte) 2);
            }else if (workById.getCheckWorkType() == 1){
                updateWorkParams.setCheckWorkStatus((byte) 4);
            }
            int result = preventDangerCheckWorkService.updateCheckWorkLastTimeAndNextTime(updateWorkParams);
            System.out.println("\n【##】修改作业状态成功" + new Date());
            if (result == 0) {
                throw new BusinessException(ResultCodes.SERVER_UPDATE_ERROR);
            }
            step = 3;
        }else {
            throw new BusinessException(E.ADD_FAIL, "修改下次调度时间失败");
        }
        // todo
 
        if(step == 3){
            //发送执行提醒消息
            PreventNoticeExecTaskMsg preventNoticeExecTaskMsg = new PreventNoticeExecTaskMsg();
            preventNoticeExecTaskMsg.setTaskId(taskId);
            preventNoticeExecTaskMsg.setNoticeTaskTime(noticeTime);
            System.out.println("发送mq消息");
            SendResult sendResult = rocketMQTemplate.syncSend(preventNoticeTaskTopic, preventNoticeExecTaskMsg);
            System.out.println("发送成功");
            if (sendResult.getSendStatus() != SendStatus.SEND_OK){
                throw new RuntimeException("MQ发送任务通知信息失败");
            }
            logger.info("\n【##】向Notice消息\t" + "MSG ID : " + sendResult.getMsgId()+"\t" +
                    "预计执行时间" + preventNoticeExecTaskMsg.getNoticeTaskTime());
            step = 4;
        }else {
            logger.info("第3步出错");
        }
 
        /**向MQ发送消息*/
        if (step == 4){
            //发送待执行消息
            PreventWaitExecTaskMsg preventWaitExecTaskMsg = new PreventWaitExecTaskMsg();
            //TASK ID
            preventWaitExecTaskMsg.setTaskId(taskId);
            //预期执行时间
            preventWaitExecTaskMsg.setExecTaskTime(workById.getNextCheckTime());
            SendResult sendResult = rocketMQTemplate.syncSend(preventWaitWorkTopic, preventWaitExecTaskMsg);
            if (sendResult.getSendStatus() != SendStatus.SEND_OK){
                throw new RuntimeException("MQ发送作业等待信息失败");
            }
            logger.info("\n【##】向WaitExec消息\t" + "MSG ID : " + sendResult.getMsgId()+"\t" +
                    "预计执行时间" + preventWaitExecTaskMsg.getExecTaskTime());
            step = 5;
        }else {
            logger.info("第4步出错");
        }
 
        if(step == 5){
            //发送 改为 任务可调度状态
            if (workById.getCheckWorkType() == 1){
                PreventTimeOutTaskMsg preventTimeOutTaskMsg = new PreventTimeOutTaskMsg();
                preventTimeOutTaskMsg.setTaskId(taskId);
                preventTimeOutTaskMsg.setWorkId(workId);
                preventTimeOutTaskMsg.setOutTaskTime(validTime);
                SendResult sendResult =rocketMQTemplate.syncSend(preventTimeOutTaskTopic, preventTimeOutTaskMsg);
                if (sendResult.getSendStatus() != SendStatus.SEND_OK){
                    throw new RuntimeException("MQ发送任务状态变更通知信息失败");
                }
                logger.info("\n【##】向TimeOut消息\t"+ "MSG ID : " + sendResult.getMsgId()+"\t" +
                        "预计执行时间" +preventTimeOutTaskMsg.getOutTaskTime());
            }
            step = 6;
            createResult = true;
        }else {
            logger.info("第5步出错");
        }
 
        if (step ==6) {
            System.out.println("\n【##】任务创建完成:" + taskId + "----任务开始时间" + workById.getNextCheckTime());
        }else {
            logger.info("第6步出错");
        }
 
//        System.out.println("\n【##】任务创建完成:" + taskId + "----任务开始时间" + workById.getNextCheckTime());
//        createResult = true;
 
        //释放分布式锁
        createTaskLock.unlock();
 
        return createResult;
    }
 
    /**
     * 隐患排查任务-数据上报
     */
    @Transactional
    @Override
    public ResultVO<PreventDangerCheckTask> updateTask(Long userId, PreventDangerCheckTaskUpdateReqDTO taskUpdateReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("任务结果提交成功");
        PreventDangerCheckTaskUpdateParams updateParams = new PreventDangerCheckTaskUpdateParams();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userInfo = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userInfo)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
 
        //获取执行人信息
        PreventDangerCheckTask taskById = preventDangerCheckTaskService.getTaskById(taskUpdateReqDTO.getId());
        UserRPCRespDTO execUser = (UserRPCRespDTO)rpcResult.getData();
        if (taskById.getExecUserId() .equals(userInfo.getUid())){
            throw new BusinessException(E.DATA_PARAM_NULL, "非认领人,无法操作");
        }
        if (taskUpdateReqDTO.getCheckResults().size() <= 0){
            throw new BusinessException(E.DATA_PARAM_NULL, "检查结果不能为空");
        }
 
        //获取需要填充的信息
        int tag = 0;
        Byte resultTag = 1;
        PreventCheckResultParams checkResultParams = new PreventCheckResultParams();
        for (CheckResultReportDO CheckResults : taskUpdateReqDTO.getCheckResults()) {
            //封装填报参数
            checkResultParams.setId(CheckResults.getId());
            checkResultParams.setCheckResult(CheckResults.getCheckResult());
            checkResultParams.setControlMeasureId(CheckResults.getControlMeasureId());
            preventTaskAndMeasureService.updateCheckResult(checkResultParams);
            tag = tag + 1;
            if (CheckResults.getCheckResult().equals(WorkStatusEnum.TASK_EXEC_RESULT_ERROR.getCode())){
                 resultTag = WorkStatusEnum.TASK_EXEC_RESULT_ERROR.getCode();
            }
        }
        if (tag < taskUpdateReqDTO.getCheckResults().size()){
            throw new BusinessException(E.UPDATE_FAIL, "填报遇到问题,请稍后再尝试");
        }
 
        updateParams.setId(taskUpdateReqDTO.getId());
        updateParams.setLastEditUserName(userInfo.getRealName());
        updateParams.setGmtModitify(new Date());
        updateParams.setTaskStatus(WorkStatusEnum.TASK_EXEC_USED.getCode());
        updateParams.setResult(resultTag);
        int result = preventDangerCheckTaskService.updateTask(updateParams);
        if (result < 1){
            throw new BusinessException(E.UPDATE_FAIL, "任务结果提交失败");
        }
 
        return resultVO;
    }
 
    /**
     * 隐患排查任务-删除
     */
    @Override
    public ResultVO<PreventDangerCheckTask> deleteTask(Long userId, PreventDangerCheckTaskDeleteReqDTO taskDeleteReqDTO) {
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("删除成功");
        PreventDeleteParams deleteParams = new PreventDeleteParams();
 
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(userId);
        if (rpcResult == null) {
            throw new BusinessException(ResultCodes.RPC_RESULT_NULL);
        }
        if (!ResultCodes.OK.getCode().equals(rpcResult.getCode())) {
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
        if (rpcResult.getData() == null) {
            throw new BusinessException(ResultCodes.RPC_DATA_NULL);
        }
        if (!(rpcResult.getData() instanceof UserRPCRespDTO)) {
            throw new BusinessException(ResultCodes.RPC_DATA_TYPE_NOT_MATCH);
        }
        UserRPCRespDTO userById = (UserRPCRespDTO)rpcResult.getData();
 
        if (ObjectUtils.isEmpty(userById)) {
            throw new BusinessException(ResultCodes.CLIENT_IDENTITY_CHECK_ERROR);
        }
        //校验参数
        if (taskDeleteReqDTO.getId() == null ){
            throw new BusinessException(E.DATA_PARAM_NULL, "请选择正确的查询内容");
        }
 
        UserInfo userInfo = userService.getByUserId(userId);
        if (userInfo.getType().equals(3)){
            throw new BusinessException(E.DATA_PARAM_NULL, "非管理员,无法删除");
        }
        Date date = new Date();
        //封装参数
        deleteParams.setId(taskDeleteReqDTO.getId());
        deleteParams.setGmtModitify(date);
        deleteParams.setLastEditUserName(userInfo.getRealname());
        deleteParams.setUpdateReportDataTime(date);
 
        int deleteResult = preventDangerCheckTaskService.deleteTask(deleteParams);
 
        resultVO.setCount(deleteResult);
 
        return resultVO;
    }
 
    /**
     * 隐患排查任务单元-批量删除
     */
    @Override
    public ResultVO<PreventDangerCheckTaskUnit> deleteBatchTaskUnit(ContextCacheUser currentUser, DeleteBatchReqDTO deleteBatchReqDTO) {
 
        ResultVO resultVO = new ResultVO<>();
        resultVO.setCode("200");
        resultVO.setMsg("删除成功");
        int i = 0;
 
        //校验参数
        if (ObjectUtils.isEmpty(deleteBatchReqDTO)){
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "请选择正确的删除内容");
        }
        //获取需要填充的信息
        Date date = new Date();
        for (Long id : deleteBatchReqDTO.getIdList()) {
            PreventDangerCheckTaskUnitDeleteParams deleteParams = new PreventDangerCheckTaskUnitDeleteParams();
            //封装删除需要的参数
            deleteParams.setId(id);
            deleteParams.setGmtModitify(date);
            deleteParams.setLastEditUserName(currentUser.getRealName());
 
            //检查该单元是否有措施
            List<PreventTaskUnitAndMeasure> listByUnitId = preventTaskUnitAndMeasureService.getListByUnitId(id);
            //重置关联关系
            if (listByUnitId != null && listByUnitId.size() > 0){
                PreventTaskUnitAndMeasureParams taskUnitAndMeasureParams = new PreventTaskUnitAndMeasureParams();
                taskUnitAndMeasureParams.setTaskUnitId(id);
                taskUnitAndMeasureParams.setGmtModitify(date);
                taskUnitAndMeasureParams.setLastEditUserName(currentUser.getRealName());
                taskUnitAndMeasureParams.setDeleteStatus(StatusEnum.DELETE_STATUS_DISCARD.getCode());
                preventTaskUnitAndMeasureService.deleteTaskUnitAndMeasure(taskUnitAndMeasureParams);
            }
 
            //删除单元
            int result = preventDangerCheckTaskUnitService.deleteTaskUnit(deleteParams);
            i++;
        }
 
        resultVO.setCount(i);
 
        return resultVO;
 
    }
 
}