package com.example.aliossjava.common;


import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author shiming.zhao
 * @date 2019/04/18
 */
@Service
public class CompanyNameUtils {

    public Map<String,Object> nameList(String region, String type, String modality, String nameNum, String goodName, Integer from, Integer size) throws Exception {
        Map<String, Object> stringObjectMap = querycompanyNanme(goodName, nameNum, from, size);
        List<String> querycompanyNanme = (List<String>) stringObjectMap.get("companyName");
        List<String> nameList = new ArrayList<>();
        for (String str : querycompanyNanme){
            JSONObject responseObj = JSONObject.parseObject(str);
            nameList.add(region + responseObj.getString("company_name") + type + modality);
        }
        stringObjectMap.remove("companyName");
        stringObjectMap.put("companyName",nameList);
        return stringObjectMap;
    }

    /**
     * 根据条件查询公司名称
     *
     * @return
     */
    public static Map<String,Object> querycompanyNanme(String fieldKey, String fieldnum, Integer from, Integer size) throws Exception {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "w3OL+51eo*)c=^7"));
        RestClientBuilder client = RestClient.builder(new HttpHost("es-cn-zz11nl9y20001gczg.public.elasticsearch.aliyuncs.com", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });
        RestHighLevelClient restClient = new RestHighLevelClient(client);
        SearchRequest searchRequest = new SearchRequest("treasure_name");
        searchRequest.types("_doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        if (fieldnum.equals("不限") && fieldKey.equals("")) {
            searchSourceBuilder.query(QueryBuilders.boolQuery());
        } else if (fieldKey.equals("")) {
            searchSourceBuilder.query(QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("name_size", Integer.valueOf(fieldnum))));
        } else if (fieldnum.equals("不限")) {
            searchSourceBuilder.query(QueryBuilders.boolQuery().
                    must(QueryBuilders.termsQuery("company_name", fieldKey)));
        } else {
            searchSourceBuilder.query(QueryBuilders.boolQuery().
                    must(QueryBuilders.termsQuery("company_name", fieldKey))
                    .must(QueryBuilders.termQuery("name_size", Integer.valueOf(fieldnum))));
        }
        searchSourceBuilder.from(from);
        searchSourceBuilder.size(size);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restClient.search(searchRequest, RequestOptions.DEFAULT);
        List<String> result = new LinkedList<>();
        SearchHits searchHits=searchResponse.getHits();
        HashMap<String, Object> hashMap = new HashMap<>();
        searchHits.forEach(i->{
            result.add(i.getSourceAsString());
        });
        hashMap.put("companyName",result);
        hashMap.put("count",searchHits.getTotalHits().value);
        return hashMap;
    }

}
