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 rpcResult = accountAuthService.getUserById(safeCheckTaskPageReqDTO.getCreateUserId()); UserRPCRespDTO userInfo = UserInfoUtil.judgeUserInfo(rpcResult); if (userInfo == null) { safeCheckTask.setCreateUserName(null); } String createUserName = userInfo.getRealName(); safeCheckTask.setCreateUserName(createUserName); } List depIds = null; if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){ ResultVO> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId()); depIds = (List) resultVO.getData(); } Page pageInfoResult = safeCheckTaskService.listTaskByPage(pageInfo,safeCheckTask,depIds); List tasks = pageInfoResult.getRecords(); if (tasks == null || tasks.size() == 0 ){ return pageInfoResult; } List 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 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 records = pageInfoResult.getRecords(); if (records == null || records.size() == 0){ return pageInfoResult; } List respDTOs = records.stream().map((record)->{ SafeCheckTaskAndQuotaPageRespDTO respDTO = new SafeCheckTaskAndQuotaPageRespDTO(); BeanUtils.copyProperties(record,respDTO); // String dataReportType = DataReportTypeEnum.getValue(record.getDataReportType()); // respDTO.setDataReportType(dataReportType); Map 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 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 respDTOs = records.stream().map((record)->{ SafeCheckTaskAndQuotaPageRespDTO respDTO = new SafeCheckTaskAndQuotaPageRespDTO(); BeanUtils.copyProperties(record,respDTO); // String dataReportType = DataReportTypeEnum.getValue(record.getDataReportType()); // respDTO.setDataReportType(dataReportType); Map 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 status = new ArrayList<>(); status.add(TaskStatusEnum.TASK_CHECK_RUNNING.getStatus()); status.add(TaskStatusEnum.TASK_CHECK_FINISHED.getStatus()); List depIds = null; if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){ ResultVO> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId()); depIds = (List) resultVO.getData(); } IPage pageResult = safeCheckTaskService.listTaskByPageGrByStatusOrByTime(pageInfo, status,depIds); List records = pageResult.getRecords(); if (records == null || records.size() == 0){ throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"巡检任务数据查询失败"); } List 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 taskAndQuotas = safeCheckTaskAndQuotaService.listTaskAndQuotaByTaskId(taskId); if (taskAndQuotas == null){ return null; } SafeCheckTaskQuotaResultClassifyAndSummarizeRespDTO summarizeRespDTO = new SafeCheckTaskQuotaResultClassifyAndSummarizeRespDTO(); List>> summarizeTaskAndQuotas = new ArrayList<>(); //首先根据region进行分组 HashSet regionSet = new HashSet<>(); for (SafeCheckTaskAndQuota taskAndQuota : taskAndQuotas) { regionSet.add(taskAndQuota.getRegion()); } Iterator regionName = regionSet.iterator(); //把这个巡检点的结果加入到按region分组的list中 List> allTaskAndQuota = new ArrayList<>(); while (regionName.hasNext()){ //相当于每一个region名建立了一个list装起来 String region = regionName.next(); List taskAndQuotasByRegion = new ArrayList<>(); for (SafeCheckTaskAndQuota taskAndQuota : taskAndQuotas) { if (region.equals(taskAndQuota.getRegion())){ taskAndQuotasByRegion.add(taskAndQuota); } } allTaskAndQuota.add(taskAndQuotasByRegion); } //每次轮询相当于是每一个region相同的集合 for (List quotaByReion : allTaskAndQuota) { List> allTaskAndQuotaByQuota = new ArrayList<>(); HashSet quotaSet = new HashSet<>(); for (SafeCheckTaskAndQuota taskAndQuota : quotaByReion) { quotaSet.add(taskAndQuota.getQuotaContent()+"("+taskAndQuota.getQuotaUnit()+")"); } //对存储指标名称的set进行遍历 Iterator iterator = quotaSet.iterator(); while (iterator.hasNext()) { List 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 depIds = null; if (UserTypeEnum.STAFF.getCode() == currentUser.getType()){ ResultVO> resultVO = accountDepartmentService.listDepAndSubDepIds(currentUser.getDepId()); depIds = (List) resultVO.getData(); } LocalDateTime zeroPointDateTime = LocalDate.now().atStartOfDay(); LocalDateTime now = LocalDateTime.now(); SafeCheckTaskByNoCheckTaskAndAbnormalTaskRepsDTO data = new SafeCheckTaskByNoCheckTaskAndAbnormalTaskRepsDTO(); //根据时间获取当天截至目前超时未巡检任务 List 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 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 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 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 referenceValue(SafeCheckTaskAndQuota taskAndQuota){ HashMap 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; } }