package com.liangjian.dataplatform.member.util;

import com.liangjian.dataplatform.member.entity.po.secondary.FiltersUtils;
import com.liangjian.dataplatform.member.jobUtil.EsUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class QueryBuilderUtil {


    /**
     *
     * @param index  索引
     * @param type  类型
     * @param filrer   获取数据条件
     * @return
     */
    public static SearchHit[] search(String index, String type,int pageIndex, int pageSize,List<FiltersUtils> filrer)  {

        SearchHit[] searchHits = null;
        try {
            RestHighLevelClient client = EsUtils.getEsClient();
            SearchSourceBuilder sourceBuilder = setParams(filrer);


            sourceBuilder.from(pageIndex); //设置确定结果要从哪个索引开始搜索的from选项，默认为0
            if(pageSize == 0){
                sourceBuilder.size(15); //设置确定搜素命中返回数的size选项，默认为10
            }else{
                sourceBuilder.size(pageSize); //设置确定搜素命中返回数的size选项，默认为10
            }
            sourceBuilder.timeout(new TimeValue(5, TimeUnit.SECONDS)); //设置一个可选的超时，控制允许搜索的时间。

            sourceBuilder.fetchSource(); //第一个是获取字段，第二个是过滤的字段，默认获取全部
            SearchRequest searchRequest = new SearchRequest(index); //索引
//            searchRequest.types(type); //类型
            searchRequest.source(sourceBuilder);

            SearchResponse response = null;
            response = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = response.getHits();  //SearchHits提供有关所有匹配的全局信息，例如总命中数或最高分数：
            searchHits = hits.getHits();
        }catch (Exception e){
            e.printStackTrace();
        }
        return searchHits;
    }


    public static SearchSourceBuilder setParams(List<FiltersUtils> filrer){
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        for (FiltersUtils filtersUtils : filrer) {
            String operate = filtersUtils.getOperate();
            switch (operate) {
                case "equal":
                    sourceBuilder.query(equalsQuery(filtersUtils));
                    break;
                case "notequal":
                    sourceBuilder.query(notQuery(filtersUtils));
                    break;
                case "less":
                    break;
                case "lessorequal":
                    break;
                case "greater":
                    break;
                case "greateroreeuqal":
                    break;
                case "contains":
                    sourceBuilder.query(wildcardQuery(filtersUtils));
                    break;
                case "startswith":
                    break;
                case "endswith":
                    break;
                case "or":
                    break;
                case "nestand":
                    break;
                case "group":
                    String fieId = filtersUtils.getField();
                    TermsAggregationBuilder field = AggregationBuilders.terms(fieId).field(fieId);
                    sourceBuilder.aggregation(field);
                    break;
            }
        }
        return sourceBuilder;
    }

    // 等于条件
    public static QueryBuilder equalsQuery(FiltersUtils Filters) {
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        String fieId = Filters.getField();
        String value = Filters.getValue();
        boolBuilder.must(QueryBuilders.termQuery(fieId, value));
        return boolBuilder;
    }

    // 条件不等于
    public static QueryBuilder notQuery(FiltersUtils Filters) {
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        String fieId = Filters.getField();
        String value = Filters.getValue();
        boolBuilder.mustNot(QueryBuilders.termQuery(fieId, value));
        return boolBuilder;
    }

    /**
     * 模糊查询
     * @param
     * @return
     */
    public static QueryBuilder wildcardQuery(FiltersUtils Filters){
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        String fieId = Filters.getField();
        String value = Filters.getValue();
        boolBuilder.must(QueryBuilders.wildcardQuery(fieId, value));
        return boolBuilder;
    }


}
