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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 班级上课时间仓储层
    /// </summary>
    public class RB_Class_TimeRepository : BaseRepository<RB_Class_Time>
    {
        /// <summary>
        /// 获取班级上课时间列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Time_ViewModel> GetClassTimeListRepository(RB_Class_Time_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT *
FROM RB_Class_Time
WHERE 1=1
");
            if (query != null)
            {
                if (query.ClassPlanId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_Time_ViewModel.ClassPlanId), query.ClassPlanId);
                }
                if (query.ClassTimeId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_Time_ViewModel.ClassTimeId), query.ClassTimeId);
                }
                if (!string.IsNullOrEmpty(query.QClassPlanIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Class_Time_ViewModel.ClassPlanId), query.QClassPlanIds);
                }
                if (query.ClassId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_Time_ViewModel.ClassId), query.ClassId);
                }
                if (!string.IsNullOrEmpty(query.QClassIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Class_Time_ViewModel.ClassId), query.QClassIds);
                }
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_Plan_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_Plan_ViewModel.School_Id), query.School_Id);
                }
            }
            return Get<RB_Class_Time_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 根据班级编号删除班级上课时间
        /// </summary>
        /// <param name="ClassId">班级编号</param>
        /// <returns></returns>
        public bool DeleteClassTimeRepository(int ClassId)
        {
            return base.DeleteOne(new VT.FW.DB.WhereHelper(nameof(RB_Class_Time_ViewModel.ClassId), ClassId));
        }

        /// <summary>
        /// 根据上课计划编号删除上课计划
        /// </summary>
        /// <param name="ClassPlanIds"></param>
        /// <returns></returns>
        public bool DeleteClassTimeByPlanIdsRepository(string ClassPlanIds)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" DELETE FROM RB_Class_Time WHERE ClassPlanId IN({0}) ", ClassPlanIds);
            return base.Execute(builder.ToString()) > 0;
        }

        /// <summary>
        /// 获取班级上课时间
        /// </summary>
        /// <param name="classIds"></param>
        /// <returns></returns>
        public List<RB_Class_Time_ViewModel> GetClassTimeListRepository(string classIds)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.ClassId,DATE_FORMAT(MIN(A.NewPlanDateTime),'%Y-%m-%d %h:%i')  AS NewPlanDateTime FROM 
(
SELECT A.ClassPlanId,A.ClassId,A.ClassDate,A.`Status`,A.ClassRoomId,A.Group_Id,A.School_Id,A.TeacherId,B.ClassTimeId,B.StartTime,B.EndTime
       ,CONCAT( DATE_FORMAT(A.ClassDate,'%Y-%m-%d') ,' ',B.StartTime,':00') AS NewPlanDateTime
FROM rb_class_plan AS A INNER JOIN rb_class_time AS B ON A.ClassPlanId=B.ClassPlanId
) AS A
WHERE 1=1 
");
            if (!string.IsNullOrEmpty(classIds))
            {
                builder.AppendFormat(" AND A.ClassId IN({0}) ", classIds);
            }
            builder.AppendFormat(" GROUP BY A.ClassId ");
            return Get<RB_Class_Time_ViewModel>(builder.ToString()).ToList();
        }

        #region 教师上课挂件查询

        /// <summary>
        /// 获取班级上课时间
        /// </summary>
        /// <param name="classIds"></param>
        /// <returns></returns>
        public List<RB_Class_Time_Extend> GetClassTimeByRoomId(int RoomId,int Group_Id)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat($@"SELECT t.TeacherName,t.TeacherIcon,c.ClassHours,c.ClassName,cou.CourseName,lp.LessonPlanId,A.ClassPlanId,A.ClassId,A.`Status`,A.ClassRoomId,A.Group_Id,A.School_Id,A.TeacherId,B.ClassTimeId,CONCAT( DATE_FORMAT(A.ClassDate,'%Y-%m-%d') ,' ',B.StartTime,':00') AS NewPlanDateTime,
B.EndTime,B.StartTime,B.SuiPai,B.ClassStatus,CONCAT( DATE_FORMAT(A.ClassDate,'%Y-%m-%d') ,' ',B.EndTime,':00') AS NewEndPlanDateTime
FROM rb_class_plan AS A INNER JOIN rb_class_time AS B ON A.ClassPlanId=B.ClassPlanId
LEFT JOIN rb_teacher as t on a.TeacherId=t.TId
LEFT JOIN rb_class as c on c.ClassId=a.ClassId
LEFT JOIN rb_course as cou on cou.CourseId=c.CouseId
LEFT JOIN (SELECT ClassPlanId,MAX(LessonPlanId) as LessonPlanId from rb_class_lessonplan where `Status`=0 GROUP BY ClassPlanId) as  lp on lp.ClassPlanId=a.ClassPlanId
where a.`Status`=0 and c.ClassStatus =2  and a.ClassRoomId={RoomId} and a.Group_Id={Group_Id} and DATE_FORMAT(A.ClassDate,'%Y-%m-%d')>=DATE_FORMAT(NOW(),'%Y-%m-%d') ORDER BY CONCAT( DATE_FORMAT(A.ClassDate,'%Y-%m-%d') ,' ',B.EndTime,':00')  asc LIMIT 10");
            return Get<RB_Class_Time_Extend>(builder.ToString()).ToList();
        }

        #endregion
    }
}
