All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zys.mybatis.crud.AbstractQuery Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package com.zys.mybatis.crud;

import com.zys.mybatis.condition.ConditionKey;
import com.zys.mybatis.constant.CharFinal;
import com.zys.mybatis.crud.base.Condition;
import com.zys.mybatis.dao.base.BaseDao;
import com.zys.mybatis.exception.MybatisZysNotFountException;
import com.zys.mybatis.utils.ArrayUtils;
import com.zys.mybatis.utils.FieldUtils;
import com.zys.mybatis.utils.LambdaUtils;
import com.zys.mybatis.utils.SpringUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zys
 * @version 1.0
 * @date 2021/1/21 17:28
 */
public abstract class AbstractQuery {

    /**
     * 表名
     */
    protected String table;

    /**
     * 实体类
     */
    protected Class entity;

    /**
     * 子类
     */
    protected final R q = (R) this;

    /**
     * 参数
     */
    private Map params;


    public Class getEntity(){
        return entity;
    }

    public Map getParams(){
        if (params == null){
            return params = new LinkedHashMap<>();
        }
        return params;
    }

    public R setParams(Map params){
        this.params = params;
        return q;
    }

    public String getTable(){
        return table;
    }

    /**
     * 类型转换
     * @param column 字段
     * @param condition 条件
     * @param isArray 是否是数组
     * @param isPut 是非判null
     * @param values 值
     * @return 条件构造器
     */
    protected R converter(String column, String condition, boolean isArray, boolean isPut, Object... values) {
        if (isPut) {
            if (isArray) {
                addCondition(column, condition, values);
            } else {
                addCondition(column, condition, values[0]);
            }
        }
        return q;
    }

    /**
     * 构造器
     * @param column 字段
     * @param isException 是否抛出异常
     * @param isPut 是否参加条件判断
     * @param query 查询构造器
     * @return 当前对象
     */
    protected R converter(String column, boolean isException, boolean isPut, Query query) {
        if (isPut) {
            BaseDao baseDao = SpringUtil.getPollBean(BaseDao.class);
            List list = baseDao.queryAll(query);
            if (CollectionUtils.isEmpty(list)) {
                if (isException) {
                    throw new MybatisZysNotFountException(query.getPage(), "未查询到数据");
                }
                return q;
            }
            Object[] values = list.stream().map(e -> {
                MetaObject metaObject = SystemMetaObject.forObject(e);
                return metaObject.getValue((String) query.getField());
            }).toArray();
            addCondition(column, CharFinal.IN, values);
        }
        return q;
    }

    /**
     * 添加条件
     * @param column 字段
     * @param condition 条件
     * @param value 值
     */
    private void addCondition(String column,String condition,Object value){
        getParams().put(new ConditionKey(column,condition),appendValue(condition,value));
    }

    /**
     * 追加值
     * @param condition 条件
     * @param value 值
     * @return 条件构造器
     */
    private Object appendValue(String condition,Object value){
        if (CharFinal.LIKE.equals(condition)) {
            return CharFinal.DIM + value + CharFinal.DIM;
        }
        return value;
    }

    public R like(String column, Object object){
        return like(column, object, ArrayUtils.isNotEmpty(object));
    }

    @SuppressWarnings("unchecked")
    public R like(SFunction column, Object object) {
        return like(column, object, true);
    }

    /**
     * @param column 字段名称
     * @param object value
     * @param isPut 是否put null也要put
     */
    public R like(String column, Object object, boolean isPut){
        return converter(column, CharFinal.LIKE, false, isPut, object);
    }

