using Edu.Common.Enum;
using Edu.Common.Enum.Question;
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_QuestionRepository : BaseRepository<Model.Entity.Question.RB_Question>
    {
        /// <summary>
        /// 获取问题列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Question_ViewModel> GetQuestionListRepository(RB_Question_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM  RB_Question
WHERE 1=1 
");
            if (query == null)
            {
                return new List<RB_Question_ViewModel>();
            }
            else
            {
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Question_ViewModel.CourseId), query.CourseId);
                }
                return Get<RB_Question_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_ViewModel> GetQuestionPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Question_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.`Name` ,'') AS QuestionTypeName
FROM  RB_Question AS A LEFT JOIN RB_Question_Type B ON A.QuestionTypeId=B.QId   
WHERE 1=1 ");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Question_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Question_ViewModel>();
            }
            else
            {
                if (query.BankId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Question_ViewModel.BankId), query.BankId);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Question_ViewModel.CourseId), query.CourseId);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(" AND (A.{0} LIKE @Title OR A.{1} LIKE @Title OR A.{2} LIKE @Title) "
                        ,nameof(RB_Question_ViewModel.Title)
                        ,nameof(RB_Question_ViewModel.QuestionContent)
                        ,nameof(RB_Question_ViewModel.AnswerParse)
                    );
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.PointName))
                {
                    builder.Append(@"  AND A.Knowledge<>''  AND EXISTS (SELECT 1 FROM rb_question_point WHERE PointId IN(A.Knowledge) AND A.CourseId=CourseId AND PointName LIKE @PointName ) ");
                    parameters.Add("PointName", "%" + query.PointName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QQuestionTypeStr))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Question_ViewModel.QuestionTypeId), query.QQuestionTypeStr);
                }
                if (!string.IsNullOrEmpty(query.QDifficultyType))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Question_ViewModel.DifficultyType), query.QDifficultyType);
                }
                if (!string.IsNullOrEmpty(query.QCategoryId))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Question_ViewModel.Category), query.QCategoryId);
                }
                builder.AppendFormat(" ORDER BY A.{0} ASC ", nameof(RB_Question_ViewModel.SortNum));
                return GetPage<RB_Question_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
            }
        }

        /// <summary>
        /// 批量更新问题考级程度
        /// </summary>
        /// <param name="levelTypeEnum"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        public bool SetBatchQuestionLevelRepository(LevelTypeEnum levelTypeEnum, List<int> questionIdList)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" UPDATE RB_Question SET LevelType={0} WHERE QuestionId IN ({1}) ", (int)levelTypeEnum, string.Join(",", questionIdList));
            return base.Execute(builder.ToString()) > 0;
        }

    }
}