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

namespace Edu.Repository.Reserve
{
    /// <summary>
    /// 约课班级信息仓储层
    /// </summary>
    public class RB_Reserve_ClassRepository : BaseRepository<RB_Reserve_Class>
    {
        /// <summary>
        /// 获取约课班级分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Reserve_Class_Extend> GetReserveClassPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Reserve_Class_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.Append(@"
SELECT A.*,IFNULL(D.TeacherName,'') AS TeacherName,IFNULL(E.RoomName,'') AS RoomName,IFNULL(E.SeatNum,0) AS RoomNum
       ,IFNULL(F.LessonName,'') AS LessonName
FROM RB_Reserve_Class AS A LEFT JOIN rb_teacher AS D ON A.TeacherId=D.TId
     LEFT JOIN rb_class_room AS E ON A.ClassRoomId=E.RoomId
     LEFT JOIN rb_triallesson AS F ON A.TrialLessonId=F.Id
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.StartClassDate))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Reserve_Class_Extend.ClassDate), query.StartClassDate);
                }
                if (!string.IsNullOrEmpty(query.EndClassDate))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Reserve_Class_Extend.ClassDate), query.EndClassDate);
                }
                if (query.TeacherId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.TeacherId), query.TeacherId);
                }
                if (query.ClassRoomId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.ClassRoomId), query.ClassRoomId);
                }
                if (!string.IsNullOrEmpty(query.LessonName))
                {
                    builder.AppendFormat(" AND F.{0} LIKE @LessonName ", nameof(RB_Reserve_Class_Extend.LessonName));
                    parameters.Add("LessonName", "%" + query.LessonName.Trim() + "%");
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} ASC ", nameof(RB_Reserve_Class_Extend.ClassDate));
            return GetPage<RB_Reserve_Class_Extend>(pageIndex,pageSize,out rowsCount, builder.ToString(),parameters).ToList();
        }

        /// <summary>
        /// 获取约课班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Reserve_Class_Extend> GetReserveClassListRepository(RB_Reserve_Class_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.Append(@"
SELECT A.*,IFNULL(D.TeacherName,'') AS TeacherName,IFNULL(E.RoomName,'') AS RoomName,IFNULL(E.SeatNum,0) AS RoomNum
       ,IFNULL(G.LessonName,'') AS LessonName
FROM RB_Reserve_Class AS A LEFT JOIN rb_teacher AS D ON A.TeacherId=D.TId
     LEFT JOIN rb_class_room AS E ON A.ClassRoomId=E.RoomId
	 LEFT JOIN rb_triallesson AS G ON A.TrialLessonId=G.Id
WHERE 1=1  
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.StartClassDate))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Reserve_Class_Extend.ClassDate), query.StartClassDate);
                }
                if (!string.IsNullOrEmpty(query.EndClassDate))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Reserve_Class_Extend.ClassDate), query.EndClassDate);
                }
                if (query.TeacherId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.TeacherId), query.TeacherId);
                }
                if (query.ClassRoomId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.ClassRoomId), query.ClassRoomId);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.School_Id), query.School_Id);
                }
                if (query.ReserveClassId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Reserve_Class_Extend.ReserveClassId), query.ReserveClassId);
                }
            }
            return Get<RB_Reserve_Class_Extend>(builder.ToString(),parameters).ToList();
        }

        /// <summary>
        /// 检查是否存在
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool ExistsReserveClassRepository(RB_Reserve_Class_Extend query)
        {
            int result = 0;
            var startTime = query.ClassDateStr + " " + query.ClassTime + ":00";
            var endTime= query.ClassDateStr + " " + query.EndTime + ":00";
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT 1
FROM RB_Reserve_Class c
WHERE 1=1 AND c.Status=0 AND NOT(CONCAT(substring(C.ClassDate,1,10),' ',C.ClassTime,':00')> '{0}' or CONCAT(substring(C.ClassDate,1,10),' ',C.EndTime,':00')< '{1}')
 ", endTime, startTime);
            if (query.TeacherId > 0)
            {
                builder.AppendFormat(" AND C.TeacherId={0} ", query.TeacherId);
            }
            if (query.ClassRoomId > 0)
            {
                builder.AppendFormat(" AND C.ClassRoomId={0} ", query.ClassRoomId);
            }
            if (query.ReserveClassId > 0)
            {
                builder.AppendFormat(" AND C.ReserveClassId<>{0} ", query.ReserveClassId);
            }
            var obj= base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result>0;
        }

        /// <summary>
        /// 获取试听班级列表
        /// </summary>
        /// <param name="rB_Reserve_Class_Extend"></param>
        /// <returns></returns>
        public List<RB_Reserve_Class_Extend> GetClassReserveList(RB_Reserve_Class_Extend dmodel)
        {
            string where = " 1=1 and c.Status =0 and c.TrialLessonId >0 and t.CalculateType =2";
            if (dmodel.Group_Id > 0)
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.Group_Id)} ={dmodel.Group_Id}";
            }
            if (!string.IsNullOrEmpty(dmodel.TeacherIds))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.TeacherId)} in({dmodel.TeacherIds})";
            }
            if (!string.IsNullOrEmpty(dmodel.ClassTime))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.ClassDate)} >='{dmodel.ClassTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.ClassDate)} <='{dmodel.EndTime} 23:59:59'";
            }

            string sql = $@"select c.*,t.ClassHourFee,t.LessonName as ClassName,(select count(0) from rb_visitor_reserve v where v.ReserveClassId = c.ReserveClassId and v.Status =0 ) as CheckNum 
from RB_Reserve_Class c 
left join rb_triallesson t on c.TrialLessonId = t.Id
where {where}";
            return Get<RB_Reserve_Class_Extend>(sql).ToList();
        }

        /// <summary>
        /// 获取试听班级列表
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="schoolId"></param>
        /// <param name="classId"></param>
        /// <param name="startMonth"></param>
        /// <param name="endMonth"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public List<RB_Reserve_Class_Extend> GetTeacherHoursDetialList(string userIds, int schoolId, int classId, string startMonth, string endMonth, int groupId)
        {
            string where = " 1=1 and c.Status =0 and c.TrialLessonId >0 and t.CalculateType =2";
            if (groupId > 0)
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.Group_Id)} ={groupId}";
            }
            if (schoolId > 0)
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.School_Id)} ={schoolId}";
            }
            if (!string.IsNullOrEmpty(userIds))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.TeacherId)} in({userIds})";
            }
            if (!string.IsNullOrEmpty(startMonth))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.ClassDate)} >='{startMonth}'";
            }
            if (!string.IsNullOrEmpty(endMonth))
            {
                where += $@" and c.{nameof(RB_Reserve_Class_Extend.ClassDate)} <='{endMonth} 23:59:59'";
            }

            string sql = $@"select c.*,t.ClassHourFee,t.LessonName,t2.TeacherName,t2.BaseHoursEnabled,t2.EnableTime,s.SName
from RB_Reserve_Class c 
left join rb_triallesson t on c.TrialLessonId = t.Id
LEFT JOIN rb_teacher t2 on c.TeacherId = t2.TId
left join rb_school s on s.SId = c.School_Id
where {where}";
            return Get<RB_Reserve_Class_Extend>(sql).ToList();
        }
    }
}
