using Edu.Common.Enum;
using Edu.Model.Entity.Scroll;
using Edu.Model.ViewModel.Scroll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Scroll
{
    /// <summary>
    /// 滚动开班预约仓储层
    /// </summary>
    public class RB_Scroll_AppointmentRepository : BaseRepository<RB_Scroll_Appointment>
    {

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Scroll_Appointment_ViewModel> GetList(RB_Scroll_Appointment_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_Scroll_Appointment_ViewModel.Status)} =0 and r.{nameof(RB_Scroll_Appointment_ViewModel.State)} <>5";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EntTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} <='{demodel.EntTime} 23:59:59'";
            }

            string sql = $@" 
SELECT r.*,c.AccountId as TeacherId,s.StuName,cou.CourseName,cou.ScrollMinNum,cou.ScrollMaxNum From RB_Scroll_Appointment r
inner join rb_account c on r.AccountId = c.Id
inner join rb_student s on r.StuId = s.StuId
inner join rb_course cou on r.CourseId = cou.CourseId
WHERE {where} 
ORDER BY r.Id asc ";
            return Get<RB_Scroll_Appointment_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 获取我预约的次数
        /// </summary>
        /// <param name="stuId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public RB_Scroll_Appointment_ViewModel GetMyAppointmentNum(int stuId, int group_Id)
        {
            string sql = $@"SELECT MAX(Date) AS Date, COUNT(0) as ScrollMinNum FROM rb_scroll_appointment WHERE `Status` =0 and State <>5 and CourseEndTime >='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' and Group_Id ={group_Id} and StuId ={stuId} HAVING COUNT(0) >0";
            return Get<RB_Scroll_Appointment_ViewModel>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Scroll_Appointment_ViewModel> GetAppointPageList(int pageIndex, int pageSize, out long count, RB_Scroll_Appointment_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_Scroll_Appointment_ViewModel.Status)} =0 and r.{nameof(RB_Scroll_Appointment_ViewModel.State)} <>5";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.State > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.State)} ={(int)demodel.State}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and c.AccountId ={demodel.TeacherId}";
            }
            if (demodel.StuId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.StuId)} ={demodel.StuId}";
            }
            if (demodel.CourseId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.CourseId)} ={demodel.CourseId}";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EntTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} <='{demodel.EntTime} 23:59:59'";
            }

            string sql = $@" 
SELECT r.*,c.AccountId as TeacherId,t.TeacherName,t.TeacherHead,s.StuName,cou.CourseName,cou.ScrollMinNum,cou.ScrollMaxNum,cou.CourseTimeId,cr.RoomName,sc.SName as RoomSchoolName From RB_Scroll_Appointment r
inner join rb_account c on r.AccountId = c.Id
inner join rb_teacher t on c.AccountId = t.TId
inner join rb_student s on r.StuId = s.StuId
inner join rb_course cou on r.CourseId = cou.CourseId
inner join rb_class_room cr on r.RoomId = cr.RoomId
left join rb_school sc on sc.SId = cr.School_Id
WHERE {where} 
ORDER BY r.Id desc ";
            return GetPage<RB_Scroll_Appointment_ViewModel>(pageIndex, pageSize, out count, sql, parameters).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Scroll_Appointment_ViewModel> GetAppointList(RB_Scroll_Appointment_ViewModel demodel)
        {
            DynamicParameters parameters = new DynamicParameters();
            string where = $@" 1=1 and r.{nameof(RB_Scroll_Appointment_ViewModel.Status)} =0 and r.{nameof(RB_Scroll_Appointment_ViewModel.State)} <>5";
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.State > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.State)} ={(int)demodel.State}";
            }
            if (demodel.AccountId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.AccountId)} ={demodel.AccountId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and c.AccountId ={demodel.TeacherId}";
            }
            if (demodel.StuId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.StuId)} ={demodel.StuId}";
            }
            if (demodel.CourseId > 0)
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.CourseId)} ={demodel.CourseId}";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EntTime))
            {
                where += $@" and r.{nameof(RB_Scroll_Appointment_ViewModel.Date)} <='{demodel.EntTime} 23:59:59'";
            }

            string sql = $@" 
SELECT r.*,c.AccountId as TeacherId,c.WorkUserId,t.TeacherName,t.TeacherHead,s.StuName,sch.OpenId as StuOpenId,cou.CourseName,cou.ScrollMinNum,cou.ScrollMaxNum,cou.CourseTimeId,cr.RoomName,sc.SName as RoomSchoolName 
From RB_Scroll_Appointment r
inner join rb_account c on r.AccountId = c.Id
inner join rb_teacher t on c.AccountId = t.TId
inner join rb_student s on r.StuId = s.StuId
inner join rb_account sch on sch.AccountId = s.StuId and sch.AccountType =4
inner join rb_course cou on r.CourseId = cou.CourseId
inner join rb_class_room cr on r.RoomId = cr.RoomId
left join rb_school sc on sc.SId = cr.School_Id
WHERE {where} 
ORDER BY r.Id desc ";
            return Get<RB_Scroll_Appointment_ViewModel>(sql, parameters).ToList();
        }
    }
}
