tuqin
2026-10-29 0a3129cb06fcea6677245097715dcc6fa3de65e5
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.ruoyi.lims.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.lims.pojo.CleanRule;
import com.ruoyi.lims.pojo.CollectionVerify;
import com.ruoyi.lims.pojo.DataCollection;
import com.ruoyi.lims.service.CleanRuleService;
import com.ruoyi.lims.service.CollectionVerifyService;
import com.ruoyi.lims.service.DataCollectionService;
import com.ruoyi.lims.service.LimsCleanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class LimsCleanServiceImpl implements LimsCleanService {
 
    private final DataCollectionService collectionService;
    private final CleanRuleService ruleService;
    private final CollectionVerifyService verifyService;
 
    @Override
    public Map<String, Object> cleanOne(Long collectionId) {
        Map<String, Object> ret = blankResult();
        if (collectionId == null) {
            ret.put("message", "collectionId 不能为空");
            return ret;
        }
        DataCollection dc = collectionService.getById(collectionId);
        if (dc == null) {
            ret.put("message", "采集记录不存在");
            return ret;
        }
        doClean(dc, ret);
        return ret;
    }
 
    @Override
    public Map<String, Object> cleanBatch(List<Long> collectionIds) {
        Map<String, Object> ret = blankResult();
        if (collectionIds == null || collectionIds.isEmpty()) {
            ret.put("message", "请传入要清洗的记录ID");
            return ret;
        }
        for (Long id : collectionIds) {
            DataCollection dc = collectionService.getById(id);
            if (dc == null) continue;
            doClean(dc, ret);
        }
        return ret;
    }
 
    @Override
    public Map<String, Object> cleanPending(int limit) {
        QueryWrapper<DataCollection> w = new QueryWrapper<>();
        w.and(x -> x.eq("clean_status", "pending").or().isNull("clean_status"))
                .orderByAsc("id");
        w.last("limit " + Math.max(1, limit));
        List<DataCollection> list = collectionService.list(w);
 
        Map<String, Object> ret = blankResult();
        ret.put("scanned", list.size());
        for (DataCollection dc : list) {
            doClean(dc, ret);
        }
        return ret;
    }
 
    /* ==================== 核心清洗逻辑 ==================== */
 
    private void doClean(DataCollection dc, Map<String, Object> ret) {
        ret.put("scanned", (Integer) ret.get("scanned") + 1);
 
        List<CleanRule> rules = loadRules(dc);
        List<String> logs = new ArrayList<>();
        boolean discarded = false;
        String verifyStatus = "passed";
        List<String> verifyMsgs = new ArrayList<>();
 
        // 重复剔除规则:同设备同类型最近 5 分钟内已有相同数值
        boolean needDupCheck = rules.stream().anyMatch(r -> "duplicate".equalsIgnoreCase(r.getRuleType()));
 
        for (CleanRule r : rules) {
            if (discarded) break;
            String type = lower(r.getRuleType());
            String expr = r.getConditionExpr() == null ? "" : r.getConditionExpr().trim();
            String action = lower(r.getActionType());
            String actionVal = r.getActionValue();
 
            try {
                switch (type) {
                    case "empty": {
                        if (dc.getCollectionValue() == null) {
                            logs.add("[" + r.getRuleName() + "] 采集数值为空");
                            if ("discard".equals(action)) {
                                discarded = true;
                            } else if ("replace".equals(action) && actionVal != null) {
                                dc.setCollectionValue(new BigDecimal(actionVal));
                                logs.add("  → 替换为 " + actionVal);
                            } else {
                                logs.add("  → 仅标记");
                            }
                        }
                        break;
                    }
                    case "range": {
                        if (dc.getCollectionValue() == null) break;
                        String[] bounds = parseRange(expr);
                        if (bounds == null) break;
                        BigDecimal lo = bounds[0] == null ? null : new BigDecimal(bounds[0]);
                        BigDecimal hi = bounds[1] == null ? null : new BigDecimal(bounds[1]);
                        BigDecimal v = dc.getCollectionValue();
                        boolean out = (lo != null && v.compareTo(lo) < 0) || (hi != null && v.compareTo(hi) > 0);
                        if (!out) break;
 
                        logs.add("[" + r.getRuleName() + "] 数值 " + v.stripTrailingZeros().toPlainString()
                                + " 超出范围 " + expr);
                        if ("discard".equals(action)) {
                            discarded = true;
                        } else if ("clamp".equals(action)) {
                            BigDecimal nv = v;
                            if (lo != null && v.compareTo(lo) < 0) nv = lo;
                            if (hi != null && v.compareTo(hi) > 0) nv = hi;
                            dc.setCollectionValue(nv);
                            logs.add("  → 截断为 " + nv.stripTrailingZeros().toPlainString());
                        } else if ("replace".equals(action) && actionVal != null) {
                            dc.setCollectionValue(new BigDecimal(actionVal));
                            logs.add("  → 替换为 " + actionVal);
                        } else {
                            logs.add("  → 仅标记");
                        }
                        // 范围不通过也记一条验证记录
                        verifyStatus = "failed";
                        verifyMsgs.add(r.getRuleName() + ":数值 " + v.stripTrailingZeros().toPlainString() + " 超出范围 " + expr);
                        writeVerify(dc, r, false, expr, v.stripTrailingZeros().toPlainString(),
                                "超出允许范围");
                        break;
                    }
                    case "outlier": {
                        if (dc.getCollectionValue() == null) break;
                        String[] bounds = parseRange(expr);
                        if (bounds == null) break;
                        BigDecimal lo = bounds[0] == null ? null : new BigDecimal(bounds[0]);
                        BigDecimal hi = bounds[1] == null ? null : new BigDecimal(bounds[1]);
                        BigDecimal v = dc.getCollectionValue();
                        boolean out = (lo != null && v.compareTo(lo) < 0) || (hi != null && v.compareTo(hi) > 0);
                        if (!out) break;
                        logs.add("[" + r.getRuleName() + "] 疑似异常值 " + v.stripTrailingZeros().toPlainString());
                        if ("discard".equals(action)) {
                            discarded = true;
                        } else if ("replace".equals(action) && actionVal != null) {
                            dc.setCollectionValue(new BigDecimal(actionVal));
                            logs.add("  → 替换为 " + actionVal);
                        }
                        // 异常值只标记,不算验证不通过
                        break;
                    }
                    case "duplicate": {
                        if (!needDupCheck || dc.getCollectionValue() == null) break;
                        QueryWrapper<DataCollection> dw = new QueryWrapper<>();
                        dw.eq("device_code", dc.getDeviceCode())
                                .eq("collection_value", dc.getCollectionValue())
                                .ne("id", dc.getId())
                                .ge("collection_time", dc.getCollectionTime() == null
                                        ? LocalDateTime.now().minusMinutes(5) : dc.getCollectionTime().minusMinutes(5));
                        long same = collectionService.count(dw);
                        if (same > 0) {
                            logs.add("[" + r.getRuleName() + "] 5 分钟内出现 " + same + " 条相同数值,判定重复");
                            if ("discard".equals(action)) {
                                discarded = true;
                            }
                        }
                        break;
                    }
                    case "unit": {
                        // 单位换算:actionValue 形如 "mL>L:0.001"
                        if (actionVal == null || !actionVal.contains(":")) break;
                        String[] kv = actionVal.split(":", 2);
                        if (kv[0].equals(dc.getUnit()) && dc.getCollectionValue() != null) {
                            BigDecimal rate = new BigDecimal(kv[1].trim());
                            BigDecimal nv = dc.getCollectionValue().multiply(rate).setScale(6, RoundingMode.HALF_UP);
                            String toUnit = kv[0].contains(">") ? kv[0].substring(kv[0].indexOf('>') + 1) : dc.getUnit();
                            logs.add("[" + r.getRuleName() + "] 单位换算 "
                                    + dc.getCollectionValue().stripTrailingZeros().toPlainString()
                                    + " " + dc.getUnit() + " → "
                                    + nv.stripTrailingZeros().toPlainString() + " " + toUnit);
                            dc.setCollectionValue(nv);
                            dc.setUnit(toUnit);
                        }
                        break;
                    }
                    default:
                        break;
                }
            } catch (Exception e) {
                logs.add("[" + r.getRuleName() + "] 执行异常:" + e.getMessage());
            }
        }
 
        // 写回
        DataCollection upd = new DataCollection();
        upd.setId(dc.getId());
        upd.setCollectionValue(dc.getCollectionValue());
        upd.setUnit(dc.getUnit());
        if (discarded) {
            upd.setCleanStatus("discarded");
            upd.setVerifyStatus("failed");
            upd.setVerifyMsg("按规则丢弃:" + String.join(";", logs));
            ret.put("discarded", (Integer) ret.get("discarded") + 1);
        } else {
            upd.setCleanStatus(logs.isEmpty() ? "cleaned" : "cleaned");
            upd.setCleanLog(logs.isEmpty() ? "无规则命中" : String.join("\n", logs));
            upd.setVerifyStatus(verifyStatus);
            upd.setVerifyMsg(verifyMsgs.isEmpty() ? null : String.join(";", verifyMsgs));
            ret.put("cleaned", (Integer) ret.get("cleaned") + 1);
        }
        if (verifyStatus.equals("failed")) {
            ret.put("verifyFailed", (Integer) ret.get("verifyFailed") + 1);
        }
        try {
            collectionService.updateById(upd);
        } catch (Exception e) {
            ret.put("failed", (Integer) ret.get("failed") + 1);
            ret.put("message", "写回失败:" + e.getMessage());
        }
    }
 
    /**
     * 加载适用规则:接口专属规则 + 全局规则,按 priority 升序。
     *
     * 注意:清洗规则是「全局配置」,不应该受多租户过滤影响(tenant_id 为 NULL 的规则属于全局),
     * 所以这里临时跳过租户插件,否则预置的全局规则会被自动加上的 tenant_id 条件过滤掉。
     */
    private List<CleanRule> loadRules(DataCollection dc) {
        QueryWrapper<CleanRule> w = new QueryWrapper<>();
        w.eq("enabled", "1");
        w.orderByAsc("priority").orderByAsc("id");
 
        com.ruoyi.common.handler.CustomTenantLineHandler.TEMP_IGNORE.set(Boolean.TRUE);
        try {
            return ruleService.list(w);
        } finally {
            com.ruoyi.common.handler.CustomTenantLineHandler.TEMP_IGNORE.remove();
        }
    }
 
    /**
     * 解析条件表达式:支持 "<0"、">1000"、"0~100"、"<0 或 >1000"
     * 返回 [lo, hi],任一端为 null 表示不限
     */
    private String[] parseRange(String expr) {
        if (expr == null || expr.trim().isEmpty()) return null;
        String e = expr.replace(" ", "");
        // 形如 0~100
        if (e.contains("~")) {
            String[] p = e.split("~");
            if (p.length == 2) return new String[]{p[0], p[1]};
            return null;
        }
        // 形如 <0 或 >1000
        e = e.replace("或", "|").replace("and", "|").replace("&", "|");
        if (e.contains("|")) {
            String[] segs = e.split("\\|");
            String lo = null, hi = null;
            for (String s : segs) {
                if (s.startsWith("<=")) lo = s.substring(2);
                else if (s.startsWith("<")) lo = s.substring(1);
                else if (s.startsWith(">=")) hi = s.substring(2);
                else if (s.startsWith(">")) hi = s.substring(1);
            }
            if (lo == null && hi == null) return null;
            return new String[]{lo, hi};
        }
        if (e.startsWith("<=")) return new String[]{e.substring(2), null};
        if (e.startsWith("<")) return new String[]{e.substring(1), null};
        if (e.startsWith(">=")) return new String[]{null, e.substring(2)};
        if (e.startsWith(">")) return new String[]{null, e.substring(1)};
        return null;
    }
 
    private void writeVerify(DataCollection dc, CleanRule r, boolean passed, String expected, String actual, String msg) {
        try {
            CollectionVerify v = new CollectionVerify();
            v.setCollectionId(dc.getId());
            v.setCollectionCode(dc.getCollectionCode());
            v.setRuleId(r.getId());
            v.setRuleName(r.getRuleName());
            v.setRuleType(r.getRuleType());
            v.setPassed(passed ? "1" : "0");
            v.setExpected(expected);
            v.setActual(actual);
            v.setMessage(msg);
            v.setVerifyTime(LocalDateTime.now());
            verifyService.save(v);
        } catch (Exception ignore) {
        }
    }
 
    private Map<String, Object> blankResult() {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("scanned", 0);
        m.put("cleaned", 0);
        m.put("discarded", 0);
        m.put("verifyFailed", 0);
        m.put("failed", 0);
        return m;
    }
 
    private static String lower(String s) {
        return s == null ? null : s.trim().toLowerCase();
    }
}