package com.iailab.framework.mybatis.core.query;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.iailab.framework.common.util.collection.ArrayUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.util.StringUtils;
import java.util.Collection;
/**
* 拓展 MyBatis Plus QueryWrapper 类,主要增加如下功能:
*
* 1. 拼接条件的方法,增加 xxxIfPresent 方法,用于判断值不存在的时候,不要拼接到条件中。
*
* @param 数据类型
*/
public class LambdaQueryWrapperX extends LambdaQueryWrapper {
public LambdaQueryWrapperX likeIfPresent(SFunction column, String val) {
if (StringUtils.hasText(val)) {
return (LambdaQueryWrapperX) super.like(column, val);
}
return this;
}
public LambdaQueryWrapperX inIfPresent(SFunction column, Collection> values) {
if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) {
return (LambdaQueryWrapperX) super.in(column, values);
}
return this;
}
public LambdaQueryWrapperX inIfPresent(SFunction column, Object... values) {
if (ObjectUtil.isAllNotEmpty(values) && !ArrayUtil.isEmpty(values)) {
return (LambdaQueryWrapperX) super.in(column, values);
}
return this;
}
public LambdaQueryWrapperX eqIfPresent(SFunction column, Object val) {
if (ObjectUtil.isNotEmpty(val)) {
return (LambdaQueryWrapperX) super.eq(column, val);
}
return this;
}
public LambdaQueryWrapperX neIfPresent(SFunction column, Object val) {
if (ObjectUtil.isNotEmpty(val)) {
return (LambdaQueryWrapperX) super.ne(column, val);
}
return this;
}
public LambdaQueryWrapperX gtIfPresent(SFunction column, Object val) {
if (val != null) {
return (LambdaQueryWrapperX) super.gt(column, val);
}
return this;
}
public LambdaQueryWrapperX geIfPresent(SFunction column, Object val) {
if (val != null) {
return (LambdaQueryWrapperX) super.ge(column, val);
}
return this;
}
public LambdaQueryWrapperX ltIfPresent(SFunction column, Object val) {
if (val != null) {
return (LambdaQueryWrapperX) super.lt(column, val);
}
return this;
}
public LambdaQueryWrapperX leIfPresent(SFunction column, Object val) {
if (val != null) {
return (LambdaQueryWrapperX) super.le(column, val);
}
return this;
}
public LambdaQueryWrapperX betweenIfPresent(SFunction column, Object val1, Object val2) {
if (val1 != null && val2 != null) {
return (LambdaQueryWrapperX) super.between(column, val1, val2);
}
if (val1 != null) {
return (LambdaQueryWrapperX) ge(column, val1);
}
if (val2 != null) {
return (LambdaQueryWrapperX) le(column, val2);
}
return this;
}
public LambdaQueryWrapperX betweenIfPresent(SFunction column, Object[] values) {
Object val1 = ArrayUtils.get(values, 0);
Object val2 = ArrayUtils.get(values, 1);
return betweenIfPresent(column, val1, val2);
}
// ========== 重写父类方法,方便链式调用 ==========
@Override
public LambdaQueryWrapperX eq(boolean condition, SFunction column, Object val) {
super.eq(condition, column, val);
return this;
}
@Override
public LambdaQueryWrapperX eq(SFunction column, Object val) {
super.eq(column, val);
return this;
}
@Override
public LambdaQueryWrapperX orderByDesc(SFunction column) {
super.orderByDesc(true, column);
return this;
}
@Override
public LambdaQueryWrapperX last(String lastSql) {
super.last(lastSql);
return this;
}
@Override
public LambdaQueryWrapperX in(SFunction column, Collection> coll) {
super.in(column, coll);
return this;
}
}