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;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Course
{
    /// <summary>
    /// 学员补课仓储层
    /// </summary>
    public class RB_Student_MakeupRepository:BaseRepository<RB_Student_Makeup>
    {
        /// <summary>
        /// 获取学员补课列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Makeup_ViewModel> GetStudentMakeupPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Student_Makeup_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,C.GuestName, D.ClassName,E.CourseName,F.TeacherName,G.RoomName,C.GuestState,H.StartTime,H.EndTime
FROM RB_Student_Makeup AS A LEFT JOIN rb_class_check AS B ON A.ClassCheckId=B.ClassCheckId
     LEFT JOIN rb_order_guest AS C ON A.OrderGuestId=C.Id
		 LEFT JOIN rb_class AS D ON A.ClassId=D.ClassId
		 LEFT JOIN rb_course AS E ON D.CouseId=E.CourseId
		 LEFT JOIN rb_teacher AS F ON B.TeacherId=F.TId
		 LEFT JOIN rb_class_room AS G ON B.ClassRoomId=G.RoomId
		 LEFT JOIN rb_class_time AS H ON B.ClassTimeId=H.ClassTimeId
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Makeup_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.GuestName))
                {
                    builder.AppendFormat(" AND C.{0} LIKE @GuestName ", nameof(RB_Student_Makeup_ViewModel.GuestName));
                    parameters.Add("GuestName", "%" + query.GuestName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.ClassName))
                {
                    builder.AppendFormat(" AND D.{0} LIKE @ClassName ", nameof(RB_Student_Makeup_ViewModel.ClassName));
                    parameters.Add("ClassName", "%" + query.ClassName.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.CourseName))
                {
                    builder.AppendFormat(" AND E.{0} LIKE @CourseName ", nameof(RB_Student_Makeup_ViewModel.CourseName));
                    parameters.Add("CourseName", "%" + query.CourseName.Trim() + "%");
                }
                if (query.MakeUpStatus>0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Makeup_ViewModel.MakeUpStatus),query.MakeUpStatus);
                }
                if (!string.IsNullOrEmpty(query.StartDate))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Student_Makeup_ViewModel.ClassDate), query.StartDate);
                }
                if (!string.IsNullOrEmpty(query.EndDate))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Student_Makeup_ViewModel.ClassDate), query.EndDate);
                }
            }
            builder.Append(" ORDER BY A.ClassId,A.ClassDate,A.OrderGuestId ");
            return GetPage<RB_Student_Makeup_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 新增修改学员补课信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetStudentMakeUpRepository(RB_Student_Makeup model)
        {
            bool flag = false;
            var newModel = new RB_Student_Makeup()
            {
                Id = 0,
                ClassCheckId = model.ClassCheckId,
                ClassDate = model.ClassDate,
                ClassId = model.ClassId,
                CreateBy = model.CreateBy,
                CreateTime = model.CreateTime,
                Group_Id = model.Group_Id,
                MakeUpStatus = 3,
                OrderGuestId = model.OrderGuestId
            };
            newModel.Id = CheckStudentMakeUpExistsRepository(model);
            if (newModel.Id <= 0)
            {
                flag = base.Insert(newModel) > 0;
            }
            else
            {
                flag = base.Update(newModel);
            }
            return flag;
        }

        /// <summary>
        /// 检查数据是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int CheckStudentMakeUpExistsRepository(RB_Student_Makeup model)
        {
            List<WhereHelper> whereHelpers = new List<WhereHelper>
            {
                new WhereHelper(nameof(RB_Student_Makeup.ClassCheckId), model.ClassCheckId),
                new WhereHelper(nameof(RB_Student_Makeup.OrderGuestId), model.OrderGuestId)
            };
            return base.Exists("Id", whereHelpers);
        }
    }
}
