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 IFcProductOutboundRepository
    {
        Task<BaseResult> Add(FcProductOutboundElastic FcProductOutbound);

        Task<BaseResult> AddManyAsync(IEnumerable<FcProductOutboundElastic> FcProductOutbounds);

        Task<BulkResponse> DeleteFcProductOutbounds(IEnumerable<FcProductOutboundElastic> FcProductOutbounds);

        Task<DeleteByQueryResponse> DeleteFcProductOutboundsByQuery(IEnumerable<long> ids);

        Task<SmartTableResult<FcProductOutboundElastic>> SearchForExcel(BoolQueryDescriptor<FcProductOutboundElastic> query);
    }

    public class FcProductOutboundRepository : IFcProductOutboundRepository
    {
        private readonly ElasticIndex<FcProductOutboundElastic> _dbContext;

        public FcProductOutboundRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcProductOutboundElastic>(settings.Value.ClusterUrl, "fcbusinessproductoutbound");
        }

        #region search

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

        #endregion search

        #region Add order

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

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

        #endregion Add order
    }
}