using System;

namespace DigitalSchoolApi.Models
{
    /// <summary>
    /// Common
    /// </summary>
    public class Common
    {
        /// <summary>
        /// 根据当前时间获取学年和学期
        /// </summary>
        /// <returns></returns>
        public static (string AcademicYearShort,string AcademicYearLong,string Semester) GetSemesterAndYear()
        {
            string AcademicYearShort = "";//短格式学年度
            string AcademicYearLong = "";//长格式学年度
            string Semester = "";//学期
            if (DateTime.Now.Month >= 8)
            {
                AcademicYearShort = DateTime.Now.Year.ToString().Substring(2, 2) + "-";
                AcademicYearShort += (DateTime.Now.Year + 1).ToString().Substring(2, 2);
                Semester = "1";
            }
            else
            {
                if (DateTime.Now.Month >= 2)//根据当前时间获取学年和学期,注意跨年份
                {
                    AcademicYearShort = (DateTime.Now.Year - 1).ToString().Substring(2, 2) + "-";
                    AcademicYearShort += DateTime.Now.Year.ToString().Substring(2, 2);
                    AcademicYearLong = (DateTime.Now.Year - 1).ToString() + "-";
                    AcademicYearLong += DateTime.Now.Year.ToString();
                    Semester = "2";
                }
                else
                {
                    AcademicYearShort = (DateTime.Now.Year - 1).ToString().Substring(2, 2) + "-";
                    AcademicYearShort += DateTime.Now.Year.ToString().Substring(2, 2);
                    AcademicYearLong = (DateTime.Now.Year - 1).ToString() + "-";
                    AcademicYearLong += DateTime.Now.Year.ToString();
                    Semester = "1";
                }
            }
            return (AcademicYearShort, AcademicYearLong, Semester);
        }
        /// <summary>
        /// 计算周一的时间
        /// </summary>
        /// <param name="someDate"></param>
        /// <returns></returns>
        public static DateTime CalculateFirstDateOfWeek(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Monday;
            if (i == -1) i = 6;// i值 > = 0 ，因为枚举原因，Sunday排在最前，此时Sunday-Monday=-1，必须+7=6。
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Subtract(ts);
        }
        /// <summary>
        /// 计算周日的时间
        /// </summary>
        /// <param name="someDate"></param>
        /// <returns></returns>
        public static DateTime CalculateLastDateOfWeek(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Sunday;
            if (i != 0) i = 7 - i;// 因为枚举原因，Sunday排在最前，相减间隔要被7减。
            TimeSpan ts = new TimeSpan(i, 0, 0, 0);
            return someDate.Add(ts);
        }

        /// <summary>
        /// 自动生成编号  201008251145409865
        /// </summary>
        /// <returns></returns>
        public static string CreateNo()
        {
            Random random = new Random();
            string strRandom = random.Next(1000, 10000).ToString(); //生成编号 
            string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
            return code;
        }
    }
}