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
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
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);
                }
            }
            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
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
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);
                }
            }
            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();
            //CONCAT(substring(C.ClassDate,1,10),' ',C.ClassTime,':00') AS StartTime,CONCAT(substring(C.ClassDate,1,10),' ',C.EndTime,':00') AS EndTime
            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;
        }
    }
}
