ludc
2024-10-17 d02571d59633367ac76b7f58ab38584698b1aa1b
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.vci.ubcs.starter.util;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.vci.ubcs.starter.exception.VciBaseException;
import org.springblade.core.mp.support.SqlKeyword;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * in 参数截取,返回wrapper/或者where条件字符串
 *
 * @author ludc
 * @date 2023/5/5
 */
public class MybatisParameterUtil {
 
    /**
     * Oracle IN 查询的最大参数限制
      */
    private static final int MAX_IN_CLAUSE_SIZE = 1000;
 
    /**
     * 将给定的 ID 列表转换为适合 Oracle 的查询条件
     *
     * @param ids ID 列表
     * @return 适合 Oracle 的查询条件
     */
    public static String convertToOrConditions(String filed,List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return "";
        }
 
        StringBuilder queryBuilder = new StringBuilder();
        int size = ids.size();
 
        // 分批处理,避免超过 Oracle IN 查询的最大限制
        for (int i = 0; i < size; i += MAX_IN_CLAUSE_SIZE) {
            int end = Math.min(i + MAX_IN_CLAUSE_SIZE, size);
            List<String> subList = ids.subList(i, end);
            if (queryBuilder.length() > 0) {
                queryBuilder.append(" OR ");
            }
            queryBuilder.append("(")
                .append(getInClause(filed,subList)).append(")");
        }
 
        return queryBuilder.toString();
    }
 
     /*
     * 获取 IN 查询条件字符串
     * @param ids ID 列表
     * @return IN 查询条件字符串
     */
    private static String getInClause(String filed,List<String> ids) {
        StringBuilder inClauseBuilder = new StringBuilder();
        inClauseBuilder.append(filed + " IN (");
 
        for (int i = 0; i < ids.size(); i++) {
            inClauseBuilder.append("'").append(ids.get(i)).append("'");
            if (i < ids.size() - 1) {
                inClauseBuilder.append(", ");
            }
        }
        inClauseBuilder.append(")");
 
        return inClauseBuilder.toString();
    }
 
    /**
     * 获取 IN 查询条件字符串(不包含in)
     *
     * @param ids ID 列表
     * @return IN 查询条件字符串
     */
    public static String getInClause(List<String> ids) {
        StringBuilder inClauseBuilder = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            inClauseBuilder.append("'").append(ids.get(i)).append("'");
            if (i < ids.size() - 1) {
                inClauseBuilder.append(", ");
            }
        }
        return inClauseBuilder.toString();
    }
 
    /**
     * in作为查询条件时,防止大于1000出现报错,对条件进行截取,连表查询的wrapper
     * @param wrapper MPJLambdaWrapper,不进行连表查询时使用
     * @param column 作为in的条件列
     * @param coll 查询参数
     * @param <T> LambdaQueryWrapper的泛型
     * @param <F> 查询参数类型
     * @return
     * @throws VciBaseException
     */
    public static <T, F> MPJLambdaWrapper<T> cutInParameter(MPJLambdaWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws VciBaseException {
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
            throw new VciBaseException("参数错误");
        } else if (newList.size() == 1) {
            wrapper.in(column, newList.get(0));
            return wrapper;
        }
        wrapper.and(i -> {
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
                i.or().in(column, objects);
            }
        });
        return wrapper;
    }
 
    /**
     * in作为查询条件时,防止大于1000出现报错,对条件进行截取,不具备连表查询的wrapper
     * @param wrapper LambdaQueryWrapper,不进行连表查询时使用
     * @param column 作为in的条件列
     * @param coll 查询参数
     * @param <T> LambdaQueryWrapper的泛型
     * @param <F> 查询参数类型
     * @return
     * @throws VciBaseException
     */
    public static <T, F> LambdaQueryWrapper<T> cutInParameter(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws VciBaseException {
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
            throw new VciBaseException("参数错误");
        } else if (newList.size() == 1) {
            wrapper.in(column, newList.get(0));
            return wrapper;
        }
        wrapper.and(i -> {
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
                i.or().in(column, objects);
            }
        });
        return wrapper;
    }
 
    /**
     * notin作为查询条件时,防止大于1000出现报错,对条件进行截取,连表查询的wrapper
     * @param wrapper MPJLambdaWrapper,不进行连表查询时使用
     * @param column 作为in的条件列
     * @param coll 查询参数
     * @param <T> LambdaQueryWrapper的泛型
     * @param <F> 查询参数类型
     * @return
     * @throws VciBaseException
     */
    public static <T, F> MPJLambdaWrapper<T> cutNotInParameter(MPJLambdaWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws VciBaseException {
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
            throw new VciBaseException("参数错误");
        } else if (newList.size() == 1) {
            wrapper.notIn(column, newList.get(0));
            return wrapper;
        }
        wrapper.and(i -> {
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
                i.or().notIn(column, objects);
            }
        });
        return wrapper;
    }
 
    /**
     * notin作为查询条件时,防止大于1000出现报错,对条件进行截取,不具备连表查询的wrapper
     * @param wrapper LambdaQueryWrapper,不进行连表查询时使用
     * @param column 作为in的条件列
     * @param coll 查询参数
     * @param <T> LambdaQueryWrapper的泛型
     * @param <F> 查询参数类型
     * @return
     * @throws VciBaseException
     */
    public static <T, F> LambdaQueryWrapper<T> cutNotInParameter(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, List<F> coll) throws VciBaseException {
        List<List<F>> newList = splitList(coll, 900);
        if (ObjectUtils.isEmpty(newList)) {
            throw new VciBaseException("参数错误");
        } else if (newList.size() == 1) {
            wrapper.notIn(column, newList.get(0));
            return wrapper;
        }
        wrapper.and(i -> {
            i.in(column, newList.get(0));
            newList.remove(0);
            for (List<F> objects : newList) {
                i.or().notIn(column, objects);
            }
        });
        return wrapper;
    }
 
    public static <F> List<List<F>> splitList(List<F> list, int groupSize) {
        int length = list.size();
        // 计算可以分成多少组
        int num = (length + groupSize - 1) / groupSize;
        List<List<F>> newList = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            // 开始位置
            int fromIndex = i * groupSize;
            // 结束位置
            int toIndex = Math.min((i + 1) * groupSize, length);
            newList.add(list.subList(fromIndex, toIndex));
        }
        return newList;
    }
 
}