using System;
using System.Collections.Generic;
using System.Text;

namespace Utils
{
    public class Utility
    {
        //创建一个随机数实例
        private static Random ro = new Random();

        /// <summary>
        /// right函数
        /// </summary>
        /// <param name="sSource"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public static string Right(string sSource, int iLength)
        {
            if (iLength == -1)
            {
                return "";
            }
            return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
        }

        /// <summary>
        /// left函数
        /// </summary>
        /// <param name="sSource"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public static string Left(string sSource, int iLength)
        {
            if (iLength == -1)
            {
                return "";
            }
            return sSource.Substring(0, iLength > sSource.Length ? sSource.Length : iLength);
        }

        #region 常用日期函数
        /// <summary>
        /// 上月第一天
        /// </summary>
        public static string PreMonthFirstDay
        {
            get { return DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(-1).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 上月最后一天
        /// </summary>
        public static string PreMonthLastDay
        {
            get { return DateTime.Now.AddDays(1 - DateTime.Now.Day).AddDays(-1).ToString("yyyy-MM-dd"); }
        }
        /// <summary>
        /// 上周一
        /// </summary>
        public static string PreWeekMonday
        {
            get { return DateTime.Now.AddDays(1 - System.Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).AddDays(-7).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 上周日
        /// </summary>
        public static string PreWeekSunday
        {
            get { return DateTime.Now.AddDays(1 - System.Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).AddDays(-1).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 昨天
        /// </summary>
        public static string Yesterday
        {
            get { return DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd"); }
        }
        /// <summary>
        /// 今天
        /// </summary>
        public static string Today
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 本周一
        /// </summary>
        public static string ThisWeekMonday
        {
            get { return DateTime.Now.AddDays(1 - System.Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 本周日
        /// </summary>
        public static string ThisWeekSunday
        {
            get { return DateTime.Now.AddDays(1 - System.Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))).AddDays(6).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 本月第一天
        /// </summary>
        public static string ThisMonthFirstDay
        {
            get { return DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 本月最后一天
        /// </summary>
        public static string ThisMonthLastDay
        {
            get { return DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 长日期转换为yyyy-mm-dd形式显示
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ConvertDateToYYYYMMDD(object date)
        {
            if (date.IsNullOrEmpty())
            {
                return "";
            }
            return System.Convert.ToDateTime(date).ToString("yyyy-MM-dd");
        }
        #endregion


        /// <summary>
        /// 是否本地服务器
        /// </summary>
        /// <returns></returns>
        public static bool IsLocal()
        {
            string address = RequestHelper.GetServerIp();
            //本地不作处理
            if (address.Contains("192.168."))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据IP获取webconfig中的本地域名或者网站域名
        /// </summary>
        /// <returns></returns>
        public static string GetWebSiteDomain()
        {
            if (Utility.IsLocal())
            {
                return System.Configuration.ConfigurationManager.AppSettings["LocalDomain"].ToString();
            }
            else
            {
                return System.Configuration.ConfigurationManager.AppSettings["WebSiteDomain"].ToString();
            }
        }


        /// <summary>
        /// 时分秒+随机数字符串
        /// </summary>
        /// <returns></returns>
        public static string GetRandomNumber()
        {
            string orderNo = DateTime.Now.ToString("yyyyMMddHHmmss");
            //获取一个大于1000小于9999的随机数
            string rnd = ro.Next(1000, 9999).ToString();
            orderNo += rnd;
            return orderNo;
        }
        /// <summary>
        /// 获取随机6位数
        /// </summary>
        /// <returns></returns>
        public static string GetRandomSix()
        {
            string rnd = ro.Next(100000, 999999).ToString();
            return rnd;
        }
       
       
        
        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        /// <summary>
        /// 比较两个一维字符串数组是否相同，这个相同是指元素个数相等，并且值相同，但是元素位置可能不同
        /// </summary>
        /// <param name="arr1"></param>
        /// <param name="arr2"></param>
        /// <returns></returns>
        public static bool CompareArrayEqual(string[] arr1, string[] arr2)
        {
            if (arr1 == null || arr2 == null) return false;
            if (arr1.Length != arr2.Length) return false;
            Array.Sort(arr1);
            Array.Sort(arr2);
            for (int i = 0; i < arr1.Length; i++)
            {
                if (arr1[i] != arr2[i])
                {
                    return false;
                }
            }
            return true;
        }
    }
}
