郑永安
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
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.AccountUserService;
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.exception.AusinessException;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.safeCheck.entity.*;
import com.gkhy.safePlatform.safeCheck.enums.*;
import com.gkhy.safePlatform.safeCheck.model.dto.req.SafeCheckTaskAndQuotaPageReqDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.req.SafeCheckTaskPageReqDTO;
import com.gkhy.safePlatform.safeCheck.model.dto.resp.*;
import com.gkhy.safePlatform.safeCheck.model.query.TaskUnitDBQuery;
import com.gkhy.safePlatform.safeCheck.service.SafeCheckTaskResultManagerService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckPointService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckTaskAndQuotaService;
import com.gkhy.safePlatform.safeCheck.service.baseService.SafeCheckTaskService;
import com.gkhy.safePlatform.safeCheck.util.TimeUtils;
import com.gkhy.safePlatform.safeCheck.util.UserInfoUtil;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import io.lettuce.core.StrAlgoArgs;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
 
@Service
public class SafeCheckTaskResultManagerServiceImpl implements SafeCheckTaskResultManagerService {
 
    @DubboReference(check = false)
    private AccountAuthService accountAuthService;
 
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
 
    @Autowired
    private SafeCheckTaskService safeCheckTaskService;
 
    @DubboReference(check = false)
    private AccountUserService accountUserService;
 
    @DubboReference(check = false)
    private AccountGroupService accountGroupService;
 
    @Autowired
    private SafeCheckPointService safeCheckPointService;
 
 
    @Autowired
    private SafeCheckTaskAndQuotaService safeCheckTaskAndQuotaService;
 
 
    /**
     * @description 查询所有巡检任务数据并进行分页(包含条件查询)
     */
    @Override
    public Page listTaskByPage(Page pageInfo, SafeCheckTaskPageReqDTO safeCheckTaskPageReqDTO,ContextCacheUser currentUser) {
        SafeCheckTask safeCheckTask = new SafeCheckTask();
        BeanUtils.copyProperties(safeCheckTaskPageReqDTO,safeCheckTask);
        if (safeCheckTaskPageReqDTO.getCreateUserId() == null) {
            safeCheckTask.setCreateUserName(null);
        }else {
            //获取用户信息
            ResultVO<UserRPCRespDTO> rpcResult = accountAuthService.getUserById(safeCheckTaskPageReqDTO.getCreateUserId());
            UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult);
            if (userInfo == null) {
                safeCheckTask.setCreateUserName(null);
            }
            String createUserName = userInfo.getRealName();
            safeCheckTask.setCreateUserName(createUserName);
        }
 
