using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Nirvana.Data
{
    public interface IRepository<T> where T : class
    {
        #region 单模型 CRUD 操作

        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="entity">实体模型</param>
        /// <returns></returns>
        object Add(T entity);


        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">实体模型</param>
        /// <returns></returns>
        bool Update(T entity);




        /// <summary>
        /// 通过Lamda表达式获取实体
        /// </summary>
        /// <param name="predicate">Lamda表达式（p=>p.Id==Id）</param>
        /// <returns></returns>
        T GetModel(Expression<Func<T, bool>> predicate);


        #endregion

        #region 多模型 操作

        /// <summary>
        /// 增加多条记录，同一模型
        /// </summary>
        /// <param name="TModels">实体模型集合</param>
        /// <returns></returns>
        bool AddList(List<T> TModels);


        /// <summary>
        /// 更新多条记录，同一模型
        /// </summary>
        /// <param name="TModels">实体模型集合</param>
        /// <returns></returns>
        bool UpdateList(List<T> TModels);

        #endregion

        #region 删除操作

        /// <summary>
        /// 通过Lamda表达式，删除一条或多条记录
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        bool Delete(Expression<Func<T, bool>> predicate);

        #endregion

        #region 获取多条数据操作
        /// <summary>
        /// Lamda返回IQueryable集合，延时加载数据
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        List<T> GetListAsync(Expression<Func<T, bool>> predicate);

        #endregion

        #region 验证是否存在

        /// <summary>
        /// 验证当前条件是否存在相同项
        /// </summary>
        bool IsExist(Expression<Func<T, bool>> predicate);

        #endregion

        #region 分页操作

        /// <summary>
        /// 对IQueryable对象进行分页逻辑处理，过滤、查询项、排序对IQueryable操作
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="whereLambda">条件拉姆达表达式</param>
        /// <param name="isAsc">true=正序  false=倒叙</param>
        /// <param name="orderBy">排序拉姆达表达式</param>
        /// <returns></returns>
        PageData<T> GetPageList(int pageIndex, int pageSize,
            Expression<Func<T, bool>> whereLambda, params MutilOrderExpress<T>[] orderBy);

        /// <summary>
        /// 对IQueryable对象进行分页逻辑处理，过滤、查询项、排序对IQueryable操作
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="strwhere">sql拼接条件</param>
        /// <param name="strorder">sql排序自定义</param>
        /// <returns></returns>
        PageData<T> GetPageList(int pageIndex, int pageSize, string strwhere, string strorder);

        #endregion
    }

    public class MutilOrderExpress<T>
    {
        public MutilOrderExpress(Expression<Func<T, object>> expression, OrderByType? orderby)
        {
            this.Expression = expression;
            this.OrderBy = orderby;
        }
        public Expression<Func<T, object>> Expression { get; set; }
        public OrderByType? OrderBy { get; set; }
    }
}
