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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 课程章节单词仓储层
    /// </summary>
    public class RB_Course_WordsRepository : BaseRepository<RB_Course_Words>
    {
        /// <summary>
        /// 获取课程单词分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Words_Extend> GetCourseWordsPageRepository(int pageIndex,int pageSize,out long rowsCount, RB_Course_Words_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Course_Words AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.Group_Id), query.Group_Id);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.CourseId), query.CourseId);
                }
                if (query.ChapterId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.ChapterId), query.ChapterId);
                }
                if (!string.IsNullOrEmpty(query.QCourseIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_Words_Extend.CourseId), query.QCourseIds);
                }
            }
            return GetPage<RB_Course_Words_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取课程单词列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Words_Extend> GetCourseWordsListRepository(RB_Course_Words_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Course_Words AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.Group_Id), query.Group_Id);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.CourseId), query.CourseId);
                }
                if (!string.IsNullOrEmpty(query.QCourseIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_Words_Extend.CourseId), query.QCourseIds);
                }
                if (query.ChapterId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_Words_Extend.ChapterId), query.ChapterId);
                }
                if (!string.IsNullOrEmpty(query.QChapterIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_Words_Extend.ChapterId), query.QChapterIds);
                }
                if (query.LessThanOrEqualChapterId > 0)
                {
                    builder.AppendFormat(" AND A.{0}<={1} ", nameof(RB_Course_Words_Extend.ChapterId), query.LessThanOrEqualChapterId);
                }
            }
            return Get<RB_Course_Words_Extend>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 批量添加课程单词
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool BatchInsertCourseWordsRepository(List<RB_Course_Words_Extend> list)
        {
            bool flag = true;
            if (list != null && list.Count > 0)
            {
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat(" INSERT INTO RB_Course_Words(CourseId,ChapterId,WordType,WordContent,WordTone,WordWrite,ChineseMean,Status,Group_Id,CreateBy,CreateTime,UpdateBy,UpdateTime) ");
                builder.AppendFormat(" VALUES ");
                string tempStr = "";
                foreach (var item in list)
                {
                    tempStr += $",({item.CourseId},{item.ChapterId},'{item.WordType}','{item.WordContent}','{item.WordTone}','{item.WordWrite}','{item.ChineseMean}',0,{item.Group_Id},{item.CreateBy},'{item.CreateTime}',{item.UpdateBy},'{item.UpdateTime}') ";
                }
                builder.Append(tempStr.TrimStart(','));
                flag = base.Execute(builder.ToString()) > 0;
            }
            return flag;
        }

        /// <summary>
        /// 获取章节单词数量
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="nextCourseNo"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public int GetCourseWordsNum(int courseId, int nextCourseNo, int groupId)
        {
            string sql = $@"select count(0) from RB_Course_Words where Status =0 and Group_Id ={groupId} and CourseId ={courseId} and ChapterId ={nextCourseNo}";
            var obj = ExecuteScalar(sql);
            return obj != null ? Convert.ToInt32(obj) : 0;
        }
    }
}
