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_Course_SubjectRepository : BaseRepository<RB_Course_Subject>
    {
        /// <summary>
        /// 获取科目分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Subject_ViewModel> GetCourseSubjectPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Course_Subject_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*
FROM  RB_Course_Subject AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Subject_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Subject_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.SubjectName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @SubjectName ", nameof(RB_Course_Subject_ViewModel.SubjectName));
                    parameters.Add("SubjectName", "%" + query.SubjectName.Trim() + "%");
                }
            }
            return GetPage<RB_Course_Subject_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取科目列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Subject_ViewModel> GetCourseSubjectListRepository(RB_Course_Subject_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*
FROM  RB_Course_Subject AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Subject_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Subject_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.SubjectName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @SubjectName ", nameof(RB_Course_Subject_ViewModel.SubjectName));
                    parameters.Add("SubjectName", "%" + query.SubjectName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QIds))
                {
                    builder.AppendFormat(" AND A.{0} IN ({1}) ", nameof(RB_Course_Subject_ViewModel.Id), query.QIds);
                }
            }
            return Get<RB_Course_Subject_ViewModel>(builder.ToString(), parameters).ToList();
        }
    }
}
