yeg
2026-09-02 409b5ac5a3b160eacb1fefa6a10bf97b5f6860d7
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
package com.ruoyi.system.service.impl;
 
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.HashMap;
import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.TTestDataOverride;
import com.ruoyi.system.mapper.TTestMapper;
import com.ruoyi.system.service.ITTestService;
import com.ruoyi.system.service.ITestDataQueryService;
 
/** 测试数据查询模块;实现不访问设备或模拟器历史。 */
@Service
public class TestDataQueryServiceImpl implements ITestDataQueryService
{
    private final TTestMapper mapper;
    private final ITTestService tests;
 
    public TestDataQueryServiceImpl(TTestMapper mapper, ITTestService tests)
    {
        this.mapper = mapper;
        this.tests = tests;
    }
 
    @Override public List<Map<String, Object>> records(Long testId)
    {
        requireTest(testId);
        List<Map<String, Object>> source = new java.util.ArrayList<>();
        List<Map<String, Object>> cabinet = mapper.selectPersistedRecords(testId);
        List<Map<String, Object>> collector = mapper.selectPersistedCollectorRecords(testId);
        if (cabinet != null) source.addAll(cabinet);
        if (collector != null) source.addAll(collector);
        return applyEffectiveData(testId, "REALTIME", source);
    }
 
    @Override public List<Map<String, Object>> steps(Long testId)
    {
        requireTest(testId);
        return applyEffectiveData(testId, "STEP", mapper.selectPersistedSteps(testId));
    }
 
    @Override public List<Map<String, Object>> cycles(Long testId)
    {
        requireTest(testId);
        return applyEffectiveData(testId, "CYCLE", mapper.selectPersistedCycles(testId));
    }
 
    /** 将最新人工覆盖应用到设备事实,删除只影响当前有效视图,不抹除原始采样。 */
    private List<Map<String, Object>> applyEffectiveData(Long testId, String dataType,
            List<Map<String, Object>> source)
    {
        List<Map<String, Object>> effective = new java.util.ArrayList<>(
                source == null ? List.<Map<String, Object>>of() : source);
        List<Map<String, Object>> imports = mapper.selectConsoleImportRecords(testId, dataType);
        for (Map<String, Object> imported : imports == null ? List.<Map<String, Object>>of() : imports)
        {
            try
            {
                @SuppressWarnings("unchecked")
                Map<String, Object> row = (Map<String, Object>) (Map<?, ?>) JSON.parseObject(
                        String.valueOf(imported.get("rowValues")), Map.class);
                row.put("stableIdentity", imported.get("stableIdentity"));
                effective.add(row);
            }
            catch (RuntimeException ignored) { }
        }
        List<TTestDataOverride> overrides = mapper.selectDataOverrides(testId, dataType);
        if (effective.isEmpty() || overrides == null || overrides.isEmpty()) return effective;
        Map<String, TTestDataOverride> byIdentity = new HashMap<>();
        for (TTestDataOverride override : overrides)
            if (override != null && override.getStableIdentity() != null)
                byIdentity.put(override.getStableIdentity(), override);
        List<Map<String, Object>> filtered = new java.util.ArrayList<>(effective.size());
        for (Map<String, Object> row : effective)
        {
            String identity = row.get("stableIdentity") == null ? stableIdentity(dataType, row)
                    : String.valueOf(row.get("stableIdentity"));
            TTestDataOverride override = byIdentity.get(identity);
            if (override != null && Boolean.TRUE.equals(override.getDeleted())) continue;
            Map<String, Object> copy = new LinkedHashMap<>(row);
            if (override != null && override.getFieldValues() != null
                    && !override.getFieldValues().isBlank())
            {
                try
                {
                    Map<String, Object> values = JSON.parseObject(override.getFieldValues(), Map.class);
                    if (values != null) copy.putAll(values);
                }
                catch (RuntimeException ignored)
                {
                    // 覆盖已由修订服务校验;解析异常时保留原始事实,避免查询整体失败。
                }
            }
            filtered.add(copy);
        }
        return filtered;
    }
 
    private String stableIdentity(String dataType, Map<String, Object> row)
    {
        if ("REALTIME".equals(dataType) && "COLLECTOR".equals(String.valueOf(row.get("sourceType"))))
            return String.join(":", "collector", identityPart(row, "collectorId"),
                    identityPart(row, "channelCode"), value(row, "sequenceId"), identityPart(row, "metricCode"));
        String adapterId = identityPart(row, "adapterId");
        String devtype = identityPart(row, "devtype");
        String deviceId = identityPart(row, "deviceId");
        String subdevid = identityPart(row, "subdevid");
        String channelId = identityPart(row, "channelId");
        String cycleNo = value(row, "cycleNo");
        String channelIdentity = String.join(":", adapterId, devtype, deviceId, subdevid, channelId);
        if ("REALTIME".equals(dataType)) return channelIdentity + ":" + value(row, "sequenceId");
        if ("STEP".equals(dataType)) return "step:" + channelIdentity + ":" + cycleNo + ":" + value(row, "stepNo");
        return "cycle:" + channelIdentity + ":" + cycleNo;
    }
 
    private String identityPart(Map<String, Object> row, String key)
    {
        Object value = row == null ? null : row.get(key);
        return value == null ? "" : String.valueOf(value);
    }
 
    private String value(Map<String, Object> row, String key)
    {
        return identityPart(row, key);
    }
 
    private void requireTest(Long testId)
    {
        if (testId == null || tests.selectTestById(testId) == null)
            throw new ServiceException("测试不存在");
    }
}