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

namespace Edu.Repository.Question
{
    /// <summary>
    /// 题目知识点仓储层
    /// </summary>
    public class RB_Question_PointRepository : BaseRepository<Model.Entity.Question.RB_Question_Point>
    {
        /// <summary>
        /// 获取问题知识点列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_Point_ViewModel> GetPointListRepository(RB_Question_Point_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM RB_Question_Point
WHERE 1=1 
");
            builder.AppendFormat(" AND {0}={1} ", nameof(RB_Question_Point_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Question_Point_ViewModel.CourseId), query.CourseId);
                }
                if (!string.IsNullOrEmpty(query.QPointIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Question_Point_ViewModel.PointId), query.QPointIds);
                }
            }
            return Get<RB_Question_Point_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_Question_Point_ViewModel> GetPointPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Question_Point_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM RB_Question_Point
WHERE 1=1 
");
            builder.AppendFormat(" AND {0}={1} ", nameof(RB_Question_Point_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Question_Point_ViewModel.CourseId), query.CourseId);
                }
                if (!string.IsNullOrEmpty(query.QPointIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Question_Point_ViewModel.PointId), query.QPointIds);
                }
                if (!string.IsNullOrEmpty(query.PointName))
                {
                    builder.AppendFormat(" AND {0} LIKE @PointName ", nameof(RB_Question_Point_ViewModel.PointName));
                    parameters.Add("PointName", "%" + query.PointName.Trim() + "%");
                }
            }
            return GetPage<RB_Question_Point_ViewModel>(pageIndex,pageSize,out rowsCount,builder.ToString(), parameters).ToList();
        }
    }
}