using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.Grade;
using Edu.Model.ViewModel.Grade;

namespace Edu.Repository.Grade
{
    /// <summary>
    /// 上课反馈记录表
    /// </summary>
    public class RB_Class_FeedBackRepository : BaseRepository<RB_Class_FeedBack>
    {
        /// <summary>
        /// 获取班级反馈列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_FeedBack_ViewModel> GetClassFeedBackList(RB_Class_FeedBack_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"  SELECT A.*,b.TeacherName,b.TeacherHead as TeacherIcon FROM RB_Class_FeedBack AS A  LEFT JOIN rb_teacher as b on a.CreateBy=b.TId WHERE 1=1  ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.Group_Id), query.Group_Id);
                }
               
                if (query.ClassId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.ClassId), query.ClassId);
                }
                if (query.FeedBackId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.FeedBackId), query.FeedBackId);
                }
                if (query.OrderGuestId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.OrderGuestId), query.OrderGuestId);
                }
                if (!string.IsNullOrEmpty(query.Q_ClassIds))
                {
                    builder.AppendFormat(" AND A.{0} in({1}) ", nameof(RB_Class_FeedBack_ViewModel.ClassId), query.Q_ClassIds);
                }
                if (!string.IsNullOrEmpty(query.Q_ClassTimeIds))
                {
                    builder.AppendFormat(" AND A.{0} in({1}) ", nameof(RB_Class_FeedBack_ViewModel.ClassTimeId), query.Q_ClassTimeIds);
                }
                if (query.ClassTimeId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.ClassTimeId), query.ClassTimeId);
                }
                if (query.ClassPlanId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.ClassPlanId), query.ClassPlanId);
                }
                if (!string.IsNullOrWhiteSpace(query.Q_ClassTimeIds))
                {
                    builder.AppendFormat(" AND A.{0} in ({1}) ", nameof(RB_Class_FeedBack_ViewModel.ClassTimeId), query.Q_ClassTimeIds);
                }
            }
            return Get<RB_Class_FeedBack_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_Class_FeedBack_ViewModel> GetClassFeedBackPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_FeedBack_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*,b.TeacherName,b.TeacherHead as TeacherIcon,plan.ClassDate,time.StartTime,time.EndTime,IFNULL(course.CourseName,'') AS CourseName
FROM RB_Class_FeedBack AS A  LEFT JOIN rb_teacher as b on a.TeacherId=b.TId 
     LEFT JOIN rb_student_orderguest as sog on sog.GuestId=a.OrderGuestId
     LEFT JOIN rb_student as s on s.StuId = sog.Student_Id 
     LEFT JOIN rb_class_plan AS plan ON A.ClassPlanId=plan.ClassPlanId
     LEFT JOIN rb_class_time AS time ON A.ClassTimeId=time.ClassTimeId
LEFT JOIN(
			SELECT guest.Id,course.CourseName
			FROM rb_order_guest AS guest INNER JOIN rb_order AS orderTab ON guest.OrderId=orderTab.OrderId
			LEFT JOIN rb_course AS course ON orderTab.CourseId=course.CourseId
         ) AS course ON A.OrderGuestId=course.Id
WHERE  1=1 AND a.`Status`=0 AND sog.`Status`=0 AND s.`Status`=0
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.ClassTimeId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.ClassTimeId), query.ClassTimeId);
                }
                if (query.ClassId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.ClassId), query.ClassId);
                }
                if (query.OrderGuestId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.OrderGuestId), query.OrderGuestId);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND s.{0}={1} ", nameof(RB_Class_FeedBack_ViewModel.StuId), query.StuId);
                }
            }

            builder.AppendFormat("  ORDER BY a.CreateTime desc ");
            var list= GetPage<RB_Class_FeedBack_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
            return list;
        }

    }
}
