xiejun
2023-09-21 52ffefd06e59cbd56c1a919972866592379cfed2
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
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
 *
 * @author ludc
 * @date 2023/5/5
 */
public class MybatisParameterUtil {
 
    /**
     * 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;
    }
 
 
}