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 IFcProductInboundRepository
    {
        Task<BaseResult> Add(FcProductInboundElastic FcProductInbound);

        Task<BaseResult> AddManyAsync(IEnumerable<FcProductInboundElastic> FcProductInbounds);

        Task<BulkResponse> DeleteFcProductInbounds(IEnumerable<FcProductInboundElastic> FcProductInbounds);

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

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

    public class FcProductInboundRepository : IFcProductInboundRepository
    {
        private readonly ElasticIndex<FcProductInboundElastic> _dbContext;

        public FcProductInboundRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<FcProductInboundElastic>(settings.Value.ClusterUrl, "fcbusinessproductinbound");
        }

        #region search

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

        #endregion search

        #region Add order

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

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

        #endregion Add order
    }
}