package com.keyware.shandan.dynacmicform.core.basic.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * 查询参数构建器
 */
public class QueryParamBuilder {

    private final QueryParam queryParam = new QueryParam();
    private final Map<String, ParamBuilder> builderMap = new HashMap<>();

    /**
     * 填充一个字段，并生成一个参数构建器
     *
     * @param column 字段名
     * @return 参数构建器
     */
    public ParamBuilder column(String column) {
        ParamBuilder paramBuilder = new ParamBuilder(this, column);
        builderMap.put(column, paramBuilder);
        return paramBuilder;
    }

    /**
     * 开始构建参数，并返回参数对象
     *
     * @return 参数对象
     */
    public QueryParam build() {
        if (builderMap.size() > 0) {
            for (Map.Entry<String, ParamBuilder> entry : builderMap.entrySet()) {
                queryParam.put(entry.getKey(), entry.getValue().build());
            }
        }
        return queryParam;
    }

    /**
     * 参数构建器
     */
    public static class ParamBuilder {
        private final QueryParam.Param param = new QueryParam.Param();
        private final QueryParamBuilder parent;

        public ParamBuilder(QueryParamBuilder parent, String column) {
            this.parent = parent;
            param.setColumn(column);
        }

        /**
         * 设置判断逻辑符号，并返回当前参数构建器
         *
         * @param logic 逻辑符号
         * @return 返回当前参数构建器
         */
        public ParamBuilder logic(String logic) {
            param.setLogic(QueryParam.LogicSymbol.valueOf(logic));
            return this;
        }

        public ParamBuilder logic(QueryParam.LogicSymbol logic){
            param.setLogic(logic);
            return this;
        }

        /**
         * 设置参数值，并返回父级构建器
         *
         * @param value 值
         * @return 返回父级构建器
         */
        public QueryParamBuilder value(Object value) {
            param.setValue(value);
            return parent;
        }

        public ParamBuilder eq(){
            param.setLogic(QueryParam.LogicSymbol.eq);
            return this;
        }
        public QueryParamBuilder eq(Object object){
            param.setLogic(QueryParam.LogicSymbol.eq);
            return value(object);
        }

        public ParamBuilder ne(){
            param.setLogic(QueryParam.LogicSymbol.ne);
            return this;
        }

        public QueryParamBuilder ne(Object object){
            param.setLogic(QueryParam.LogicSymbol.ne);
            return value(object);
        }

        public ParamBuilder lt(){
            param.setLogic(QueryParam.LogicSymbol.lt);
            return this;
        }

        public QueryParamBuilder lt(Object object){
            param.setLogic(QueryParam.LogicSymbol.lt);
            return value(object);
        }

        public ParamBuilder gt(){
            param.setLogic(QueryParam.LogicSymbol.gt);
            return this;
        }

        public QueryParamBuilder gt(Object object){
            param.setLogic(QueryParam.LogicSymbol.gt);
            return value(object);
        }

        public ParamBuilder le(){
            param.setLogic(QueryParam.LogicSymbol.le);
            return this;
        }

        public QueryParamBuilder le(Object object){
            param.setLogic(QueryParam.LogicSymbol.le);
            return value(object);
        }

        public ParamBuilder ge(){
            param.setLogic(QueryParam.LogicSymbol.ge);
            return this;
        }

        public QueryParamBuilder ge(Object object){
            param.setLogic(QueryParam.LogicSymbol.ge);
            return value(object);
        }

        public ParamBuilder like(){
            param.setLogic(QueryParam.LogicSymbol.ge);
            return this;
        }

        public QueryParamBuilder like(Object object){
            param.setLogic(QueryParam.LogicSymbol.like);
            return value(object);
        }

        public ParamBuilder likeLeft(){
            param.setLogic(QueryParam.LogicSymbol.likeLeft);
            return this;
        }

        public QueryParamBuilder likeLeft(Object object){
            param.setLogic(QueryParam.LogicSymbol.likeLeft);
            return value(object);
        }

        public ParamBuilder likeRight(){
            param.setLogic(QueryParam.LogicSymbol.likeRight);
            return this;
        }

        public QueryParamBuilder likeRight(Object object){
            param.setLogic(QueryParam.LogicSymbol.likeRight);
            return value(object);
        }

        public ParamBuilder in(){
            param.setLogic(QueryParam.LogicSymbol.in);
            return this;
        }

        public QueryParamBuilder in(Object object){
            param.setLogic(QueryParam.LogicSymbol.in);
            return value(object);
        }

        public ParamBuilder notIn(){
            param.setLogic(QueryParam.LogicSymbol.notIn);
            return this;
        }

        public QueryParamBuilder notIn(Object object){
            param.setLogic(QueryParam.LogicSymbol.notIn);
            return value(object);
        }

        /**
         * 开始构建参数并将其返回
         *
         * @return 构建后的参数
         */
        public QueryParam.Param build() {
            return param;
        }
    }
}
