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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 教室管理仓储层
    /// </summary>
    public class RB_Class_RoomRepository : BaseRepository<RB_Class_Room>
    {
        /// <summary>
        /// 获取教室分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Room_ViewModel> GetClassRoomPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_Room_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,B.SName AS SchoolName 
FROM RB_Class_Room AS A LEFT JOIN rb_school AS B ON A.School_Id=B.SId
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_Room_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_Room_ViewModel.School_Id), query.School_Id);
                }
                if (query.Status >=0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_Room_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrEmpty(query.RoomName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @RoomName ", nameof(RB_Class_Room_ViewModel.RoomName));
                    parameters.Add("RoomName", "%" + query.RoomName.Trim() + "%");
                }
            }
            return GetPage<RB_Class_Room_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取教室列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Room_ViewModel> GetClassRoomListRepository(RB_Class_Room_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Class_Room AS A
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_Room_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_Room_ViewModel.School_Id), query.School_Id);
                }
                if (!string.IsNullOrEmpty(query.RoomName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @RoomName ", nameof(RB_Class_Room_ViewModel.RoomName));
                    parameters.Add("RoomName", "%" + query.RoomName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QRoomIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Class_Room_ViewModel.RoomId), query.QRoomIds);
                }
            }
            return Get<RB_Class_Room_ViewModel>(builder.ToString(), parameters).ToList();
        }
    }
}