using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses.Bills;
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 IFcOutboundBillRepository
    {
        Task<BaseResult> Add(FcOutboundBillElastic FcOutboundBill);
        Task<BaseResult> AddManyAsync(IEnumerable<FcOutboundBillElastic> FcOutboundBills);
        Task<BulkResponse> DeleteFcOutboundBills(IEnumerable<FcOutboundBillElastic> FcOutboundBills);
        Task<DeleteByQueryResponse> DeleteFcOutboundBillsByQuery(IEnumerable<long> ids);
        Task<SmartTableResult<FcOutboundBillElastic>> SearchForExcel(BoolQueryDescriptor<FcOutboundBillElastic> query);
        Task<SmartTableResult<FcOutboundBillElastic>> GetByIds(List<long> ids);

    }
    public class FcOutboundBillRepository : IFcOutboundBillRepository
    {
        private readonly ElasticIndex<FcOutboundBillElastic> _dbContext;

        public FcOutboundBillRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcOutboundBillElastic>(settings.Value.ClusterUrl, "fcoutboundbill");
        }

        #region  search

        public async Task<SmartTableResult<FcOutboundBillElastic>> SearchForExcel(BoolQueryDescriptor<FcOutboundBillElastic> query)
        {
            return await _dbContext.Search(query);
        }

        #endregion

        #region Add order

        public async Task<BaseResult> Add(FcOutboundBillElastic FcOutboundBill)
        {
            var result = new BaseResult() { Result = Dto.Results.Result.Success };
            var rs = await _dbContext.Insert(FcOutboundBill);
            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<FcOutboundBillElastic> FcOutboundBills)
        {
            var result = new BaseResult { Result = Dto.Results.Result.Success };
            try
            {
                var rs = await _dbContext.InsertMany(FcOutboundBills);
                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> DeleteFcOutboundBills(IEnumerable<FcOutboundBillElastic> FcOutboundBills)
        {
            return await _dbContext.DeleteMany(FcOutboundBills);
        }

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

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

        #endregion
    }
}
