using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using XYY.Common.Standard;
namespace XYY.Core.Standard.Data.Infrastructure
{
    public interface IViewRepository<T> where T : class
    {
        [NonTrans]
        Task<T> GetAsync(int id);
        [NonTrans]
        Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression);
        [NonTrans]
        Task<IEnumerable<T>> QueryAsync(string sql, object param = null);
        [NonTrans]
        Task<IEnumerable<T>> QueryBySqlAsync(string sql);
        [NonTrans]
        Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression);
        [NonTrans]
        Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql);
        [NonTrans]
        Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null);
        [NonTrans]
        Task<IEnumerable<T>> QueryAsync();
        [NonTrans]
        Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null);
        [NonTrans]
        Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null);
        [NonTrans]
        Task<int> CountAsync(Expression<Func<T, bool>> expression);
        [NonTrans]
        Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null);
        [NonTrans]
        string GetWhere(List<QueryParamer> paramer);
    }

    public abstract class ViewRepository<T> : IViewRepository<T> where T : class
    {
        //protected readonly IDbConnection _unitOfWork.Connection;
        //protected readonly IDbTransaction _unitOfWork.Transaction;

        protected readonly IUnitOfWork _unitOfWork;

        //public ViewRepository(IUnitOfWork unitOfWork)
        //{
        //    _unitOfWork.Connection = connection;
        //    _unitOfWork.Transaction = transaction;
        //}
        public ViewRepository(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        public async Task<T> GetAsync(int id)
        {
            Ensure.GreaterThan(0, id, "id");
            return await _unitOfWork.GetAsync<T>(id);
        }
        public async Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression)
        {
            Ensure.NotNull(expression, "expression");

            return await _unitOfWork.IsExistsAsync(expression);
        }

        public async Task<IEnumerable<T>> QueryAsync(string sql, object param = null)
        {
            Ensure.NotNullOrEmpty(sql, "sql");
            return await _unitOfWork.Connection.QueryAsync<T>(sql, param, _unitOfWork.Transaction);
        }

        public async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression)
        {
            Ensure.NotNull(expression, "expression");
            return await _unitOfWork.QueryAsync<T>(expression);
        }

        public virtual async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql)
        {
            Ensure.NotNull(expression, "expression");
            if (string.IsNullOrEmpty(customSql))
                return await this.QueryAsync(expression);
            return await _unitOfWork.QueryAsync<T>(expression, customSql);
        }

        public async Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null)
        {
            Ensure.NotNullOrEmpty(sql, "sql");
            return await _unitOfWork.Connection.QueryMultipleAsync(sql, param, _unitOfWork.Transaction);
        }

        public async Task<IEnumerable<T>> QueryAsync()
        {
            return await _unitOfWork.QueryAsync<T>();
        }

        public async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null)
        {
            Ensure.NotNull(queryModel, "queryModel");
            Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
            Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
            return await _unitOfWork.GetPagingListAsync<T>(queryModel, timeout);
        }


        public virtual async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null)
        {
            if (string.IsNullOrEmpty(customSql.Trim()))
                return await QueryPagerAsync(queryModel);
            Ensure.NotNull(queryModel, "queryModel");
            Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
            Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");

            return await _unitOfWork.GetPagingListAsync<T>(queryModel, customSql, timeout);
        }


        public async Task<int> CountAsync(Expression<Func<T, bool>> expression)
        {
            return await _unitOfWork.CountAsync(expression);
        }

        public async Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null)
        {
            return await _unitOfWork.GroupCountAsync<T, TProperty>(propName, expression, model);
        }
        public async Task<IEnumerable<T>> QueryBySqlAsync(string sql)
        {
            return await _unitOfWork.QueryBySqlAsync<T>(sql);
        }
        public string GetWhere(List<QueryParamer> paramer)
        {
            return _unitOfWork.GetWhere(paramer);
        }
    }
}
