using Bp.Sync.BackEnd.Infrastructure.ElasticDbContext;
using Bp.Sync.Domain.Elastics.FcBusinesses;
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
{
    public interface IRetailStatisticRepository
    {
        Task<BaseResult> Add(RetailStatisticElastic RetailStatistic);
        Task<BaseResult> AddManyAsync(IEnumerable<RetailStatisticElastic> RetailStatistics);
        Task<BulkResponse> DeleteRetailStatistics(IEnumerable<RetailStatisticElastic> RetailStatistics);
        Task<DeleteByQueryResponse> DeleteByIds(IEnumerable<long> ids);
        Task<SmartTableResult<RetailStatisticElastic>> Search(BoolQueryDescriptor<RetailStatisticElastic> query);
        Task<SmartTableResult<RetailStatisticElastic>> Search(BoolQueryDescriptor<RetailStatisticElastic> query, int count);

    }
    public class RetailStatisticRepository : IRetailStatisticRepository
    {
        private readonly ElasticIndex<RetailStatisticElastic> _dbContext;

        public RetailStatisticRepository(IOptions<ElasticConnectionSettings> settings)
        {
            _dbContext = new ElasticIndex<RetailStatisticElastic>(settings.Value.ClusterUrl, "fcbusinessretailstatistic");
        }

        #region  search

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

        #endregion

        #region Add order

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

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

        #endregion
    }
}
