using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Edu.Common
{
    /// <summary>
    /// 类型转换帮助类
    /// </summary>
    public static class ConvertHelper
    {
        /// <summary>
        /// 格式化时间【yyyy-MM-dd HH:mm:ss】
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatTime(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                }
                catch
                { 
                
                }
            }
            return timeStr;
        }

        /// <summary>
        /// 格式化时间【yyyy-MM-dd】
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatDate(object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy-MM-dd");
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 字符串转List
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<int> StringToList(object value)
        {
            List<int> list = new List<int>();
            if (value!=null && !string.IsNullOrEmpty(value.ToString()))
            {
                var tempArray = value.ToString().Split(',');
                if (tempArray != null && tempArray.Length > 0)
                {
                    foreach (var item in tempArray)
                    {
                        Int32.TryParse(item, out int NewValue);
                        if (NewValue > 0)
                        {
                            list.Add(NewValue);
                        }
                    }
                }
            }
            return list;
        }
    }
}