From 98bd0499c78731f2d21f3275bad29a18c8564d97 Mon Sep 17 00:00:00 2001
From: zwl <1051256694@qq.com>
Date: 星期五, 20 三月 2026 09:33:02 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/common/web/BaseController.java | 91 ++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 81 insertions(+), 10 deletions(-)
diff --git a/src/main/java/com/zy/common/web/BaseController.java b/src/main/java/com/zy/common/web/BaseController.java
index 995c3d9..0c43dad 100644
--- a/src/main/java/com/zy/common/web/BaseController.java
+++ b/src/main/java/com/zy/common/web/BaseController.java
@@ -21,6 +21,9 @@
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.time.temporal.Temporal;
import java.util.*;
/**
@@ -154,7 +157,11 @@
if (Cools.isEmpty(condition)) {
return;
}
- List<String> columns = new ArrayList<>();
+ String keyword = String.valueOf(condition).trim();
+ if (keyword.isEmpty()) {
+ return;
+ }
+ boolean appended = false;
for (Field field :Cools.getAllFields(cls)){
if (Modifier.isFinal(field.getModifiers())
|| Modifier.isStatic(field.getModifiers())
@@ -169,19 +176,83 @@
column = field.getName();
}
if (!set.contains(column)) {
- columns.add(column);
+ if (supportsLikeSearch(field.getType())) {
+ if (!appended) {
+ wrapper.andNew();
+ } else {
+ wrapper.or();
+ }
+ wrapper.like(column, keyword);
+ appended = true;
+ continue;
+ }
+ Object exactValue = parseExactSearchValue(field.getType(), keyword);
+ if (exactValue != null) {
+ if (!appended) {
+ wrapper.andNew();
+ } else {
+ wrapper.or();
+ }
+ wrapper.eq(column, exactValue);
+ appended = true;
+ }
}
}
- if (columns.isEmpty()) {
- return;
+ }
+
+ private boolean supportsLikeSearch(Class<?> fieldType) {
+ return CharSequence.class.isAssignableFrom(fieldType)
+ || fieldType == Character.class
+ || fieldType == char.class
+ || Date.class.isAssignableFrom(fieldType)
+ || Temporal.class.isAssignableFrom(fieldType);
+ }
+
+ private Object parseExactSearchValue(Class<?> fieldType, String keyword) {
+ if (!isNumericField(fieldType)) {
+ return null;
}
- for (int i=0;i<columns.size();i++){
- if (i==0){
- wrapper.andNew();
- } else {
- wrapper.or();
+ try {
+ BigDecimal numericValue = new BigDecimal(keyword);
+ if (fieldType == Integer.class || fieldType == int.class) {
+ return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.intValueExact() : null;
}
- wrapper.like(columns.get(i), condition);
+ if (fieldType == Long.class || fieldType == long.class) {
+ return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.longValueExact() : null;
+ }
+ if (fieldType == Short.class || fieldType == short.class) {
+ return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.shortValueExact() : null;
+ }
+ if (fieldType == Byte.class || fieldType == byte.class) {
+ return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.byteValueExact() : null;
+ }
+ if (fieldType == BigInteger.class) {
+ return numericValue.stripTrailingZeros().scale() <= 0 ? numericValue.toBigIntegerExact() : null;
+ }
+ if (fieldType == Float.class || fieldType == float.class) {
+ float value = numericValue.floatValue();
+ return Float.isFinite(value) ? value : null;
+ }
+ if (fieldType == Double.class || fieldType == double.class) {
+ double value = numericValue.doubleValue();
+ return Double.isFinite(value) ? value : null;
+ }
+ if (fieldType == BigDecimal.class) {
+ return numericValue;
+ }
+ } catch (Exception ignored) {
+ return null;
}
+ return null;
+ }
+
+ private boolean isNumericField(Class<?> fieldType) {
+ return Number.class.isAssignableFrom(fieldType)
+ || fieldType == byte.class
+ || fieldType == short.class
+ || fieldType == int.class
+ || fieldType == long.class
+ || fieldType == float.class
+ || fieldType == double.class;
}
}
--
Gitblit v1.9.1