package com.example.repurchase.utils;

import com.example.repurchase.configuration.ElasticSearchPoolUtil;
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.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;

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

@Component
public class TagsUtils {


    public HashMap<String, Map<String, Object>> getHousePriceTags() throws Exception {

        RestHighLevelClient restHighLevelClient = ElasticSearchPoolUtil.getClient();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("TagTypeId", "8"));
        searchSourceBuilder.size(1000);

        SearchRequest searchRequest = new SearchRequest("mem_tag").source(searchSourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        HashMap<String, Map<String, Object>> tagsHashMap = new HashMap<>();
        for (SearchHit hit : hits) {
            tagsHashMap.put(hit.getSourceAsMap().get("TagId").toString(), hit.getSourceAsMap());
        }

        ElasticSearchPoolUtil.returnClient(restHighLevelClient);
        return tagsHashMap;
    }

    /**
     * 输入房价，返回标签
     *
     * @param housePrice 房价
     * @return
     */
    public String getHouseTag(Integer housePrice) {
        if (housePrice <= 1000) {
            return "8000001";
        } else if (housePrice > 1001 & housePrice <= 2000) {
            return "8000002";
        } else if (housePrice > 2001 & housePrice <= 3000) {
            return "8000003";
        } else if (housePrice > 3101 & housePrice <= 4000) {
            return "8000004";
        } else if (housePrice > 4001 & housePrice <= 5000) {
            return "8000005";
        } else if (housePrice > 5001 & housePrice <= 6000) {
            return "8000006";
        } else if (housePrice > 6001 & housePrice <= 7000) {
            return "8000007";
        } else if (housePrice > 7001 & housePrice <= 8000) {
            return "8000008";
        } else if (housePrice > 8001 & housePrice <= 9000) {
            return "8000009";
        } else if (housePrice > 9001 & housePrice <= 10000) {
            return "8000010";
        } else if (housePrice > 10001 & housePrice <= 12000) {
            return "8000011";
        } else if (housePrice > 12001 & housePrice <= 14000) {
            return "8000012";
        } else if (housePrice > 14001 & housePrice <= 16000) {
            return "8000013";
        } else if (housePrice > 16001 & housePrice <= 18000) {
            return "8000014";
        } else if (housePrice > 18001 & housePrice <= 20000) {
            return "8000015";
        } else if (housePrice > 20001 & housePrice <= 25000) {
            return "8000016";
        } else if (housePrice > 25001 & housePrice <= 30000) {
            return "8000017";
        } else if (housePrice > 30001 & housePrice <= 35000) {
            return "8000018";
        } else if (housePrice > 35001 & housePrice <= 40000) {
            return "8000019";
        } else if (housePrice > 40001 & housePrice <= 50000) {
            return "8000020";
        } else if (housePrice > 50001 & housePrice <= 60000) {
            return "8000021";
        } else if (housePrice > 60001 & housePrice <= 70000) {
            return "8000022";
        } else if (housePrice > 70001 & housePrice <= 80000) {
            return "8000023";
        } else if (housePrice > 80001 & housePrice <= 90000) {
            return "8000024";
        } else if (housePrice > 90001 & housePrice <= 100000) {
            return "8000025";
        } else if (housePrice > 100001 & housePrice <= 150000) {
            return "8000026";
        } else if (housePrice > 150001 & housePrice <= 200000) {
            return "8000026";
        } else {
            return "8000027";
        }
    }

    /**
     * 查询ElasticSearch的mem_tag表获取tagTypeId相应的标签数据
     *
     * @return tagTypeId相应的标签数据
     */
    public HashMap<String, Map<String, Object>> getTagsById(String tagTypeId) throws Exception {

        RestHighLevelClient restHighLevelClient = ElasticSearchPoolUtil.getClient();

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("TagTypeId", tagTypeId)).size(10000);

        SearchRequest searchRequest = new SearchRequest("mem_tag").source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        HashMap<String, Map<String, Object>> tagsHashMap = new HashMap<>();
        for (SearchHit hit : hits) {
            tagsHashMap.put(hit.getSourceAsMap().get("TagName").toString(), hit.getSourceAsMap());
        }

        ElasticSearchPoolUtil.returnClient(restHighLevelClient);
        return tagsHashMap;

    }

}
