From 0d04bc5d8080b82338302fba0a59fccff2eaedfc Mon Sep 17 00:00:00 2001 From: Junjie <fallin.jie@qq.com> Date: 星期日, 06 七月 2025 11:28:29 +0800 Subject: [PATCH] # --- zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/domain/PageParam.java | 147 +++++++++++++++++++++++++++++------------------- 1 files changed, 89 insertions(+), 58 deletions(-) diff --git a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/domain/PageParam.java b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/domain/PageParam.java index 82f0a05..83c0a9e 100644 --- a/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/domain/PageParam.java +++ b/zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/common/domain/PageParam.java @@ -1,12 +1,18 @@ package com.zy.asrs.wcs.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.wcs.utils.Utils; +import com.zy.asrs.wcs.utils.CommonUtils; +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 @@ -22,6 +28,8 @@ */ private final boolean isToUnderlineCase; + private final Class<T> cls; + public PageParam() { this(null); } @@ -30,10 +38,20 @@ 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()); @@ -48,8 +66,16 @@ return this.buildWrapper(false); } - @SuppressWarnings("all") + 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()) { @@ -63,14 +89,14 @@ if (null != list) { key = key.replaceAll("Range", ""); if (this.isToUnderlineCase) { - key = Utils.toSymbolCase(key, '_'); + key = CommonUtils.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, '_'); + key = CommonUtils.toSymbolCase(key, '_'); } if (like) { queryWrapper.like(key, val); @@ -79,64 +105,69 @@ } } + } + if (null != consumer) { + consumer.accept(queryWrapper); + } -// 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(","))); -// } -// break; -// case NOT_IN_STR: -// if (fieldValue instanceof String) { -// queryWrapper.notIn(fieldName, Arrays.asList(((String) fieldValue).split(","))); -// } -// break; -// } + Field[] fields = null; + if (!Cools.isEmpty(cls)) { + fields = Cools.getAllFields(cls); + for (Field field : fields) { + if ("createTime".equals(field.getName())) { + queryWrapper.orderByDesc(CommonUtils.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 = CommonUtils.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; + } + } -- Gitblit v1.9.1