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.Wrappers; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.vci.ubcs.starter.exception.VciBaseException; import org.springframework.util.ObjectUtils; import java.util.ArrayList; import java.util.List; /** * in 参数截取,返回wrapper * * @author ludc * @date 2023/5/5 */ public class MybatisParameterUtils { public static LambdaQueryWrapper cutInParameter(LambdaQueryWrapper wrapper, SFunction column, List coll) throws VciBaseException { List> 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 objects : newList) { i.or().in(column, objects); } }); return wrapper; } public static LambdaQueryWrapper cutNotInParameter(LambdaQueryWrapper wrapper, SFunction column, List coll) throws VciBaseException { List> 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 objects : newList) { i.or().notIn(column, objects); } }); return wrapper; } public static List> splitList(List list, int groupSize) { int length = list.size(); // 计算可以分成多少组 int num = (length + groupSize - 1) / groupSize; List> 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; } }