package com.liangjian.dataplatform.member.entity.vo.request;

import com.liangjian.dataplatform.member.entity.pojo.BirdGirdFilter;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/10/23
 */
public class BirdGirdRequest {
    private List<BirdGirdFilter> filters;
    private List<String> sumFields;
    private Integer pageIndex;
    private Integer pageSize;
    private Integer sortDirection;
    private String sortField;

    public List<BirdGirdFilter> getFilters() {
        return filters;
    }

    public void setFilters(List<BirdGirdFilter> filters) {
        this.filters = filters;
    }

    public List<String> getSumFields() {
        return sumFields;
    }

    public void setSumFields(List<String> sumFields) {
        this.sumFields = sumFields;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getSortDirection() {
        return sortDirection;
    }

    public void setSortDirection(Integer sortDirection) {
        this.sortDirection = sortDirection;
    }

    public String getSortField() {
        return sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String filtersToSql() {
        return filters.stream()
                .map(i -> i.toSql())
                .reduce((i, j) -> i + j).orElse("");
    }

    public BoolQueryBuilder filtersToEsQuery() {
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        filters.forEach(i -> {
            switch (i.getOperate()) {
                case "equal":
                    qb.must(
                            QueryBuilders.termQuery(i.getField(), i.getValue()));
                    break;
                case "notequal":
                    qb.mustNot(
                            QueryBuilders.termQuery(i.getField(), i.getValue()));
                    break;
                case "less":
                    qb.must(
                            QueryBuilders.rangeQuery(i.getField()).lt(i.getValue()));
                    break;
                case "lessorequal":
                    qb.must(
                            QueryBuilders.rangeQuery(i.getField()).lte(i.getValue()));
                    break;
                case "greater":
                    qb.must(
                            QueryBuilders.rangeQuery(i.getField()).gt(i.getValue()));
                    break;
                case "greateroreeuqal":
                    qb.must(
                            QueryBuilders.rangeQuery(i.getField()).gte(i.getValue()));
                    break;
                case "contains":
                    qb.must(
                            QueryBuilders.matchPhraseQuery(i.getField(), i.getValue()));
                    break;
                case "startswith":
                    qb.must(
                            QueryBuilders.matchPhrasePrefixQuery(i.getField(), i.getValue()));
                    break;
                case "endswith":
                    qb.must(
                            QueryBuilders.matchPhraseQuery(i.getField(), i.getValue()));
                    break;
                default:
                    throw new RuntimeException("传递参数比较无法匹配");
            }
        });
        return qb;
    }
}
