From b5c2dbf09848014f08523262a12fe16ac8d72674 Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Sun, 4 Feb 2024 19:45:31 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E9=A3=8E=E9=99=A9=E7=82=B9=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=AF=BC=E5=87=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hnac/hzims/common/support/utils/Condition.java | 63 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java index c593bb0..c1a2f53 100644 --- a/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java @@ -5,7 +5,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.common.collect.Lists; import com.hnac.hzims.common.support.constants.Order; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.mp.support.QueryField; +import org.springblade.core.mp.support.SqlCondition; import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.Func; +import org.springframework.util.Assert; import java.lang.reflect.Field; import java.util.*; @@ -24,7 +29,7 @@ public class Condition extends org.springblade.core.mp.support.Condition { } public static LambdaQueryWrapper getQueryWrapper(Object query, Class clazz, Map> orderFields) { - QueryWrapper queryWrapper = getQueryWrapper(BeanUtil.copy(query,clazz)); + QueryWrapper queryWrapper = getQueryWrapper(query,clazz); List fields = getFieldList(query); // 填充排序字段 orderFields.forEach((fieldName,order) -> { @@ -46,4 +51,60 @@ public class Condition extends org.springblade.core.mp.support.Condition { c = c.replaceAll("([a-z])([A-Z])", "$1" + separator + "$2").toUpperCase(); return c; } + + private static QueryWrapper getQueryWrapper(Object query, Class clazz) { + QueryWrapper qw = new QueryWrapper(); + qw.setEntityClass(clazz); + List fieldList = getFieldList(query); + for(Field field:fieldList){ + field.setAccessible(true); + QueryField queryField = field.getAnnotation(QueryField.class); + if(queryField==null) { + continue; + } + Object value; + try { + value = field.get(query); + } catch (Exception e) { + throw new ServiceException("获取属性性出错"); + } + if(value == null) { + continue; + } + List list = null; + if(value instanceof List){ + list = (List)value; + if(list.size() == 0) { + continue; + } + } + String condition = queryField.condition(); + if(Func.isBlank(condition)) { + continue; + } + String fileName = camel2under(field.getName()); + if(SqlCondition.EQUAL.equals(condition)) { + qw.eq(fileName, value); + }else if(SqlCondition.LIKE.equals(condition)){ + qw.like(fileName,value); + }else if(SqlCondition.LIKE_LEFT.equals(condition)){ + qw.likeLeft(fileName,value); + }else if(SqlCondition.LIKE_RIGHT.equals(condition)){ + qw.likeRight(fileName,value); + }else if(SqlCondition.NOT_IN.equals(condition)){ + String columnName = queryField.columnName(); + Assert.isTrue(Func.isBlank(columnName),() -> { + throw new ServiceException("查询不包含条件时需要指定列名"); + }); + qw.notIn(camel2under(columnName),list); + }else if(SqlCondition.IN.equals(condition)){ + String columnName = queryField.columnName(); + Assert.isTrue(Func.isBlank(columnName), () -> { + throw new ServiceException("查询包含条件时需要指定列名"); + }); + qw.in(camel2under(columnName),list); + } + } + return qw; + } }