package cn.besteam.modules.system.util; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; import org.apache.commons.lang3.ObjectUtils; import java.util.ArrayList; import java.util.List; /** * @author xhb 2022-01-07 **/ public class MybatisParameterUtils { public static void cutInParameter(LambdaQueryWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.in(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().in(column, objects); } }); } public static void cutNotInParameter(LambdaQueryWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.notIn(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().notIn(column, objects); } }); } public static void cutInParameter(LambdaQueryChainWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.in(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().in(column, objects); } }); } public static void cutNotInParameter(LambdaQueryChainWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.notIn(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().notIn(column, objects); } }); } public static void cutInParameter(LambdaUpdateWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.in(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().in(column, objects); } }); } public static void cutNotInParameter(LambdaUpdateWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.notIn(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().notIn(column, objects); } }); } public static void cutInParameter(LambdaUpdateChainWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.in(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().in(column, objects); } }); } public static void cutNotInParameter(LambdaUpdateChainWrapper wrapper, SFunction column, List coll) throws Exception { List> newList = splitList(coll, 900); if (CollectionUtils.isEmpty(newList)) { throw new Exception("参数错误"); } else if (newList.size() == 1) { wrapper.notIn(column, newList.get(0)); return; } wrapper.and(i -> { i.in(column, newList.get(0)); newList.remove(0); for (List objects : newList) { i.or().notIn(column, objects); } }); } 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; } }