using Edu.Common.Enum;
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_TrialLessonRepository : BaseRepository<RB_TrialLesson>
    {
        /// <summary>
        /// 获取试听课程分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCuont"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_TrialLesson_Extend> GetTrialLessonPageRepository(int pageIndex, int pageSize, out long rowsCuont, RB_TrialLesson_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.* ,IFNULL(D.TeacherName,'') AS TeacherName
FROM RB_TrialLesson AS A LEFT JOIN rb_teacher AS D ON A.TeacherId=D.TId
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_TrialLesson_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_TrialLesson_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrWhiteSpace(query.LessonName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @LessonName ", nameof(RB_TrialLesson_Extend.LessonName));
                    parameters.Add("LessonName", "%" + query.LessonName.Trim() + "%");
                }
            }
            return GetPage<RB_TrialLesson_Extend>(pageIndex, pageSize, out rowsCuont, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取试听课程列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_TrialLesson_Extend> GetTrialLessonListRepository(RB_TrialLesson_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_TrialLesson AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_TrialLesson_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_TrialLesson_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrWhiteSpace(query.LessonName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @LessonName ", nameof(RB_TrialLesson_Extend.LessonName));
                    parameters.Add("LessonName", "%" + query.LessonName.Trim() + "%");
                }
            }
            return Get<RB_TrialLesson_Extend>(builder.ToString(), parameters).ToList();
        }
    }
}
