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_PlanRepository : BaseRepository<RB_Scroll_Plan>
    {

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

            string sql = $@" 
SELECT * From RB_Scroll_Plan r
WHERE {where} 
ORDER BY r.Id asc ";
            return Get<RB_Scroll_Plan_ViewModel>(sql, parameters).ToList();
        }

        /// <summary>
        /// 获取日期范围老师排课列表
        /// </summary>
        /// <param name="date"></param>
        /// <param name="schoolId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public List<RB_Scroll_Plan_ViewModel> GetTeacherPlanForDay(string date, int schoolId, int group_Id)
        {
            string sql = $@"SELECT p.*,a.AccountId as TeacherId,t.TeacherName,t.TeacherHead,t.TeacherIntro,r.RoomName,s.SName as SchoolName FROM rb_scroll_plan p
inner join rb_account a on p.AccountId = a.Id
inner join rb_teacher t on a.AccountId = t.TId
inner join rb_class_room r on p.RoomId = r.RoomId
inner join rb_school s on r.School_Id = s.SId
WHERE p.`Status` =0 and p.Group_Id ={group_Id} and p.Date ='{date}' and p.ShiftId >0";
            if (schoolId >= 0) {
                sql += $@" and r.School_Id ={schoolId}";
            }
            return Get<RB_Scroll_Plan_ViewModel>(sql).ToList();
        }
    }
}
