luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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;
    }
 
}