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 extends Page { private static final long serialVersionUID = 1L; private final U where; /** * 是否把字段名称驼峰转下划线 */ private final boolean isToUnderlineCase; private final Class cls; public PageParam() { this(null); } public PageParam(U where) { this(where, true); } public PageParam(U where, Class cls) { this(where, true, cls); } public PageParam(U where, boolean isToUnderlineCase) { this(where, isToUnderlineCase, null); } public PageParam(U where, boolean isToUnderlineCase, Class 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 buildWrapper() { return this.buildWrapper(false); } public QueryWrapper buildWrapper(Consumer> consumer) { return this.buildWrapper(false, consumer); } public QueryWrapper buildWrapper(boolean like) { return this.buildWrapper(like, null); } @SuppressWarnings("all") public QueryWrapper buildWrapper(boolean like, Consumer> consumer) { QueryWrapper queryWrapper = new QueryWrapper<>(); Map map = where.getMap(); for (String key : map.keySet()) { Object val = map.get(key); if (key.contains("Range")) { ArrayList list = null; if (val instanceof ArrayList) { list = (ArrayList) 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 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 wrapper.like(column, condition)); } else { queryWrapper.or().like(column, condition); } } } } return queryWrapper; } public Map pickOutMap() { return pickOutMap(false); } public Map pickOutMap(boolean mergeCondition) { Map map = where.getMap(); if (mergeCondition) { if (!Cools.isEmpty(where.getCondition())) { map.put("condition", where.getCondition()); } } return map; } }