package com.elab.core.dao.model;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表动态查询对象
 *
 * @author ： liukx
 * @time ： 2020/4/16 - 16:29
 */
public class ListDynamicSearch {
    /**
     * 字段排序
     */
    private OrderBySearch orderBy;
    /**
     * 集合大小
     */
    private Integer limit;
    /**
     * 日期范围查询
     */
    private List<DataRangeSearch> dataRangeSearches;

    /**
     * in查询
     */
    private List<InSearch> inSearches;
    /**
     * like 查询
     */
    private List<LikeSearch> likeSearches;

    public ListDynamicSearch() {
    }


    private ListDynamicSearch(Builder builder) {
        setLikeSearches(builder.likeSearches);
        setOrderBy(builder.orderBy);
        setLimit(builder.limit);
        setDataRangeSearches(builder.dataRangeSearches);
        setInSearches(builder.inSearches);
    }


    public List<LikeSearch> getLikeSearches() {
        return likeSearches;
    }

    public void setLikeSearches(List<LikeSearch> likeSearches) {
        this.likeSearches = likeSearches;
    }

    public OrderBySearch getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(OrderBySearch orderBy) {
        this.orderBy = orderBy;
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public List<DataRangeSearch> getDataRangeSearches() {
        return dataRangeSearches;
    }

    public void setDataRangeSearches(List<DataRangeSearch> dataRangeSearches) {
        this.dataRangeSearches = dataRangeSearches;
    }

    public List<InSearch> getInSearches() {
        return inSearches;
    }

    public void setInSearches(List<InSearch> inSearches) {
        this.inSearches = inSearches;
    }


    public static final class Builder {
        private OrderBySearch orderBy = new OrderBySearch();
        private Integer limit;
        private List<DataRangeSearch> dataRangeSearches = new ArrayList<>();
        private List<InSearch> inSearches = new ArrayList<>();
        private List<LikeSearch> likeSearches = new ArrayList<>();

        private Builder() {
        }


        /**
         * 根据指定字段排序
         *
         * @param field     字段名称
         * @param orderType 字段类型  {@link OrderBySearch}
         * @return
         */
        public Builder orderBy(String field, String orderType) {
            orderBy.setField(field);
            orderBy.setSortType(orderType);
            return this;
        }

        /**
         * 根据某字段排序，默认是DESC
         *
         * @param field 数据库字段名称
         * @return
         */
        public Builder orderBy(String field) {
            orderBy.setField(field);
            return this;
        }

        public Builder limit(Integer val) {
            limit = val;
            return this;
        }

        public Builder dataRangeSearches(String filed, Object startData, Object endData) {
            dataRangeSearches.add(DataRangeSearch.newBuilder().filed(filed).startData(startData).endData(endData)
                    .build());
            return this;
        }

        public Builder addInSearches(String field, List dataList) {
            inSearches.add(InSearch.newBuilder().filed(field).dataList(dataList).build());
            return this;
        }

        public ListDynamicSearch build() {
            return new ListDynamicSearch(this);
        }

        public Builder addLikeSearches(String field, String value) {
            likeSearches.add(LikeSearch.newBuilder().field(field).value(value).build());
            return this;
        }
    }
}
