using Edu.Common.Enum;
using Edu.Model.Entity.Exam;
using Edu.Model.ViewModel.Exam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Exam
{
    /// <summary>
    /// 试卷题目仓储层
    /// </summary>
    public class RB_Examination_DetailsRepository : BaseRepository<RB_Examination_Details>
    {
        /// <summary>
        /// 获取试卷题目列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Details_ViewModel> GetExaminationDetailsListRepository(RB_Examination_Details_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Examination_Details AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Details_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.PaperId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Details_ViewModel.PaperId), query.PaperId);
                }
                if (!string.IsNullOrEmpty(query.QPaperIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Examination_Details_ViewModel.PaperId), query.QPaperIds);
                }
            }
            return Get<RB_Examination_Details_ViewModel>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 获取试卷题目列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Details_ViewModel> GetExaminationDetailsStaticListRepository(RB_Examination_Details_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.PaperId,COUNT(1) AS QuestionCount
FROM RB_Examination_Details AS A
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Details_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.PaperId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Examination_Details_ViewModel.PaperId), query.PaperId);
                }
                if (!string.IsNullOrEmpty(query.QPaperIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Examination_Details_ViewModel.PaperId), query.QPaperIds);
                }
            }
            builder.Append(" GROUP BY A.PaperId ");
            return Get<RB_Examination_Details_ViewModel>(builder.ToString()).ToList();
        }
    }
}
