无关风月
2026-02-11 b2d906c32062383824fd85ce1318e7d8478ce3dd
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DeviceUsageServiceImpl.java
@@ -2,6 +2,7 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -150,6 +151,8 @@
                .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, String> assetParamMap = clientParamMapper.selectList(new LambdaQueryWrapper<ClientParam>())
                .stream().collect(Collectors.toMap(ClientParam::getClientId, ClientParam::getAssetProject));
        Map<Integer, List<Task>> taskMap = taskMapper.selectList(new LambdaQueryWrapper<Task>()
                        .ge(Task::getCreateTime, startTime)
                        .le(Task::getCreateTime, LocalDateTime.now())
@@ -170,6 +173,8 @@
                .eq(Record::getDeleted, false));
        List<EntrustProjectVo> deviceList = this.baseMapper.projectList(page, query);
        for (EntrustProjectVo deviceListVo : deviceList) {
            if (query.getCategory() == 1) {
                // 在建
            List<EvalResult> evalResults = resultMap.get(deviceListVo.getId());
            if (evalResults != null) {
                float temp = 0;
@@ -209,6 +214,70 @@
            } else {
                deviceListVo.setRisk(null);
            }
            } else {
                // 查询检查任务
                LambdaQueryWrapper<Task> taskWrapperPro = new LambdaQueryWrapper<>();
                taskWrapperPro.eq(Task::getProjectId, deviceListVo.getId())
                        .eq(Task::getDeleted, false);
                List<Task> tasks = taskMapper.selectList(taskWrapperPro);
                List<Integer> checkTaskIdsPro = tasks.stream().map(Task::getId).collect(Collectors.toList());
                // 查询重大隐患记录(level 表示隐患等级,0 表示重大隐患)
                LambdaQueryWrapper<Record> recordWrapperPro = new LambdaQueryWrapper<>();
                recordWrapperPro.eq(Record::getDeleted, false)
                        .in(Record::getCheckTaskId, checkTaskIdsPro)
                ; // 重大隐患等级
                List<Record> records1 = recordMapper.selectList(recordWrapperPro);
//                long c1 = records1.stream().filter(e -> e.getLevel() != null && e.getLevel() == 0).count();
//                long c2 = records1.stream().filter(e -> e.getLevel() != null && e.getLevel() == 0).count();
//                long c3 = records1.stream().filter(e -> e.getLevel() != null && e.getLevel() == 0).count();
//                long c4 = records1.stream().filter(e -> e.getLevel() != null && e.getLevel() == 0).count();
//                // 固定资产
//                String s = clientParamMap.get(query.getClientId());
//                JSONObject jsonObject = JSON.parseObject(s);
//                JSONArray a = jsonObject.getJSONArray("a");
//                for (Object o : a) {
//                    // 转化为jsonObject
//                    JSONObject jsonObject1 = (JSONObject) o;
//                }
//                JSONArray b = jsonObject.getJSONArray("b");
//                for (Object o : b) {
//                    // 转化为jsonObject
//                    JSONObject jsonObject1 = (JSONObject) o;
//                }
//                JSONArray c = jsonObject.getJSONArray("c");
//                for (Object o : c) {
//                    // 转化为jsonObject
//                    JSONObject jsonObject1 = (JSONObject) o;
//                }
                // 统计各等级隐患数量
                Map<Integer, Long> levelCountMap = records1.stream()
                        .filter(e -> e.getLevel() != null)
                        .collect(Collectors.groupingBy(Record::getLevel, Collectors.counting()));
// 获取客户参数配置
                String s = clientParamMap.get(query.getClientId());
                JSONObject jsonObject = JSON.parseObject(s);
// 初始化风险等级
                String riskLevel = null;
// 先判断 c 条件
                JSONArray cArray = jsonObject.getJSONArray("c");
                if (cArray != null && matchesCondition(cArray, levelCountMap)) {
                    riskLevel = "C类";
                }
// 再判断 b 条件
                else if (jsonObject.getJSONArray("b") != null && matchesCondition(jsonObject.getJSONArray("b"), levelCountMap)) {
                    riskLevel = "B类";
                }
// 最后判断 a 条件
                else if (jsonObject.getJSONArray("a") != null && matchesCondition(jsonObject.getJSONArray("a"), levelCountMap)) {
                    riskLevel = "A类";
                }
            }
            List<Task> tasks = taskMap.get(deviceListVo.getId());
            if (tasks != null && !tasks.isEmpty()) {
                List<Integer> taskIds = tasks.stream().map(Task::getId).toList();
@@ -234,6 +303,39 @@
        return deviceList;
    }
    /**
     * 判断条件数组是否满足隐患统计结果
     *
     * @param conditionArray 条件数组,例如 [[{"4":"1"},{"4":"1"}], [{"4":"2"}]]
     * @param levelCountMap  隐患等级统计结果,key为隐患等级,value为数量
     * @return 是否满足条件
     */
    private boolean matchesCondition(JSONArray conditionArray, Map<Integer, Long> levelCountMap) {
        for (Object item : conditionArray) {
            if (item instanceof JSONArray) {
                JSONArray andConditions = (JSONArray) item;
                boolean allMatch = true;
                for (Object condition : andConditions) {
                    if (condition instanceof JSONObject) {
                        JSONObject conditionObj = (JSONObject) condition;
                        for (Map.Entry<String, Object> entry : conditionObj.entrySet()) {
                            int level = Integer.parseInt(entry.getKey()); // 隐患等级
                            int requiredCount = Integer.parseInt(entry.getValue().toString()); // 所需数量
                            Long actualCount = levelCountMap.getOrDefault(level, 0L);
                            if (actualCount < requiredCount) {
                                allMatch = false;
                                break;
                            }
                        }
                    }
                    if (!allMatch) break;
                }
                if (allMatch) return true; // 满足任意一个“或”条件即可返回 true
            }
        }
        return false;
    }
    private static Float calculateDeviceScore(String deviceResMapJson) {
        try {
            // 1. 解析 JSON 字符串