| | |
| | | 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.mapper.EvalResultMapper; |
| | | import com.ruoyi.system.model.DeviceUsage; |
| | | import com.ruoyi.system.model.EvalResult; |
| | | import com.ruoyi.system.model.Record; |
| | | import com.ruoyi.system.query.*; |
| | | 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.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; |
| | | |
| | | @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)); |
| | | // 获取本月第一天凌晨 |
| | | List<EntrustProjectVo> deviceList = this.baseMapper.projectList(page,query); |
| | | for (EntrustProjectVo deviceListVo : deviceList) { |
| | | 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; |
| | | } |
| | | // todo 查询项目所属委托人风险等级参数设置 |
| | | deviceListVo.setRisk("A类"); |
| | | |
| | | }else{ |
| | | deviceListVo.setRisk("C类"); |
| | | } |
| | | LocalDateTime checkTime = deviceListVo.getCheckTime(); |
| | | // 判断checkTime是否是本月的时间 |
| | | if (checkTime != null && YearMonth.from(checkTime).equals(YearMonth.now())) { |
| | |
| | | } |
| | | 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); |