package com.bawei.mall.search.es.instance;

import com.bawei.common.core.exception.ServiceException;
import com.bawei.common.core.utils.StringUtils;
import com.bawei.mall.search.es.config.EsAddressConfig;
import com.bawei.mall.search.es.config.EsConfig;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author DongZl
 * @description: Es实例化对象
 * @Date 2022-10-14 上午 11:26
 */
@Component
public class EsInstance {
    private static final Logger log = LoggerFactory.getLogger(EsInstance.class);

    /**
     * ES实例对象
     */
    private RestHighLevelClient client = null;

    private EsConfig esConfig;

    public EsInstance (EsConfig esConfig) {
        this.esConfig = esConfig;
    }

    @PostConstruct
    public void init(){
        long startTime = System.currentTimeMillis();
        try {
            log.info("Es初始开始");
            List<EsAddressConfig> addressList = this.esConfig.getAddressList();
            int hostSize = addressList.size();
            HttpHost[] httpHosts = new HttpHost[hostSize];
            for (int index = 0 ; index  < hostSize ; index ++) {
                EsAddressConfig esAddressConfig = addressList.get(index);
                log.info("实例化Es客户端程序地址：[{}],端口：[{}]",
                        esAddressConfig.getHost(),
                        esAddressConfig.getPort());
                httpHosts[index] = new HttpHost(esAddressConfig.getHost(),
                        esAddressConfig.getPort(),
                        "http");
            }
            client = new RestHighLevelClient(
                    RestClient.builder(httpHosts));
            log.info("Es初始化成功 耗时：[{}MS]", System.currentTimeMillis() - startTime);
        }catch (Exception e){
            log.error("Es初始化失败", e);
            // 阻止程序启动
            throw e;
        }
    }

    public RestHighLevelClient getClient () {
        return client;
    }

    /**
     * 获取索引操作对象
     * @param index
     * @return 索引操作对象
     */
    public IndexRequest indexRequestBuilder(String index){
        if (StringUtils.isEmpty(index)){
            throw new ServiceException("Es 索引不可以为空");
        }
        return new IndexRequest(index);
    }

    /**
     * 获取索引操作对象
     * @param index
     * @return
     */
    public DeleteRequest DeleteRequestBuilder(String index){
        if (StringUtils.isEmpty(index)){
            throw new ServiceException("Es 索引不可以为空");
        }
        return new DeleteRequest(index);
    }

    /**
     * 索引搜索对象
     * @param index
     * @return
     */
    public SearchRequest SearchRequestBuilder(String index){
        if (StringUtils.isEmpty(index)){
            throw new ServiceException("Es 索引不可以为空");
        }
        return new SearchRequest(index);
    }

}
