using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses.Products;
using Bp.Sync.Dto.Results;
using Bp.Sync.Dto.SmartTable;
using Microsoft.Extensions.Options;
using Nest;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Bp.Sync.BackEnd.Infrastructure.Repositories.FcBusinesses
{
    public interface IFcProductShipmentRepository
    {
        Task<BaseResult> Add(FcProductShipmentElastic FcProductShipment);
        Task<BaseResult> AddManyAsync(IEnumerable<FcProductShipmentElastic> FcProductShipments);
        Task<BulkResponse> DeleteFcProductShipments(IEnumerable<FcProductShipmentElastic> FcProductShipments);
        Task<DeleteByQueryResponse> DeleteFcProductShipmentsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<FcProductShipmentElastic>> SearchForExcel(BoolQueryDescriptor<FcProductShipmentElastic> query);
        Task<SmartTableResult<FcProductShipmentElastic>> GetByIds(List<long> ids);
        Task<SmartTableResult<FcProductShipmentElastic>> Search(BoolQueryDescriptor<FcProductShipmentElastic> query, int count);

    }
    public class FcProductShipmentRepository : IFcProductShipmentRepository
    {
        private readonly ElasticIndex<FcProductShipmentElastic> _dbContext;

        public FcProductShipmentRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcProductShipmentElastic>(settings.Value.ClusterUrl, "fcproductshipment");
        }

        #region  search

        public async Task<SmartTableResult<FcProductShipmentElastic>> SearchForExcel(BoolQueryDescriptor<FcProductShipmentElastic> query)
        {
            return await _dbContext.Search(query);
        }
        public async Task<SmartTableResult<FcProductShipmentElastic>> Search(BoolQueryDescriptor<FcProductShipmentElastic> query, int count)
        {
            return await _dbContext.Search(query, count);
        }
        #endregion

        #region Add order

        public async Task<BaseResult> Add(FcProductShipmentElastic FcProductShipment)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(FcProductShipment);
            if (rs.IsValid)
            {
                result.Result = Dto.Results.Result.Success;
            }
            else
            {
                result.Result = Dto.Results.Result.SystemError;
                result.Message = "Không tạo được bản ghi";
            }
            return result;
        }
        public async Task<BaseResult> AddManyAsync(IEnumerable<FcProductShipmentElastic> FcProductShipments)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(FcProductShipments);
                if (rs.IsValid) return result;
                else
                {
                    result.Result = Dto.Results.Result.Failed;
                    result.Message = rs.Errors.ToString();
                    return result;
                }
            }
            catch (Exception exx)
            {
                result.Result = Dto.Results.Result.Failed;
                result.Message = exx.Message.ToString();
                return result;
            }
        }

        public async Task<BulkResponse> DeleteFcProductShipments(IEnumerable<FcProductShipmentElastic> FcProductShipments)
        {
            return await _dbContext.DeleteMany(FcProductShipments);
        }

        public async Task<DeleteByQueryResponse> DeleteFcProductShipmentsByQuery(IEnumerable<long> ids)
        {
            return await _dbContext.DeleteByIds(ids);
        }

        public async Task<SmartTableResult<FcProductShipmentElastic>> GetByIds(List<long> ids)
        {
            var query = new BoolQueryDescriptor<FcProductShipmentElastic>();
            var idsQ = new QueryContainerDescriptor<FcProductShipmentElastic>();
            idsQ.Terms(t => t.Field(f => f.Id).Terms(ids));
            query.Filter(fl => idsQ);
            return await _dbContext.Search(query);
        }

        #endregion
    }
}
