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; + } }