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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 学员退课单据仓储层
    /// </summary>
    public class RB_Student_BackClassRepository : BaseRepository<RB_Student_BackClass>
    {
        /// <summary>
        /// 获取学员退课单据分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_BackClass_ViewModel> GetStudentBackClassPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Student_BackClass_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            string isSpecNode = "";
            if (query.Conditon == Common.Enum.Course.BackClassConditionEnum.WaitAgree)
            {
                isSpecNode = string.Format(",IFNULL((SELECT SpecialNode FROM rb_student_backrelevance WHERE Id IN(SELECT BackrelevanceId FROM rb_student_backrecord WHERE ConditionId=A.BackId AND AuditStatus=1 AND AuditEmId={0})),0) AS SpecialNode ", query.CreateBy);
            }
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.GuestName,'') AS GuestName,IFNULL(C.SName,'') AS SchoolName,IFNULL(D.ClassName,'') AS ClassName,IFNULL(T.TeacherName,'') AS TeacherName
      ,IFNULL(E.EnterID,0) AS EnterID,IFNULL(F.CourseName,'') AS CourseName {0}
FROM RB_Student_BackClass AS A LEFT JOIN rb_order_guest AS B ON A.GuestId=B.Id
     LEFT JOIN rb_school AS C ON A.SchoolId=C.SId
     LEFT JOIN rb_class AS D ON A.ClassId=D.ClassId
     LEFT JOIN (SELECT account.Id,teac.TeacherName FROM rb_account AS account INNER JOIN rb_teacher AS teac ON account.AccountId=teac.TId AND account.AccountType=2)
               AS T ON A.TeacherId=T.Id
     LEFT JOIN rb_order AS E ON A.OrderId=E.OrderId
     LEFT JOIN rb_course AS F ON D.CouseId=F.CourseId
WHERE 1=1  ", isSpecNode);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Student_BackClass_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.ClassName))
                {
                    builder.AppendFormat(@" AND (B.{0} LIKE @ClassName OR D.{1} @ClassName OR T.{2} LIKE @ClassName) ",
                        nameof(RB_Student_BackClass_ViewModel.GuestName), nameof(RB_Student_BackClass_ViewModel.ClassName), nameof(RB_Student_BackClass_ViewModel.TeacherName));
                    parameters.Add("ClassName", "%" + query.ClassName.Trim() + "%");
                }
                if (query.AuditStatus > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Student_BackClass_ViewModel.AuditStatus), (int)query.AuditStatus);
                }
                if (!string.IsNullOrEmpty(query.QStartTime))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Student_BackClass_ViewModel.CreateTime), query.QStartTime);
                }
                if (!string.IsNullOrEmpty(query.QEndTime))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Student_BackClass_ViewModel.CreateTime), query.QEndTime);
                }
                if (query.Conditon != null && query.Conditon>0)
                {
                    switch (query.Conditon)
                    {
                        //待我审批
                        case Common.Enum.Course.BackClassConditionEnum.WaitAgree:
                            builder.AppendFormat(" AND EXISTS (SELECT 1 FROM rb_student_backrecord WHERE ConditionId=A.BackId AND AuditStatus=1 AND AuditEmId={0}) ", query.CreateBy);
                            break;
                        //已审批的
                        case Common.Enum.Course.BackClassConditionEnum.Agree:
                            builder.AppendFormat(" AND EXISTS (SELECT 1 FROM rb_student_backrecord WHERE ConditionId=A.BackId AND AuditStatus IN(2,5) AND AuditEmId={0}) ", query.CreateBy);
                            break;
                        //我发起的
                        case Common.Enum.Course.BackClassConditionEnum.Launch:
                            builder.AppendFormat(" AND A.CreateBy={0} ", query.CreateBy);
                            break;
                        //抄送给我的
                        case Common.Enum.Course.BackClassConditionEnum.CarbonCopy:
                            builder.AppendFormat(" AND FIND_IN_SET({0},A.RecipientIds) ", query.CreateBy);
                            break;
                    }
                }
            }
            builder.AppendFormat(" ORDER BY A.CreateTime DESC ");
            return GetPage<RB_Student_BackClass_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取总退课条数
        /// </summary>
        /// <returns></returns>
        public int GetStudentBackClassNumRepository()
        {
            int result = 0;
            string sql = "SELECT COUNT(1) FROM RB_Student_BackClass ";
            var obj = base.ExecuteScalar(sql);
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result + 1;
        }
    }
}