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_StudentRepository:BaseRepository<RB_Examination_Student>
    {
        /// <summary>
        /// 获取考试学生分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Student_ViewModel> GetExaminationStudentPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Examination_Student_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.GuestName,'') AS GuestName,c.ClassName,b.Mobile,s.StuIcon,p.ExamStartTime AS PaperStartTime,p.ExamEndTime AS PaperEndTime
FROM RB_Examination_Student AS A LEFT JOIN rb_order_guest AS B ON A.GuestId=B.Id
    LEFT JOIN rb_class as c on c.ClassId=a.ClassId
    LEFT JOIN rb_student_orderguest as sog on sog.GuestId=a.GuestId
    LEFT JOIN rb_student as s on s.StuId = sog.Student_Id
    LEFT JOIN rb_examination_publish AS p ON A.PublishId=P.Id
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.PublishId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.PublishId), query.PublishId);
                }
                if (!string.IsNullOrWhiteSpace(query.GuestName))
                {
                    builder.AppendFormat(" AND B.{0} like '%{1}%' ", nameof(RB_Examination_Student_ViewModel.GuestName), query.GuestName);
                }
            }
            return GetPage<RB_Examination_Student_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取考试学生列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Examination_Student_ViewModel> GetExaminationStudentListRepository(RB_Examination_Student_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.GuestName,'') AS GuestName
FROM RB_Examination_Student AS A LEFT JOIN rb_order_guest AS B ON A.GuestId=B.Id
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.PublishId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.PublishId), query.PublishId);
                }
                if (query.PaperId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.PaperId), query.PaperId);
                }
                if (query.GuestId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Examination_Student_ViewModel.GuestId), query.GuestId);
                }
                if (!string.IsNullOrEmpty(query.QPublishIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Examination_Student_ViewModel.PublishId), query.QPublishIds);
                }
            }
            return Get<RB_Examination_Student_ViewModel>(builder.ToString()).ToList();
        }
    }
}