“mitao”
2026-03-03 86c03bbc2e288482c57f74cf4e40eee386c132ca
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
package com.estate.expert.admin.utils;
 
import com.estate.expert.admin.domain.business.device.Device;
import com.estate.expert.admin.domain.business.device.DeviceType;
import com.estate.expert.admin.domain.exam.eval.score.ScoreResult;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 设备分数计算器
 */
public class DeviceScoreComputer {
 
    private Map<Integer, ScoreResult> deviceResMap;
 
    private List<Device> devices;
 
    /**
     * 构造函数
     * @param deviceResMap 设备分数结果
     * @param devices 设备列表
     */
    public DeviceScoreComputer(Map<Integer, ScoreResult> deviceResMap, List<Device> devices) {
        this.deviceResMap = deviceResMap;
        this.devices = devices;
    }
 
    /**
     * 计算设备分数
     * @return 设备分数
     */
    public float computeScore() {
        // 根据设备类型计算分数
        Map<DeviceType, Float> scoreMap = new HashMap<>();
        Map<DeviceType, Integer> countMap = new HashMap<>();
 
        for (Map.Entry<Integer, ScoreResult> entry : deviceResMap.entrySet()) {
            Device device = getDevice(entry.getKey());
            if (device == null) {
                continue;
            }
            DeviceType deviceType = device.getType();
            ScoreResult scoreResult = entry.getValue();
            if (scoreResult == null || scoreResult.root() == null || scoreResult.root().getData() == null) {
                continue;
            }
            float score = scoreResult.root().getData().getScore();
            if (scoreMap.containsKey(deviceType)) {
                scoreMap.put(deviceType, scoreMap.get(deviceType) + score);
                countMap.put(deviceType, countMap.get(deviceType) + 1);
            } else {
                scoreMap.put(deviceType, score);
                countMap.put(deviceType, 1);
            }
        }
 
        // 根据设备类型计算平均分数
        float totalScore = 0;
        int totalCount = 0;
        for (Map.Entry<DeviceType, Float> entry : scoreMap.entrySet()) {
            DeviceType deviceType = entry.getKey();
            float score = entry.getValue();
            int count = countMap.get(deviceType);
            totalScore += score / count;
            totalCount += 1;
        }
        return totalScore / totalCount;
    }
 
    /**
     * 获取设备
     * @param deviceId 设备ID
     * @return 设备
     */
    private Device getDevice(int deviceId) {
        for (Device device : devices) {
            if (device.getId() == deviceId) {
                return device;
            }
        }
        return null;
    }
}