郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package com.gkhy.safePlatform.safeCheck.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountGroupService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.GroupRPCRespDTO;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.idService.SnowFlow;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.safeCheck.common.RocketMQTemplateHelper;
import com.gkhy.safePlatform.safeCheck.entity.SafeCheckTaskUnit;
import com.gkhy.safePlatform.safeCheck.entity.SafeCheckTaskUnitAndWorkDO;
import com.gkhy.safePlatform.safeCheck.entity.SafeCheckUnitAndQuota;
import com.gkhy.safePlatform.safeCheck.entity.SafeCheckWork;
import com.gkhy.safePlatform.safeCheck.enums.*;
import com.gkhy.safePlatform.safeCheck.model.dto.req.SafeCheckTaskUnitPageReqDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.req.SafeCheckTaskUnitReqDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.req.SafeCheckUnitAndQuotaReqDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.resp.SafeCheckTaskUnitPageRespDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.resp.SafeCheckTaskUnitRespDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.resp.SafeCheckUnitAndQuotaRespDTO;
import com.gkhy.safePlatform.safeCheck.model.query.TaskUnitDBQuery;
import com.gkhy.safePlatform.safeCheck.mq.msg.SafeCheckCreateTaskMsg;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckTaskManagerService;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckTaskUnitSchedulesService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckTaskUnitService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckUnitAndQuotaService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckWorkService;
import com.gkhy.safePlatform.safeCheck.util.UserInfoUtil;
import com.gkhy.safePlatform.safeCheck.util.timeChangeUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.producer.SendResult;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.swing.text.AbstractDocument;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service
public class SafeCheckTaskManagerServiceImpl implements SafeCheckTaskManagerService {
 
    @DubboReference(check = false)
    private AccountAuthService accountAuthService;
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @DubboReference(check = false)
    private AccountGroupService accountGroupService;
 
    @Autowired
    private RocketMQTemplateHelper rocketMQTemplateHelper;
 
    @Value("${rocketmq.topic.safeCheckCreateTaskTopic}")
    private String safeCheckCreateTaskTopic;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private SafeCheckTaskUnitSchedulesService taskUnitSchedulesService;
 
    @Autowired
    private SafeCheckTaskUnitService safeCheckTaskUnitService;
 
    @Autowired
    private SafeCheckWorkService safeCheckWorkService;
 
