Browse Source

Merge remote-tracking branch 'origin/master'

zhongwei
ty 10 months ago
parent
commit
3c3075f6f3
  1. 63
      hzims-biz-common/src/main/java/com/hnac/hzims/common/support/utils/Condition.java

63
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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.hzims.common.support.constants.Order; 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.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.util.Assert;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
@ -24,7 +29,7 @@ public class Condition extends org.springblade.core.mp.support.Condition {
} }
public static <T> LambdaQueryWrapper<T> getQueryWrapper(Object query, Class<T> clazz, Map<String,? extends Enum<Order>> orderFields) { public static <T> LambdaQueryWrapper<T> getQueryWrapper(Object query, Class<T> clazz, Map<String,? extends Enum<Order>> orderFields) {
QueryWrapper<T> queryWrapper = getQueryWrapper(BeanUtil.copy(query,clazz)); QueryWrapper<T> queryWrapper = getQueryWrapper(query,clazz);
List<Field> fields = getFieldList(query); List<Field> fields = getFieldList(query);
// 填充排序字段 // 填充排序字段
orderFields.forEach((fieldName,order) -> { 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(); c = c.replaceAll("([a-z])([A-Z])", "$1" + separator + "$2").toUpperCase();
return c; return c;
} }
private static <T> QueryWrapper<T> getQueryWrapper(Object query, Class<T> clazz) {
QueryWrapper qw = new QueryWrapper();
qw.setEntityClass(clazz);
List<Field> 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;
}
} }

Loading…
Cancel
Save