using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Data.Model.Standard.Tracking;
namespace XYY.Service.Standard.TrackingService
{

    public class TrackingES : IDisposable
    {
        private readonly string Url = "";

        private ElasticClient Client;
        private ElasticClient _supplementClient;

        public TrackingES(string url)
        {
            this.Url = url;
        }



        public ElasticClient GetElasticClient()
        {
            if (Client == null)
            {
                string esNodeUrl = this.Url;
                string indexName = "logistics_tracking";
                Uri node = new Uri(esNodeUrl);
                ConnectionSettings settings = new ConnectionSettings(node).DefaultMappingFor<Logistics_TraceGroup>(x =>
                x.IdProperty(i => i.TrackingNumber).IndexName(indexName).TypeName("trackingGroup"));
                //600s超时
                settings.RequestTimeout(new TimeSpan(0, 0, 600));

                IIndexState indexState = new IndexState()
                {
                    Settings = new IndexSettings()
                    {
                        NumberOfReplicas = 0,//副本数
                        NumberOfShards = 1//分片数
                    }
                };

                this.Client = new ElasticClient(settings);

                if (!Client.IndexExists(indexName).Exists)
                {
                    Client.CreateIndex(indexName, p => p.InitializeUsing(indexState));
                }
            }
            return Client;
        }
        /// <summary>
        /// 补充轨迹的节点
        /// </summary>
        /// <returns></returns>
        public ElasticClient GetSupplementElasticClient()
        {
            if (_supplementClient == null)
            {
                string esNodeUrl = this.Url;
                string indexName = "logistics_supplement";
                Uri node = new Uri(esNodeUrl);
                ConnectionSettings settings = new ConnectionSettings(node).DefaultMappingFor<Logistics_TraceGroup>(x =>
                x.IdProperty(i => i.TrackingNumber).IndexName(indexName).TypeName("supplement"));
                //600s超时
                settings.RequestTimeout(new TimeSpan(0, 0, 600));

                IIndexState indexState = new IndexState()
                {
                    Settings = new IndexSettings()
                    {
                        NumberOfReplicas = 0,//副本数
                        NumberOfShards = 1//分片数
                    }
                };

                this._supplementClient = new ElasticClient(settings);

                if (!_supplementClient.IndexExists(indexName).Exists)
                {
                    _supplementClient.CreateIndex(indexName, p => p.InitializeUsing(indexState));
                }
            }
            return _supplementClient;
        }



        public void Dispose()
        {

        }
    }
}