        List<Long> depIds = null;
        if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){
            ResultVO<List<Long>> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId());
            depIds = (List<Long>) resultVO.getData();
        }
 
        Page pageInfoResult = safeCheckTaskService.listTaskByPage(pageInfo,safeCheckTask,depIds);
 
        List<SafeCheckTask> tasks = pageInfoResult.getRecords();
        if (tasks == null || tasks.size() == 0 ){
            return pageInfoResult;
        }
        List<SafeCheckTaskPageRespDTO> taskPages = tasks.stream().map((task)->{
            SafeCheckTaskPageRespDTO taskPage = new SafeCheckTaskPageRespDTO();
            BeanUtils.copyProperties(task,taskPage);
            if (task.getTaskClaimTime() != null && task.getReportTime() != null){
                long time = task.getReportTime().getTime() - task.getTaskClaimTime().getTime();
                String taskTimeConsuming = TimeUtils.formatTime(time);
                taskPage.setTaskTimeConsuming(taskTimeConsuming);
            }
//            taskPage.setTaskType(WorkTypeEnum.getValue(task.getTaskType()));
//            String timeUnit = TimeUnitEnum.getValue(task.getCheckCycleUnit());
//            taskPage.setCheckCycleUnit(timeUnit);
//            ResultVO<GroupRPCRespDTO> group = accountGroupService.getGroupInfoByGroupId(task.getExecClassgroupId());
//            GroupRPCRespDTO groupInfo = (GroupRPCRespDTO) group.getData();
//            taskPage.setExecClassgroup(groupInfo.getGroupName());
            taskPage.setEndTime(task.getValidTime());
//            taskPage.setTaskStatus(TaskStatusEnum.getValue(task.getTaskStatus()));
//            taskPage.setResultStatus(TaskResultEnum.getValue(task.getResultStatus()));
            return taskPage;
        }).collect(Collectors.toList());
        pageInfoResult.setRecords(taskPages);
        return pageInfoResult;
    }
 
    /**
     * @description 根据任务id查询该任务下所有的巡检点检查结果  暂时不用
     */
    @Override
    public Page listTaskAndQuotaByPage(Page pageInfo, SafeCheckTaskAndQuotaPageReqDTO taskPageReqDTO) {
        Long taskId = taskPageReqDTO.getId();
        if (taskId == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务id不能为空");
        }
        Page pageInfoResult = safeCheckTaskAndQuotaService.listTaskAndQuotaByPage(pageInfo,taskId);
        List<SafeCheckTaskAndQuota> records = pageInfoResult.getRecords();
        if (records == null || records.size() == 0){
            return pageInfoResult;
        }
        List<SafeCheckTaskAndQuotaPageRespDTO> respDTOs = records.stream().map((record)->{
            SafeCheckTaskAndQuotaPageRespDTO respDTO = new SafeCheckTaskAndQuotaPageRespDTO();
            BeanUtils.copyProperties(record,respDTO);
//            String dataReportType = DataReportTypeEnum.getValue(record.getDataReportType());
//            respDTO.setDataReportType(dataReportType);
            Map<String, Object> map = referenceValue(record);
            if (record.getDataReportType().intValue() == 1){
                Byte firstReferenceValue = (Byte) map.get("firstReferenceValue");
                respDTO.setFirstReferenceValue(firstReferenceValue);
            }
            if (record.getDataReportType().intValue() == 2){
                String secondReferenceValue = (String) map.get("secondReferenceValue");
                respDTO.setSecondReferencValue(secondReferenceValue);
            }
            if (record.getDataReportType().intValue() == 3){
                Byte firstReferenceValue = (Byte) map.get("firstReferenceValue");
                respDTO.setFirstReferenceValue(firstReferenceValue);
                String secondReferenceValue = (String) map.get("secondReferenceValue");
                respDTO.setSecondReferencValue(secondReferenceValue);
            }
            return respDTO;
        }).collect(Collectors.toList());
 
        pageInfoResult.setRecords(respDTOs);
        return pageInfoResult;
    }
 
    /**
     * @description 根据任务id查询该任务下主内容及所有巡检链信息
     */
    @Override
    public SafeCheckTaskMainAndQuotaRespDTO listTaskMainAndQuota(Long taskId) {
        if (taskId == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务id不能为空");
        }
        List<SafeCheckTaskAndQuota> records = safeCheckTaskAndQuotaService.listTaskAndQuotaByTaskId(taskId);
        SafeCheckTask task = safeCheckTaskService.getTaskById(taskId);
        if (task == null){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"巡检任务不存在");
        }
        SafeCheckTaskMainAndQuotaRespDTO mainAndQuotaRespDTO = new SafeCheckTaskMainAndQuotaRespDTO();
        BeanUtils.copyProperties(task,mainAndQuotaRespDTO);
        mainAndQuotaRespDTO.setId(taskId);
 
        if (records == null || records.size() == 0){
            return mainAndQuotaRespDTO;
        }
        List<SafeCheckTaskAndQuotaPageRespDTO> respDTOs = records.stream().map((record)->{
            SafeCheckTaskAndQuotaPageRespDTO respDTO = new SafeCheckTaskAndQuotaPageRespDTO();
            BeanUtils.copyProperties(record,respDTO);
//            String dataReportType = DataReportTypeEnum.getValue(record.getDataReportType());
//            respDTO.setDataReportType(dataReportType);
 
            Map<String, Object> map = referenceValue(record);
            if (record.getDataReportType().intValue() == 1){
                Byte firstReferenceValue = (Byte) map.get("firstReferenceValue");
                respDTO.setFirstReferenceValue(firstReferenceValue);
            }
            if (record.getDataReportType().intValue() == 2){
                String secondReferenceValue = (String) map.get("secondReferenceValue");
                respDTO.setSecondReferencValue(secondReferenceValue);
            }
            if (record.getDataReportType().intValue() == 3){
                Byte firstReferenceValue = (Byte) map.get("firstReferenceValue");
                respDTO.setFirstReferenceValue(firstReferenceValue);
                String secondReferenceValue = (String) map.get("secondReferenceValue");
                respDTO.setSecondReferencValue(secondReferenceValue);
            }
            return respDTO;
        }).collect(Collectors.toList());
 
        mainAndQuotaRespDTO.setPoints(respDTOs);
        return mainAndQuotaRespDTO;
    }
 
    /**
     * @description 查询所有巡检任务数据并进行分页(只查询已完成,巡检中) 对任务状态进行分组 对结束时间由近到远
     */
    @Override
    public IPage listTaskByPageGrByStatusOrByTime(Page pageInfo,ContextCacheUser currentUser) {
        List<Byte> status = new ArrayList<>();
        status.add(TaskStatusEnum.TASK_CHECK_RUNNING.getStatus());
        status.add(TaskStatusEnum.TASK_CHECK_FINISHED.getStatus());
        List<Long> depIds = null;
        if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){
            ResultVO<List<Long>> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId());
            depIds = (List<Long>) resultVO.getData();
        }
 
        IPage pageResult = safeCheckTaskService.listTaskByPageGrByStatusOrByTime(pageInfo, status,depIds);
        List<SafeCheckeGrByStatusOrByTimeDO> records = pageResult.getRecords();
        if (records == null || records.size() == 0){
            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"巡检任务数据查询失败");
        }
        List<SafeCheckPageGrByStatusOrByTimeRespDTO> respDTOs = records.stream().map((record)->{
            SafeCheckPageGrByStatusOrByTimeRespDTO respDTO = new SafeCheckPageGrByStatusOrByTimeRespDTO();
            BeanUtils.copyProperties(record,respDTO);
            return respDTO;
        }).collect(Collectors.toList());
        pageResult.setRecords(respDTOs);
        return pageResult;
    }
 
    /**
     * @description 根据任务id查询所关联的巡检链巡检结果 结果先按region分组 然后按照指标分组 然后按照巡检点分组
     */
    @Override
    public SafeCheckTaskQuotaResultClassifyAndSummarizeRespDTO listTaskQuotaGbRegionGbQuotaGbPoint(ContextCacheUser currentUser, Long taskId) {
        List<SafeCheckTaskAndQuota> taskAndQuotas = safeCheckTaskAndQuotaService.listTaskAndQuotaByTaskId(taskId);
        if (taskAndQuotas == null){
            return null;
        }
        SafeCheckTaskQuotaResultClassifyAndSummarizeRespDTO summarizeRespDTO = new SafeCheckTaskQuotaResultClassifyAndSummarizeRespDTO();
        List<List<List<SafeCheckTaskAndQuotaResultClassifyAndSummarizeRespDTO>>> summarizeTaskAndQuotas = new ArrayList<>();
        //首先根据region进行分组
        HashSet<String> regionSet = new HashSet<>();
        for (SafeCheckTaskAndQuota taskAndQuota : taskAndQuotas) {
            regionSet.add(taskAndQuota.getRegion());
        }
 
        Iterator<String> regionName = regionSet.iterator();
        //把这个巡检点的结果加入到按region分组的list中
 
        List<List<SafeCheckTaskAndQuota>> allTaskAndQuota = new ArrayList<>();
        while (regionName.hasNext()){
            //相当于每一个region名建立了一个list装起来
            String region = regionName.next();
            List<SafeCheckTaskAndQuota> taskAndQuotasByRegion = new ArrayList<>();
            for (SafeCheckTaskAndQuota taskAndQuota : taskAndQuotas) {
                if (region.equals(taskAndQuota.getRegion())){
                    taskAndQuotasByRegion.add(taskAndQuota);
                }
            }
            allTaskAndQuota.add(taskAndQuotasByRegion);
        }
 
        //每次轮询相当于是每一个region相同的集合
        for (List<SafeCheckTaskAndQuota> quotaByReion : allTaskAndQuota) {
            List<List<SafeCheckTaskAndQuotaResultClassifyAndSummarizeRespDTO>> allTaskAndQuotaByQuota = new ArrayList<>();
            HashSet<String> quotaSet = new HashSet<>();
            for (SafeCheckTaskAndQuota taskAndQuota : quotaByReion) {
                quotaSet.add(taskAndQuota.getQuotaContent()+"("+taskAndQuota.getQuotaUnit()+")");
            }
 
            //对存储指标名称的set进行遍历
            Iterator<String> iterator = quotaSet.iterator();
            while (iterator.hasNext()) {
                List<SafeCheckTaskAndQuotaResultClassifyAndSummarizeRespDTO> taskAndQuotaByQuota = new ArrayList<>();
                String quota = iterator.next();
                for (SafeCheckTaskAndQuota taskAndQuota : quotaByReion) {
                    if (quota.equals(taskAndQuota.getQuotaContent()+"("+taskAndQuota.getQuotaUnit()+")")){
                        SafeCheckTaskAndQuotaResultClassifyAndSummarizeRespDTO respDTO = new SafeCheckTaskAndQuotaResultClassifyAndSummarizeRespDTO();
                        BeanUtils.copyProperties(taskAndQuota,respDTO);
                        taskAndQuotaByQuota.add(respDTO);
                    }
                }
                allTaskAndQuotaByQuota.add(taskAndQuotaByQuota);
            }
            summarizeTaskAndQuotas.add(allTaskAndQuotaByQuota);
        }
        summarizeRespDTO.setTaskAndQuotas(summarizeTaskAndQuotas);
        return summarizeRespDTO;
    }
 
    /**
     * @description 获取当前0点-当前时间异常的任务和没有检查的任务
     */
    @Override
    public SafeCheckTaskByNoCheckTaskAndAbnormalTaskRepsDTO listTaskByNoCheckTaskAndAbnormalTask(ContextCacheUser currentUser) {
        List<Long> depIds = null;
        if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){
            ResultVO<List<Long>> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId());
            depIds = (List<Long>) resultVO.getData();
        }
        LocalDateTime zeroPointDateTime = LocalDate.now().atStartOfDay();
        LocalDateTime now = LocalDateTime.now();
        SafeCheckTaskByNoCheckTaskAndAbnormalTaskRepsDTO data = new SafeCheckTaskByNoCheckTaskAndAbnormalTaskRepsDTO();
        //根据时间获取当天截至目前超时未巡检任务
        List<SafeCheckTask> noCheckTask = safeCheckTaskService.listNoCheckTaskByValidTime(zeroPointDateTime,now,TaskStatusEnum.TASK_TIMEOUT_NO_CHECK.getStatus(),depIds);
        if (noCheckTask == null || noCheckTask.size() ==0){
            data.setNoCheckTaskCount(0);
            data.setNoCheckTasks(null);
        }else {
            List<SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO> noCheckTasks = noCheckTask.stream().map((task)->{
                SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO repsDTO = new SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO();
                BeanUtils.copyProperties(task,repsDTO);
                return repsDTO;
            }).collect(Collectors.toList());
            data.setNoCheckTasks(noCheckTasks);
            data.setNoCheckTaskCount(noCheckTask.size());
        };
        //根据时间获取当天截至目前异常任务
        List<SafeCheckTask> abnormalTask = safeCheckTaskService.listAbnormalTaskByValidTime(zeroPointDateTime,now,
                TaskStatusEnum.TASK_CHECK_FINISHED.getStatus(),TaskResultEnum.TASK_RESULT_UNUSUAL.getCode(),depIds);
        if (abnormalTask == null || abnormalTask.size() == 0){
            data.setAbnormalTaskCount(0);
            data.setAbnormalTasks(null);
        }else {
            List<SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO> abnormalTasks = abnormalTask.stream().map((task)->{
                SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO repsDTO = new SafeCheckTaskByNoCheckTaskAndAbnormalTaskBaseRepsDTO();
                BeanUtils.copyProperties(task,repsDTO);
                return repsDTO;
            }).collect(Collectors.toList());
            data.setAbnormalTasks(abnormalTasks);
            data.setAbnormalTaskCount(abnormalTasks.size());
        }
        return data;
    }
 
 
    /**
     * @description 根据选择类型返回不同的参考值
     */
    private Map<String,Object> referenceValue(SafeCheckTaskAndQuota taskAndQuota){
        HashMap<String, Object> map = new HashMap<>();
        if (taskAndQuota == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务所关联的巡检点不能为空,无法返回数据填报参考值");
        }
        Byte dataReportType = taskAndQuota.getDataReportType();
        if (taskAndQuota.getDataReportType() == null){
            throw new AusinessException(E.DATA_PARAM_NULL,"任务所关联的巡检点数据填报类型不能为空,无法返回数据填报参考值");
        }
        if (dataReportType.intValue() == 1){
//            String firstReferenceValue = GeneralResultsEnum.getValue(taskAndQuota.getFirstReferenceValue());
            map.put("firstReferenceValue",taskAndQuota.getFirstReferenceValue());
            return map;
        }
        if (dataReportType.intValue() == 2){
            String referenceValue = supportReference(taskAndQuota);
            map.put("secondReferenceValue",referenceValue);
            return map;
        }
        if (dataReportType.intValue() == 3){
//            String firstReferenceValue = GeneralResultsEnum.getValue(taskAndQuota.getFirstReferenceValue());
            map.put("firstReferenceValue",taskAndQuota.getFirstReferenceValue());
            String referenceValue = supportReference(taskAndQuota);
            map.put("secondReferenceValue",referenceValue);
        }
        return map;
    };
 
    /**
     * @description 第二种大于小于参考值
     */
    private String supportReference(SafeCheckTaskAndQuota taskAndQuota){
        String referenceValue = null;
        if (taskAndQuota.getSecondReferenceSign() != null && taskAndQuota.getSecondReferenceValue() != null){
            String secondReferenceSign = ReferenceValueTypeEnum.getValue(taskAndQuota.getSecondReferenceSign());
            BigDecimal secondReferenceValue = taskAndQuota.getSecondReferenceValue();
            referenceValue = " 数值 "+secondReferenceSign+secondReferenceValue.toString() ;
        }
        if (taskAndQuota.getThirdReferenceSign() != null && taskAndQuota.getThirdReferenceValue() != null){
            String thirdReferenceSign = ReferenceValueTypeEnum.getValue(taskAndQuota.getThirdReferenceSign());
            BigDecimal thirdReferenceValue = taskAndQuota.getThirdReferenceValue();
            referenceValue = " 数值 "+thirdReferenceSign+thirdReferenceValue.toString();
        }
        if (taskAndQuota.getSecondReferenceSign() != null && taskAndQuota.getSecondReferenceValue() != null &&
                taskAndQuota.getThirdReferenceSign() != null && taskAndQuota.getThirdReferenceValue() != null)
        {
            String secondReferenceSign = null;
            if (taskAndQuota.getSecondReferenceSign() == 1){
                secondReferenceSign = ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LT.getValue();
            }
            if (taskAndQuota.getSecondReferenceSign() == 2) {
                secondReferenceSign = ReferenceValueTypeEnum.REFERENCE_VALUE_TYPE_LE.getValue();
            }
            BigDecimal secondRerenceValue = taskAndQuota.getSecondReferenceValue();
            String thirdReferenceSign = ReferenceValueTypeEnum.getValue(taskAndQuota.getThirdReferenceSign());
            BigDecimal thirdReferenceValue = taskAndQuota.getThirdReferenceValue();
            referenceValue = secondRerenceValue.toString() + secondReferenceSign +" 数值 " + thirdReferenceSign + thirdReferenceValue.toString()  ;
        }
        return referenceValue;
    }
}