haungxing
10 months ago
5 changed files with 72 additions and 23 deletions
@ -0,0 +1,5 @@ |
|||||||
|
package com.hnac.hzims.common.support.constants; |
||||||
|
|
||||||
|
public enum Order { |
||||||
|
ASC,DESC; |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
package com.hnac.hzims.common.support.utils; |
||||||
|
|
||||||
|
|
||||||
|
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.tool.utils.BeanUtil; |
||||||
|
|
||||||
|
import java.lang.reflect.Field; |
||||||
|
import java.util.*; |
||||||
|
import java.util.function.Function; |
||||||
|
|
||||||
|
public class Condition extends org.springblade.core.mp.support.Condition { |
||||||
|
|
||||||
|
public static List<Field> getFieldList(Object object) { |
||||||
|
Class<?> clazz = object.getClass(); |
||||||
|
List<Field> fields = Lists.newArrayList(); |
||||||
|
while (null != clazz){ |
||||||
|
fields.addAll(Arrays.asList(clazz.getDeclaredFields())); |
||||||
|
clazz = clazz.getSuperclass(); |
||||||
|
} |
||||||
|
return fields; |
||||||
|
} |
||||||
|
|
||||||
|
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)); |
||||||
|
List<Field> fields = getFieldList(query); |
||||||
|
// 填充排序字段
|
||||||
|
orderFields.forEach((fieldName,order) -> { |
||||||
|
Optional<Field> existField = fields.stream().filter(field -> camel2under(fieldName).equals(camel2under(field.getName()))).findAny(); |
||||||
|
if(existField.isPresent()) { |
||||||
|
if(Order.ASC.equals(order)) { |
||||||
|
queryWrapper.orderByAsc(fieldName); |
||||||
|
} |
||||||
|
else if(Order.DESC.equals(order)) { |
||||||
|
queryWrapper.orderByDesc(fieldName); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
return queryWrapper.lambda(); |
||||||
|
} |
||||||
|
|
||||||
|
private static String camel2under(String c) { |
||||||
|
String separator = "_"; |
||||||
|
c = c.replaceAll("([a-z])([A-Z])", "$1" + separator + "$2").toUpperCase(); |
||||||
|
return c; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue