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_StuWords_PrepRepository : BaseRepository<RB_StuWords_Prep>
    {

        /// <summary>
        /// 获取单词预习列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StuWords_Prep_Extend> GetStuWordsPrepListRepository(RB_StuWords_Prep_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT A.* FROM RB_StuWords_Prep AS A WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.Group_Id), query.Group_Id);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.CourseId), query.CourseId);
                }
                if (query.ChapterId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.ChapterId), query.ChapterId);
                }
                if (query.Stu_Account_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.Stu_Account_Id), query.Stu_Account_Id);
                }
            }
            return Get<RB_StuWords_Prep_Extend>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取单词预习分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public decimal GetStuWordsPrepScoreRepository(RB_StuWords_Prep_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT SUM(A.Score) AS Score FROM RB_StuWords_Prep AS A WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.Group_Id), query.Group_Id);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.CourseId), query.CourseId);
                }
                if (query.ChapterId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.ChapterId), query.ChapterId);
                }
                if (query.Stu_Account_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_StuWords_Prep_Extend.Stu_Account_Id), query.Stu_Account_Id);
                }
            }
            decimal totalScore = 0;
            object obj = base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                decimal.TryParse(obj.ToString(), out totalScore);
            }
            return totalScore;
        }
    }
}
