liujie
2026-04-13 eff3c1988ffa0537419d6646688f4119cebe138a
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.estate.expert.admin.controller.admin;
 
import com.estate.expert.admin.io.processor.impl.matrix.WeightMatrixProcessor;
import com.estate.expert.admin.service.expert.DeviceExpertService;
import com.estate.expert.admin.service.expert.SysExpertService;
import com.estate.expert.common.domain.Session;
import com.estate.expert.common.rpc.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
 
/**
 * 专家矩阵数据导入导出(JSON格式)
 */
@Slf4j
@CrossOrigin
@Api(tags = "管理后台/专家矩阵数据")
@RequestMapping("/admin/matrix")
@Controller
@Validated
public class MatrixController {
 
    @Resource
    private SysExpertService sysExpertService;
 
    @Resource
    private DeviceExpertService deviceExpertService;
 
    //////  打分专家 - 权重矩阵导入导出  //////
 
    @ApiOperation("综合体系-打分评价-权重矩阵导出")
    @PostMapping("/system/score/weightMatrix/export")
    public void exportSystemScoreWeightMatrix(
            @ApiParam("专家ID") @RequestParam Integer expertId,
            HttpServletResponse response) {
        var expert = sysExpertService.get(expertId);
        if(expert == null) {
            throw new RuntimeException("专家不存在");
        }
        
        var hierarchyTree = sysExpertService.getHierarchyTree(expertId);
        var weightMap = expert.getWeightMap();
        
        // 如果没有权重数据,生成默认权重(所有节点权重为1.0)
        if(weightMap == null || weightMap.isEmpty()) {
            weightMap = generateDefaultWeightMap(hierarchyTree);
        }
        
        try {
            String filename = "综合体系-" + expert.getName() + "-权重矩阵";
            String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()).replace("+", "%20");
            
            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFilename + ".json");
            
            new WeightMatrixProcessor(hierarchyTree).saveToJson(response.getOutputStream(), weightMap);
            response.getOutputStream().flush();
        } catch (Exception e) {
            log.error("综合体系权重矩阵导出失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }
 
    @ApiOperation("综合体系-打分评价-权重矩阵导入模版")
    @PostMapping("/system/score/weightMatrix/template")
    public void downloadSystemScoreWeightMatrixTemplate(
            @ApiParam("专家ID") @RequestParam Integer expertId,
            HttpServletResponse response) {
        exportSystemScoreWeightMatrix(expertId, response);
    }
 
    @ApiOperation("综合体系-打分评价-权重矩阵导入")
    @PostMapping("/system/score/weightMatrix/import")
    @ResponseBody
    public Result importSystemScoreWeightMatrix(
            Session session,
            @ApiParam("专家ID") @RequestParam Integer expertId,
            @RequestParam("file") MultipartFile file) {
        try {
            var hierarchyTree = sysExpertService.getHierarchyTree(expertId);
            var weightMap = new WeightMatrixProcessor(hierarchyTree).loadFromJson(file.getInputStream());
            
            // 更新专家的权重数据
            var expert = sysExpertService.get(expertId);
            expert.setWeightMap(weightMap);
            sysExpertService.edit(expert);
            
            return Result.success("权重矩阵导入成功,共导入 " + weightMap.size() + " 个节点");
        } catch (Exception e) {
            log.error("综合体系权重矩阵导入失败", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
 
    @ApiOperation("设备体系-打分评价-权重矩阵导出")
    @PostMapping("/device/score/weightMatrix/export")
    public void exportDeviceScoreWeightMatrix(
            @ApiParam("专家ID") @RequestParam Integer expertId,
            HttpServletResponse response) {
        var expert = deviceExpertService.get(expertId);
        if(expert == null) {
            throw new RuntimeException("专家不存在");
        }
        
        var hierarchyTree = deviceExpertService.getHierarchyTree(expertId);
        var weightMap = expert.getWeightMap();
        
        // 如果没有权重数据,生成默认权重(所有节点权重为1.0)
        if(weightMap == null || weightMap.isEmpty()) {
            weightMap = generateDefaultWeightMap(hierarchyTree);
        }
        
        try {
            String filename = "设备体系-" + expert.getName() + "-权重矩阵";
            String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()).replace("+", "%20");
            
            response.setContentType("application/json;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFilename + ".json");
            
            new WeightMatrixProcessor(hierarchyTree).saveToJson(response.getOutputStream(), weightMap);
            response.getOutputStream().flush();
        } catch (Exception e) {
            log.error("设备体系权重矩阵导出失败", e);
            throw new RuntimeException("导出失败: " + e.getMessage());
        }
    }
 
    @ApiOperation("设备体系-打分评价-权重矩阵导入模版")
    @PostMapping("/device/score/weightMatrix/template")
    public void downloadDeviceScoreWeightMatrixTemplate(
            @ApiParam("专家ID") @RequestParam Integer expertId,
            HttpServletResponse response) {
        exportDeviceScoreWeightMatrix(expertId, response);
    }
 
    @ApiOperation("设备体系-打分评价-权重矩阵导入")
    @PostMapping("/device/score/weightMatrix/import")
    @ResponseBody
    public Result importDeviceScoreWeightMatrix(
            Session session,
            @ApiParam("专家ID") @RequestParam Integer expertId,
            @RequestParam("file") MultipartFile file) {
        try {
            var hierarchyTree = deviceExpertService.getHierarchyTree(expertId);
            var weightMap = new WeightMatrixProcessor(hierarchyTree).loadFromJson(file.getInputStream());
            
            // 更新专家的权重数据
            var expert = deviceExpertService.get(expertId);
            expert.setWeightMap(weightMap);
            deviceExpertService.edit(expert);
            
            return Result.success("权重矩阵导入成功,共导入 " + weightMap.size() + " 个节点");
        } catch (Exception e) {
            log.error("设备体系权重矩阵导入失败", e);
            return Result.error("导入失败: " + e.getMessage());
        }
    }
    
    /**
     * 生成默认的权重Map(所有节点权重为1.0)
     */
    private java.util.Map<String, Float> generateDefaultWeightMap(
            com.estate.expert.admin.utils.tree.TreeRoot<com.estate.expert.admin.domain.standard.hierachy.HierarchyData> hierarchyTree) {
        java.util.Map<String, Float> weightMap = new java.util.TreeMap<>();
        
        // 递归遍历所有节点,生成默认权重
        generateDefaultWeightForNode(hierarchyTree.root(), weightMap);
        
        return weightMap;
    }
    
    /**
     * 递归为节点及其子节点生成默认权重
     */
    private void generateDefaultWeightForNode(
            com.estate.expert.admin.utils.tree.TreeNode<com.estate.expert.admin.domain.standard.hierachy.HierarchyData> node,
            java.util.Map<String, Float> weightMap) {
        
        // 跳过根节点
        if(!"/".equals(node.getPath()) && node.getPath() != null && node.getPath().length() > 1) {
            // 设置默认权重为1.0
            weightMap.put(node.getPath(), 1.0f);
        }
        
        // 递归处理子节点
        if(node.getChildren() != null && !node.getChildren().isEmpty()) {
            for(var child : node.getChildren().values()) {
                generateDefaultWeightForNode(child, weightMap);
            }
        }
    }
}