skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
zy-asrs-wms/src/main/java/com/zy/asrs/wms/common/domain/PageParam.java
@@ -1,195 +1,195 @@
package com.zy.asrs.wms.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.asrs.wms.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 (val == null) {
                continue;
            }
            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 (key.equals("_param")) {
                Map param = (Map) val;
                for (Object value : param.entrySet()) {
                    Map.Entry entry = (Map.Entry) value;
                    String paramKey = entry.getKey().toString();
                    if (this.isToUnderlineCase) {
                        paramKey = Utils.toSymbolCase(paramKey, '_');
                    }
                    if (!Cools.isEmpty(entry.getValue())) {
                        queryWrapper.like(paramKey, entry.getValue());
                    }
                }
            } else if (val instanceof List) {
                if (this.isToUnderlineCase) {
                    key = Utils.toSymbolCase(key, '_');
                }
                queryWrapper.in(key, (List) val);
            } 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 ("id".equals(field.getName())) {
                    queryWrapper.orderByDesc("id");
                } else 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;
    }
}
package com.zy.asrs.wms.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.asrs.wms.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 (val == null) {
                continue;
            }
            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 (key.equals("_param")) {
                Map param = (Map) val;
                for (Object value : param.entrySet()) {
                    Map.Entry entry = (Map.Entry) value;
                    String paramKey = entry.getKey().toString();
                    if (this.isToUnderlineCase) {
                        paramKey = Utils.toSymbolCase(paramKey, '_');
                    }
                    if (!Cools.isEmpty(entry.getValue())) {
                        queryWrapper.like(paramKey, entry.getValue());
                    }
                }
            } else if (val instanceof List) {
                if (this.isToUnderlineCase) {
                    key = Utils.toSymbolCase(key, '_');
                }
                queryWrapper.in(key, (List) val);
            } 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 ("id".equals(field.getName())) {
                    queryWrapper.orderByDesc("id");
                } else 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;
    }
}