    /**
     * @param column 字段名称
     * @param object value
     * @param isPut 是否put null也要put
     */
    @SuppressWarnings("unchecked")
    public R like(SFunction column, Object object, boolean isPut) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.LIKE, false, true, object);
    }

    public R eq(String column, Object object){
        return eq(column, object, ArrayUtils.isNotEmpty(object));
    }

    /**
     * = 条件查询
     * @param column 字段
     * @param object 值
     * @param isPut 是否put null也要put
     */
    public R eq(String column, Object object, boolean isPut){
        return converter(column, CharFinal.EQ, false, isPut, object);
    }

    @SuppressWarnings("unchecked")
    public R eq(SFunction column, Object object) {
        return eq(column, object, true);
    }

    /**
     * @param column Lambda 字段
     * @param object 值
     * @param isPut 是否put
     */
    @SuppressWarnings("unchecked")
    public R eq(SFunction column, Object object, boolean isPut) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.EQ, false, true, object);
    }

    public R complex(SFunction column, Condition query) {
        return complex(true, true, column, query);
    }

    public R complex(SFunction column, boolean isException, Condition query) {
        return complex(true, isException, column, query);
    }

    public R complex(boolean isPut, SFunction column, Condition query) {
        return complex(isPut, true, column, query);
    }

    /**
     * column in (x, x, x...)
     * @param isPut 是否生效
     * @param isException 未找到数据是否抛出异常
     * @param column 字段
     * @param query 条件
     */
    public R complex(boolean isPut, boolean isException, SFunction column, Condition query) {
        if (query == null) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)),
                isException, isPut, query.getQuery().setParams(query.getParams()));
    }

    public R notIn(String column, Object... object){
        return notIn(ArrayUtils.isNotEmpty(object), column, object);
    }

    /**
     * @param isPut 是否put
     * @param column 字段
     * @param object 值
     */
    public R notIn(boolean isPut, String column, Object... object){
        return converter(column, CharFinal.NOT_IN, true, isPut, object);
    }

    @SuppressWarnings("unchecked")
    public R notIn(SFunction column, Object... object) {
        return notIn(true, column, object);
    }

    public R notIn(boolean isPut, SFunction column, Object... object) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.NOT_IN, true, true, object);
    }

    /**
     * in条件查询
     * @param column 字段名称
     * @param objects value
     * @return 当前对象
     */
    public R in(String column, Object... objects){
        return in(ArrayUtils.isNotEmpty(objects), column, objects);
    }

    public R in(boolean isPut, String column, Object... objects){
        return converter(column, CharFinal.IN, true, isPut, objects);
    }

    @SuppressWarnings("unchecked")
    public R in(boolean isPut, SFunction column, Object... object) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.IN, true, true, object);
    }

    @SuppressWarnings("unchecked")
    public R in(SFunction column, Object... object) {
        return in(true, column, object);
    }

    /**
     * gt 大于
     * @param column 字段
     * @param object 值
     * @return 条件构造器
     */
    public R gt(String column, Object object){
        return gt(column, object, ArrayUtils.isNotEmpty(object));
    }

    public R gt(String column, Object object, boolean isPut){
        return converter(column, CharFinal.GT, false, isPut, object);
    }

    @SuppressWarnings("unchecked")
    public R gt(SFunction column, Object object) {
        return gt(column, object, true);
    }

    public R gt(SFunction column, Object object, boolean isPut) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.GT, false, true, object);
    }

    /**
     * gt 小于
     * @param column 字段
     * @param object 值
     * @return 条件构造器
     */
    public R lt(String column, Object object){
        return lt(column, object, ArrayUtils.isNotEmpty(object));
    }

    public R lt(String column, Object object, boolean isPut){
        return converter(column, CharFinal.LT, false, isPut, object);
    }

    @SuppressWarnings("unchecked")
    public R lt(SFunction column, Object object) {
        return lt(column, object, true);
    }

    public R lt(SFunction column, Object object, boolean isPut) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.LT, false, true, object);
    }


    public R or(String column, Object... objects) {
        return or(ArrayUtils.isNotEmpty(objects), column, objects);
    }

    /**
     * 或者
     * @param isPut 是否put
     * @param column 字段
     * @param objects 值
     * @return 条件构造器
     */
    public R or(boolean isPut, String column, Object... objects) {
        return converter(column, CharFinal.OR, true, ArrayUtils.isNotEmpty(objects), objects);
    }

    @SuppressWarnings("unchecked")
    public R or(SFunction column, Object... object) {
        return or(true, column, object);
    }

    public R or(boolean isPut, SFunction column, Object... object) {
        if (!isPut) return q;
        return converter(FieldUtils.getColumn(LambdaUtils.resolve(column)), CharFinal.OR, true, true, object);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy