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;
|
}
|
|
}
|