liujie
2026-05-16 539fa09b48cfe03c573284381cfba4ba092e8007
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
    }
}