using Edu.Common.Enum;
using Edu.Model.ViewModel.Course;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;
using System;

namespace Edu.Repository.Course
{
    /// <summary>
    /// 课程仓储层
    /// </summary>
    public class RB_CourseRepository : BaseRepository<Model.Entity.Course.RB_Course>
    {
        /// <summary>
        /// 获取课程列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_ViewModel> GetCourseListRepository(RB_Course_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.CateName,'') AS CateName
FROM RB_Course AS A LEFT JOIN rb_course_category AS B ON A.CateId=B.CateId
WHERE 1=1 
");

            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrWhiteSpace(query.CourseName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CourseName ", nameof(RB_Course_ViewModel.CourseName));
                    parameters.Add("CourseName", "%" + query.CourseName.Trim() + "%");
                }
                if (query.CateId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.CateId), query.CateId);
                }
                if (!string.IsNullOrEmpty(query.QCourseIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_ViewModel.CourseId), query.QCourseIds);
                }
                //取随机前几条
                if (query.IsRand && query.RandNum > 0 && query.CourseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}<>{1} ORDER BY RAND() LIMIT {2} ", nameof(RB_Course_ViewModel.CourseId), query.CourseId, query.RandNum);
                }
                //销售平台
                if (!string.IsNullOrEmpty(query.Saleplat))
                {
                    var salePlatList = Common.ConvertHelper.StringToList(query.Saleplat);

                    string str = "";
                    if (salePlatList != null && salePlatList.Count > 0)
                    {
                        for (var i = 0; i < salePlatList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.Saleplat)>0 ", salePlatList[i]);
                            }
                            else
                            {
                                str += string.Format(" AND FIND_IN_SET('{0}', A.Saleplat)>0 ", salePlatList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
                //查询有上架的
                if (query.IsQuerySalePlat == 1)
                {
                    builder.AppendFormat(" AND IFNULL(A.Saleplat,'')<>'' ");
                }
                //所属科目
                if (query.CourseSubject > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_ViewModel.CourseSubject), query.CourseSubject);
                }
                if (query.IsScrollClass > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.IsScrollClass), query.IsScrollClass);
                }
            }
            return Get<RB_Course_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取课程分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_ViewModel> GetCoursePageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Course_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat($@"
SELECT A.*,IFNULL(B.CateName,'') AS CateName,IFNULL(C.SubjectName,'') AS CourseSubjectName, t.OrderNum, t.GuestNum
FROM RB_Course AS A 
LEFT JOIN rb_course_category AS B ON A.CateId=B.CateId
LEFT JOIN rb_course_subject AS C ON A.CourseSubject=C.Id
LEFT JOIN (
	SELECT o.CourseId,COUNT(0) AS OrderNum, SUM(o.GuestNum) AS GuestNum FROM rb_order o WHERE o.Group_Id ={query.Group_Id} AND o.OrderState <>3 GROUP BY o.CourseId
) t on t.CourseId = A.CourseId
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.Group_Id), query.Group_Id);
            if (query != null)
            {
                if ((int)query.Status > -1)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrWhiteSpace(query.CourseName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CourseName ", nameof(RB_Course_ViewModel.CourseName));
                    parameters.Add("CourseName", "%" + query.CourseName.Trim() + "%");
                }
                if (query.CateId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.CateId), query.CateId);
                }
                if (!string.IsNullOrWhiteSpace(query.QCateIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_ViewModel.CateId), query.QCateIds);
                }
                //销售平台
                if (!string.IsNullOrEmpty(query.Saleplat))
                {
                    var salePlatList = Common.ConvertHelper.StringToList(query.Saleplat);
                    string str = "";
                    if (salePlatList != null && salePlatList.Count > 0)
                    {
                        for (var i = 0; i < salePlatList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.Saleplat)>0 ", salePlatList[i]);
                            }
                            else
                            {
                                str += string.Format(" AND FIND_IN_SET('{0}', A.Saleplat)>0 ", salePlatList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
                //所属科目
                if (query.CourseSubject > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_ViewModel.CourseSubject), (int)query.CourseSubject);
                }
                if (query.Q_OrderBy > 0) {
                    if (query.Q_OrderBy == 1)
                    {
                        builder.AppendFormat(" AND t.OrderNum asc ");
                    }
                    else if (query.Q_OrderBy == 2)
                    {
                        builder.AppendFormat(" AND t.OrderNum desc ");
                    }
                    else if (query.Q_OrderBy == 3)
                    {
                        builder.AppendFormat(" AND t.GuestNum asc ");
                    }
                    else if (query.Q_OrderBy == 4)
                    {
                        builder.AppendFormat(" AND t.GuestNum desc ");
                    }
                }
            }
            return GetPage<RB_Course_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 查询所有正常课程的章节信息
        /// </summary>
        /// <returns></returns>
        public List<RB_Course_ViewModel> GetCourseAllChapterCount(int groupId, int courseId)
        {
            StringBuilder sql = new StringBuilder("select CourseId,CourseName,(select Count(0) from rb_course_chapter b where b.CourseId=a.CourseId and ParentId=0 and b.`Status`=0) as UnitCount,(select Count(0) from rb_course_chapter b where b.CourseId=a.CourseId and ParentId>0 and b.`Status`=0) as ChapterCount from rb_course a where Status=0");
            sql.AppendFormat(" AND a.{0}={1} ", nameof(RB_Course_ViewModel.Group_Id), groupId);
            sql.AppendFormat(" AND a.{0}<>{1} ", nameof(RB_Course_ViewModel.CourseId), courseId);
            return Get<RB_Course_ViewModel>(sql.ToString()).ToList();
        }

        /// <summary>
        /// 获取课程的章节数量
        /// </summary>
        /// <param name="courseId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public int GetMaxChapterNum(int courseId, int group_Id)
        {
            string sql = $@"select Count(0) from rb_course_chapter b where b.Group_Id ={group_Id} and b.ParentId=0 and b.`Status`=0 and b.CourseId ={courseId}";
            var obj = ExecuteScalar(sql);
            return obj != null ? Convert.ToInt32(obj) : 0;
        }
    }
}
