Browse Source

2026-07-28

Lab Test优化
master
fengyuan_yang 13 hours ago
parent
commit
b7fa9c336b
  1. 110
      src/main/java/com/spring/modules/lab/service/impl/LabServiceImpl.java
  2. 9
      src/main/resources/mapper/lab/LabMapper.xml

110
src/main/java/com/spring/modules/lab/service/impl/LabServiceImpl.java

@ -3,6 +3,8 @@ package com.spring.modules.lab.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.common.utils.Constant;
import com.spring.common.utils.DateUtils;
@ -49,6 +51,8 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.spring.modules.base.utils.CommonUtils.getPropertyValue;
@ -57,6 +61,9 @@ public class LabServiceImpl extends ServiceImpl<LabMapper, LabEntity> implements
private static final String LAB_PROPERTY_TYPE = "LAB";
private static final String LAB_TEST_LAB_DICT_TYPE = "lab_test_lab";
private static final String LAB_TEST_LAB_DICT_SITE = "41";
private static final Pattern ORDER_COLUMN_PATTERN = Pattern.compile("^[A-Za-z0-9_.]+$");
private static final Pattern TESTER_EQUAL_PATTERN = Pattern.compile("(?i)\\ba\\.tester\\b\\s*=\\s*'([^']*)'");
private static final Pattern TESTER_LIKE_PATTERN = Pattern.compile("(?i)\\ba\\.tester\\b\\s+like\\s*'([^']*)'");
@Autowired
private SysUserDao sysUserDao;
@ -99,6 +106,11 @@ public class LabServiceImpl extends ServiceImpl<LabMapper, LabEntity> implements
params.put("referenceNo", trimToNull((String) params.get("referenceNo")));
params.put("applicant", trimToNull((String) params.get("applicant")));
params.put("currentApprover", trimToNull((String) params.get("currentApprover")));
params.put("tester", trimToNull((String) params.get("tester")));
params.put("testerLike", buildTesterLikeParam((String) params.get("tester")));
// 主信息列表固定按序列号倒排避免被前端排序参数覆盖
params.put(Constant.ORDER_FIELD, "reference_no");
params.put(Constant.ORDER, "desc");
IPage<LabEntity> page = this.baseMapper.queryPageWithNames(
new Query<LabEntity>().getPage(params, "reference_no", false),
@ -119,16 +131,18 @@ public class LabServiceImpl extends ServiceImpl<LabMapper, LabEntity> implements
} else {
whereClause = StringUtils.defaultString(whereClause).trim();
}
whereClause = normalizeTesterSearchWhereClause(whereClause);
orderClause = StringUtils.defaultString(orderClause).trim();
Map<String, String> params = new HashMap<>();
params.put("menuId", data.getMenuId());
params.put("userName", data.getUserName());
params.put("whereClause", whereClause);
params.put("orderClause", orderClause);
Page<LabEntity> pageReq = new Page<>(data.getNo(), data.getSize());
applyPageOrder(pageReq, orderClause);
IPage<LabEntity> page = this.baseMapper.queryPageWithNamesAny(
new com.baomidou.mybatisplus.extension.plugins.pagination.Page<LabEntity>(data.getNo(), data.getSize()),
pageReq,
params
);
return new PageUtils(page);
@ -617,6 +631,98 @@ public class LabServiceImpl extends ServiceImpl<LabMapper, LabEntity> implements
return value;
}
private String buildTesterLikeParam(String rawValue) {
String value = trimToNull(rawValue);
if (value == null) {
return null;
}
return value;
}
private String normalizeTesterSearchWhereClause(String whereClause) {
String normalizedWhereClause = StringUtils.trimToEmpty(whereClause);
if (StringUtils.isBlank(normalizedWhereClause)) {
return normalizedWhereClause;
}
normalizedWhereClause = replaceTesterConditionToTokenLike(normalizedWhereClause, TESTER_LIKE_PATTERN);
normalizedWhereClause = replaceTesterConditionToTokenLike(normalizedWhereClause, TESTER_EQUAL_PATTERN);
return normalizedWhereClause;
}
private String replaceTesterConditionToTokenLike(String whereClause, Pattern conditionPattern) {
if (StringUtils.isBlank(whereClause) || conditionPattern == null) {
return StringUtils.defaultString(whereClause);
}
Matcher matcher = conditionPattern.matcher(whereClause);
StringBuffer buffer = new StringBuffer();
boolean replaced = false;
while (matcher.find()) {
String testerValue = StringUtils.trimToEmpty(matcher.group(1));
if (StringUtils.isBlank(testerValue)) {
matcher.appendReplacement(buffer, Matcher.quoteReplacement("1 = 0"));
replaced = true;
continue;
}
String escapedTesterValue = testerValue.replace("'", "''");
String replacement = "(';' + isnull(replace(a.tester, ',', ';'), '') + ';') like ('%;" + escapedTesterValue + ";%')";
matcher.appendReplacement(buffer, Matcher.quoteReplacement(replacement));
replaced = true;
}
if (!replaced) {
return whereClause;
}
matcher.appendTail(buffer);
return buffer.toString();
}
private void applyPageOrder(Page<LabEntity> page, String orderClause) {
if (page == null) {
return;
}
String normalizedOrderClause = StringUtils.trimToEmpty(orderClause);
if (StringUtils.startsWithIgnoreCase(normalizedOrderClause, "order by")) {
normalizedOrderClause = StringUtils.trimToEmpty(normalizedOrderClause.substring(8));
}
if (StringUtils.isBlank(normalizedOrderClause)) {
page.addOrder(OrderItem.desc("reference_no"));
return;
}
boolean hasValidOrder = false;
String[] orderItems = normalizedOrderClause.split(",");
for (String orderItem : orderItems) {
String singleOrder = StringUtils.trimToEmpty(orderItem);
if (StringUtils.isBlank(singleOrder)) {
continue;
}
String orderDirection = "DESC";
String orderColumn = singleOrder;
int lastSpaceIndex = singleOrder.lastIndexOf(" ");
if (lastSpaceIndex > 0) {
String maybeDirection = StringUtils.trimToEmpty(singleOrder.substring(lastSpaceIndex + 1));
if ("ASC".equalsIgnoreCase(maybeDirection) || "DESC".equalsIgnoreCase(maybeDirection)) {
orderDirection = maybeDirection.toUpperCase();
orderColumn = StringUtils.trimToEmpty(singleOrder.substring(0, lastSpaceIndex));
}
}
if (!isSafeOrderColumn(orderColumn)) {
continue;
}
if ("ASC".equalsIgnoreCase(orderDirection)) {
page.addOrder(OrderItem.asc(orderColumn));
} else {
page.addOrder(OrderItem.desc(orderColumn));
}
hasValidOrder = true;
}
if (!hasValidOrder) {
page.addOrder(OrderItem.desc("reference_no"));
}
}
private boolean isSafeOrderColumn(String orderColumn) {
return StringUtils.isNotBlank(orderColumn) && ORDER_COLUMN_PATTERN.matcher(orderColumn).matches();
}
private String queryLabTestLabLabelByValue(String dictValue) {
List<DictData> dictRows = dictDataService.lambdaQuery()
.eq(DictData::getSite, LAB_TEST_LAB_DICT_SITE)

9
src/main/resources/mapper/lab/LabMapper.xml

@ -144,6 +144,9 @@
<if test="params.applicant != null and params.applicant != ''">
and a.applicant like #{params.applicant}
</if>
<if test="params.testerLike != null and params.testerLike != ''">
and (';' + isnull(replace(a.tester, ',', ';'), '') + ';') like ('%;' + #{params.testerLike} + ';%')
</if>
</select>
<select id="queryPageWithNamesAny" resultMap="BaseResultMap">
@ -211,12 +214,6 @@
<if test="params.whereClause != null and params.whereClause != ''">
where ${params.whereClause}
</if>
<if test="params.orderClause != null and params.orderClause != ''">
order by ${params.orderClause}
</if>
<if test="params.orderClause == null or params.orderClause == ''">
order by a.reference_no desc
</if>
</select>
<select id="getDetailWithNames" resultMap="BaseResultMap">

Loading…
Cancel
Save