liujie
2026-06-26 8fbe2f5f29b9658b6201b3daabe2d575c9da88fd
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
package com.estate.expert.admin.service.eval;
 
import com.estate.expert.admin.domain.business.device.Device;
import com.estate.expert.admin.domain.business.device.DeviceType;
import com.estate.expert.admin.domain.common.IDtoBase;
import com.estate.expert.admin.domain.exam.eval.EvalResult;
import com.estate.expert.admin.domain.exam.record.error.ErrorRecord;
import com.estate.expert.admin.domain.exam.task.check.CheckTask;
import com.estate.expert.admin.domain.standard.expert.Expert;
import com.estate.expert.admin.service.DeviceService;
import com.estate.expert.admin.service.ErrorRecordService;
import com.estate.expert.admin.service.common.CrudServiceBaseImpl;
import com.estate.expert.admin.service.expert.DeviceExpertService;
import com.estate.expert.admin.service.expert.SysExpertService;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * 评价服务的基础实现
 * 包含了评价的基本操作
 * 避免重复编写基础的评价代码以及不必要的测试
 */
public abstract class EvalServiceImpl<T extends EvalResult, D extends IDtoBase, Info, V>
        extends CrudServiceBaseImpl<T, D>
        implements EvalService<T, D, Info> {
 
    @Resource
    private ErrorRecordService errorRecordService;
    @Resource
    private DeviceExpertService deviceExpertService;
 
    @Resource
    private DeviceService deviceService;
 
    @Resource
    private SysExpertService sysExpertService;
 
    @Override
    public T eval(CheckTask checkTask) {
        V sysRes = null;
        Map<Integer, V> devResMap = new TreeMap<>();
 
        // 综合打分
        var systemExpert = checkTask.getPlan().getSystemScoreExpert();
        if(systemExpert != null) {
            var systemErrorRecords = errorRecordService.listSystemErrors(checkTask.getId());
 
            // 轮空项,如果设置了,则需要参与计算
            var byPassPaths = (enableByPass() && checkTask.getByPassInfo() != null) ? checkTask.getByPassInfo().getSystemHierarchyPaths() : null;
            sysRes = evaluate(systemExpert, sysExpertService, systemErrorRecords, byPassPaths);
        }
 
        // 设备打分
        var deviceExpert = checkTask.getPlan().getDeviceScoreExpert();
        var devices = new ArrayList<Device>();
        if(deviceExpert != null) {
            var deviceErrorRecords = errorRecordService.listDeviceErrors(checkTask.getId());
            var deviceIds = deviceErrorRecords.stream().map(r -> r.getDevice().getId()).distinct().toList();
            for(var deviceId : deviceIds) {
                var device = deviceService.get(deviceId);
                if(device == null)
                    continue;
 
                // 轮空项,如果设置了,则需要参与计算
                List<String> byPassPaths = null;
                if(enableByPass() && checkTask.getByPassInfo() != null) {
                    byPassPaths = checkTask.getByPassInfo().filterHierarchyPaths(deviceId);
                }
 
                // 设备打分
                var errorRecords = deviceErrorRecords.stream().filter(r -> Objects.equals(r.getDevice().getId(), deviceId)).toList();
                var devRes = evaluate(deviceExpert, device.getType(), deviceExpertService, errorRecords, byPassPaths);
                if(devRes != null) {
                    devResMap.put(deviceId, devRes);
                    devices.add(device);
                }
            }
        }
 
        // 保存评估结果
        return saveResult(checkTask, sysRes, devResMap, devices);
    }
 
    @Override
    public void evalOnce(CheckTask checkTask) {
        var res = getByCheckTaskId(checkTask.getId());
        if(res != null) {
            // 删除原有的评分结果,彻底删除
            deletePhysical(res.getId());
        }
 
        eval(checkTask);
    }
 
    /**
     * 初始化问题映射表
     * 
     * @param scoreExpert 评分专家
     * @param expertService 专家服务
     * @return 问题映射表
     */
    protected TreeMap<Integer, Boolean> initProblemMap(Expert scoreExpert, SysExpertService expertService) {        // 根据模型,构建问题map
        var problemMap = new TreeMap<Integer, Boolean>();
        expertService.listModelProblemIds(scoreExpert.getModel())
                .forEach(id -> problemMap.put(id, false));
        return problemMap;
    }
 
    /**
     * 初始化设备问题映射表
     * 
     * @param deviceType 设备类型
     * @param scoreExpert 评分专家
     * @param expertService 专家服务
     * @return 问题映射表
     */
    protected TreeMap<Integer, Boolean> initDeviceProblemMap(DeviceType deviceType, Expert scoreExpert, DeviceExpertService expertService) {        // 根据模型,构建问题map
        var problemMap = new TreeMap<Integer, Boolean>();
        expertService.listModelProblemIds(scoreExpert.getModel(), deviceType)
                .forEach(id -> problemMap.put((Integer) id, false));
        return problemMap;
    }
 
    protected abstract V evaluate(Expert expert, SysExpertService expertService, List<ErrorRecord> errorRecords, List<String> byPassPaths);
 
    protected abstract V evaluate(Expert expert, DeviceType deviceType, DeviceExpertService expertService, List<ErrorRecord> errorRecords, List<String> byPassPaths);
 
    protected abstract T saveResult(CheckTask checkTask, V sysRes, Map<Integer, V> devResMap, List<Device> devices);
 
    protected abstract boolean enableByPass();
}