using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using XYY.Common.Standard;

namespace System
{
    /// <summary>
    /// Dto类与Model类互转
    /// </summary>
    public static class ObjectMapper
    {
        /// <summary>
        /// Dto分页结果与Model分页结果互转
        /// </summary>
        /// <typeparam name="T">Model</typeparam>
        /// <typeparam name="D">Dto</typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static PageResult<D> MapperPager<T, D>(this PageResult<T> list)
            where D : class
            where T :class
        {
            PageResult<D> pageResult = new PageResult<D>
            {
                PageIndex = list.PageIndex,
                PageSize = list.PageSize,
                TotalCount = list.TotalCount
            };
            List<D> data = new List<D>();
            foreach (var item in list.Result)
            {
                data.Add(item.Copy<D>());
            }
            pageResult.Result = data;
            return pageResult;
        }

        /// <summary>
        /// Dto分页结果与Model分页结果互转
        /// </summary>
        /// <typeparam name="T">Model</typeparam>
        /// <typeparam name="D">Dto</typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static PageResult<D> MapperObjectPager<T, D>(PageResult<T> list)
            where D : class
            where T : class
        {
            PageResult<D> pageResult = new PageResult<D>
            {
                PageIndex = list.PageIndex,
                PageSize = list.PageSize,
                TotalCount = list.TotalCount
            };
            List<D> data = new List<D>();
            foreach (var item in list.Result)
            {
                data.Add(item.Copy<D>());
            }
            pageResult.Result = data;
            return pageResult;
        }
    }


}
