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

namespace Edu.Repository.Grade
{
    public class RB_Class_RoomUseLogRepository : BaseRepository<RB_Class_RoomUseLog>
    {
        /// <summary>
        /// 获取教异常分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_RoomUseLog_ViewModel> GetClassRoomUseLogPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_RoomUseLog_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,B.RoomName 
FROM RB_Class_RoomUseLog AS A LEFT JOIN rb_class_room AS B ON A.RoomId=B.RoomId
WHERE 1=1 ");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.Status), (int)(DateStateEnum.Normal));
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.School_Id), query.School_Id);
                }
                if (query.RoomId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.RoomId), query.RoomId);
                }

            }
            return GetPage<RB_Class_RoomUseLog_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取教室异常列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_RoomUseLog_ViewModel> GetClassRoomUseLogListRepository(RB_Class_RoomUseLog_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,B.RoomName 
FROM RB_Class_RoomUseLog AS A LEFT JOIN rb_class_room AS B ON A.RoomId=B.RoomId
WHERE 1=1 ");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.Status), (int)(DateStateEnum.Normal));
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.School_Id), query.School_Id);
                }
                if (query.RoomId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_RoomUseLog_ViewModel.RoomId), query.RoomId);
                }
                if (!string.IsNullOrWhiteSpace(query.StartTime))
                {
                    builder.AppendFormat($"  and DATE_FORMAT(a.CreateTime,'%Y-%m-%d')>=DATE_FORMAT('{query.StartTime}','%Y-%m-%d')");
                }
                if (!string.IsNullOrWhiteSpace(query.EndTime))
                {
                    builder.AppendFormat($"  and DATE_FORMAT(a.CreateTime,'%Y-%m-%d')<=DATE_FORMAT('{query.EndTime}','%Y-%m-%d')");
                }
                if (!string.IsNullOrEmpty(query.QRoomIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Class_RoomUseLog_ViewModel.RoomId), query.QRoomIds);
                }
            }
            return Get<RB_Class_RoomUseLog_ViewModel>(builder.ToString(), parameters).ToList();
        }
    }
}
