using System;
using System.Collections.Generic;
using DapperORM.Common.Enums;

namespace DapperORM.Common.Models
{
    /// <summary>
    /// 分页实体
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public class PageListModel<TModel> : ResultModel where TModel : class, new()
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PageListModel()
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="result">操作结果，1=success，0=failure</param>
        /// <param name="msg">返回信息</param>
        public PageListModel(int result, string msg = "") : base(result, msg)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="result">操作结果</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalReadCount">记录总条数</param>
        /// <param name="data">实体列表</param>
        /// <param name="msg">返回信息</param>
        public PageListModel(int result, int currentPage, int pageSize, int totalReadCount, List<TModel> data, string msg = "")
        {
            this.Result = result;
            this.CurrentPage = currentPage;
            this.PageSize = pageSize;
            this.TotalReadCount = totalReadCount;
            this.Data = data;
            this.Msg = msg;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="result">操作结果</param>
        /// <param name="msg">返回信息</param>
        public PageListModel(ResultType result, string msg = "") : base(result, msg)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="result">操作结果</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalReadCount">记录总条数</param>
        /// <param name="data">实体列表</param>
        /// <param name="msg">返回信息</param>
        public PageListModel(ResultType result, int currentPage, int pageSize, int totalReadCount, List<TModel> data, string msg = "")
        {
            this.Result = (int)result;
            this.CurrentPage = currentPage;
            this.PageSize = pageSize;
            this.TotalReadCount = totalReadCount;
            this.Data = data;
            this.Msg = msg;
        }

        /// <summary>
        /// 实体列表
        /// </summary>
        public List<TModel> Data { get; set; }

        /// <summary>
        /// 记录总条数
        /// </summary>
        public int TotalReadCount { get; set; }

        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        private int _TotalPageCount;

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount
        {
            get
            {
                if (_TotalPageCount != 0)
                    return _TotalPageCount;
                else
                {
                    if (TotalReadCount == 0 || PageSize == 0)
                        return 0;

                    _TotalPageCount = (int)Math.Floor((double)TotalReadCount / (double)PageSize);
                    return _TotalPageCount;
                }
            }
        }
    }

    /// <summary>
    /// 分页实体扩展
    /// </summary>
    public static class PageListModelMap
    {
        /// <summary>
        /// 转化pageList列表实体
        /// </summary>
        /// <typeparam name="TSource">源类型</typeparam>
        /// <typeparam name="TResult">结果</typeparam>
        /// <param name="pageList">实体列表</param>
        /// <param name="convertFun">转换方法</param>
        /// <returns></returns>
        public static PageListModel<TResult> ConvertToPageList<TSource, TResult>(this PageListModel<TSource> pageList,
            Func<TSource, TResult> convertFun)
            where TResult : class, new()
            where TSource : class, new()
        {
            if (convertFun == null)
            {
                throw new ArgumentNullException(nameof(convertFun), "转化方法不能为空！");
            }

            List<TResult> resultList = null;
            if (pageList.Data != null)
            {
                resultList = pageList.Data.ConvertAll(e => convertFun(e));
            }
            return new PageListModel<TResult>(pageList.Result, pageList.CurrentPage, pageList.PageSize, pageList.TotalReadCount, resultList, pageList.Msg);
        }
    }
}
