using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.EduTask;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.EduTask;
using Edu.Model.ViewModel.Grade;
using Edu.Model.ViewModel.Sell;
using Edu.Repository.Grade;
using Edu.Repository.Sell;
using VT.FW.DB;

namespace Edu.Repository.EduTask
{
    /// <summary>
    /// 临时上课邀请申请仓储
    /// </summary>
    public class RB_Student_TempInvitationRepository : BaseRepository<RB_Student_TempInvitation>
    {
        /// <summary>
        /// 教务单据仓储层对象
        /// </summary>
        private readonly RB_Education_ReceiptRepository education_ReceiptRepository = new RB_Education_ReceiptRepository();
        /// <summary>
        /// 临时上课邀请记录
        /// </summary>
        private readonly RB_Temporary_InvitationRepository temporaryInvitationRepository = new RB_Temporary_InvitationRepository();

        /// <summary>
        /// 订单学员仓储层对象
        /// </summary>
        private readonly RB_Order_GuestRepository order_GuestRepository = new RB_Order_GuestRepository();

        /// <summary>
        /// 班级上课计划仓储层对象
        /// </summary>
        private readonly RB_Class_PlanRepository class_PlanRepository = new RB_Class_PlanRepository();

        /// <summary>
        /// 班级上课计划上课时间仓储层对象
        /// </summary>
        private readonly RB_Class_TimeRepository class_TimeRepository = new RB_Class_TimeRepository();

        /// <summary>
        /// 获取临时上课邀请申请列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_TempInvitation_ViewModel> GetStundetTempInvitationListRepository(RB_Student_TempInvitation_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT A.*,IFNULL(B.ClassName,'') AS ClassName
FROM RB_Student_TempInvitation AS A LEFT JOIN rb_class AS B ON A.ClassId=B.ClassId
WHERE 1=1
");
            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.Q_TempInvitation_Ids))
                {
                    builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Student_TempInvitation_ViewModel.Id), query.Q_TempInvitation_Ids);
                }
            }
            return Get<RB_Student_TempInvitation_ViewModel>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 审核通过后更新临时上课记录表
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool UpdateStundetTempInvitation(object Id)
        {
            //查询当前调课信息
            bool flag = false;
            var receiptModel = education_ReceiptRepository.GetEntity(Id);
            if (receiptModel == null || receiptModel.Id == 0)
            {
                return false;
            }
            if (receiptModel.ReceiptType != Common.Enum.Finance.ReceiptTypeEnum.InvitationClass)
            {
                return false;
            }
            var model = GetEntity(receiptModel.RelationId);
            if (model == null || model.Id == 0)
            {
                return false;
            }
            else //更新学生的信息
            {
                //学生信息
                List<RB_Order_Guest_ViewModel> orderGuestList = new List<RB_Order_Guest_ViewModel>();

                //班级上课计划列表
                List<RB_Class_Plan_ViewModel> planList = new List<RB_Class_Plan_ViewModel>();
                //班级上课时间列表
                List<RB_Class_Time_ViewModel> timeList = new List<RB_Class_Time_ViewModel>();

                string orderGuestIds = model.OrderGuestIds;
                if (!string.IsNullOrWhiteSpace(orderGuestIds))
                {
                    orderGuestList = order_GuestRepository.GetStopingStudentPage(new RB_Order_Guest_ViewModel
                    {
                        OrderGuestIds = orderGuestIds
                    });
                }

                string classTimeIds = model.ClassTimeIds;
                if (!string.IsNullOrWhiteSpace(classTimeIds))
                {
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel { QClassTimeIds = classTimeIds });
                    if (timeList != null && timeList.Any())
                    {
                        string classPlanIds = string.Join(",", timeList.Select(x => x.ClassPlanId));

                        if (!string.IsNullOrWhiteSpace(classPlanIds))
                        {
                            planList = class_PlanRepository.GetClassPlanListRepository(new RB_Class_Plan_ViewModel { QClassPlanIds = classPlanIds });
                        }
                    }
                }

                List<RB_Temporary_Invitation> tempInvitationList = new List<RB_Temporary_Invitation>();

                foreach (var item in timeList)
                {
                    foreach (var itemGuest in orderGuestList)
                    {
                        RB_Temporary_Invitation invitationModel = new RB_Temporary_Invitation()
                        {
                            Id = 0,
                            ClassId = model.ClassId,
                            Group_Id = model.Group_Id,
                            School_Id = model.School_Id,
                            CourseId = model.CourseId,
                            ClassPlanId = item.ClassPlanId,
                            ClassTimeId = item.ClassTimeId,
                            OrderGuestId = itemGuest.Id,
                            CreateBy = model.CreateBy,
                            CreateTime = DateTime.Now
                        };
                        tempInvitationList.Add(invitationModel);
                    }
                }
                if (tempInvitationList!=null&& tempInvitationList.Any())
                {
                    flag = temporaryInvitationRepository.InsertBatch(tempInvitationList);
                }
            }

            return flag;
        }
    }
}
