package com.zy.ptc.kernel.common.domain;
|
|
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.zy.asrs.framework.common.Cools;
|
import com.zy.asrs.framework.common.DateUtils;
|
import com.zy.ptc.kernel.utils.Utils;
|
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Modifier;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.function.Consumer;
|
|
/**
|
* Created by vincent on 2/13/2024
|
*/
|
public class PageParam<T, U extends BaseParam> extends Page<T> {
|
|
private static final long serialVersionUID = 1L;
|
|
private final U where;
|
|
/**
|
* 是否把字段名称驼峰转下划线
|
*/
|
private final boolean isToUnderlineCase;
|
|
private final Class<T> cls;
|
|
public PageParam() {
|
this(null);
|
}
|
|
public PageParam(U where) {
|
this(where, true);
|
}
|
|
public PageParam(U where, Class<T> cls) {
|
this(where, true, cls);
|
}
|
|
public PageParam(U where, boolean isToUnderlineCase) {
|
this(where, isToUnderlineCase, null);
|
}
|
|
|
public PageParam(U where, boolean isToUnderlineCase, Class<T> cls) {
|
super();
|
this.where = where;
|
this.isToUnderlineCase = isToUnderlineCase;
|
this.cls = cls;
|
if (where != null) {
|
if (where.getCurrent() != null) {
|
setCurrent(where.getCurrent());
|
}
|
if (where.getPageSize() != null) {
|
setSize(where.getPageSize());
|
}
|
}
|
}
|
|
public QueryWrapper<T> buildWrapper() {
|
return this.buildWrapper(false);
|
}
|
|
public QueryWrapper<T> buildWrapper(Consumer<QueryWrapper<T>> consumer) {
|
return this.buildWrapper(false, consumer);
|
}
|
|
public QueryWrapper<T> buildWrapper(boolean like) {
|
return this.buildWrapper(like, null);
|
}
|
|
@SuppressWarnings("all")
|
public QueryWrapper<T> buildWrapper(boolean like, Consumer<QueryWrapper<T>> consumer) {
|
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
Map<String, Object> map = where.getMap();
|
for (String key : map.keySet()) {
|
Object val = map.get(key);
|
|
if (key.contains("Range")) {
|
ArrayList<String> list = null;
|
if (val instanceof ArrayList) {
|
list = (ArrayList<String>) val;
|
}
|
if (null != list) {
|
key = key.replaceAll("Range", "");
|
if (this.isToUnderlineCase) {
|
key = Utils.toSymbolCase(key, '_');
|
}
|
queryWrapper.ge(key, DateUtils.convert(list.get(0)));
|
queryWrapper.le(key, DateUtils.convert(list.get(1)));
|
}
|
} else {
|
if (this.isToUnderlineCase) {
|
key = Utils.toSymbolCase(key, '_');
|
}
|
if (like) {
|
queryWrapper.like(key, val);
|
} else {
|
queryWrapper.eq(key, val);
|
}
|
|
}
|
}
|
|
if (null != consumer) {
|
consumer.accept(queryWrapper);
|
}
|
|
Field[] fields = null;
|
if (!Cools.isEmpty(cls)) {
|
fields = Cools.getAllFields(cls);
|
for (Field field : fields) {
|
if ("createTime".equals(field.getName())) {
|
queryWrapper.orderByDesc(Utils.toSymbolCase(field.getName(), '_'));
|
}
|
}
|
}
|
|
if (!Cools.isEmpty(where.getCondition()) && !Cools.isEmpty(fields)) {
|
List<String> columns = new ArrayList<>();
|
for (Field field : fields){
|
if (Modifier.isFinal(field.getModifiers())
|
|| Modifier.isStatic(field.getModifiers())
|
|| Modifier.isTransient(field.getModifiers())){
|
continue;
|
}
|
|
if (field.isAnnotationPresent(TableField.class)){
|
TableField annotation = field.getAnnotation(TableField.class);
|
if (!annotation.exist()) {
|
continue;
|
}
|
}
|
|
String column = Utils.toSymbolCase(field.getName(), '_');
|
columns.add(column);
|
}
|
if (!columns.isEmpty()) {
|
for (int i=0;i<columns.size();i++){
|
String column = columns.get(i);
|
String condition = where.getCondition();
|
|
if (i == 0) {
|
queryWrapper.and(wrapper -> wrapper.like(column, condition));
|
} else {
|
queryWrapper.or().like(column, condition);
|
}
|
}
|
}
|
}
|
return queryWrapper;
|
}
|
|
public Map<String, Object> pickOutMap() {
|
return pickOutMap(false);
|
}
|
|
public Map<String, Object> pickOutMap(boolean mergeCondition) {
|
Map<String, Object> map = where.getMap();
|
if (mergeCondition) {
|
if (!Cools.isEmpty(where.getCondition())) {
|
map.put("condition", where.getCondition());
|
}
|
}
|
return map;
|
}
|
|
}
|