|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.wcs.common.domain; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.annotation.TableField; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.annotation.TableLogic; | 
|---|
|  |  |  | 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.domain.QueryField; | 
|---|
|  |  |  | import com.zy.asrs.framework.domain.QueryType; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.DateUtils; | 
|---|
|  |  |  | import com.zy.asrs.wcs.utils.Utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.lang.reflect.Field; | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private final boolean isToUnderlineCase; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final Class<T> cls; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public PageParam() { | 
|---|
|  |  |  | this(null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.getPageIdx() != null) { | 
|---|
|  |  |  | setCurrent(where.getPageIdx()); | 
|---|
|  |  |  | if (where.getCurrent() != null) { | 
|---|
|  |  |  | setCurrent(where.getCurrent()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (where.getPageSize() != null) { | 
|---|
|  |  |  | setSize(where.getPageSize()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param excludes 不包含的字段 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public QueryWrapper<T> getWrapper(String... excludes) { | 
|---|
|  |  |  | return buildWrapper(null, Arrays.asList(excludes)); | 
|---|
|  |  |  | public QueryWrapper<T> buildWrapper() { | 
|---|
|  |  |  | return this.buildWrapper(false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param columns 只包含的字段 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public QueryWrapper<T> getWrapperWith(String... columns) { | 
|---|
|  |  |  | return buildWrapper(Arrays.asList(columns), null); | 
|---|
|  |  |  | public QueryWrapper<T> buildWrapper(Consumer<QueryWrapper<T>> consumer) { | 
|---|
|  |  |  | return this.buildWrapper(false, consumer); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public QueryWrapper<T> buildWrapper(boolean like) { | 
|---|
|  |  |  | return this.buildWrapper(like, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private QueryWrapper<T> buildWrapper(List<String> columns, List<String> excludes) { | 
|---|
|  |  |  | @SuppressWarnings("all") | 
|---|
|  |  |  | public QueryWrapper<T> buildWrapper(boolean like, Consumer<QueryWrapper<T>> consumer) { | 
|---|
|  |  |  | QueryWrapper<T> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | Map<String, Object> map = Cools.conver(where); | 
|---|
|  |  |  | for (String fieldName : map.keySet()) { | 
|---|
|  |  |  | Object fieldValue = map.get(fieldName); | 
|---|
|  |  |  | Field field = Cools.getField(where.getClass(), fieldName); | 
|---|
|  |  |  | assert field != null; | 
|---|
|  |  |  | // 过滤不包含的字段 | 
|---|
|  |  |  | if (columns != null && !columns.contains(fieldName)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> map = where.getMap(); | 
|---|
|  |  |  | for (String key : map.keySet()) { | 
|---|
|  |  |  | Object val = map.get(key); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 过滤排除的字段 | 
|---|
|  |  |  | if (excludes != null && excludes.contains(fieldName)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 过滤逻辑删除字段 | 
|---|
|  |  |  | if (field.getAnnotation(TableLogic.class) != null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 过滤租户id字段 | 
|---|
|  |  |  | if (fieldName.equals("hostId")) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取注解指定的查询字段及查询方式 | 
|---|
|  |  |  | QueryType queryType = QueryType.LIKE; | 
|---|
|  |  |  | QueryField queryField = field.getAnnotation(QueryField.class); | 
|---|
|  |  |  | if (queryField != null) { | 
|---|
|  |  |  | if (!Cools.isEmpty(queryField.value())) { | 
|---|
|  |  |  | fieldName = queryField.value(); | 
|---|
|  |  |  | if (key.contains("Range")) { | 
|---|
|  |  |  | ArrayList<String> list = null; | 
|---|
|  |  |  | if (val instanceof ArrayList) { | 
|---|
|  |  |  | list = (ArrayList<String>) val; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | queryField.type(); | 
|---|
|  |  |  | queryType = queryField.type(); | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  | // 过滤非本表的字段 | 
|---|
|  |  |  | TableField tableField = field.getAnnotation(TableField.class); | 
|---|
|  |  |  | if (tableField != null && !tableField.exist()) { | 
|---|
|  |  |  | 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 (this.isToUnderlineCase) { | 
|---|
|  |  |  | fieldName = Utils.toSymbolCase(fieldName, '_'); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (field.isAnnotationPresent(TableField.class)){ | 
|---|
|  |  |  | TableField annotation = field.getAnnotation(TableField.class); | 
|---|
|  |  |  | if (!annotation.exist()) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | switch (queryType) { | 
|---|
|  |  |  | case EQ: | 
|---|
|  |  |  | queryWrapper.eq(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case NE: | 
|---|
|  |  |  | queryWrapper.ne(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case GT: | 
|---|
|  |  |  | queryWrapper.gt(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case GE: | 
|---|
|  |  |  | queryWrapper.ge(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LT: | 
|---|
|  |  |  | queryWrapper.lt(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LE: | 
|---|
|  |  |  | queryWrapper.le(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LIKE: | 
|---|
|  |  |  | queryWrapper.like(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case NOT_LIKE: | 
|---|
|  |  |  | queryWrapper.notLike(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LIKE_LEFT: | 
|---|
|  |  |  | queryWrapper.likeLeft(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case LIKE_RIGHT: | 
|---|
|  |  |  | queryWrapper.likeRight(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case IS_NULL: | 
|---|
|  |  |  | queryWrapper.isNull(fieldName); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case IS_NOT_NULL: | 
|---|
|  |  |  | queryWrapper.isNotNull(fieldName); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case IN: | 
|---|
|  |  |  | queryWrapper.in(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case NOT_IN: | 
|---|
|  |  |  | queryWrapper.notIn(fieldName, fieldValue); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case IN_STR: | 
|---|
|  |  |  | if (fieldValue instanceof String) { | 
|---|
|  |  |  | queryWrapper.in(fieldName, Arrays.asList(((String) fieldValue).split(","))); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case NOT_IN_STR: | 
|---|
|  |  |  | if (fieldValue instanceof String) { | 
|---|
|  |  |  | queryWrapper.notIn(fieldName, Arrays.asList(((String) fieldValue).split(","))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|