package com.estate.expert.admin.io.processor.impl.matrix;
|
|
import com.alibaba.fastjson.JSON;
|
import com.estate.expert.admin.domain.standard.hierachy.HierarchyData;
|
import com.estate.expert.admin.utils.tree.TreeNode;
|
import com.estate.expert.admin.utils.tree.TreeRoot;
|
import lombok.AllArgsConstructor;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.io.*;
|
import java.nio.charset.StandardCharsets;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 权重矩阵处理器(打分专家)
|
*
|
* 用于处理打分专家的权重数据导入导出(JSON格式)
|
*/
|
@Slf4j
|
@AllArgsConstructor
|
public class WeightMatrixProcessor {
|
|
private TreeRoot<HierarchyData> hierarchyTree;
|
|
/**
|
* 导出权重数据为JSON
|
*/
|
public void saveToJson(OutputStream outputStream, Map<String, Float> weightMap) throws IOException {
|
List<WeightItem> weightItems = new ArrayList<>();
|
|
if(weightMap != null && !weightMap.isEmpty()) {
|
for(var entry : weightMap.entrySet()) {
|
String path = entry.getKey();
|
Float weight = entry.getValue();
|
|
var node = hierarchyTree.get(path);
|
if(node != null) {
|
WeightItem item = new WeightItem();
|
item.setPath(path);
|
item.setWeight(weight);
|
item.setNames(node.getNames());
|
item.setName(node.getName());
|
weightItems.add(item);
|
} else {
|
log.warn("路径 {} 对应的节点不存在", path);
|
}
|
}
|
}
|
|
// 输出JSON
|
String json = JSON.toJSONString(weightItems, true);
|
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
|
outputStream.flush();
|
}
|
|
/**
|
* 从JSON导入权重数据
|
*/
|
public Map<String, Float> loadFromJson(InputStream inputStream) throws IOException {
|
// 读取JSON内容
|
String json = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
|
// 解析JSON
|
List<WeightItem> weightItems = JSON.parseArray(json, WeightItem.class);
|
|
// 转换为Map
|
Map<String, Float> weightMap = new java.util.TreeMap<>();
|
if(weightItems != null) {
|
for(WeightItem item : weightItems) {
|
if(item.getPath() != null && item.getWeight() != null) {
|
weightMap.put(item.getPath(), item.getWeight());
|
}
|
}
|
}
|
|
return weightMap;
|
}
|
|
/**
|
* 权重项
|
*/
|
@Data
|
public static class WeightItem {
|
/**
|
* 体系路径
|
*/
|
private String path;
|
|
/**
|
* 节点名称
|
*/
|
private String name;
|
|
/**
|
* 完整名称路径
|
*/
|
private List<String> names;
|
|
/**
|
* 权重值
|
*/
|
private Float weight;
|
}
|
}
|