using Edu.Model.Entity.Course;
using Edu.Model.Entity.EduTask;
using Edu.Model.ViewModel.EduTask;
using Edu.Repository.Course;
using Edu.Repository.EduTask;
using System.Collections.Generic;
using System.Linq;
using VT.FW.AOP.CustomerAttribute;
using VT.FW.DB;

namespace Edu.Module.EduTask
{
    /// <summary>
    /// 调课计划处理类
    /// </summary>
    public class StopStudentClassModule
    {
        /// <summary>
        /// 教务单据仓储层对象
        /// </summary>
        private readonly RB_Education_ReceiptRepository education_ReceiptRepository = new RB_Education_ReceiptRepository();

        /// <summary>
        /// 停课计划仓储层对象
        /// </summary>
        private readonly RB_Student_StopClassRepository student_StopClassRepository = new RB_Student_StopClassRepository();

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

        /// <summary>
        /// 获取调课计划列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_StopClass_ViewModel> GetStudentStopClassListRepository(RB_Student_StopClass_ViewModel query)
        {
            return student_StopClassRepository.GetStudentStopClassListRepository(query);
        }


        /// <summary>
        /// 定时处理停课申请生效日期以及学员的上课状态
        /// </summary>
        /// <returns></returns>
        public bool UpdateAuditThroughStudent()
        {
            var list = order_GuestRepository.GetOrderStopClassing(new Model.ViewModel.Course.RB_Order_Guest_ViewModel { GuestState = 6 });
            foreach (var item in list.Where(x => x.StopClassEffectTime.HasValue && x.StopClassEffectTime < System.DateTime.Now))
            {
                //将学员的学习状态变成停课申请中
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Order_Guest.GuestState),5},
                    {nameof(RB_Order_Guest.ChangeEffectTime),item.StopClassEffectTime},
                    {nameof(RB_Order_Guest.UpdateTime),System.DateTime.Now},
                };
                order_GuestRepository.Update(fileds, new WhereHelper(nameof(RB_Order_Guest.Id), item.Id));
            }
            return true;
        }


        /// <summary>
        /// 新增修改停课申请
        /// </summary>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetStudentStopClassModule(RB_Student_StopClass_ViewModel extModel)
        {
            bool flag = false;
            if (extModel.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Student_StopClass_ViewModel.OrderGuestId),extModel.OrderGuestId },
                    {nameof(RB_Student_StopClass_ViewModel.ClassId),extModel.ClassId },
                     {nameof(RB_Student_StopClass_ViewModel.CourseId),extModel.CourseId },
                      {nameof(RB_Student_StopClass_ViewModel.TakeEffectTime),extModel.TakeEffectTime },
                    {nameof(RB_Student_StopClass_ViewModel.UpdateBy),extModel.UpdateBy },
                    {nameof(RB_Student_StopClass_ViewModel.UpdateTime),extModel.UpdateTime },
                };
                flag = student_StopClassRepository.Update(fileds, new WhereHelper(nameof(RB_Student_StopClass_ViewModel.Id), extModel.Id));
            }
            else
            {
                var newId = student_StopClassRepository.Insert(extModel);
                extModel.Id = newId;
                flag = newId > 0;
                if (flag)
                {
                    //将学员的学习状态变成停课申请中

                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Order_Guest.GuestState),6},

                };
                    flag = order_GuestRepository.Update(fileds, new WhereHelper(nameof(RB_Order_Guest.Id), extModel.OrderGuestId));
                    var educationReceipt = new RB_Education_Receipt()
                    {
                        Id = 0,
                        Title = "停课申请",
                        ReceiptType = Common.Enum.Finance.ReceiptTypeEnum.StopClass,
                        RelationId = newId,
                        ClassId = extModel.ClassId,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        CreateBy = extModel.CreateBy,
                        CreateTime = extModel.CreateTime,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        VerifyStatus = Common.Enum.EduTask.EduTaskRrocessStatus.NotAudit
                    };
                    flag = education_ReceiptRepository.SetEducationReceiptRepository(educationReceipt, out string message);
                }
            }
            return flag;
        }
    }
}
