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;
|
}
|
}
|