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();
|
}
|
}
|