using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace System.Common
{
    public class BaseRepository<T> : BaseContext, IRepository<T> where T : class, new()
    {
        //public SimpleClient<T> CurrentDb => new SimpleClient<T>(Db);

        /// <summary>
        /// 获取数量
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public async Task<int> GetCount(Expression<Func<T, bool>> whereExpression = null)
        {
            if (whereExpression != null)
                return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync(whereExpression);
            else
                return await Db.Queryable<T>().With(SqlWith.NoLock).CountAsync();
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetList()
        {
            return await Db.Queryable<T>().With(SqlWith.NoLock).ToListAsync();
            //return CurrentDb.GetList();
        }
        public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression)
        {
            return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToListAsync();
        }

        public async Task<List<T>> GetListALL(List<IConditionalModel> conModels, string orderby)
        {
            return await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToListAsync();
        }

        public async Task<List<T>> GetListALL(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, OrderByType ordertype = OrderByType.Asc)
        {
            return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderExpression,ordertype).ToListAsync();
        }

        /// <summary>
        /// 分页获取所有
        /// </summary>
        /// <param name="conModels"></param>
        /// <param name="pagemodel"></param>
        /// <returns></returns>
        public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, MyPageModel pagemodel,string orderby)
        {
            var list= await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
            PageData<T> pd = new PageData<T>();
            pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;// CurrentDb.GetPageList(conModels, pagemodel);
        }

        public async Task<PageData<T>> GetListByPage(List<IConditionalModel> conModels, MyPageModel pagemodel)
        {
            var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(conModels).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
            PageData<T> pd = new PageData<T>();
            pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;// CurrentDb.GetPageList(conModels, pagemodel);
        }

        public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, MyPageModel pagemodel)
        {
            var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
        PageData<T> pd = new PageData<T>();
        pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;// CurrentDb.GetPageList(conModels, pagemodel);
        }

        public async Task<PageData<T>> GetListByPage(Expression<Func<T, bool>> whereExpression, MyPageModel pagemodel, string orderby)
        {
            var list = await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).OrderBy(orderby).ToPageListAsync(pagemodel.PageIndex, pagemodel.PageSize, pagemodel.PageCount);
            PageData<T> pd = new PageData<T>();
            pd.Rows = list;
            pd.Totals = pagemodel.PageCount;
            return pd;// CurrentDb.GetPageList(conModels, pagemodel);
        }

        public async Task<T> GetSingle(Expression<Func<T, bool>> whereExpression)
        {
            return await Db.Queryable<T>().With(SqlWith.NoLock).Where(whereExpression).SingleAsync();
            //return CurrentDb.GetSingle(whereExpression);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> Add(T obj)
        {
            return await Db.Insertable<T>(obj).ExecuteReturnIdentityAsync();
            //return CurrentDb.InsertReturnIdentity(obj);
        }


        /// <summary>
        /// 添加排除某列
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> AddIgnore(T obj, Expression<Func<T, object>> IgnoreColumns)
        {
            return await Db.Insertable<T>(obj).IgnoreColumns(IgnoreColumns).ExecuteReturnIdentityAsync();
            //return CurrentDb.InsertReturnIdentity(obj);
        }

        /// <summary>
        /// 强势插入，插入自增值 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isStrong">是否强势</param>
        /// <returns></returns>
        public async Task<int> AddStrong(T obj,bool isStrong=false)
        {
            return await Db.Insertable<T>(obj).IgnoreColumns(true, isStrong).ExecuteReturnIdentityAsync();
            //return CurrentDb.InsertReturnIdentity(obj);
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="objlist"></param>
        /// <returns></returns>
        public async Task<bool> AddMany(List<T> objlist)
        {
            var res = await Db.Insertable<T>(objlist).ExecuteCommandAsync();
            return res > 0;
        }

        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <returns></returns>
        public async Task<bool> Delete(Expression<Func<T, bool>> whereExpression)
        {
            return await Db.Deleteable<T>(whereExpression).ExecuteCommandHasChangeAsync();
            //return CurrentDb.Delete(whereExpression);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> Update(T obj)
        {
            return await Db.Updateable<T>(obj).ExecuteCommandHasChangeAsync();
            //return CurrentDb.Update(obj);
        }
        /// <summary>
        /// 批量整条更新
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<bool> UpdateList(List<T> obj)
        {
            return await Db.Updateable<T>(obj).ExecuteCommandHasChangeAsync();
            //return CurrentDb.Update(obj);
        }

        /// <summary>
        /// 批量指定字段更新  方法有待考究
        /// </summary>
        /// <param name="objColumns">model => new { model.Name }</param>
        /// <returns></returns>
        [Obsolete]
        public async Task<bool> UpdateListToColumns(List<T> obj, Expression<Func<T, object>> objColumns)
        {
            return await Db.Updateable<T>(obj).UpdateColumns(objColumns).ExecuteCommandHasChangeAsync();
            //return CurrentDb.Update(obj);
        }
    }
}
