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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 班级仓储层
    /// </summary>
    public class RB_ClassRepository : BaseRepository<RB_Class>
    {
        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassListRepository(RB_Class_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM rb_class AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.CouseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_ViewModel.CouseId), query.CouseId);
                }
            }
            return Get<RB_Class_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取班级分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,B.CourseName,C.AssistName,IFNULL(C.AssistIcon,'') AS AssistIcon,D.TeacherName,IFNULL(D.TeacherHead,'') AS TeacherHead
FROM rb_class AS A LEFT JOIN rb_course AS B ON A.CouseId=B.CourseId
     LEFT JOIN rb_assist AS C ON A.Assist_Id=C.AId
     LEFT JOIN rb_teacher AS D ON A.Teacher_Id=D.TId
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Status >=0 )
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrWhiteSpace(query.ClassName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @ClassName ", nameof(RB_Class_ViewModel.ClassName));
                    parameters.Add("ClassName", "%" + query.ClassName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.CourseName))
                {
                    builder.AppendFormat(" AND B.{0} LIKE @CourseName ", nameof(RB_Class_ViewModel.CourseName));
                    parameters.Add("CourseName", "%" + query.CourseName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.AssistName))
                {
                    builder.AppendFormat(" AND C.{0} LIKE @AssistName ", nameof(RB_Class_ViewModel.AssistName));
                    parameters.Add("AssistName", "%" + query.AssistName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.TeacherName))
                {
                    builder.AppendFormat(" AND D.{0} LIKE @TeacherName ", nameof(RB_Class_ViewModel.TeacherName));
                    parameters.Add("TeacherName", "%" + query.TeacherName.Trim() + "%");
                }
            }
            return GetPage<RB_Class_ViewModel>(pageIndex,pageSize,out rowsCount,builder.ToString(),parameters).ToList();
        }

    }
}
