| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.system.mapper.DeviceUsageMapper; |
| | | import com.ruoyi.system.model.DeviceUsage; |
| | | import com.ruoyi.system.mapper.*; |
| | | import com.ruoyi.system.model.*; |
| | | import com.ruoyi.system.model.Record; |
| | | import com.ruoyi.system.query.*; |
| | | import com.ruoyi.system.service.ClientParamService; |
| | | import com.ruoyi.system.service.DeviceUsageService; |
| | | import com.ruoyi.system.vo.*; |
| | | import com.ruoyi.system.vo.DeviceCountVo; |
| | | import com.ruoyi.system.vo.DeviceListVo; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.YearMonth; |
| | | import java.util.ArrayList; |
| | | import java.util.Comparator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | * @author administrator |
| | | * @since 2026-01-13 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class DeviceUsageServiceImpl extends ServiceImpl<DeviceUsageMapper, DeviceUsage> implements DeviceUsageService { |
| | | |
| | | @Autowired |
| | | private EvalResultMapper evalResultMapper; |
| | | @Autowired |
| | | private ProjectMapper projectMapper; |
| | | @Autowired |
| | | private ClientParamMapper clientParamMapper; |
| | | @Autowired |
| | | private TaskMapper taskMapper; |
| | | @Autowired |
| | | private RecordMapper recordMapper; |
| | | |
| | | @Override |
| | | public List<String> getDeviceUsageList(Integer id) { |
| | |
| | | return this.baseMapper.getProjectDeviceCount(id); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<EntrustProjectVo> projectList( Page<EntrustProjectVo> page,EntrustListQuery query) { |
| | | Integer pageNum = query.getPageNum(); |
| | | Integer pageSize = query.getPageSize(); |
| | | LocalDateTime startTime = LocalDateTime.of(LocalDate.now().withDayOfMonth(1), LocalDateTime.MIN.toLocalTime()); |
| | | |
| | | Map<Integer, List<EvalResult>> resultMap = evalResultMapper.selectList(new LambdaQueryWrapper<EvalResult>() |
| | | .ge(EvalResult::getCreateTime, startTime) |
| | | .le(EvalResult::getCreateTime, LocalDateTime.now()) |
| | | ).stream().collect(Collectors.groupingBy(EvalResult::getProjectId)); |
| | | Map<Integer, Integer> projectMap = projectMapper.selectList(new LambdaQueryWrapper<Project>() |
| | | .eq(Project::getDeleted, false)).stream().collect(Collectors.toMap(Project::getId, Project::getClientId)); |
| | | Map<Integer, String> clientParamMap = clientParamMapper.selectList(new LambdaQueryWrapper<ClientParam>()) |
| | | .stream().collect(Collectors.toMap(ClientParam::getClientId, ClientParam::getUnderConstructionProject)); |
| | | Map<Integer, List<Task>> taskMap = taskMapper.selectList(new LambdaQueryWrapper<Task>()) |
| | | .stream().collect(Collectors.groupingBy(Task::getProjectId)); |
| | | List<Record> records = recordMapper.selectList(new LambdaQueryWrapper<Record>() |
| | | .eq(Record::getDeleted, false)); |
| | | // 获取本月第一天凌晨 |
| | | List<EntrustProjectVo> deviceList = this.baseMapper.projectList(page,query); |
| | | for (EntrustProjectVo deviceListVo : deviceList) { |
| | | LocalDateTime checkTime = deviceListVo.getCheckTime(); |
| | | // 判断checkTime是否是本月的时间 |
| | | if (checkTime != null && YearMonth.from(checkTime).equals(YearMonth.now())) { |
| | | deviceListVo.setCheckResult("已检查"); |
| | | }else { |
| | | List<EvalResult> evalResults = resultMap.get(deviceListVo.getId()); |
| | | if (evalResults!=null){ |
| | | float temp =0; |
| | | // 计算安全检查平均分 |
| | | float avgSafetyScore = (float)evalResults.stream() |
| | | .filter(e -> e.getScore() != null) |
| | | .mapToDouble(EvalResult::getScore) |
| | | .average() |
| | | .orElse(0.0); |
| | | // 计算设备检查平均分(需要解析 device_res_map) |
| | | List<Float> deviceScores = evalResults.stream() |
| | | .filter(e -> StringUtils.hasText(e.getDeviceResMap())) |
| | | .map(e -> calculateDeviceScore(e.getDeviceResMap())) |
| | | .filter(score -> score != null && score > 0) |
| | | .toList(); |
| | | |
| | | if (!deviceScores.isEmpty()) { |
| | | float avgDeviceScore =(float) deviceScores.stream() |
| | | .mapToDouble(Float::doubleValue) |
| | | .average() |
| | | .orElse(0.0); |
| | | temp = (avgSafetyScore + avgDeviceScore)/2; |
| | | } |
| | | Integer clientId = projectMap.get(deviceListVo.getId()); |
| | | String s = clientParamMap.get(clientId); |
| | | JSONObject jsonObject = JSON.parseObject(s); |
| | | String a = jsonObject.getString("a"); |
| | | String b = jsonObject.getString("b"); |
| | | String c = jsonObject.getString("c"); |
| | | if (temp>=Float.parseFloat( a)){ |
| | | deviceListVo.setRisk("A类"); |
| | | }else if (temp>=Float.parseFloat( b)){ |
| | | deviceListVo.setRisk("B类"); |
| | | }else if (temp>=Float.parseFloat( c)){ |
| | | deviceListVo.setRisk("C类"); |
| | | } |
| | | }else{ |
| | | deviceListVo.setRisk(null); |
| | | } |
| | | List<Task> tasks = taskMap.get(deviceListVo.getId()); |
| | | if (tasks!=null&&!tasks.isEmpty()){ |
| | | List<Integer> taskIds = tasks.stream().map(Task::getId).toList(); |
| | | List<Record> recordList = records.stream().filter(e -> taskIds.contains(e.getCheckTaskId())).collect(Collectors.toList()); |
| | | Record record = recordList.stream().max(Comparator.comparing(Record::getCreateTime)).orElse(null); |
| | | if (record!=null){ |
| | | deviceListVo.setCheckTime(record.getCreateTime()); |
| | | deviceListVo.setCheckResult("已检查"); |
| | | }else{ |
| | | deviceListVo.setCheckTime(null); |
| | | deviceListVo.setCheckResult("未检查"); |
| | | deviceListVo.setRisk(null); |
| | | } |
| | | }else{ |
| | | deviceListVo.setCheckTime(null); |
| | | deviceListVo.setCheckResult("未检查"); |
| | | deviceListVo.setRisk(null); |
| | | } |
| | | } |
| | | return deviceList; |
| | | } |
| | | private static Float calculateDeviceScore(String deviceResMapJson) { |
| | | try { |
| | | // 1. 解析 JSON 字符串 |
| | | JSONObject deviceMap = JSON.parseObject(deviceResMapJson); |
| | | if (deviceMap == null || deviceMap.isEmpty()) { |
| | | log.debug("设备评估结果映射为空"); |
| | | return null; |
| | | } |
| | | |
| | | // 2. 提取所有设备的分数 |
| | | List<Float> scores = new ArrayList<>(); |
| | | for (String deviceId : deviceMap.keySet()) { |
| | | try { |
| | | // 获取设备数据对象 |
| | | JSONObject deviceData = deviceMap.getJSONObject(deviceId); |
| | | if (deviceData == null) { |
| | | log.debug("设备数据为空,deviceId: {}", deviceId); |
| | | continue; |
| | | } |
| | | |
| | | // 获取 content 对象 |
| | | JSONObject content = deviceData.getJSONObject("content"); |
| | | if (content == null) { |
| | | log.debug("设备 content 为空,deviceId: {}", deviceId); |
| | | continue; |
| | | } |
| | | |
| | | // 获取 data 对象 |
| | | JSONObject data = content.getJSONObject("data"); |
| | | if (data == null) { |
| | | log.debug("设备 data 为空,deviceId: {}", deviceId); |
| | | continue; |
| | | } |
| | | |
| | | // 获取 score 字段 |
| | | if (!data.containsKey("score")) { |
| | | log.debug("设备 data 中不包含 score 字段,deviceId: {}", deviceId); |
| | | continue; |
| | | } |
| | | |
| | | Float score = data.getFloat("score"); |
| | | if (score != null && score > 0) { |
| | | scores.add(score); |
| | | log.debug("成功提取设备得分,deviceId: {}, score: {}", deviceId, score); |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("提取设备得分失败,deviceId: {}", deviceId, e); |
| | | } |
| | | } |
| | | |
| | | // 3. 计算平均分 |
| | | if (scores.isEmpty()) { |
| | | log.debug("没有有效的设备得分数据"); |
| | | return null; |
| | | } |
| | | |
| | | float avgScore = (float) scores.stream() |
| | | .mapToDouble(Float::doubleValue) |
| | | .average() |
| | | .orElse(0.0); |
| | | |
| | | log.debug("设备平均得分计算完成,设备数量: {}, 平均分: {}", scores.size(), avgScore); |
| | | return avgScore; |
| | | |
| | | } catch (Exception e) { |
| | | log.warn("解析设备得分失败,deviceResMapJson: {}", deviceResMapJson, e); |
| | | return null; |
| | | } |
| | | } |
| | | @Override |
| | | public List<EntrustProjectReportVo> projectReport(Page<EntrustProjectReportVo> page, ProjectReportListQuery query) { |
| | | List<EntrustProjectReportVo> entrustProjectReportVoList = this.baseMapper.projectReport(page,query); |
| | |
| | | |
| | | @Override |
| | | public List<ErrorListVo> errorList(Page<ErrorListVo> page, ErrorListQuery query) { |
| | | // 查询在建工程项目(usage_type 表示项目用途类型,假设 0 表示在建工程) |
| | | LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>(); |
| | | if (query.getClientId() != null) { |
| | | projectWrapper.eq(Project::getClientId, query.getClientId()); |
| | | } |
| | | List<Project> projects = projectMapper.selectList(projectWrapper); |
| | | List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); |
| | | if (projectIds.isEmpty()){ |
| | | projectIds.add(-1); |
| | | } |
| | | // 查询检查任务 |
| | | LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>(); |
| | | taskWrapper.in(Task::getProjectId, projectIds) |
| | | .eq(Task::getDeleted, false); |
| | | List<Task> tasks = taskMapper.selectList(taskWrapper); |
| | | List<Integer> checkTaskIds = tasks.stream().map(Task::getId).collect(Collectors.toList()); |
| | | |
| | | // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患) |
| | | LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>(); |
| | | recordWrapper.eq(Record::getDeleted, false) |
| | | .in(Record::getCheckTaskId, checkTaskIds) |
| | | .eq(Record::getLevel, 0); // 重大隐患等级 |
| | | List<Integer> recordIds = recordMapper.selectList(recordWrapper).stream().map(Record::getId).toList(); |
| | | query.setRecordIds(recordIds); |
| | | List<ErrorListVo> errorList = this.baseMapper.errorList(page, query); |
| | | return errorList; |
| | | } |
| | | |
| | | @Override |
| | | public List<ErrorListVo> deviceErrorList(Page<ErrorListVo> page, ErrorListQuery query) { |
| | | // 查询在建工程项目(usage_type 表示项目用途类型,假设 0 表示在建工程) |
| | | LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>(); |
| | | if (query.getClientId() != null) { |
| | | projectWrapper.eq(Project::getClientId, query.getClientId()); |
| | | } |
| | | List<Project> projects = projectMapper.selectList(projectWrapper); |
| | | List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); |
| | | if (projectIds.isEmpty()){ |
| | | projectIds.add(-1); |
| | | } |
| | | // 查询检查任务 |
| | | LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>(); |
| | | taskWrapper.in(Task::getProjectId, projectIds) |
| | | .eq(Task::getDeleted, false); |
| | | List<Task> tasks = taskMapper.selectList(taskWrapper); |
| | | List<Integer> checkTaskIds = tasks.stream().map(Task::getId).collect(Collectors.toList()); |
| | | |
| | | // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患) |
| | | LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>(); |
| | | recordWrapper.eq(Record::getDeleted, false) |
| | | .in(Record::getCheckTaskId, checkTaskIds) |
| | | .eq(Record::getLevel, 0); // 重大隐患等级 |
| | | List<Integer> recordIds = recordMapper.selectList(recordWrapper).stream().map(Record::getId).toList(); |
| | | query.setRecordIds(recordIds); |
| | | List<ErrorListVo> errorList = this.baseMapper.deviceErrorList(page, query); |
| | | return errorList; } |
| | | |
| | | @Override |
| | | public List<ErrorListVo> safetyErrorList(Page<ErrorListVo> page, ErrorListQuery query) { |
| | | // 查询在建工程项目(usage_type 表示项目用途类型,假设 0 表示在建工程) |
| | | LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>(); |
| | | if (query.getClientId() != null) { |
| | | projectWrapper.eq(Project::getClientId, query.getClientId()); |
| | | } |
| | | List<Project> projects = projectMapper.selectList(projectWrapper); |
| | | List<Integer> projectIds = projects.stream().map(Project::getId).collect(Collectors.toList()); |
| | | if (projectIds.isEmpty()){ |
| | | projectIds.add(-1); |
| | | } |
| | | // 查询检查任务 |
| | | LambdaQueryWrapper<Task> taskWrapper = new LambdaQueryWrapper<>(); |
| | | taskWrapper.in(Task::getProjectId, projectIds) |
| | | .eq(Task::getDeleted, false); |
| | | List<Task> tasks = taskMapper.selectList(taskWrapper); |
| | | List<Integer> checkTaskIds = tasks.stream().map(Task::getId).collect(Collectors.toList()); |
| | | |
| | | // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患) |
| | | LambdaQueryWrapper<Record> recordWrapper = new LambdaQueryWrapper<>(); |
| | | recordWrapper.eq(Record::getDeleted, false) |
| | | .in(Record::getCheckTaskId, checkTaskIds) |
| | | .eq(Record::getLevel, 0); // 重大隐患等级 |
| | | List<Integer> recordIds = recordMapper.selectList(recordWrapper).stream().map(Record::getId).toList(); |
| | | query.setRecordIds(recordIds); |
| | | List<ErrorListVo> errorList = this.baseMapper.safetyErrorList(page, query); |
| | | return errorList; } |
| | | |