using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.System;

namespace Edu.Repository.System
{
    public class RB_System_HolidayRepository : BaseRepository<RB_System_Holiday>
    {
        /// <summary>
        /// 节假日列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="model">查询条件</param>
        /// <param name="count">总条数</param>
        /// <returns></returns>
        public List<RB_System_Holiday> GetPageList(int pageIndex, int pageSize, RB_System_Holiday model, out long count)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@" SELECT a.* from RB_System_Holiday as a  where 1=1");
            sb.AppendFormat(" AND A.{0}={1} ", nameof(RB_System_Holiday.Status), (int)Common.Enum.DateStateEnum.Normal);
            if (model.HolidayId > 0)
            {
                sb.AppendFormat(" AND A.{0}={1} ", nameof(RB_System_Holiday.HolidayId), model.HolidayId);
            }
            if (model.Group_Id > 0)
            {
                sb.AppendFormat(" AND A.{0}={1} ", nameof(RB_System_Holiday.Group_Id), model.Group_Id);
            }
            if (model.StartTime.HasValue)
            {
                sb.AppendFormat(" AND DATE_FORMAT(A.{0}, '%y/%m/%d') >= DATE_FORMAT('{1}', '%y/%m/%d') ", nameof(RB_System_Holiday.StartTime), model.StartTime.Value.ToString("yyyy-MM-dd"));
            }
            if (model.EndTime.HasValue)
            {
                sb.AppendFormat(" AND DATE_FORMAT(A.{0}, '%y/%m/%d') <= DATE_FORMAT('{1}', '%y/%m/%d') ", nameof(RB_System_Holiday.StartTime), model.EndTime.Value.ToString("yyyy-MM-dd"));
            }
            if (model.Group_Id > 0)
            {
                sb.AppendFormat(" AND A.{0}={1} ", nameof(RB_System_Holiday.Group_Id), model.Group_Id);
            }
            if (model.DayType > 0)
            {
                sb.AppendFormat(" AND A.{0}={1} ", nameof(RB_System_Holiday.DayType), model.DayType);
            }
            sb.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_System_Holiday.StartTime));
            return GetPage<RB_System_Holiday>(pageIndex, pageSize, out count, sb.ToString()).ToList();
        }

        /// <summary>
        /// 判断某一天是否在节假日里
        /// </summary>
        /// <param name="NowDay"></param>
        /// <returns></returns>
        public List<RB_System_Holiday> GetDayhList(string NowDay)
        {
            string sql = $@"SELECT * FROM RB_System_Holiday WHERE `Status` = 0
	AND DATE_FORMAT(StartTime, '%y/%m/%d') <= DATE_FORMAT('{NowDay}', '%y/%m/%d') AND DATE_FORMAT(EndTime, '%y/%m/%d') >= DATE_FORMAT('{NowDay}', '%y/%m/%d') ";
            return Get<RB_System_Holiday>(sql).ToList();
        }

        /// <summary>
        /// 判断某一个时间段是否在节假日里
        /// </summary>
        /// <param name="NowDay"></param>
        /// <returns></returns>
        public List<RB_System_Holiday> GetExistHoliday(string startTime, string endTime)
        {
            string sql = $@"SELECT * FROM RB_System_Holiday WHERE `Status` = 0 and 
(( DATE_FORMAT(StartTime, '%y/%m/%d') <= DATE_FORMAT('{startTime}', '%y/%m/%d')
AND DATE_FORMAT(EndTime, '%y/%m/%d') >= DATE_FORMAT('{startTime}', '%y/%m/%d')) 
or ( DATE_FORMAT(StartTime, '%y/%m/%d') <= DATE_FORMAT('{endTime}', '%y/%m/%d')
AND DATE_FORMAT(EndTime, '%y/%m/%d') >= DATE_FORMAT('{endTime}', '%y/%m/%d')) 
 or (DATE_FORMAT(EndTime, '%y/%m/%d') <= DATE_FORMAT('{endTime}', '%y/%m/%d'))
) ";
            return Get<RB_System_Holiday>(sql).ToList();
        }
    }
}
