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

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.Group_Id), query.Group_Id);
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                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);
                    }
                }
            }
            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
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.Group_Id), query.Group_Id);
            //builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_ViewModel.School_Id), query.School_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);
                    }
                }
            }
           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();
        }
    }
}