    @Autowired
    private SafeCheckUnitAndQuotaService safeCheckUnitAndQuotaService;
 
 
    /**
     * @description 新建巡检任务单元
     */
    @Transactional
    @Override
    public void saveTaskUnit(ContextCacheUser currentUser, SafeCheckTaskUnitReqDTO safeCheckTaskUnitReqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
 
        this.paramIsValid(safeCheckTaskUnitReqDTO);
 
        //0、加分布式锁
        String lockName = "SAFECHECK_TASK_UNIT_CREATE_"+safeCheckTaskUnitReqDTO.getUnitName();
        RLock createTaskUnitLock = redissonClient.getLock(lockName);
        createTaskUnitLock.lock(3, TimeUnit.SECONDS);
 
        //判断任务单元的名称是否存在
        SafeCheckTaskUnit unitNameIsExit = safeCheckTaskUnitService.taskUnitNameIsExit(safeCheckTaskUnitReqDTO.getUnitName(), DelectStatusEnum.DELECT_NO.getStatus().intValue());
        if (unitNameIsExit != null){
            throw new AusinessException(E.DATA_DATABASE_EXIST,"巡检单元名称已存在");
        }
        //雪花算法生成id和uuid
        SnowFlow snowFlow = new SnowFlow();//雪花算法生成器
        String taskUnitUuid = UUID.randomUUID().toString();
        long taskUnitId = snowFlow.nextId();
        Date date = new Date();
 
        SafeCheckTaskUnit taskUnit = new SafeCheckTaskUnit();
        taskUnit.setId(taskUnitId);
        taskUnit.setUuid(taskUnitUuid);
        taskUnit.setDeleteStatus(DelectStatusEnum.DELECT_NO.getStatus());
        taskUnit.setTaskUnitStatus(TaskUnitStatusEnum.TASK_UNIT_STATUS_OPEN.getStatus());
        taskUnit.setUnitName(safeCheckTaskUnitReqDTO.getUnitName());
        taskUnit.setEnterpriseId(userInfo.getEnterprise().getId());
        taskUnit.setEnterpriseUuid(userInfo.getEnterprise().getUuid());
        taskUnit.setCreateUserName(userInfo.getRealName());
        taskUnit.setFileAddress(safeCheckTaskUnitReqDTO.getFileAddress());
 
        taskUnit.setGmtCreate(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
        taskUnit.setGmtModitify(date);
 
 
        String workUuid = UUID.randomUUID().toString();
        long workId = snowFlow.nextId();
 
        SafeCheckWork work = new SafeCheckWork();
        BeanUtils.copyProperties(safeCheckTaskUnitReqDTO,work);
        work.setId(workId);
        work.setUuid(workUuid);
        work.setDeleteStatus(DelectStatusEnum.DELECT_NO.getStatus());
        work.setWorkStatus(WorkStatusEnum.WORK_STATUS_OPEN.getStatus());
        work.setNextNoticeTime(timeChangeUtil.nextNoticeTime(safeCheckTaskUnitReqDTO.getNoticeTime(),
                safeCheckTaskUnitReqDTO.getNoticeTimeUnit(),safeCheckTaskUnitReqDTO.getFirstStartTime()));
        work.setNextCheckTime(safeCheckTaskUnitReqDTO.getFirstStartTime());
        work.setLastCheckTime(safeCheckTaskUnitReqDTO.getFirstStartTime());
        work.setUnitId(taskUnitId);
        work.setUnitUuid(taskUnitUuid);
        int saveWorkResult = safeCheckWorkService.saveWork(work);
        if (saveWorkResult == 0){
            throw new AusinessException(E.ADD_FAIL,"调度信息插入失败");
        }
 
        //调度信息插入成功后才会往数据库中批量插入巡检链中的巡检点
        List<SafeCheckUnitAndQuotaReqDTO> points = safeCheckTaskUnitReqDTO.getPoints();
        List<SafeCheckUnitAndQuota> safeCheckUnitAndQuotas = new ArrayList<>();
        safeCheckUnitAndQuotas = points.stream().map((point)->{
            SafeCheckUnitAndQuota safeCheckUnitAndQuota = new SafeCheckUnitAndQuota();
            BeanUtils.copyProperties(point,safeCheckUnitAndQuota);
            safeCheckUnitAndQuota.setUnitId(taskUnitId);
            safeCheckUnitAndQuota.setDeleteStatus(DelectStatusEnum.DELECT_NO.getStatus());
            safeCheckUnitAndQuota.setCreateUserName(userInfo.getRealName());
            safeCheckUnitAndQuota.setGmtCreate(date);
            safeCheckUnitAndQuota.setLastEditUserName(userInfo.getRealName());
            safeCheckUnitAndQuota.setGmtModitify(date);
            return safeCheckUnitAndQuota;
        }).collect(Collectors.toList());
 
        int saveUnitAndQuotaResult = safeCheckUnitAndQuotaService.saveUnitAndQuotas(safeCheckUnitAndQuotas);
        if (saveUnitAndQuotaResult != safeCheckUnitAndQuotas.size() ){
            throw new AusinessException(E.ADD_FAIL,"巡检链插入失败");
        }
        //保存任务
        taskUnit.setPointsLength(saveUnitAndQuotaResult);
        safeCheckTaskUnitService.saveTaskUnit(taskUnit);
 
        //如果通知时间与当前时间的时间间隔小于任务单元调度的时间,就立即创建任务 目前调度时间是5分钟
        if (work.getNextNoticeTime().getTime() - (new Date()).getTime() < 5*60*1000){
            SafeCheckCreateTaskMsg createTaskMsg = new SafeCheckCreateTaskMsg();
            createTaskMsg.setWorkId(workId);
            //当前时间30秒后创建任务
            createTaskMsg.setCreateTaskTime(new Date((new Date()).getTime()+30*1000));
            rocketMQTemplateHelper.syncSend(safeCheckCreateTaskTopic, createTaskMsg);
 
            work.setWorkStatus(WorkStatusEnum.WORK_STATUS_DISPATCHING.getStatus());
            taskUnitSchedulesService.updateWorkStatusById(work);
        }
 
        //释放分布式锁
        createTaskUnitLock.unlock();
    }
 
    /**
     * @description 根据任务单元id删除任务单元
     */
    @Transactional
    @Override
    public void deleteTaskUnit(ContextCacheUser currentUser, Long taskUnitId) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
        Date date = new Date();
        //先判断这个point是否存在
        if (taskUnitId == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元id不能为空");
        }
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(taskUnitId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
        taskUnit.setGmtModitify(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
        taskUnit.setDeleteStatus(DelectStatusEnum.DELECT_YES.getStatus());
        List<Integer> taskUnitStatus = new ArrayList<>();
        taskUnitStatus.add(TaskUnitStatusEnum.TASK_UNIT_STATUS_CLOSE.getStatus().intValue());
        taskUnitStatus.add(TaskUnitStatusEnum.TASK_UNIT_STATUS_EXPIRE.getStatus().intValue());
        //step1:先将任务单元表状态置为删除
        int deleteTaskUnitResult = safeCheckTaskUnitService.deleteTaskUnit(taskUnit, taskUnitStatus, DelectStatusEnum.DELECT_NO.getStatus().intValue());
        if (deleteTaskUnitResult == 0){
            throw new AusinessException(E.NOT_DELETE,"开启状态的任务单元不允许删除,删除失败");
        }
 
        SafeCheckWork work = safeCheckWorkService.getWorkByTaskUnitId(taskUnitId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
 
        work.setDeleteStatus(DelectStatusEnum.DELECT_YES.getStatus());
        List<Integer> workStatus = new ArrayList<>();
        workStatus.add(WorkStatusEnum.WORK_STATUS_CLOSE.getStatus().intValue());
        workStatus.add(WorkStatusEnum.WORK_STATUS_EXPIRE.getStatus().intValue());
        //step2:将调度信息删除状态置为删除
        int deleteWorkResult = safeCheckWorkService.deleteWorkById(work,workStatus,DelectStatusEnum.DELECT_NO.getStatus().intValue());
        if (deleteWorkResult == 0){
            throw new AusinessException(E.NOT_DELETE,"开启、调度中状态的调度单元不允许删除,删除失败");
        }
 
 
        List<SafeCheckUnitAndQuota> unitAndQuotas = safeCheckUnitAndQuotaService.listByTaskUnitId(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        //step3:查看巡检单元与巡检链表中是否存在关联数据
 
        if (unitAndQuotas != null && unitAndQuotas.size() > 0){
            unitAndQuotas = unitAndQuotas.stream().map((unitAndQuota)->{
                unitAndQuota.setDeleteStatus(DelectStatusEnum.DELECT_YES.getStatus());
                unitAndQuota.setLastEditUserName(userInfo.getRealName());
                unitAndQuota.setGmtModitify(date);
                return unitAndQuota;
            }).collect(Collectors.toList());
            int updateUnitAndQuotaResult  = safeCheckUnitAndQuotaService.updateUnitAndQuotasById(unitAndQuotas,DelectStatusEnum.DELECT_NO.getStatus().intValue());
            if (updateUnitAndQuotaResult != unitAndQuotas.size()){
                throw new AusinessException(E.NOT_DELETE,"巡检任务单元:巡检链删除失败");
            }
        }
    }
 
 
    /**
     * @description 根据id更新巡检任务单元信息
     */
    @Transactional
    @Override
    public void updateTaskUnitMainById(ContextCacheUser currentUser, SafeCheckTaskUnitReqDTO safeCheckTaskUnitReqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
        this.paramIsValid(safeCheckTaskUnitReqDTO);
 
        String unitName = safeCheckTaskUnitReqDTO.getUnitName();
 
        //0、加分布式锁
        String lockName = "SAFECHECK_TASK_UNIT_MAIN_UPDATE_"+unitName;
        RLock regionUpdateTaskUnitMainLock = redissonClient.getLock(lockName);
        regionUpdateTaskUnitMainLock.lock(3, TimeUnit.SECONDS);
 
        SafeCheckTaskUnit newTaskUnit = safeCheckTaskUnitService.taskUnitNameIsExit(safeCheckTaskUnitReqDTO.getUnitName(), DelectStatusEnum.DELECT_NO.getStatus().intValue());
        Long taskid = safeCheckTaskUnitReqDTO.getId();
        if (taskid == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元id不能为空");
        }
        if (!taskid.equals(newTaskUnit.getId())){
            throw new AusinessException(E.DATA_DATABASE_EXIST,"巡检单元名称已存在");
        }
        Date date = new Date();
 
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(safeCheckTaskUnitReqDTO.getId(), DelectStatusEnum.DELECT_NO.getStatus().intValue());
        taskUnit.setUnitName(safeCheckTaskUnitReqDTO.getUnitName());
        taskUnit.setLastEditUserName(userInfo.getRealName());
        taskUnit.setGmtModitify(date);
        safeCheckTaskUnitService.updateTaskUnitNameById(taskUnit,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        SafeCheckWork oldWork = safeCheckWorkService.getWorkByTaskUnitId(safeCheckTaskUnitReqDTO.getId(), DelectStatusEnum.DELECT_NO.getStatus().intValue());
        Date oldNextCheckTime = oldWork.getNextCheckTime();
        BeanUtils.copyProperties(safeCheckTaskUnitReqDTO,oldWork,"id","uuid");
        if (!safeCheckTaskUnitReqDTO.getFirstStartTime().equals(oldNextCheckTime)){
            oldWork.setWorkStatus(WorkStatusEnum.WORK_STATUS_OPEN.getStatus());
        }
        oldWork.setNextNoticeTime(timeChangeUtil.nextNoticeTime(safeCheckTaskUnitReqDTO.getNoticeTime()
                ,safeCheckTaskUnitReqDTO.getNoticeTimeUnit(),safeCheckTaskUnitReqDTO.getFirstStartTime()));
        oldWork.setNextCheckTime(safeCheckTaskUnitReqDTO.getFirstStartTime());
        safeCheckWorkService.updateWorkInfoById(oldWork,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        //1、释放分布式锁
        regionUpdateTaskUnitMainLock.unlock();
    }
 
    /**
     * @description 根据id更新巡检任务单元状态
     */
    @Transactional
    @Override
    public String updateTaskUnitStatusById(ContextCacheUser currentUser, SafeCheckTaskUnitReqDTO safeCheckTaskUnitReqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
 
        Long taskUnitId = safeCheckTaskUnitReqDTO.getId();
        Integer newTaskUnitStatus = safeCheckTaskUnitReqDTO.getTaskUnitStatus().intValue();
        Date date = new Date();
 
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
        Integer oldTaskUnitStatus = taskUnit.getTaskUnitStatus().intValue();
        if (newTaskUnitStatus == oldTaskUnitStatus){
            throw new AusinessException(E.UPDATE_FAIL,"巡检任务单元已处于"+TaskUnitStatusEnum.getValue(safeCheckTaskUnitReqDTO.getTaskUnitStatus())+"状态");
        }
        taskUnit.setTaskUnitStatus(newTaskUnitStatus.byteValue());
        taskUnit.setGmtModitify(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
        //修改任务单元的状态
        safeCheckTaskUnitService.updateTaskUniStatusById(taskUnit,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        SafeCheckWork work = safeCheckWorkService.getWorkByTaskUnitId(taskUnitId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
        work.setWorkStatus(newTaskUnitStatus.byteValue());
        //修改任务单元关联调度表的状态
        safeCheckWorkService.updateWorkStatusById(work,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        //修改巡检链数据的状态
        List<SafeCheckUnitAndQuota> unitAndQuotas = safeCheckUnitAndQuotaService.listByTaskUnitId(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        if (unitAndQuotas != null && unitAndQuotas.size() > 0){
            unitAndQuotas = unitAndQuotas.stream().map((unitAndQuota)->{
                unitAndQuota.setLastEditUserName(userInfo.getRealName());
                unitAndQuota.setGmtModitify(date);
                return unitAndQuota;
            }).collect(Collectors.toList());
            int updateUnitAndQuotaResult  = safeCheckUnitAndQuotaService.updateUnitAndQuotasById(unitAndQuotas,DelectStatusEnum.DELECT_NO.getStatus().intValue());
            if (updateUnitAndQuotaResult != unitAndQuotas.size()){
                throw new AusinessException(E.UPDATE_FAIL,"巡检任务单元:巡检链状态修改失败");
            }
        }
        String newTaskUnitStatusString = TaskUnitStatusEnum.getValue(newTaskUnitStatus.byteValue());
        return newTaskUnitStatusString;
    }
 
 
    /**
     * @description 根据任务单元id查询任务单元
     */
    @Override
    public SafeCheckTaskUnitRespDTO getTaskUnitById(Long taskUnitId) {
        if (taskUnitId == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检单元id不能为空");
        }
        SafeCheckTaskUnitRespDTO taskUnitRespDTO = new SafeCheckTaskUnitRespDTO();
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
        BeanUtils.copyProperties(taskUnit,taskUnitRespDTO);
 
        SafeCheckWork work = safeCheckWorkService.getWorkByTaskUnitId(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
        BeanUtils.copyProperties(work,taskUnitRespDTO,"id","uuid");
        List<SafeCheckUnitAndQuota> unitAndQuotas = safeCheckUnitAndQuotaService.listByTaskUnitId(taskUnitId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        if (unitAndQuotas == null || unitAndQuotas.size()==0){
            taskUnitRespDTO.setPoints(null);
        }
        List<SafeCheckUnitAndQuotaRespDTO> points = unitAndQuotas.stream().map((unitAndQuota)->{
            SafeCheckUnitAndQuotaRespDTO unitAndQuotaRespDTO = new SafeCheckUnitAndQuotaRespDTO();
            BeanUtils.copyProperties(unitAndQuota,unitAndQuotaRespDTO);
            return unitAndQuotaRespDTO;
        }).collect(Collectors.toList());
        taskUnitRespDTO.setPoints(points);
        return taskUnitRespDTO;
    }
 
    /**
     * @description 查询所有巡检任务单元数据并进行分页(包含条件查询)
     */
    @Override
    public IPage listTaskUnitByPage(Page pageInfo, SafeCheckTaskUnitPageReqDTO safeCheckTaskUnitPageReqDTO,ContextCacheUser currentUser) {
        TaskUnitDBQuery taskUnitDBQuery = new TaskUnitDBQuery();
        BeanUtils.copyProperties(safeCheckTaskUnitPageReqDTO,taskUnitDBQuery);
 
        List<Long> depIds = null;
        if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){
            ResultVO<List<Long>> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId());
            depIds = (List<Long>) resultVO.getData();
        }
        taskUnitDBQuery.setDepIds(depIds);
        UserRPCRespDTO userInfo;
        if (safeCheckTaskUnitPageReqDTO.getCreateUserId() == null) {
            taskUnitDBQuery.setCreateUserName(null);
        }else {
            //获取用户信息
            ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(safeCheckTaskUnitPageReqDTO.getCreateUserId());
            userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
            if (userInfo == null){
                taskUnitDBQuery.setCreateUserName(null);
            }else {
                String realName = userInfo.getRealName();
                taskUnitDBQuery.setCreateUserName(realName);
            }
        }
        IPage pageInfoResult = safeCheckTaskUnitService.listTaskUnitByPage(pageInfo,taskUnitDBQuery);
        List<SafeCheckTaskUnitAndWorkDO> records = pageInfoResult.getRecords();
        if (records == null || records.size() == 0){
            pageInfoResult.setRecords(null);
        }
        List<SafeCheckTaskUnitPageRespDTO> pageRespDTOs =records.stream().map((safeCheckTaskUnitAndWorkDO)->{
            SafeCheckTaskUnitPageRespDTO pageRespDTO = new SafeCheckTaskUnitPageRespDTO();
            BeanUtils.copyProperties(safeCheckTaskUnitAndWorkDO,pageRespDTO);
            pageRespDTO.setCheckCycleUnit(TimeUnitEnum.getValue(safeCheckTaskUnitAndWorkDO.getCheckCycleUnit()));
            return pageRespDTO;
        }).collect(Collectors.toList());
        pageInfoResult.setRecords(pageRespDTOs);
        return pageInfoResult;
    }
 
 
    /**
     * @description 根据巡检点的id删除巡检链中的巡检点
     */
    @Transactional
    @Override
    public void deleteTaskUnitPoint(ContextCacheUser currentUser, HashMap<String, Object> taskUnitAndPointId) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
        Date date = new Date();
        Long taskUnitId = (Long) taskUnitAndPointId.get("taskUnitId");
        if (taskUnitAndPointId == null){
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        if (taskUnitAndPointId.get("id") == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点id不能为空");
        }
        if (taskUnitId == null){
            throw  new AusinessException(E.DATA_PARAM_NULL,"巡检单位id不能为空");
        }
        SafeCheckUnitAndQuota unitAndQuota = safeCheckUnitAndQuotaService.
                getUnitAndQuotaByid(taskUnitAndPointId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        unitAndQuota.setDeleteStatus(DelectStatusEnum.DELECT_YES.getStatus());
        unitAndQuota.setGmtModitify(date);
        unitAndQuota.setLastEditUserName(userInfo.getRealName());
 
        safeCheckUnitAndQuotaService.deleteTaskUnitPoint(unitAndQuota,DelectStatusEnum.DELECT_NO.getStatus().intValue());
        //删除成功后更新巡检任务单元表中巡检链的长度
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(taskUnitId, DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        taskUnit.setPointsLength(taskUnit.getPointsLength()-1);
        taskUnit.setGmtModitify(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
 
        safeCheckTaskUnitService.updateTaskUnitPointsLengthById(taskUnit,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
    }
 
    /**
     * @description 根据巡检任务单元的id及巡检单元与巡检指标关联表的id对巡检点信息内容进行修改
     */
    @Transactional
    @Override
    public void updateTaskUnitPointById(ContextCacheUser currentUser, SafeCheckUnitAndQuotaReqDTO safeCheckUnitAndQuotaReqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
        Date date = new Date();
        this.pointParamIsValid(safeCheckUnitAndQuotaReqDTO);
 
        HashMap<String, Object> taskUnitAndPointId = new HashMap<>();
 
        taskUnitAndPointId.put("taskUnitId",safeCheckUnitAndQuotaReqDTO.getUnitId());
        taskUnitAndPointId.put("id",safeCheckUnitAndQuotaReqDTO.getId());
 
        SafeCheckUnitAndQuota unitAndQuota = safeCheckUnitAndQuotaService.
                getUnitAndQuotaByid(taskUnitAndPointId,DelectStatusEnum.DELECT_NO.getStatus().intValue());
        BeanUtils.copyProperties(safeCheckUnitAndQuotaReqDTO,unitAndQuota);
        unitAndQuota.setLastEditUserName(userInfo.getRealName());
        unitAndQuota.setGmtModitify(date);
        safeCheckUnitAndQuotaService.updateTaskUnitPointById(unitAndQuota,DelectStatusEnum.DELECT_NO.getStatus().intValue());
 
        //更新下修改记录
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(safeCheckUnitAndQuotaReqDTO.getUnitId(),
                DelectStatusEnum.DELECT_NO.getStatus().intValue());
        taskUnit.setPointsLength(taskUnit.getPointsLength());
        taskUnit.setGmtModitify(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
 
        safeCheckTaskUnitService.updateTaskUnitPointsLengthById(taskUnit,DelectStatusEnum.DELECT_NO.getStatus().intValue());
    }
 
    /**
     * @description 往巡检链中新增一个巡检点
     */
    @Transactional
    @Override
    public void saveTaskUnitPoint(ContextCacheUser currentUser, SafeCheckUnitAndQuotaReqDTO safeCheckUnitAndQuotaReqDTO) {
        //获取用户信息
        ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(currentUser.getUid());
        UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
        Date date = new Date();
        this.pointParamIsValid(safeCheckUnitAndQuotaReqDTO);
        SafeCheckUnitAndQuota unitAndQuota = new SafeCheckUnitAndQuota();
        BeanUtils.copyProperties(safeCheckUnitAndQuotaReqDTO,unitAndQuota);
        unitAndQuota.setDeleteStatus(DelectStatusEnum.DELECT_NO.getStatus());
        unitAndQuota.setGmtCreate(date);
        unitAndQuota.setCreateUserName(userInfo.getRealName());
        unitAndQuota.setGmtModitify(date);
        unitAndQuota.setLastEditUserName(userInfo.getRealName());
        safeCheckUnitAndQuotaService.saveTaskUnitPoint(unitAndQuota);
 
        //新增成功后更新巡检任务单元表中巡检链的长度
        SafeCheckTaskUnit taskUnit = safeCheckTaskUnitService.getTaskUnitById(safeCheckUnitAndQuotaReqDTO.getUnitId(),
                DelectStatusEnum.DELECT_NO.getStatus().intValue());
        taskUnit.setPointsLength(taskUnit.getPointsLength()+ 1);
        taskUnit.setGmtModitify(date);
        taskUnit.setLastEditUserName(userInfo.getRealName());
 
        safeCheckTaskUnitService.updateTaskUnitPointsLengthById(taskUnit,DelectStatusEnum.DELECT_NO.getStatus().intValue());
    }
 
 
    /**
     * @description 新建巡检单元主数据进行参数校验
     */
    private void paramIsValid(SafeCheckTaskUnitReqDTO safeCheckTaskUnitReqDTO){
        if (safeCheckTaskUnitReqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getUnitName() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元名称不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getWorkType() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元作业类型不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getExecClassgroupId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元执行班组不能为空");
        }
        //todo 暂时不用部门id
//        if (safeCheckTaskUnitReqDTO.getExecDepId() == null){
//            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元执行部门不能为空");
//        }
        if (safeCheckTaskUnitReqDTO.getWorkType() == WorkTypeEnum.WORK_TYPE_CYCLE.getCode()) {
            if (safeCheckTaskUnitReqDTO.getCheckCycle() == 0) {
                throw new AusinessException(E.DATA_PARAM_NULL, "巡检任务单元巡检周期不能为空");
            }
            if (safeCheckTaskUnitReqDTO.getCheckCycleUnit() == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元巡检周期时间单位不能为空");
            }
        }
        if (safeCheckTaskUnitReqDTO.getNoticeTime() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元提前通知时间不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getNoticeTimeUnit() != null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元提前通知时间单位不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getValidTime() == 0 ){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元有效时间不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getValidTimeUnit() != null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元有效时间单位不能为空");
        }
        if (safeCheckTaskUnitReqDTO.getFirstStartTime() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检任务单元首次作业调度时间不能为空");
        }
 
        Date noticeTime = timeChangeUtil.nextNoticeTime(safeCheckTaskUnitReqDTO.getNoticeTime(),
                safeCheckTaskUnitReqDTO.getNoticeTimeUnit(), safeCheckTaskUnitReqDTO.getFirstStartTime());
        //新增任务单元设置的通知时间不能在当前时间之前
        if ((new Date()).compareTo(noticeTime) > 0){
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"任务单元设置的通知时间不能在当前时间之前");
        }
        //任务执行的间隔时间大于单次任务通知时间加上有效时间:也就是下次通知时间肯定要在本次任务执行之后 todo
        //1.获得下次通知时间
        if(safeCheckTaskUnitReqDTO.getWorkType() == WorkTypeEnum.WORK_TYPE_CYCLE.getCode()) {
            Date nextNoticeTime = timeChangeUtil.nextNoticeTimeByCurrentNoticeTime(safeCheckTaskUnitReqDTO.getCheckCycle(),
                    safeCheckTaskUnitReqDTO.getCheckCycleUnit(), noticeTime);
            //2.获得本次任务的有效截至日期
            Date currentTaskvalidTime = timeChangeUtil.nextValidTime(safeCheckTaskUnitReqDTO.getValidTime(), safeCheckTaskUnitReqDTO.getValidTimeUnit()
                    , safeCheckTaskUnitReqDTO.getFirstStartTime());
            if (nextNoticeTime.compareTo(currentTaskvalidTime) <= 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "巡检周期间隔时间需大于巡检通知时间加上巡检执行有效时间");
            }
        }
        if (safeCheckTaskUnitReqDTO.getPoints() == null && safeCheckTaskUnitReqDTO.getPoints().size() == 0){
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"任务单元关联的巡检点不能为空");
        }
        for (SafeCheckUnitAndQuotaReqDTO point : safeCheckTaskUnitReqDTO.getPoints()) {
            this.pointParamIsValidWhenTaskUnitAdd(point);
        }
 
    }
 
    /**
     * @description 任务单元首次创建时包含的point进行数据校验
     */
    private void pointParamIsValidWhenTaskUnitAdd(SafeCheckUnitAndQuotaReqDTO safeCheckUnitAndQuotaReqDTO){
        if (safeCheckUnitAndQuotaReqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点信息不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getPointId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getRegionId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检区域id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getRfidId() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"rfid值不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getQuotaId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检指标id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getQuotaUnit() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检指标单位不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getExecSequence() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"执行顺序不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getDataReportType() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型能为空");
        }
        if(safeCheckUnitAndQuotaReqDTO.getSecondReferenceValue() != null){
            if (safeCheckUnitAndQuotaReqDTO.getSecondReferenceSign() == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"请选择参考值 > or >= 符号");
            }
        }
        if (safeCheckUnitAndQuotaReqDTO.getThirdReferenceValue() != null){
            if (safeCheckUnitAndQuotaReqDTO.getThirdReferenceValue() == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"请选择参考值 < or <= 符号");
            }
        }
    }
 
    /**
     * @description 新增巡检点相关参数校验
     */
    private void pointParamIsValid(SafeCheckUnitAndQuotaReqDTO safeCheckUnitAndQuotaReqDTO){
        if (safeCheckUnitAndQuotaReqDTO == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点信息不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getUnitId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务单元id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getPointId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检点id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getRegionId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检区域id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getRfidId() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"rfid值不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getQuotaId() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检指标id不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getQuotaUnit() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"巡检指标单位不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getExecSequence() == 0){
            throw new AusinessException(E.DATA_PARAM_NULL,"执行顺序不能为空");
        }
        if (safeCheckUnitAndQuotaReqDTO.getDataReportType() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"数据填报类型能为空");
        }
        if(safeCheckUnitAndQuotaReqDTO.getSecondReferenceValue() != null){
            if (safeCheckUnitAndQuotaReqDTO.getSecondReferenceSign() == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"请选择参考值 > or >= 符号");
            }
        }
        if (safeCheckUnitAndQuotaReqDTO.getThirdReferenceValue() != null){
            if (safeCheckUnitAndQuotaReqDTO.getThirdReferenceValue() == null){
                throw new AusinessException(E.DATA_PARAM_NULL,"请选择参考值 < or <= 符号");
            }
        }
    }
}