using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum;
using Edu.Common.Enum.Course;
using Edu.Model.CacheModel;
using Edu.Model.Entity.Course;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Reserve;
using Edu.Model.ViewModel.User;
using Edu.Repository.Course;
using Edu.Repository.Finance;
using Edu.Repository.Log;
using Edu.Repository.Reserve;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VT.FW.DB;

namespace Edu.Module.Course
{
    /// <summary>
    /// 班级管理处理类
    /// </summary>
    public class ClassModule
    {
        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        /// <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>
        private readonly RB_Class_TypeRepository class_TypeRepository = new RB_Class_TypeRepository();

        /// <summary>
        /// 班级价格仓储层对象
        /// </summary>
        private readonly RB_Class_StepPriceRepository class_StepPriceRepository = new RB_Class_StepPriceRepository();

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

        /// <summary>
        /// 课程仓储层对象
        /// </summary>
        private readonly RB_CourseRepository courseRepository = new RB_CourseRepository();

        /// <summary>
        /// 订单仓储层对象
        /// </summary>
        private readonly RB_OrderRepository orderRepository = new RB_OrderRepository();
        /// <summary>
        /// 课程日志记录表
        /// </summary>
        private readonly RB_Class_LogRepository classLogRepository = new RB_Class_LogRepository();
        /// <summary>
        /// 课程签到记录表
        /// </summary>
        private readonly RB_Class_CheckRepository classCheckRepository = new RB_Class_CheckRepository();

        /// <summary>
        /// 课程日志记录表
        /// </summary>
        private readonly RB_StudentRepository studentRepository = new RB_StudentRepository();

        /// <summary>
        /// 教室仓储层对象
        /// </summary>
        private readonly RB_Class_RoomRepository class_RoomRepository = new RB_Class_RoomRepository();

        /// <summary>
        /// 教师仓储层对象
        /// </summary>
        private readonly RB_TeacherRepository teacherRepository = new RB_TeacherRepository();

        /// <summary>
        /// 开启班级收支仓储层对象
        /// </summary>
        private readonly RB_Class_FinanceRepository classFinanceRepository = new RB_Class_FinanceRepository();

        /// <summary>
        /// 课程基础配置
        /// </summary>
        private readonly RB_Class_ConfigRepository class_ConfigRepository = new RB_Class_ConfigRepository();

        /// <summary>
        /// 教育月结轧账
        /// </summary>
        private readonly RB_RollingAccountRepository rollingAccountRepository = new RB_RollingAccountRepository();

        /// <summary>
        /// 课程基础配置
        /// </summary>
        private readonly RB_Class_LessonPlanDetailsRepository ClassLessonPlanDetailsRepository = new RB_Class_LessonPlanDetailsRepository();

        /// <summary>
        /// 课程基础配置
        /// </summary>
        private readonly RB_Class_LessonPlanProjectsRepository ClassLessonPlanProjectsRepository = new RB_Class_LessonPlanProjectsRepository();

        /// <summary>
        /// 班级备案评论仓储层对象
        /// </summary>
        private readonly RB_Class_LessonCommentRepository classLessonCommentRepository = new RB_Class_LessonCommentRepository();

        /// <summary>
        /// 课程基础配置
        /// </summary>
        private readonly RB_Class_LessonPlanRepository ClassLessonPlanRepository = new RB_Class_LessonPlanRepository();

        /// <summary>
        /// 班级关联课程仓储层对象
        /// </summary>
        private readonly RB_Class_CourseRepository class_CourseRepository = new RB_Class_CourseRepository();

        /// <summary>
        /// 约课仓储层对象
        /// </summary>
        private readonly RB_Reserve_ClassRepository reserve_ClassRepository = new RB_Reserve_ClassRepository();

        /// <summary>
        /// 访客预约仓储层对象
        /// </summary>
        private readonly RB_Visitor_ReserveRepository visitor_ReserveRepository = new RB_Visitor_ReserveRepository();


        /// <summary>
        /// 教室异常仓储对象
        /// </summary>
        private readonly RB_Class_RoomUseLogRepository class_RoomUseLogRepository = new RB_Class_RoomUseLogRepository();

        /// <summary>
        /// 学员补课记录表仓储层对象
        /// </summary>
        private readonly RB_Student_MakeupRepository student_MakeupRepository = new RB_Student_MakeupRepository();

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassListModule(RB_Class_ViewModel query)
        {
            var list = classRepository.GetClassListRepository(query);
            if (list != null && list.Count > 0)
            {
                string classIds = string.Join(",", list.Select(qitem => qitem.ClassId));
                List<RB_Class_Plan_ViewModel> planList = new List<RB_Class_Plan_ViewModel>();
                List<RB_Class_Time_ViewModel> timeList = new List<RB_Class_Time_ViewModel>();
                //查询剩余的上课计划
                if (query.IsQuerySurplusPlan == 1)
                {
                    planList = class_PlanRepository.GetClassPlanListRepository(new RB_Class_Plan_ViewModel()
                    {
                        QClassIds = classIds,
                        StartTime = Common.ConvertHelper.FormatDate(DateTime.Now)
                    });
                }

                if (planList != null && planList.Count > 0)
                {
                    string planIds = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel()
                    {
                        QClassIds = classIds,
                        QClassPlanIds = planIds,
                    });
                }
                foreach (var item in list)
                {
                    var tempPlanList = planList?.Where(qitem => qitem.ClassId == item.ClassId)?.OrderBy(qitem => qitem.ClassDate)?.ToList();
                    if (tempPlanList != null && tempPlanList.Count > 0)
                    {
                        foreach (var sItem in tempPlanList)
                        {
                            sItem.PlanTimeList = timeList?.Where(qitem => qitem.ClassId == item.ClassId && qitem.ClassPlanId == sItem.ClassPlanId)?.OrderBy(qitem => qitem.StartTime)?.ToList() ?? new List<RB_Class_Time_ViewModel>();
                        }
                    }
                    item.ClassPlanList = tempPlanList ?? new List<RB_Class_Plan_ViewModel>();
                }
            }
            return list;
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassListForProperty(RB_Class_ViewModel query)
        {
            return classRepository.GetClassListForProperty(query);
        }

        /// <summary>
        /// 获取班级课程名称以及老师信息列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassAndCourseListRepository(RB_Class_ViewModel query)
        {
            return classRepository.GetClassAndCourseListRepository(query);
        }

        /// <summary>
        /// 获取班级上课时间列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Time_ViewModel> GetClassTimeListModule(RB_Class_Time_ViewModel query)
        {
            return class_TimeRepository.GetClassTimeListRepository(query);
        }


        /// <summary>
        /// 获取班级上课时间列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Time_ViewModel> GetClassTimeList_V2(RB_Class_Time_ViewModel query)
        {
            return class_TimeRepository.GetClassTimeList_V2(query);
        }

        /// <summary>
        /// 获取班级上课时间列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Time_ViewModel> GetClassTimeList(RB_Class_Time_ViewModel query)
        {
            return class_TimeRepository.GetClassTimeList(query);
        }

        /// <summary>
        /// 获取班级分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <param name="isGetStepPrice">是否查询阶梯价格</param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Class_ViewModel query, bool isGetStepPrice = false)
        {
            var list = classRepository.GetClassPageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                var classTimeList = new List<RB_Class_Time_ViewModel>();
                var classOrderList = new List<RB_Order_ViewModel>();
                var stepPriceList = new List<RB_Class_StepPrice_ViewModel>();
                var otherCourseList = new List<RB_Class_Course_Extend>();
                var planList = new List<RB_Class_Plan_ViewModel>();
                string ids = string.Join(",", list.Select(qitem => qitem.ClassId));
                if (!string.IsNullOrEmpty(ids))
                {
                    classTimeList = class_TimeRepository.GetClassTimeListRepository(ids);
                    classOrderList = orderRepository.GetClassOrderPeopleNumRepository(ids);
                    otherCourseList = GetClassCourseListModule(new RB_Class_Course_Extend() { QClassIds = ids });
                    planList = class_PlanRepository.GetClassPlanMaxDataRepository(new RB_Class_Plan_ViewModel()
                    {
                        QClassIds = ids
                    });
                }
                if (isGetStepPrice)
                {
                    stepPriceList = class_StepPriceRepository.GetClassStepPriceListRepository(new RB_Class_StepPrice_ViewModel() { QClassIds = ids })?.ToList() ?? new List<RB_Class_StepPrice_ViewModel>();
                }
                //课程计划上课日期
                foreach (var item in list)
                {
                    item.NewPlanDateTime = classTimeList?.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault()?.NewPlanDateTime;
                    item.OrderStudentCount = classOrderList?.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault()?.GuestNum ?? 0;
                    if (isGetStepPrice)
                    {
                        item.ClassStepPriceList = stepPriceList?.Where(qitem => qitem.ClassId == item.ClassId)?.ToList() ?? new List<RB_Class_StepPrice_ViewModel>();
                    }
                    item.OtherCourseList = otherCourseList?.Where(qitem => qitem.ClassId == item.ClassId)?.ToList() ?? new List<RB_Class_Course_Extend>();
                    var tempData = planList.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault();
                    if (tempData != null)
                    {
                        item.EndClassDate = tempData.ClassDate;
                    }
                    item.FinishTimeStr = Common.ConvertHelper.FormatDate(item.EndClassDate);
                }
            }
            return list;
        }


        /// <summary>
        /// 获取班级日志分页列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Class_Log_ViewModel> GetClassLogPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_Log_ViewModel query)
        {
            return classLogRepository.GetClassLogPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 新增修改班级
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetClassModule(RB_Class_ViewModel model)
        {
            bool flag = false;
            if (model.ClassId > 0)
            {
                var oldModel = GetClassModule(model.ClassId);
                if (oldModel.Teacher_Id != model.Teacher_Id && model.Teacher_Id > 0 && oldModel.Teacher_Id > 0)
                {
                    class_PlanRepository.UpdatePlanTeacherIdRepository(model.Teacher_Id, model.ClassId);
                }
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Class_ViewModel.ClassName),model.ClassName},
                    { nameof(RB_Class_ViewModel.CouseId),model.CouseId},
                    { nameof(RB_Class_ViewModel.Teacher_Id),model.Teacher_Id},
                    { nameof(RB_Class_ViewModel.Assist_Id),model.Assist_Id},
                    { nameof(RB_Class_ViewModel.UpdateBy),model.UpdateBy},
                    { nameof(RB_Class_ViewModel.UpdateTime),model.UpdateTime},
                    { nameof(RB_Class_ViewModel.ClassPersion),model.ClassPersion},
                    { nameof(RB_Class_ViewModel.OpenTime),model.OpenTime},
                    { nameof(RB_Class_ViewModel.EndOrderTime),model.EndOrderTime},
                    { nameof(RB_Class_ViewModel.OriginalPrice),model.OriginalPrice},
                    { nameof(RB_Class_ViewModel.SellPrice),model.SellPrice},
                    { nameof(RB_Class_ViewModel.IsStepPrice),model.IsStepPrice},
                    { nameof(RB_Class_ViewModel.ClassRoomId),model.ClassRoomId},
                    { nameof(RB_Class_ViewModel.IsOpenCommission),model.IsOpenCommission},
                    { nameof(RB_Class_ViewModel.CommissionType),model.CommissionType},
                    { nameof(RB_Class_ViewModel.CommissionValue),model.CommissionValue},
                    { nameof(RB_Class_ViewModel.ClassHours),model.ClassHours},
                    { nameof(RB_Class_ViewModel.ClassStyle),model.ClassStyle},
                    { nameof(RB_Class_ViewModel.InnerRemark),model.InnerRemark},
                    { nameof(RB_Class_ViewModel.ClassType),model.ClassType},
                    { nameof(RB_Class_ViewModel.DefaultTimeJson),model.DefaultTimeJson},
                    { nameof(RB_Class_ViewModel.DateJson),model.DateJson},
                    { nameof(RB_Class_ViewModel.School_Id),model.School_Id},
                    { nameof(RB_Class_ViewModel.IsSubscribe),model.IsSubscribe},
                    { nameof(RB_Class_ViewModel.IsDeduction),model.IsDeduction},
                    { nameof(RB_Class_ViewModel.Point),model.Point},
                    { nameof(RB_Class_ViewModel.CourseClassType),model.CourseClassType},
                    { nameof(RB_Class_ViewModel.StudentNumType),model.StudentNumType},
                    { nameof(RB_Class_ViewModel.ClassHourMinute),model.ClassHourMinute},
                };
                if (oldModel.OpenTime != model.OpenTime || oldModel.ClassType != model.ClassType || string.IsNullOrEmpty(oldModel.ClassNo))
                {
                    if (oldModel.OpenTime != model.OpenTime || string.IsNullOrEmpty(oldModel.ClassNo))
                    {
                        #region 生成班级号
                        model.ClassNo = "";
                        var classTypeModel = class_TypeRepository.GetEntity(model.ClassType);
                        if (classTypeModel != null)
                        {
                            model.ClassNo = classTypeModel.ClassNoPrefix;
                        }
                        //查询最大的字母
                        int CNum = classRepository.GetMaxClassNoPrefix(model.OpenTime.ToString("yyyy-MM-dd"));
                        if (CNum == 0)
                        {
                            model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + "A";
                            model.ClassLetterNum = 1;
                        }
                        else
                        {
                            string ClassNoLetterStr = Edu.Common.Config.ClassNoLetterStr;
                            model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + ClassNoLetterStr.Substring(CNum, 1);
                            model.ClassLetterNum = CNum + 1;
                        }
                        #endregion
                        fileds.Add(nameof(RB_Class_ViewModel.ClassNo), model.ClassNo);
                        fileds.Add(nameof(RB_Class_ViewModel.ClassLetterNum), model.ClassLetterNum);
                    }
                    else if (oldModel.ClassType != model.ClassType)
                    {
                        var classTypeModel = class_TypeRepository.GetEntity(model.ClassType);
                        if (classTypeModel != null)
                        {
                            model.ClassNo = classTypeModel.ClassNoPrefix;
                        }
                        model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + oldModel.ClassNo.Substring(oldModel.ClassNo.Length - 1, 1);
                        fileds.Add(nameof(RB_Class_ViewModel.ClassNo), model.ClassNo);
                    }
                }
                flag = classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), model.ClassId));
            }
            else
            {
                #region 生成班级号
                model.ClassNo = "";
                var classTypeModel = class_TypeRepository.GetEntity(model.ClassType);
                if (classTypeModel != null)
                {
                    model.ClassNo = classTypeModel.ClassNoPrefix;
                }
                //查询最大的字母
                int CNum = classRepository.GetMaxClassNoPrefix(model.OpenTime.ToString("yyyy-MM-dd"));
                if (CNum == 0)
                {
                    model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + "A";
                    model.ClassLetterNum = 1;
                }
                else
                {
                    string ClassNoLetterStr = Edu.Common.Config.ClassNoLetterStr;
                    model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + ClassNoLetterStr.Substring(CNum, 1);
                    model.ClassLetterNum = CNum + 1;
                }
                #endregion
                var newId = classRepository.Insert(model);
                model.ClassId = newId;
                flag = newId > 0;
                var classLogModel = new Model.Entity.Log.RB_Class_Log()
                {
                    LogType = Common.Enum.Course.ClassLogTypeEnum.CreateClass,
                    LogContent = "创建班级",
                    ClassId = newId,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id,
                    CreateBy = model.CreateBy,
                    CreateTime = System.DateTime.Now
                };
                classLogRepository.AddClassLogRepository(classLogModel);
            }

            #region 阶梯报价
            //未开启阶梯价格
            if (model.IsStepPrice == 0)
            {
                class_StepPriceRepository.DeleteClassStepPriceRepository(model.ClassId);
            }
            else//开启阶梯价格
            {
                var oldStepPriceList = class_StepPriceRepository.GetClassStepPriceListRepository(new RB_Class_StepPrice_ViewModel() { ClassId = model.ClassId });
                //以前没有阶梯报价信息【直接新增】
                if (oldStepPriceList == null || (oldStepPriceList != null && oldStepPriceList.Count == 0))
                {
                    if (model.ClassStepPriceList != null && model.ClassStepPriceList.Count > 0)
                    {
                        foreach (var item in model.ClassStepPriceList)
                        {
                            item.ClassStepPriceId = 0;
                            item.ClassId = model.ClassId;
                            item.Group_Id = model.Group_Id;
                            item.School_Id = model.School_Id;
                            class_StepPriceRepository.Insert(item);
                        }
                    }
                }
                else//以前有阶梯报价
                {
                    //现在没有阶梯报价了【直接删除以前的阶梯报价】
                    if (model.ClassStepPriceList == null || (model.ClassStepPriceList != null && model.ClassStepPriceList.Count == 0))
                    {
                        class_StepPriceRepository.DeleteClassStepPriceRepository(model.ClassId);
                    }
                    //找出差异的数据
                    var deleteList = oldStepPriceList.Where(qitem => !model.ClassStepPriceList.Any(oldItem => qitem.ClassStepPriceId == oldItem.ClassStepPriceId)).ToList();
                    foreach (var dItem in deleteList)
                    {
                        if (dItem.ClassStepPriceId > 0)
                        {
                            class_StepPriceRepository.Delete(dItem.ClassStepPriceId);
                        }
                    }
                    foreach (var priceItem in model.ClassStepPriceList)
                    {
                        priceItem.ClassId = model.ClassId;
                        priceItem.Group_Id = model.Group_Id;
                        priceItem.School_Id = model.School_Id;
                        if (priceItem.ClassStepPriceId == 0)
                        {
                            class_StepPriceRepository.Insert(priceItem);
                        }
                        else
                        {
                            class_StepPriceRepository.Update(priceItem);
                        }
                    }
                }
            }
            #endregion

            #region 班级计划
            //默认上课时间
            var defaultPlanTimeList = new List<ClassTimeItem>();
            var BasicMinutes = class_ConfigRepository.GetBasicMinutesRepository(model.Group_Id);
            foreach (var tItem in model.DefaultTimeList)
            {
                ClassTimeItem tModel = new ClassTimeItem()
                {
                    DateList = new List<string>(),
                    TimeList = new List<TimeItem>()
                };
                tModel.DateList = tItem.DateList;
                foreach (var subItem in tItem.TimeList)
                {
                    var startDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + subItem.StartTime + ":00");
                    var endDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + subItem.EndTime + ":00");
                    TimeSpan span = endDate.Subtract(startDate);
                    var timeModel = new TimeItem()
                    {
                        StartTime = subItem.StartTime,
                        EndTime = subItem.EndTime,
                        TimeHour = subItem.TimeHour,
                    };
                    if (timeModel.TimeHour <= 0)
                    {
                        timeModel.TimeHour = Convert.ToDecimal(span.TotalMinutes / BasicMinutes);
                    }
                    tModel.TimeList.Add(timeModel);
                    defaultPlanTimeList.Add(tModel);
                }
            }

            //班级上课计划列表
            List<RB_Class_Plan_ViewModel> classPlanList = CreateClassPlanList(model, defaultPlanTimeList, BasicMinutes);
            model.ClassPlanList = classPlanList;
            AddClassPlanModule(model);
            #endregion

            return flag;
        }

        /// <summary>
        /// 添加上课计划
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public void AddClassPlanModule(RB_Class_ViewModel model)
        {
            Task.Run(() =>
            {
                if (model.ClassPlanList != null && model.ClassPlanList.Count > 0)
                {
                    foreach (var item in model.ClassPlanList)
                    {
                        item.ClassPlanId = 0;
                        if (!class_PlanRepository.CheckExistsRepository(item))
                        {
                            //添加班级上课计划
                            var newClassPlanId = class_PlanRepository.Insert(item);
                            if (item.PlanTimeList != null && item.PlanTimeList.Count > 0)
                            {
                                foreach (var subItem in item.PlanTimeList)
                                {
                                    subItem.ClassTimeId = 0;
                                    subItem.ClassPlanId = newClassPlanId;
                                    subItem.ClassId = model.ClassId;
                                    subItem.Group_Id = model.Group_Id;
                                    subItem.School_Id = model.School_Id;
                                    //添加班级上课计划的上课时间
                                    class_TimeRepository.Insert(subItem);
                                }
                            }
                        }
                    }
                }
            });
        }

        /// <summary>
        /// 修改班级状态
        /// </summary>
        /// <param name="ClassStatus"></param>
        /// <param name="classId"></param>
        /// <returns></returns>
        public virtual bool SetClassStatusModule(ClassStatusEnum ClassStatus, int classId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
               { nameof(RB_Class_ViewModel.ClassStatus),(int)ClassStatus},
            };
            return classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), classId));
        }

        /// <summary>
        /// 更新班级完成进度
        /// </summary>
        /// <param name="classId">班级Id</param>
        /// <returns></returns>
        public virtual bool SetClassProcessModule(int classId)
        {
            int CompleteProgress = 0;//进度数
            var classModel = classRepository.GetEntity(classId);
            //班级完成的总分钟数
            var FinishMinutes = classCheckRepository.GetClassFinishMinutesRepository(classId.ToString())?.FirstOrDefault()?.FinishMinutes ?? 0;
            if (FinishMinutes > 0)
            {
                //基础课时分钟数
                var BasicMinutes = class_ConfigRepository.GetClassMinutesRepository(classId);
                //完成课时数
                var finishHours = FinishMinutes / BasicMinutes;
                if (classModel.ClassHours > 0)
                {
                    CompleteProgress = Convert.ToInt32(finishHours / Convert.ToDecimal(classModel.ClassHours) * 100);
                }
                else if (classModel.ClassHours <= 0 && finishHours > 0)
                {
                    CompleteProgress = 100;
                }
            }
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
               { nameof(RB_Class_ViewModel.CompleteProgress),CompleteProgress},
            };
            return classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), classId));
        }

        /// <summary>
        /// 获取基础课时分钟数
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public int GetClassMinutesModule(int classId)
        {
            return class_ConfigRepository.GetClassMinutesRepository(classId);
        }

        /// <summary>
        /// 获取基础课时分钟数
        /// </summary>
        /// <param name="Group_Id"></param>
        /// <returns></returns>
        public int GetBasicMinutesModule(int Group_Id)
        {
            var BasicMinutes = class_ConfigRepository.GetBasicMinutesRepository(Group_Id);
            return BasicMinutes;
        }

        /// <summary>
        /// 更新班级结课日期
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public bool UpdateClassEndDateModule(int classId)
        {
            return classRepository.UpdateClassEndDateRepository(classId);
        }

        /// <summary>
        /// 生成班级上课计划表
        /// </summary>
        /// <param name="model"></param>
        /// <param name="defaultPlanTimeList"></param>
        /// <param name="BasicMinutes"></param>
        /// <returns></returns>
        private List<RB_Class_Plan_ViewModel> CreateClassPlanList(RB_Class_ViewModel model, List<ClassTimeItem> defaultPlanTimeList, int BasicMinutes)
        {
            var planList = class_PlanRepository.GetClassPlanListRepository(new RB_Class_Plan_ViewModel() { ClassId = model.ClassId, StartTime = DateTime.Now.ToString("yyyy-MM-dd") });
            if (planList != null && planList.Count > 0)
            {
                string ids = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(ids))
                {
                    var flag1 = class_TimeRepository.DeleteClassTimeByPlanIdsRepository(ids);
                    var flag2 = class_PlanRepository.DeleteClassPlanRepository(ids);
                }
            }
            List<RB_Class_Plan_ViewModel> classPlanList = new List<RB_Class_Plan_ViewModel>();
            if (model.ClassHours > 0)
            {
                var srartDate = model.OpenTime;
                //按周排课和按月排课
                if (model.ClassStyle == ClassStyleEnum.Week || model.ClassStyle == ClassStyleEnum.Month)
                {
                    int i = 0;
                    double totalHours = 0;
                    while (model.ClassHours > totalHours)
                    {
                        var newDate = srartDate.AddDays(i);
                        foreach (var item in model.WeekDayList)
                        {
                            var planModel = new RB_Class_Plan_ViewModel()
                            {
                                ClassId = model.ClassId,
                                ClassPlanId = 0,
                                ClassRoomId = model.ClassRoomId,
                                Group_Id = model.Group_Id,
                                School_Id = model.School_Id,
                                PlanTimeList = new List<RB_Class_Time_ViewModel>(),
                                TeacherId = model.Teacher_Id,
                                ClassDate = newDate
                            };
                            if (model.ClassStyle == ClassStyleEnum.Week)
                            {
                                switch (item)
                                {
                                    case "1":
                                        if (newDate.DayOfWeek == DayOfWeek.Monday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "2":
                                        if (newDate.DayOfWeek == DayOfWeek.Tuesday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "3":
                                        if (newDate.DayOfWeek == DayOfWeek.Wednesday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "4":
                                        if (newDate.DayOfWeek == DayOfWeek.Thursday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "5":
                                        if (newDate.DayOfWeek == DayOfWeek.Friday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "6":
                                        if (newDate.DayOfWeek == DayOfWeek.Saturday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                    case "7":
                                        if (newDate.DayOfWeek == DayOfWeek.Sunday)
                                        {
                                            var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                            if (timeList != null && timeList.Count > 0)
                                            {
                                                var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                                totalHours += dayHours;
                                                classPlanList.Add(planModel);
                                            }
                                        }
                                        break;
                                }
                            }
                            else if (model.ClassStyle == ClassStyleEnum.Month)
                            {
                                if (newDate.Day == Convert.ToInt32(item))
                                {
                                    var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                                    if (timeList != null && timeList.Count > 0)
                                    {
                                        var dayHours = CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                                        totalHours += dayHours;
                                        classPlanList.Add(planModel);
                                    }
                                }
                            }
                        }
                        i++;
                    }
                }
                //固定日期
                else if (model.ClassStyle == ClassStyleEnum.FixedDate)
                {
                    foreach (var item in model.WeekDayList)
                    {
                        var planModel = new RB_Class_Plan_ViewModel()
                        {
                            ClassId = model.ClassId,
                            ClassPlanId = 0,
                            ClassRoomId = model.ClassRoomId,
                            Group_Id = model.Group_Id,
                            School_Id = model.School_Id,
                            PlanTimeList = new List<RB_Class_Time_ViewModel>(),
                            TeacherId = model.Teacher_Id,
                            ClassDate = Convert.ToDateTime(item)
                        };
                        var timeList = defaultPlanTimeList.Where(qitem => qitem.DateList.Contains(item))?.FirstOrDefault()?.TimeList;
                        if (timeList != null && timeList.Count > 0)
                        {
                            CalcDayClassHoursModule(timeList, planModel.PlanTimeList, model, BasicMinutes);
                            classPlanList.Add(planModel);
                        }
                    }
                }
                //约课
                else
                {

                }
            }
            return classPlanList;
        }

        /// <summary>
        /// 计算每天课时
        /// </summary>
        /// <param name="timeList"></param>
        /// <param name="classTimeList"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private double CalcDayClassHoursModule(List<TimeItem> timeList, List<RB_Class_Time_ViewModel> classTimeList, RB_Class_ViewModel model, int BasicMinutes)
        {
            double dayClassHours = 0;
            double dayMinutes = 0;
            foreach (var sItem in timeList)
            {
                var timeModel = new RB_Class_Time_ViewModel()
                {
                    StartTime = sItem.StartTime,
                    EndTime = sItem.EndTime,
                    TimeHour = sItem.TimeHour,
                };
                classTimeList.Add(timeModel);

                var startDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + sItem.StartTime + ":00");
                var endDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + sItem.EndTime + ":00");
                TimeSpan span = endDate.Subtract(startDate);
                if (span.TotalMinutes > 0)
                {
                    dayMinutes += span.TotalMinutes;
                }
            }

            if (model.ClassHourMinute > 0)
            {
                dayClassHours = dayMinutes / model.ClassHourMinute;
            }
            else
            {
                dayClassHours = dayMinutes / BasicMinutes;
            }
            return dayClassHours;
        }

        /// <summary>
        /// 根据班级编号获取班级详细信息
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public RB_Class_ViewModel GetClassModule(object ClassId)
        {
            var extModel = classRepository.GetEntity<RB_Class_ViewModel>(ClassId);
            if (extModel != null && extModel.ClassId > 0)
            {
                if (extModel.CouseId > 0)
                {
                    extModel.CourseName = courseRepository.GetEntity(extModel.CouseId)?.CourseName ?? "";
                }
                if (extModel.Teacher_Id > 0)
                {
                    extModel.TeacherName = teacherRepository.GetEntity(extModel.Teacher_Id)?.TeacherName ?? "";
                }
                //课程梯度价格
                extModel.ClassStepPriceList = class_StepPriceRepository.GetClassStepPriceListRepository(new RB_Class_StepPrice_ViewModel() { ClassId = extModel.ClassId })?.ToList() ?? new List<RB_Class_StepPrice_ViewModel>();
                //课程计划上课日期
                extModel.ClassPlanList = class_PlanRepository.GetClassPlanListRepository(new RB_Class_Plan_ViewModel() { ClassId = extModel.ClassId })?.ToList() ?? new List<RB_Class_Plan_ViewModel>();
                //班级上课计划时间列表
                var timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { ClassId = extModel.ClassId })?.ToList() ?? new List<RB_Class_Time_ViewModel>();
                if (extModel.ClassPlanList != null && extModel.ClassPlanList.Count > 0 && timeList != null && timeList.Count > 0)
                {
                    foreach (var item in extModel.ClassPlanList)
                    {
                        item.PlanTimeList = timeList?.Where(qitem => qitem.ClassPlanId == item.ClassPlanId)?.ToList() ?? new List<RB_Class_Time_ViewModel>();
                    }
                }
                if (!string.IsNullOrEmpty(extModel.DefaultTimeJson))
                {
                    extModel.DefaultTimeList = Common.Plugin.JsonHelper.DeserializeObject<List<ClassTimeItem>>(extModel.DefaultTimeJson);
                }
                if (!string.IsNullOrEmpty(extModel.DateJson) && extModel.ClassStyle != Common.Enum.Course.ClassStyleEnum.OnLine)
                {
                    extModel.WeekDayList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(extModel.DateJson);
                }
            }
            return extModel;
        }

        /// <summary>
        /// 修改班级【正常/删除】状态
        /// </summary>
        /// <param name="ClassId"></param>
        /// <param name="Status"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool RemoveClassModule(int ClassId, int Status, out string message)
        {
            message = "";
            var orderList = orderRepository.GetOrderPageRepository(1, 1, out _, new RB_Order_ViewModel()
            {
                ClassId = ClassId,
                Q_OrderState = 1
            });
            if (orderList != null && orderList.Count > 0)
            {
                message = "此班级下存在正常订单!不能删除!";
                return false;
            }
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_ViewModel.Status),Status},
            };
            return classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), ClassId));
        }

        /// <summary>
        /// 获取班级计划列表
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <param name="monthStr">月份</param>
        /// <returns></returns>
        public object GetClassPlanModule(int classId, string monthStr)
        {
            List<object> list = new List<object>();
            var timeList = new List<RB_Class_Time_ViewModel>();
            var planList = class_PlanRepository.GetClassPlanListRepository(new RB_Class_Plan_ViewModel() { ClassId = classId, QMonth = monthStr })?.OrderBy(qitem => qitem.ClassDate);
            if (planList != null && planList.Count() > 0)
            {
                string Ids = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(Ids))
                {
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassPlanIds = Ids });
                }
                var minDate = planList.Min(qitem => qitem.ClassDate);
                var maxDate = planList.Max(qitem => qitem.ClassDate);
                var checkList = classCheckRepository.GetClassCheckListRepository(new RB_Class_Check_ViewModel() { ClassId = classId, StartDate = Common.ConvertHelper.FormatDate(minDate), EndDate = Common.ConvertHelper.FormatDate(maxDate) });
                DateTime today = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                foreach (var item in planList)
                {
                    List<object> classTimeList = new List<object>();
                    item.PlanTimeList = timeList?.Where(qitem => qitem.ClassPlanId == item.ClassPlanId)?.OrderBy(qitem => qitem.StartTime)?.ToList();
                    if (item.PlanTimeList != null && item.PlanTimeList.Count > 0)
                    {
                        foreach (var subItem in item.PlanTimeList)
                        {
                            var tempCheckList = checkList?.Where(qitem => qitem.ClassId == item.ClassId && qitem.ClassDate == item.ClassDate && qitem.ClassTimeId == subItem.ClassTimeId)?.ToList() ?? new List<RB_Class_Check_ViewModel>();
                            classTimeList.Add(new
                            {
                                subItem.ClassTimeId,
                                subItem.StartTime,
                                subItem.EndTime,
                                subItem.TimeHour,
                                CheckNum = tempCheckList?.Where(qitem => qitem.CheckStatus == 0)?.Count(),
                                NoCheckNum = tempCheckList?.Where(qitem => qitem.CheckStatus == 1)?.Count(),
                            });
                        }
                    }
                    list.Add(new
                    {
                        IsEndDate = item.ClassDate <= today,
                        item.ClassPlanId,
                        item.ClassId,
                        item.ClassDate,
                        ClassDateStr = Common.ConvertHelper.FormatDate(item.ClassDate),
                        item.ClassRoomId,
                        item.RoomName,
                        item.TeacherId,
                        item.TeacherName,
                        item.UserIcon,
                        TimeList = classTimeList
                    });
                }
            }
            return list;
        }

        /// <summary>
        /// 获取班级计划列表
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <param name="monthStr">月份</param>
        /// <returns></returns>
        public object GetClassPlan_V2(RB_Class_Plan_ViewModel model)
        {
            List<object> list = new List<object>();
            var timeList = new List<RB_Class_Time_ViewModel>();
            var planList = class_PlanRepository.GetClassPlanListRepository(model)?.OrderBy(qitem => qitem.ClassDate);
            if (planList != null && planList.Count() > 0)
            {
                string Ids = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(Ids))
                {
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassPlanIds = Ids });
                }
                DateTime today = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                foreach (var item in planList)
                {
                    item.PlanTimeList = timeList?.Where(qitem => qitem.ClassPlanId == item.ClassPlanId)?.ToList();
                    list.Add(new
                    {
                        IsEndDate = item.ClassDate <= today,
                        item.ClassPlanId,
                        item.ClassId,
                        item.ClassDate,
                        ClassDateStr = Common.ConvertHelper.FormatDate(item.ClassDate),
                        item.ClassRoomId,
                        item.RoomName,
                        item.TeacherId,
                        item.TeacherName,
                        item.UserIcon,
                        TimeList = item?.PlanTimeList?.Select(qitem => new { qitem.ClassTimeId, qitem.StartTime, qitem.EndTime })?.OrderBy(qitem => qitem.StartTime)
                    });
                }
            }
            return list;
        }

        /// <summary>
        /// 根据计划编号删除上课计划
        /// </summary>
        /// <param name="ClassPlanId"></param>
        /// <param name="UserInfo">用户信息</param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool RemoveClassPlanModule(int ClassPlanId, UserInfo UserInfo, out string message)
        {
            bool flag = false;
            message = "";
            var planModel = class_PlanRepository.GetEntity(ClassPlanId);
            if (planModel != null)
            {
                classLogRepository.AddClassLogRepository(new Model.Entity.Log.RB_Class_Log()
                {
                    LogType = Common.Enum.Course.ClassLogTypeEnum.Class,
                    LogContent = "删除【" + Common.ConvertHelper.FormatDate(planModel?.ClassDate) + "】上课计划",
                    ClassId = planModel.ClassId,
                    CreateBy = UserInfo.Id,
                    Group_Id = UserInfo.Group_Id,
                    School_Id = UserInfo.School_Id,
                });
                flag = UpdateClassEndDateModule(planModel.ClassId);
                flag = class_PlanRepository.DeleteOne(new WhereHelper(nameof(RB_Class_Plan_ViewModel.ClassPlanId), ClassPlanId));
            }
            else
            {
                message = "此上课计划不存在,请刷新后再试!";
            }
            return flag;
        }

        /// <summary>
        /// 检查班级上课日期是否存在
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool CheckPlanModule(RB_Class_Plan_ViewModel extModel)
        {
            List<WhereHelper> list = new List<WhereHelper>()
            {
                new WhereHelper (nameof(RB_Class_Plan_ViewModel.ClassId),extModel.ClassId),
                new WhereHelper (nameof(RB_Class_Plan_ViewModel.ClassDate),extModel.ClassDate),
                new WhereHelper (nameof(RB_Class_Plan_ViewModel.Status),(int)DateStateEnum.Normal),
            };
            if (extModel.ClassPlanId > 0)
            {
                list.Add(new WhereHelper(nameof(RB_Class_Plan_ViewModel.ClassPlanId), extModel.ClassPlanId, OperatorEnum.NotEqual));
            }
            return class_PlanRepository.Exists(list);
        }

        /// <summary>
        /// 新增修改班级上课计划
        /// </summary>
        /// <param name="extModel"></param>
        /// <param name="createBy">操作人</param>
        /// <returns></returns>
        public virtual bool SetClassPlanModule(RB_Class_Plan_ViewModel extModel, int createBy)
        {
            bool flag;
            string logContent = "";
            if (extModel.ClassPlanId > 0)
            {
                var oldPlanModel = class_PlanRepository.GetEntity(extModel.ClassPlanId);
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Class_Plan_ViewModel.ClassRoomId),extModel.ClassRoomId },
                    {nameof(RB_Class_Plan_ViewModel.TeacherId),extModel.TeacherId },
                    {nameof(RB_Class_Plan_ViewModel.ClassDate),extModel.ClassDate },
                };

                if (oldPlanModel.ClassRoomId != extModel.ClassRoomId)
                {
                    string roomIds = oldPlanModel.ClassRoomId + "," + extModel.ClassRoomId;
                    var roomList = class_RoomRepository.GetClassRoomListRepository(new RB_Class_Room_ViewModel() { QRoomIds = roomIds });
                    logContent += string.Format(",教室由【{0}】修改为【{1}】.",
                        (roomList?.Where(qitem => qitem.RoomId == oldPlanModel.ClassRoomId)?.FirstOrDefault()?.RoomName ?? ""),
                        (roomList?.Where(qitem => qitem.RoomId == extModel.ClassRoomId)?.FirstOrDefault()?.RoomName ?? "")
                        );
                }
                if (oldPlanModel.TeacherId != extModel.TeacherId)
                {
                    string tIds = oldPlanModel.TeacherId + "," + extModel.TeacherId;
                    var teacherList = teacherRepository.GetTeacherListRepository(new RB_Teacher_ViewModel() { QTIds = tIds });
                    logContent += string.Format(",讲师由【{0}】修改为【{1}】.",
                        (teacherList?.Where(qitem => qitem.TId == oldPlanModel.TeacherId)?.FirstOrDefault()?.TeacherName ?? ""),
                        (teacherList?.Where(qitem => qitem.TId == extModel.TeacherId)?.FirstOrDefault()?.TeacherName ?? "")
                        );
                }
                if (oldPlanModel.ClassDate != extModel.ClassDate)
                {
                    logContent += string.Format(",日期由【{0}】修改为【{1}】.", Common.ConvertHelper.FormatDate(oldPlanModel.ClassDate), Common.ConvertHelper.FormatDate(extModel.ClassDate));
                }

                flag = class_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Plan_ViewModel.ClassPlanId), extModel.ClassPlanId));
            }
            else
            {
                var newId = class_PlanRepository.Insert(extModel);
                extModel.ClassPlanId = newId;
                flag = newId > 0;
                logContent = "新增【" + Common.ConvertHelper.FormatDate(extModel?.ClassDate) + "】上课计划";
            }
            var oldPlanList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel()
            {
                ClassId = extModel.ClassId,
                ClassPlanId = extModel.ClassPlanId,
            });

            //原来没有计划
            if (oldPlanList == null || (oldPlanList != null && oldPlanList.Count == 0))
            {
                foreach (var item in extModel.PlanTimeList)
                {
                    item.ClassId = extModel.ClassId;
                    item.ClassPlanId = extModel.ClassPlanId;
                    item.Group_Id = extModel.Group_Id;
                    item.School_Id = extModel.School_Id;
                    item.ClassTimeId = 0;
                    flag = class_TimeRepository.Insert(item) > 0;
                }
            }
            //以前有计划
            else
            {
                //现在没有设置时间【直接删除以前的时间】
                if (extModel.PlanTimeList == null || (extModel.PlanTimeList != null && extModel.PlanTimeList.Count == 0))
                {
                    class_TimeRepository.DeleteClassTimeByPlanIdsRepository(extModel.ClassPlanId.ToString());
                }
                //找出差异的数据
                var deleteList = oldPlanList.Where(qitem => !extModel.PlanTimeList.Any(oldItem => qitem.ClassTimeId == oldItem.ClassTimeId)).ToList();
                foreach (var dItem in deleteList)
                {
                    if (dItem.ClassTimeId > 0)
                    {
                        logContent += string.Format(",删除{0}上课时间【{1} - {2}】", Common.ConvertHelper.FormatDate(extModel.ClassDate), dItem.StartTime, dItem.EndTime);
                        class_TimeRepository.Delete(dItem.ClassTimeId);
                    }
                }
                foreach (var item in extModel.PlanTimeList)
                {
                    item.ClassId = extModel.ClassId;
                    item.ClassPlanId = extModel.ClassPlanId;
                    item.Group_Id = extModel.Group_Id;
                    item.School_Id = extModel.School_Id;
                    if (item.ClassTimeId == 0)
                    {
                        logContent += string.Format(",新增{0}上课时间【{1} - {2}】", Common.ConvertHelper.FormatDate(extModel.ClassDate), item.StartTime, item.EndTime);
                        flag = class_TimeRepository.Insert(item) > 0;
                    }
                    else
                    {
                        var oldModel = oldPlanList?.Where(qitem => qitem.ClassTimeId == item.ClassTimeId)?.FirstOrDefault();
                        if (oldModel != null)
                        {
                            if (oldModel.StartTime != item.StartTime)
                            {
                                logContent += string.Format(",{0}上课开始时间由【{1}】修改为【{2}】", Common.ConvertHelper.FormatDate(extModel.ClassDate), oldModel.StartTime, item.EndTime);
                            }
                            if (oldModel.EndTime != item.EndTime)
                            {
                                logContent += string.Format(",{0}上课结束时间由【{1}】修改为【{2}】", Common.ConvertHelper.FormatDate(extModel.ClassDate), oldModel.EndTime, item.EndTime);
                            }
                        }
                        flag = class_TimeRepository.Update(item);
                    }
                }
            }
            if (!string.IsNullOrEmpty(logContent))
            {
                classLogRepository.AddClassLogRepository(
                    new Model.Entity.Log.RB_Class_Log()
                    {
                        LogType = Common.Enum.Course.ClassLogTypeEnum.Class,
                        LogContent = logContent,
                        ClassId = extModel.ClassId,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        CreateBy = createBy
                    });
            }
            return flag;
        }

        /// <summary>
        /// 获取班级学员列表
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <returns></returns>
        public object GetClassStudentListModule(int classId, int school_Id, int group_Id, int ClassTimeId, int QEffectStatus = 0)
        {
            List<object> list = new List<object>();
            var orderStudentList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel()
            {
                ClassId = classId,
                Group_Id = group_Id,
                School_Id = school_Id,
                QEffectStatus = QEffectStatus,
            }).ToList();
            if (QEffectStatus == 1)
            {
                orderStudentList = orderStudentList.Where(qitem => qitem.GuestState == 1).ToList();
            }
            var classModel = GetClassModule(classId);
            var courseModel = courseRepository.GetEntity((classModel?.CouseId ?? 0));
            List<RB_Class_Check_ViewModel> checkList = new List<RB_Class_Check_ViewModel>();
            if (ClassTimeId > 0)
            {
                checkList = GetClassCheckList(new RB_Class_Check_ViewModel { Group_Id = group_Id, ClassId = classId, ClassTimeId = ClassTimeId });
                var classTimeModel = class_TimeRepository.GetClassTimeList_V2(new RB_Class_Time_ViewModel { ClassTimeId = ClassTimeId, Group_Id = group_Id }).FirstOrDefault();
                orderStudentList = orderStudentList.Where(x => x.GuestState == 1 || x.GuestState == 6 || ((x.GuestState == 5 || x.GuestState == 7) && x.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(x.ChangeEffectTime)) >= Convert.ToDateTime(Common.ConvertHelper.FormatDate(classTimeModel.NewPlanDateTime)))).ToList();
            }
            if (orderStudentList != null && orderStudentList.Count > 0)
            {
                foreach (var item in orderStudentList)
                {
                    int checkStatus = -1;
                    if (ClassTimeId > 0 && checkList != null && checkList.Any())
                    {
                        checkStatus = checkList?.Where(qitem => qitem.ClassTimeId == ClassTimeId && qitem.ClassId == item.ClassId && qitem.OrderGuestId == item.Id)?.FirstOrDefault()?.CheckStatus ?? -1;
                    }
                    list.Add(new
                    {
                        UserIcon = item.StuIcon,
                        item.Id,
                        SexStr = item.Sex == 1 ? "男" : "女",
                        item.GuestName,
                        item.Age,
                        CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                        item.Mobile,
                        item.OrderId,
                        ClassHours = item.CompleteHours + "-" + (classModel?.ClassHours ?? 0),
                        IsRenew = item.IsRenewOrder,
                        item.RenewOrderId,
                        CheckStatus = checkStatus,
                        item.IsChaBan,
                        item.StartClassHours,
                        item.CourseName,
                        item.EffectStatus,
                        item.EffectStatusStr,
                        EffectTime = Common.ConvertHelper.FormatDate(item.EffectTime),
                        item.UpOrderId,
                        item.GuestState,
                        item.GuestStateStr,
                    });
                }
            }
            return list;
        }


        /// <summary>
        /// 根据班级id获取订单学员信息
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public List<RB_Order_Guest_ViewModel> GetOrderGuestListModule(RB_Order_Guest_ViewModel model)
        {
            return order_GuestRepository.GetOrderGuestListRepository(model);
        }


        /// <summary>
        /// 更新学生的退课信息
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="DropOutRemark"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool UpdateClassStudentDropOutModule(string ids, string dropOutRemark, int classId, int createBy, int group_Id, int school_Id, string studentName)
        {
            bool flag = false;
            foreach (var item in ids.Split(","))
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Order_Guest_ViewModel.GuestState),2},
                        {nameof(RB_Order_Guest_ViewModel.DropOutRemark),dropOutRemark}
                    };
                    //写日志
                    flag = order_GuestRepository.Update(fileds, new WhereHelper(nameof(RB_Order_Guest_ViewModel.Id), item));
                }
            }
            if (flag)
            {
                var classLogModel = new Model.Entity.Log.RB_Class_Log()
                {
                    LogType = Common.Enum.Course.ClassLogTypeEnum.DropOutStudent,
                    LogContent = studentName + " 学员因" + dropOutRemark + "退课",
                    ClassId = classId,
                    Group_Id = group_Id,
                    School_Id = school_Id,
                    CreateBy = createBy,
                    CreateTime = System.DateTime.Now
                };
                classLogRepository.AddClassLogRepository(classLogModel);
            }
            return flag;
        }

        /// <summary>
        /// 更新学生的退课信息
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="DropOutRemark"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool AddClassStudentDropOutModule(RB_Order_Guest_ViewModel model, string CreateName)
        {
            //写日志
            bool flag = order_GuestRepository.Insert(model) > 0;
            if (flag)
            {
                //更新学生的状态
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Student_ViewModel.StuStatus),3},
                };
                //写日志
                studentRepository.Update(fileds, new WhereHelper(nameof(RB_Student_ViewModel.StuId), model.StudentId));
                var classLogModel = new Model.Entity.Log.RB_Class_Log()
                {
                    LogType = Common.Enum.Course.ClassLogTypeEnum.AddStudent,
                    LogContent = CreateName + "邀请  " + model.GuestName + " 加入班级",
                    ClassId = model.ClassId,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id,
                    CreateBy = model.CreateBy,
                    CreateTime = System.DateTime.Now
                };
                classLogRepository.AddClassLogRepository(classLogModel);
            }
            return flag;
        }

        /// <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> GetStudentMakeupPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Student_Makeup_ViewModel query)
        {
            return student_MakeupRepository.GetStudentMakeupPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 新增签到
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="DropOutRemark"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool AddClassCheckModule(List<RB_Class_Check_ViewModel> list)
        {
            //基础课时分钟数
            var BasicMinutes = 0;
            //BasicMinutes = class_ConfigRepository.GetBasicMinutesRepository(list[0].Group_Id);
            BasicMinutes = class_ConfigRepository.GetClassMinutesRepository(list[0].ClassId);
            var timeModel = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel()
            {
                ClassTimeId = list[0].ClassTimeId
            })?.FirstOrDefault();
            //写日志
            bool flag = true;
            foreach (var item in list)
            {
                if (flag)
                {
                    var guestModel = order_GuestRepository.GetEntity(item.OrderGuestId);
                    item.CurrentDeductionHours = timeModel?.TimeHour ?? 0;
                    if (item.ClassCheckId > 0)
                    {
                        Dictionary<string, object> fileds = new Dictionary<string, object>()
                        {
                            { nameof(RB_Class_Check_ViewModel.CheckStatus),item.CheckStatus},
                            { nameof(RB_Class_Check_ViewModel.StudyNum),item.StudyNum},
                            { nameof(RB_Class_Check_ViewModel.CurrentDeductionHours),item.CurrentDeductionHours},
                        };
                        List<WhereHelper> wheres = new List<WhereHelper>()
                        {
                          new WhereHelper (nameof(RB_Class_Check_ViewModel.ClassCheckId),item.ClassCheckId)
                        };
                        flag = classCheckRepository.Update(fileds, wheres);
                    }
                    else
                    {
                        item.MakeUpStatus = 1;
                        var newId = classCheckRepository.Insert(item);
                        item.ClassCheckId = newId;
                        flag = newId > 0;
                    }
                    var oldModel = classCheckRepository.GetGuestFinishMinutesRepository(item.OrderGuestId.ToString(), ClassCheckId: item.ClassCheckId)?.FirstOrDefault();
                    if (flag)
                    {
                        //更新学员完成课时
                        var completeHours = (oldModel?.FinishClassHours ?? 0) + item.CurrentDeductionHours;
                        Dictionary<string, object> orderFileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Order_Guest_ViewModel.CompleteHours),completeHours }
                        };
                        flag = order_GuestRepository.Update(orderFileds, new WhereHelper(nameof(RB_Order_Guest_ViewModel.Id), item.OrderGuestId));
                        //缺勤和请假
                        if (item.CheckStatus == 1 || item.CheckStatus == 2)
                        {
                            flag = CalcGuestHoursModule(guestModel, item);
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 初始化缺勤数据
        /// </summary>
        /// <returns></returns>
        public bool InitDataModule()
        {
            bool flag = true;
            var list = GetClassCheckListRepository(new RB_Class_Check_ViewModel());
            var tempList = list?.Where(qitem => qitem.CheckStatus > 0)?.ToList();

            foreach (var item in tempList)
            {
                if (item.ClassId > 5)
                {
                    var guestModel = order_GuestRepository.GetEntity(item.OrderGuestId);
                    flag = CalcGuestHoursModule(guestModel, item);
                }
            }
            return flag;
        }

        /// <summary>
        /// 计算学员缺课课时
        /// </summary>
        /// <param name="guestModel"></param>
        /// <param name="checkModel"></param>
        /// <returns></returns>
        public bool CalcGuestHoursModule(RB_Order_Guest guestModel, RB_Class_Check_ViewModel checkModel)
        {
            bool flag = true;
            if (guestModel.GuestState == 1)
            {
                decimal NoFinishHours = 0;
                //获取以前所有签到列表【倒叙】
                var checkList = classCheckRepository.GetClassCheckListRepository(new RB_Class_Check_ViewModel()
                {
                    ClassId = checkModel.ClassId,
                    OrderGuestId = checkModel.OrderGuestId,
                    EndDate = Common.ConvertHelper.FormatDate(checkModel.ClassDate)
                }).OrderByDescending(qitem => qitem.ClassDate).ToList();
                for (var i = 0; i < checkList.Count; i++)
                {
                    if ((checkList[i].CheckStatus == 1) && (checkList[i].MakeUpStatus == 3 || checkList[i].MakeUpStatus == 1))
                    {
                        NoFinishHours += checkList[i].CurrentDeductionHours;
                    }
                    else
                    {
                        break;
                    }
                }
                Dictionary<string, object> fileds = new Dictionary<string, object>();
                Dictionary<string, object> checkFileds = new Dictionary<string, object>();
                //缺勤课时超过12课时更新学员状态为【停课】
                if (NoFinishHours >= Common.Config.AbsentHours)
                {
                    fileds.Add(nameof(RB_Order_Guest_ViewModel.GuestState), 5);
                    checkFileds.Add(nameof(RB_Class_Check_ViewModel.IsAbsentHours), 1);
                    checkFileds.Add(nameof(RB_Class_Check_ViewModel.MakeUpStatus), 3);
                }
                if (fileds != null && fileds.Count > 0 && checkFileds != null && checkFileds.Count > 0)
                {
                    List<WhereHelper> wheres = new List<WhereHelper>()
                    {
                        new WhereHelper (nameof(RB_Order_Guest_ViewModel.Id),guestModel.Id),
                    };
                    flag = order_GuestRepository.Update(fileds, wheres);
                    List<WhereHelper> checkWheres = new List<WhereHelper>()
                    {
                        new WhereHelper (nameof(RB_Class_Check_ViewModel.ClassCheckId),checkModel.ClassCheckId),
                    };
                    flag = classCheckRepository.Update(checkFileds, checkWheres);
                }

                if (flag)
                {
                    flag = student_MakeupRepository.SetStudentMakeUpRepository(new RB_Student_Makeup()
                    {
                        Id = 0,
                        ClassCheckId = checkModel.ClassCheckId,
                        ClassDate = checkModel.ClassDate,
                        ClassId = checkModel.ClassId,
                        CreateBy = checkModel.CreateBy,
                        CreateTime = checkModel.CreateTime,
                        Group_Id = checkModel.Group_Id,
                        MakeUpStatus = 3,
                        OrderGuestId = checkModel.OrderGuestId
                    });
                }
            }
            return flag;
        }

        /// <summary>
        /// 设置修改学员补课状态
        /// </summary>
        /// <param name="MakeUpStatus"></param>
        /// <param name="ClassCheckId"></param>
        /// <param name="GuestId"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetMakeUpStatusModule(int Id, int MakeUpStatus, int ClassCheckId, int GuestId, UserInfo userinfo)
        {
            bool flag = true;
            if (MakeUpStatus == 2)
            {
                Dictionary<string, object> makeUpFileds = new Dictionary<string, object>()
                {
                   {nameof(RB_Student_Makeup_ViewModel.MakeUpStatus), MakeUpStatus },
                   {nameof(RB_Student_Makeup_ViewModel.DealBy),userinfo.Id},
                   {nameof(RB_Student_Makeup_ViewModel.DealTime),DateTime.Now},
                };
                //更新补课表状态
                flag = student_MakeupRepository.Update(makeUpFileds, new WhereHelper(nameof(RB_Student_Makeup_ViewModel.Id), Id));
                if (flag)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Class_Check_ViewModel.MakeUpStatus), MakeUpStatus }
                    };
                    //更新签到表状态
                    flag = classCheckRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Check_ViewModel.ClassCheckId), ClassCheckId));
                }

                if (flag)
                {
                    //更新学员状态和已补课时
                    var guestModel = order_GuestRepository.GetEntity(GuestId);
                    var checkModel = classCheckRepository.GetEntity(ClassCheckId);
                    var makeUpHours = (guestModel?.MakeUpHours ?? 0) + (checkModel?.CurrentDeductionHours ?? 0);
                    Dictionary<string, object> guestFileds = new Dictionary<string, object>()
                    {
                        { nameof(RB_Order_Guest_ViewModel.GuestState), 1 },
                        { nameof(RB_Order_Guest_ViewModel.MakeUpHours),makeUpHours }
                    };
                    flag = order_GuestRepository.Update(guestFileds, new WhereHelper(nameof(RB_Order_Guest_ViewModel.Id), GuestId));
                }
            }
            return flag;
        }

        /// <summary>
        /// 获取班级上课记录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public object GetClassPlanLogPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Class_Plan_ViewModel query)
        {
            List<object> result = new List<object>();
            var list = class_PlanRepository.GetClassPlanLogPageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                List<RB_Class_Check_ViewModel> checkLogList = new List<RB_Class_Check_ViewModel>();
                List<RB_Class_Time_ViewModel> timeList = new List<RB_Class_Time_ViewModel>();
                if (list != null && list.Any())
                {
                    checkLogList = classCheckRepository.GetClassCheckListRepository(new RB_Class_Check_ViewModel { Group_Id = query.Group_Id, School_Id = query.School_Id, ClassId = query.ClassId, StartDate = list.Min(x => x.ClassDate).ToString("yyyy-MM-dd HH:mm:ss"), EndDate = list.Max(x => x.ClassDate).ToString("yyyy-MM-dd HH:mm:ss") });
                }
                string Ids = string.Join(",", list.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(Ids))
                {
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassPlanIds = Ids });
                }
                foreach (var item in list)
                {
                    List<object> time = new List<object>();
                    var tempTimeList = timeList?.Where(qitem => qitem.ClassPlanId == item.ClassPlanId)?.OrderBy(qitem => qitem.StartTime)?.ToList();
                    int tempTotalNum = order_GuestRepository.GetOrderGuestNumRepository(new RB_Order_Guest_ViewModel() { ClassId = item.ClassId, GuestState = 1 });
                    int TotalNum = 0;
                    int NoCheckNum = 0;
                    if (tempTimeList != null && tempTimeList.Count > 0)
                    {
                        foreach (var subItem in tempTimeList)
                        {
                            TotalNum += tempTotalNum;
                            var checkList = checkLogList.Where(x => x.CheckStatus == 0 && x.ClassTimeId == subItem.ClassTimeId).ToList();
                            var noCheckList = checkLogList.Where(x => x.CheckStatus == 1 && x.ClassTimeId == subItem.ClassTimeId).ToList();
                            NoCheckNum += noCheckList.Count();
                            var obj = new
                            {
                                subItem.StartTime,
                                subItem.EndTime,
                                CheckNum = checkList.Count(),
                                CheckList = checkList.Select(qitem => new { qitem.GuestName }),
                                NoCheckNum = noCheckList.Count(),
                                NoCheckList = noCheckList.Select(qitem => new { qitem.GuestName }),
                            };
                            time.Add(obj);
                        }
                    }
                    var planCheckObj = new
                    {
                        item.TeacherName,
                        ColorType = item.TeacherId != item.Teacher_Id ? 1 : (TotalNum == NoCheckNum ? 2 : 3),
                        IsAlternate = item.TeacherId == item.Teacher_Id ? 0 : 1,//是否代课老师
                        MonthStr = item.ClassDate.ToString("MM"),
                        DayStr = item.ClassDate.ToString("dd"),
                        YearStr = item.ClassDate.ToString("yyyy"),
                        TimeList = time
                    };
                    result.Add(planCheckObj);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取班级上课计划
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetClassPlanModelRepository(RB_Class_Plan_ViewModel query)
        {
            return class_PlanRepository.GetClassPlanModelRepository(query);
        }

        /// <summary>
        /// 获取每天签到记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Check_ViewModel> GetClassCheckListRepository(RB_Class_Check_ViewModel query)
        {
            return classCheckRepository.GetClassCheckListRepository(query);
        }

        /// <summary>
        /// 获取班级签到列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Check_ViewModel> GetClassCheckList(RB_Class_Check_ViewModel query)
        {
            return classCheckRepository.GetClassCheckList(query);
        }

        /// <summary>
        /// 获取班级上课计划列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetClassPlanListModule(RB_Class_Plan_ViewModel query)
        {
            return class_PlanRepository.GetClassPlanListRepository(query);
        }

        /// <summary>
        /// 获取班级上课计划列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetClassPlanListExtModule(RB_Class_Plan_ViewModel query)
        {
            return class_PlanRepository.GetClassPlanListExtRepository(query);
        }


        /// <summary>
        /// 获取上课计划
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetClassPlanList_V2(RB_Class_Plan_ViewModel query)
        {
            var list = class_PlanRepository.GetClassPlanList_V2(query);
            if (list != null && list.Any())
            {
                string ClassIds = string.Join(",", list.Distinct().Select(x => x.ClassId));
                var orderStudentList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel()
                {
                    ClassIds = ClassIds,
                    Group_Id = query.Group_Id,
                    School_Id = -1
                }).Where(x => x.GuestState == 1).ToList();

                if (orderStudentList != null && orderStudentList.Any())
                {
                    foreach (var item in list)
                    {
                        item.GuestList = new List<RB_Order_Guest_ViewModel>();
                        item.GuestList = orderStudentList.Where(x => x.ClassId == item.ClassId).ToList();
                    }
                }
            }
            return list;
        }


        /// <summary>
        /// 获取上课计划
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetClassPlanList_V3(RB_Class_Plan_ViewModel query)
        {
            var list = class_PlanRepository.GetClassPlanList_V2(query);

            return list;
        }

        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetStudentListRepository(RB_Student_ViewModel query)
        {
            return studentRepository.GetStudentListRepository(query);
        }

        #region 班级收支信息
        /// <summary>
        /// 获取班级收支信息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Finance_ViewModel> GetClassFinanceListRepository(RB_Class_Finance_ViewModel query)
        {
            return classFinanceRepository.GetClassFinanceListRepository(query);
        }

        /// <summary>
        /// 新增/修改班级收支设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetClassFinance(RB_Class_Finance_ViewModel model)
        {
            var oldModel = GetClassFinanceListRepository(model).FirstOrDefault();
            if (oldModel == null || oldModel.FinanceId == 0)
            {
                return classFinanceRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Class_Finance_ViewModel.IsOpen),model.IsOpen},
                    {nameof(RB_Class_Finance_ViewModel.UpdateBy),model.UpdateBy},
                    {nameof(RB_Class_Finance_ViewModel.UpdateTime),model.UpdateTime}
                };
                return order_GuestRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Finance_ViewModel.FinanceId), oldModel.FinanceId));
            }

        }

        #endregion

        #region 老师教案
        /// <summary>
        /// 新增/修改老师教案
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool SetClassLessPlan(Class_LessonPlan_ViewModel model)
        {
            bool flag = false;
            if (model.LessonPlanList != null && model.LessonPlanList.Any())
            {
                foreach (var item in model.LessonPlanList)
                {
                    int LessonPlanId = 0;
                    item.ClassId = model.ClassId;
                    item.ClassPlanId = model.ClassPlanId;
                    item.Group_Id = model.Group_Id;
                    item.School_Id = model.School_Id;
                    item.IsTemplate = model.IsTemplate;
                    item.CourseNum = model.CourseNum;
                    item.LessonPlan = model.LessonPlan;
                    item.CreateBy = model.CreateBy;
                    item.CreateTime = model.CreateTime;
                    item.UpdateBy = model.UpdateBy;
                    item.UpdateTime = model.UpdateTime;
                    if (item.LessonPlanId > 0)//更新
                    {
                        LessonPlanId = item.LessonPlanId;
                        flag = ClassLessonPlanRepository.Update(item);
                        //删除之前有，现在没有的数据
                        var detailsList = GetLessonPlanDetailsListRepository(new RB_Class_LessonPlanDetails_ViewModel { Group_Id = item.Group_Id, School_Id = item.School_Id, LessonPlanId = item.LessonPlanId });
                        if (item.LessonPlanDetailsList != null && item.LessonPlanDetailsList.Any())
                        {
                            List<int> detailsList1 = detailsList.Select(x => x.LessonPlanDetailsId).ToList();
                            List<int> detailsList2 = item.LessonPlanDetailsList.Where(x => x.LessonPlanDetailsId > 0).Select(x => x.LessonPlanDetailsId).ToList();
                            var deleDetailsList = detailsList1.Except(detailsList2).ToList();
                            foreach (var itemDele in deleDetailsList)
                            {
                                ClassLessonPlanDetailsRepository.Delete(detailsList.Where(x => x.LessonPlanDetailsId == itemDele).FirstOrDefault());
                            }
                        }
                        else
                        {
                            ClassLessonPlanDetailsRepository.DeleteBatch(detailsList);
                        }

                        //删除之前有，现在没有的数据
                        var projectsList = GetLessonPlanProjectsListRepository(new RB_Class_LessonPlanProjects_ViewModel { Group_Id = item.Group_Id, School_Id = item.School_Id, LessonPlanId = item.LessonPlanId });
                        if (item.LessonPlanDetailsList != null && item.LessonPlanDetailsList.Any())
                        {
                            List<int> projectsList1 = projectsList.Select(x => x.LessonPlanProjectsId).ToList();
                            List<int> projectsList2 = item.LessonPlanProjectsList.Where(x => x.LessonPlanProjectsId > 0).Select(x => x.LessonPlanProjectsId).ToList();
                            var deleProjectsList = projectsList1.Except(projectsList2).ToList();
                            foreach (var itemDele in deleProjectsList)
                            {
                                ClassLessonPlanProjectsRepository.Delete(projectsList.Where(x => x.LessonPlanProjectsId == itemDele).FirstOrDefault());
                            }
                        }
                        else
                        {
                            ClassLessonPlanProjectsRepository.DeleteBatch(projectsList);
                        }
                    }
                    else
                    {
                        LessonPlanId = ClassLessonPlanRepository.Insert(item);
                        flag = LessonPlanId > 0;

                    }
                    if (flag)
                    {
                        Dictionary<string, object> fileds = new Dictionary<string, object>()
                            {
                                {nameof(RB_Class_Plan_ViewModel.BeiKeStatus),1},
                            };
                        class_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Plan_ViewModel.ClassPlanId), item.ClassPlanId));
                    }
                    foreach (var itemDetails in item.LessonPlanDetailsList)
                    {
                        itemDetails.LessonPlanId = LessonPlanId;
                        itemDetails.School_Id = item.School_Id;
                        itemDetails.Group_Id = item.Group_Id;
                        if (itemDetails.LessonPlanDetailsId == 0)
                        {
                            ClassLessonPlanDetailsRepository.Insert(itemDetails);
                        }
                        else
                        {
                            ClassLessonPlanDetailsRepository.Update(itemDetails);
                        }
                    }
                    foreach (var itemProjects in item.LessonPlanProjectsList)
                    {
                        itemProjects.LessonPlanId = LessonPlanId;
                        itemProjects.School_Id = item.School_Id;
                        itemProjects.Group_Id = item.Group_Id;
                        if (itemProjects.LessonPlanProjectsId == 0)
                        {
                            ClassLessonPlanProjectsRepository.Insert(itemProjects);
                        }
                        else
                        {
                            ClassLessonPlanProjectsRepository.Update(itemProjects);
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 新增/修改老师教案
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool UpdateClassLessPlan(Class_LessonPlan_ViewModel model)
        {
            bool flag = true;
            if (model.LessonPlanList != null && model.LessonPlanList.Any())
            {
                foreach (var item in model.LessonPlanList)
                {
                    foreach (var itemDetails in item.LessonPlanDetailsList)
                    {
                        Dictionary<string, object> fileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Class_LessonPlanDetails_ViewModel.Summary),itemDetails.Summary}
                        };
                        if (flag)
                        {
                            flag = ClassLessonPlanDetailsRepository.Update(fileds, new WhereHelper(nameof(RB_Class_LessonPlanDetails_ViewModel.LessonPlanDetailsId), itemDetails.LessonPlanDetailsId));
                        }
                    }
                }
            }
            return flag;
        }


        /// <summary>
        /// 新增/修改老师教案
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool GetUpdateClassLessPlanSummary(Class_LessonPlan_ViewModel model)
        {
            bool flag = true;
            if (model.LessonPlanList != null && model.LessonPlanList.Any())
            {
                foreach (var item in model.LessonPlanList)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_Class_LessonPlan_ViewModel.Summary),item.Summary},
                               {nameof(RB_Class_LessonPlan_ViewModel.ProjectPic),item.ProjectPic}
                        };
                    if (flag)
                    {
                        flag = ClassLessonPlanRepository.Update(fileds, new WhereHelper(nameof(RB_Class_LessonPlan_ViewModel.LessonPlanId), item.LessonPlanId));
                    }
                }
                if (flag)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                            {
                                {nameof(RB_Class_Plan_ViewModel.FanKuiStatus),1},
                            };
                    class_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Plan_ViewModel.ClassPlanId), model.LessonPlanList.FirstOrDefault().ClassPlanId));
                }
            }
            return flag;
        }


        /// <summary>
        /// 获取老师教案详情分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlanDetails_ViewModel> GetLessonPlanDetailsListRepository(RB_Class_LessonPlanDetails_ViewModel query)
        {
            return ClassLessonPlanDetailsRepository.GetLessonPlanDetailsListRepository(query);
        }


        /// <summary>
        /// 获取老师教案项目分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlanProjects_ViewModel> GetLessonPlanProjectsListRepository(RB_Class_LessonPlanProjects_ViewModel query)
        {
            return ClassLessonPlanProjectsRepository.GetLessonPlanProjectsListRepository(query);
        }

        /// <summary>
        /// 获取老师教案分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlan_ViewModel> GetLessonPlanPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_LessonPlan_ViewModel query)
        {
            return ClassLessonPlanRepository.GetLessonPlanPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }


        /// <summary>
        /// 获取老师教案列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlan_ViewModel> GetLessonPlanListRepository(RB_Class_LessonPlan_ViewModel query)
        {
            return ClassLessonPlanRepository.GetLessonPlanListRepository(query);
        }

        /// <summary>
        /// 获取老师计划列表
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <param name="monthStr">月份</param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetTeacherPlanModule(RB_Class_ViewModel model)
        {
            var timeList = new List<RB_Class_Time_ViewModel>();
            var planList = class_PlanRepository.GetTeacherPlanListRepository(new RB_Class_Plan_ViewModel() { School_Id = model.School_Id, TeacherId = model.Teacher_Id, Group_Id = model.Group_Id, StartTime = model.StartTime, EndTime = model.EndTime });
            if (planList != null && planList.Any())
            {
                string Ids = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(Ids))
                {
                    timeList = class_TimeRepository.GetClassTimeList_V2(new RB_Class_Time_ViewModel() { QClassPlanIds = Ids });
                }
                string ClassIds = string.Join(",", planList.GroupBy(x => x.ClassId).Select(x => x.Key));
                var orderStudentList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel()
                {
                    ClassIds = ClassIds,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id
                });

                string timeIds = string.Join(",", timeList.Select(x => x.ClassTimeId));
                var checkList = classCheckRepository.GetClassCheckList(new RB_Class_Check_ViewModel
                {
                    ClassTimeIds = timeIds,
                    Group_Id = model.Group_Id

                });
                foreach (var item in planList)
                {
                    item.PlanTimeList = new List<RB_Class_Time_ViewModel>();
                    item.PlanTimeList = timeList.Where(x => x.ClassId == item.ClassId &&(x.ClassPlanId == item.ClassPlanId || item.ClassDate == Convert.ToDateTime(Common.ConvertHelper.FormatDate(x.NewPlanDateTime)))).ToList();

                    foreach (var itemTime in item.PlanTimeList)
                    {
                        itemTime.GuestList = new List<RB_Order_Guest_ViewModel>();
                        itemTime.GuestList = orderStudentList.Where(x => x.ClassId == itemTime.ClassId && (x.GuestState == 1 || x.GuestState == 6 || ((x.GuestState == 5 || x.GuestState == 7) && x.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(x.ChangeEffectTime)) >= item.ClassDate))).ToList();
                        foreach (var itemCheck in itemTime.GuestList)
                        {
                            //0-正常，1-缺勤，2-未签到
                            itemCheck.IsCheck = (checkList != null && checkList.Any()) ? checkList.Where(y => y.OrderGuestId == itemCheck.Id && y.ClassTimeId == itemTime.ClassTimeId).FirstOrDefault()?.CheckStatus ?? 2 : 2;
                        }

                    }
                }
            }
            return planList;
        }

        /// <summary>
        /// 获取备案评论分页列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Class_LessonComment_ViewModel> GetLessonCommentList(RB_Class_LessonComment_ViewModel query)
        {
            return classLessonCommentRepository.GetLessonCommentList(query);
        }

        /// <summary>
        /// 评论/删除评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool SetLessonComment(RB_Class_LessonComment_ViewModel model)
        {
            bool flag = false;
            if (model.LessonCommentId == 0)
            {
                flag = classLessonCommentRepository.Insert(model) > 0;
                if (flag && model.Score > 0)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Class_Plan.ParentFanKuiStatus),1}
                    };
                    class_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Plan.ClassPlanId), model.ClassPlanId));
                }
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Class_LessonComment_ViewModel.Score),model.Score},
                     {nameof(RB_Class_LessonComment_ViewModel.Comment),model.Comment}
                };
                flag = classLessonCommentRepository.Update(fileds, new WhereHelper(nameof(RB_Class_LessonComment_ViewModel.LessonCommentId), model.LessonCommentId));
            }
            return flag;
        }
        /// <summary>
        /// 评论/删除评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool DelLessonComment(RB_Class_LessonComment_ViewModel model)
        {
           
            Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Class_LessonComment_ViewModel.Status),1}
                };
            return classLessonCommentRepository.Update(fileds, new WhereHelper(nameof(RB_Class_LessonComment_ViewModel.LessonCommentId), model.LessonCommentId));

          
        }
        #endregion

        #region 根据学生id获取最近的上课信息
        /// <summary>
        /// 根据学生id获取教师账户
        /// </summary>
        /// <param name="teacherIds"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetListByStudentId(int Student_Id, int Group_Id)
        {
            var list = classRepository.GetListByStudentId(Student_Id, Group_Id);
            if (list != null && list.Any())
            {
                string classPlanIds = string.Join(",", list.Select(x => x.ClassPlanId));
                var timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel { QClassPlanIds = classPlanIds, Group_Id = Group_Id });
                if (timeList != null && timeList.Any())
                {
                    foreach (var item in list)
                    {
                        item.ClassTimeList = new List<RB_Class_Time_ViewModel>();
                        item.ClassTimeList = timeList.Where(x => x.ClassPlanId == item.ClassPlanId).ToList();
                        item.ClassTimeList.ForEach(x => x.NewPlanDateTime = item.ClassDate.Value.ToString("yyyy-MM-dd") + " " + x.StartTime);
                    }
                }
            }
            return list;
        }



        /// <summary>
        /// 获取学生上课计划列表
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <param name="monthStr">月份</param>
        /// <returns></returns>
        public List<RB_Class_Plan_ViewModel> GetStudentPlanModule(RB_Class_Plan_ViewModel model)
        {
            var timeList = new List<RB_Class_Time_ViewModel>();
            var planList = class_PlanRepository.GetStudentPlanListRepository(model);
            if (planList != null && planList.Any())
            {
                string Ids = string.Join(",", planList.Select(qitem => qitem.ClassPlanId));
                if (!string.IsNullOrEmpty(Ids))
                {
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassPlanIds = Ids });
                }
                string ClassIds = string.Join(",", planList.GroupBy(x => x.ClassId).Select(x => x.Key));
                //var orderStudentList = order_GuestRepository.GetList(new RB_Order_Guest_ViewModel()
                //{
                //    ClassIds = ClassIds,
                //    Group_Id = model.Group_Id,
                //    School_Id = model.School_Id
                //}).Where(x => x.GuestState == 1).ToList();
                foreach (var item in planList)
                {
                    item.PlanTimeList = new List<RB_Class_Time_ViewModel>();
                    item.PlanTimeList = timeList.Where(x => x.ClassPlanId == item.ClassPlanId).ToList();
                    //item.GuestList = new List<RB_Order_Guest_ViewModel>();
                    //item.GuestList = orderStudentList.Where(x => x.ClassId == item.ClassId).ToList();
                }
            }
            return planList;
        }


        /// <summary>
        /// 获取app约课
        /// </summary>
        /// <param name="Group_Id"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetStuAppointment(int Group_Id)
        {
            return classRepository.GetStuAppointment(Group_Id);
        }


        #endregion

        #region  教师上课挂件查询
        /// <summary>
        /// 获取班级上课时间
        /// </summary>
        /// <param name="classIds"></param>
        /// <returns></returns>
        public List<RB_Class_Time_Extend> GetClassTimeByRoomId(int RoomId, int Group_Id)
        {
            return class_TimeRepository.GetClassTimeByRoomId(RoomId, Group_Id);
        }

        /// <summary>
        /// 获取班级上课时间
        /// </summary>
        /// <param name="classIds"></param>
        /// <returns></returns>
        public List<RB_Class_Time_Extend> GetClassTimeByClassId(int ClassId, int Group_Id)
        {
            return class_TimeRepository.GetClassTimeByClassId(ClassId, Group_Id);
        }

        /// <summary>
        /// 获取班级上课时间
        /// </summary>
        /// <param name="classIds"></param>
        /// <returns></returns>
        public List<RB_Class_Time_Extend> GetClassTimeByTId(int TId, int Group_Id)
        {
            return class_TimeRepository.GetClassTimeByTId(TId, Group_Id);
        }


        /// <summary>
        /// 计算当前班级完成课时
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int GetNowClassHours(RB_Class_Time_Extend model)
        {
            int classHours = 0;
            //班级完成的总分钟数
            var FinishMinutes = classCheckRepository.GetClassFinishMinutesRepository(model.ClassId.ToString())?.FirstOrDefault()?.FinishMinutes ?? 0;
            //基础课时分钟数
            var BasicMinutes = class_ConfigRepository.GetClassMinutesRepository(model.ClassId);
            var nowHours = model.NewEndPlanDateTime.Value.Subtract(model.NewPlanDateTime.Value).TotalSeconds;
            if (FinishMinutes > 0)
            {
                //完成课时数
                classHours = Convert.ToInt32((FinishMinutes + Convert.ToDecimal(nowHours)) / BasicMinutes);
            }
            else
            {
                classHours = Convert.ToInt32(FinishMinutes / BasicMinutes);
            }
            return classHours;
        }

        /// <summary>
        /// 班级签到完成学习分钟数以及课时
        /// </summary>
        /// <param name="classIds">班级编号</param>
        /// <returns></returns>
        public List<GuestFinishMinute_ViewModel> GetClassFinishMinutesRepository(string classIds)
        {
            return classCheckRepository.GetClassFinishMinutesRepository(classIds);
        }

        /// <summary>
        /// 更新课堂随拍
        /// </summary>
        /// <param name="SuiPai"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool UpdateTimeSuiPai(string SuiPai, int Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_Time_ViewModel.SuiPai),SuiPai},
            };
            return class_TimeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Time_ViewModel.ClassTimeId), Id));
        }


        /// <summary>
        /// 更新课堂上课状态
        /// </summary>
        /// <param name="SuiPai"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool UpdateTimeClassStatus(int classStatus, int Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Class_Time_ViewModel.ClassStatus),classStatus},
                };
            if (classStatus == 1)
            {
                fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Class_Time_ViewModel.ClassStatus),classStatus},
                    { nameof(RB_Class_Time_ViewModel.InClassTime),System.DateTime.Now},
                };
            }
            else if (classStatus == 2)
            {
                fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Class_Time_ViewModel.ClassOverTime),System.DateTime.Now},
                    { nameof(RB_Class_Time_ViewModel.ClassStatus),classStatus},
                };
            }

            return class_TimeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Time_ViewModel.ClassTimeId), Id));
        }

        /// <summary>
        /// 更新课堂视频
        /// </summary>
        /// <param name="SuiPai"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool UpdateTimeClassVideo(string videoUrl, int Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_Time_ViewModel.VideoUrl),videoUrl},
            };
            return class_TimeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Time_ViewModel.ClassTimeId), Id));
        }
        #endregion

        /// <summary>
        /// 修改班级默认上课时间
        /// </summary>
        /// <param name="ClassId"></param>
        /// <param name="DefaultTimeJson"></param>
        /// <returns></returns>
        public bool UpdateClassTimeModule(int ClassId, string DefaultTimeJson)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_ViewModel.DefaultTimeJson), DefaultTimeJson},
            };
            var flag = classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), ClassId));
            return flag;
        }

        /// <summary>
        /// 更新上课计划中的课时数
        /// </summary>
        /// <param name="ClassTimeId"></param>
        /// <param name="TimeHour"></param>
        /// <returns></returns>
        public bool UpdateClassPlanTimeModule(int ClassTimeId, decimal TimeHour)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_Time.TimeHour), TimeHour},
            };
            var flag = class_TimeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Time.ClassTimeId), ClassTimeId));
            return flag;
        }

        #region 班级课程关联管理

        /// <summary>
        /// 新增修改班级关联课程
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetClassCourseModule(List<RB_Class_Course_Extend> list)
        {
            bool flag = false;
            foreach (var model in list)
            {
                var newModel = class_CourseRepository.GetClassCourseListRepository(new RB_Class_Course_Extend()
                {
                    CourseId = model.CourseId,
                    ClassId = model.ClassId
                })?.FirstOrDefault();
                model.Id = newModel?.Id ?? 0;
                if (model.Id > 0)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Class_Course_Extend.UpdateBy),model.UpdateBy },
                        {nameof(RB_Class_Course_Extend.UpdateTime),model.UpdateTime },
                    };
                    flag = class_CourseRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Course_Extend.Id), model.Id));
                }
                else
                {
                    var newId = class_CourseRepository.Insert(model);
                    model.Id = newId;
                    flag = newId > 0;
                }
            }
            return flag;
        }

        /// <summary>
        /// 获取班级关联课程列表
        /// </summary>
        /// <param name="query"></param>
        public List<RB_Class_Course_Extend> GetClassCourseListModule(RB_Class_Course_Extend query)
        {
            return class_CourseRepository.GetClassCourseListRepository(query);
        }

        /// <summary>
        /// 删除班级关联课程
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool RemoveClassCourseModule(int Id, out string message)
        {
            message = "";
            var classCourse = class_CourseRepository.GetEntity(Id);
            var orderList = orderRepository.GetOrderListRepository(new RB_Order_ViewModel()
            {
                ClassId = classCourse.ClassId,
                CourseId = classCourse.CourseId,
                OrderState = OrderStateEnum.Normal
            });
            if (orderList != null && orderList.Count > 0)
            {
                message = "此关联课程下有正常的订单,不能取消!";
                return false;
            }
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Class_Course_Extend.Status),(int)DateStateEnum.Delete },
            };
            var flag = class_CourseRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Course_Extend.Id), Id));
            return flag;
        }
        #endregion

        /// <summary>
        /// 上课计划统计
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="ClassType">课程类型(0-全部,1-正常,2-试听课)</param>
        /// <returns></returns>
        public object GetClassPlanStatisticalModule(RB_Class_Plan_ViewModel query, int ClassType = 0)
        {
            List<object> mondayList = new List<object>();
            List<object> tuesdayList = new List<object>();
            List<object> wednesdayList = new List<object>();
            List<object> thursdayList = new List<object>();
            List<object> fridayList = new List<object>();
            List<object> saturdayList = new List<object>();
            List<object> sundayList = new List<object>();
            var startTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekMonday(query.StartTime));
            var endTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekSunday(query.EndTime));
            TimeSpan sp = endTime.Subtract(startTime);
            var days = sp.Days + 1;

            //正常上课计划列表
            var list = new List<RB_Class_Plan_ViewModel>();
            //试听课列表
            var reserveList = new List<RB_Reserve_Class_Extend>();

            var reserveQuery = new RB_Reserve_Class_Extend()
            {
                StartClassDate = query.StartTime,
                EndClassDate = query.EndTime,
                TeacherId = query.TeacherId,
                ClassRoomId = query.ClassRoomId
            };
            if (ClassType == 0)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }
            else if (ClassType == 1)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
            }
            else if (ClassType == 2)
            {
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }

            if (list != null || reserveList != null)
            {
                var timeList = new List<RB_Class_Time_ViewModel>();
                var guestList = new List<RB_Order_Guest_ViewModel>();
                if (list != null && list.Count > 0)
                {
                    string classIds = string.Join(",", list.Select(qitem => qitem.ClassId));
                    string planIds = string.Join(",", list.Select(qitem => qitem.ClassPlanId));
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassIds = classIds, QClassPlanIds = planIds });
                    guestList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel() { ClassIds = classIds });
                }
                var visitorList = new List<RB_Visitor_Reserve_Extend>();
                if (reserveList != null && reserveList.Count > 0)
                {
                    string reserveClassIds = string.Join(",", reserveList.Select(qitem => qitem.ReserveClassId));
                    visitorList = visitor_ReserveRepository.GetVisitorReserveListRepository(new Model.ViewModel.Reserve.RB_Visitor_Reserve_Extend()
                    {
                        Q_ReserveClassIds = reserveClassIds
                    });
                }

                for (var i = 0; i < days; i++)
                {
                    var ClassDate = startTime.AddDays(i);
                    List<object> subList = new List<object>();
                    var tempList = list.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.StartTime);
                    foreach (var subItem in tempList)
                    {
                        var tempTimeList = timeList?.Where(qitem => qitem.ClassPlanId == subItem.ClassPlanId)?.ToList()?.OrderBy(qitem => qitem.StartTime);
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (tempTimeList?.FirstOrDefault()?.StartTime ?? "");
                        subList.Add(new
                        {
                            ClassType = 1,
                            subItem.ClassId,
                            subItem.ClassName,
                            subItem.CourseName,
                            subItem.RoomName,
                            subItem.TeacherName,
                            subItem.CompleteProgress,
                            subItem.Ranks,
                            subItem.TotalPlanNum,
                            TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                            StartTime = tempTimeList?.FirstOrDefault()?.StartTime ?? "",
                            EndTime = tempTimeList?.LastOrDefault()?.EndTime ?? "",
                            GuestList = guestList.Where(qitem => qitem.ClassId == subItem.ClassId && (qitem.GuestState == 1 || qitem.GuestState == 6 || ((qitem.GuestState == 5 || qitem.GuestState == 7) && qitem.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(qitem.ChangeEffectTime)) >= Convert.ToDateTime(Common.ConvertHelper.FormatDate(ClassDate)))))?.Select(qitem => new { qitem.GuestName })
                        });
                    }

                    var tempReserveList = reserveList.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.ClassTime);
                    foreach (var subItem in tempReserveList)
                    {
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (subItem?.ClassTime ?? "");
                        subList.Add(new
                        {
                            ClassType = 2,
                            ClassId = subItem.ReserveClassId,
                            ClassName = "试听课",
                            CourseName = subItem.ClassContent,
                            subItem.RoomName,
                            subItem.TeacherName,
                            Ranks = 0,
                            TotalPlanNum = 0,
                            TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                            StartTime = subItem.ClassTime,
                            subItem.EndTime,
                            CompleteProgress = -1,
                            GuestList = visitorList?.Where(qitem => qitem.ReserveClassId == subItem.ReserveClassId)?.Select(qitem => new { GuestName = qitem.VisitorName })
                        });
                    }
                    var obj = new
                    {
                        ClassDate = Common.ConvertHelper.FormatDate(ClassDate),
                        WeekStr = Common.ConvertHelper.GetWeekDay(ClassDate),
                        SubList = subList
                    };
                    switch (ClassDate.DayOfWeek)
                    {
                        case DayOfWeek.Monday:
                            mondayList.Add(obj);
                            break;
                        case DayOfWeek.Tuesday:
                            tuesdayList.Add(obj);
                            break;
                        case DayOfWeek.Wednesday:
                            wednesdayList.Add(obj);
                            break;
                        case DayOfWeek.Thursday:
                            thursdayList.Add(obj);
                            break;
                        case DayOfWeek.Friday:
                            fridayList.Add(obj);
                            break;
                        case DayOfWeek.Saturday:
                            saturdayList.Add(obj);
                            break;
                        case DayOfWeek.Sunday:
                            sundayList.Add(obj);
                            break;
                    }
                }
            }
            return new { mondayList, tuesdayList, wednesdayList, thursdayList, fridayList, saturdayList, sundayList };
        }

        /// <summary>
        /// 获取教师课时统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public object GetTeacherClassHoursStatisticalModule(RB_Class_Plan_ViewModel query)
        {
            List<object> result = new List<object>();
            var BasicMinutes = class_ConfigRepository.GetBasicMinutesRepository(query.Group_Id);
            var list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
            if (list != null)
            {
                var groupList = list.GroupBy(qitem => new { qitem.Teacher_Id, qitem.TeacherName }).Select(qitem => new { qitem.Key.Teacher_Id, qitem.Key.TeacherName });
                foreach (var item in groupList)
                {
                    var totalClassMinutes = list.Where(qitem => qitem.Teacher_Id == item.Teacher_Id)?.Sum(qitem => qitem.ClassMinutes) ?? 0;
                    var obj = new
                    {
                        item.TeacherName,
                        TeacherId = item.Teacher_Id,
                        ClassHours = totalClassMinutes / BasicMinutes
                    };
                    result.Add(obj);
                }
            }
            return result;
        }


        /// <summary>
        /// 教室使用记录
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="ClassType">课程类型(0-全部,1-正常,2-试听课)</param>
        /// <returns></returns>
        public object GetClassUseStatisticalModule(RB_Class_Plan_ViewModel query, int ClassType = 0)
        {
            List<object> mondayList = new List<object>();
            List<object> tuesdayList = new List<object>();
            List<object> wednesdayList = new List<object>();
            List<object> thursdayList = new List<object>();
            List<object> fridayList = new List<object>();
            List<object> saturdayList = new List<object>();
            List<object> sundayList = new List<object>();
            var startTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekMonday(query.StartTime));
            var endTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekSunday(query.EndTime));
            TimeSpan sp = endTime.Subtract(startTime);
            var days = sp.Days + 1;

            //正常上课计划列表
            var list = new List<RB_Class_Plan_ViewModel>();
            //试听课列表
            var reserveList = new List<RB_Reserve_Class_Extend>();
            List<RB_Class_RoomUseLog_ViewModel> roomUserLogList = new List<RB_Class_RoomUseLog_ViewModel>();
            var reserveQuery = new RB_Reserve_Class_Extend()
            {
                StartClassDate = query.StartTime,
                EndClassDate = query.EndTime,
                TeacherId = query.TeacherId,
                ClassRoomId = query.ClassRoomId
            };
            if (ClassType == 0)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }
            else if (ClassType == 1)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
            }
            else if (ClassType == 2)
            {
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }

            if (list != null || reserveList != null)
            {
                var timeList = new List<RB_Class_Time_ViewModel>();
                var guestList = new List<RB_Order_Guest_ViewModel>();
                List<int> classRoomId = new List<int>();

                if (list != null && list.Any())
                {
                    classRoomId.AddRange(list.Select(x => x.ClassRoomId).ToList());
                }

                if (reserveList != null && reserveList.Any())
                {
                    classRoomId.AddRange(reserveList.Select(x => x.ClassRoomId).ToList());
                }

                string roomIds = string.Join(",", classRoomId.Select(x => x).Distinct());
                if (!string.IsNullOrWhiteSpace(roomIds))
                {
                    roomUserLogList = class_RoomUseLogRepository.GetClassRoomUseLogListRepository(new RB_Class_RoomUseLog_ViewModel { StartTime = query.StartTime, EndTime = query.EndTime, QRoomIds = roomIds });
                }
                if (list != null && list.Count > 0)
                {
                    string classIds = string.Join(",", list.Select(qitem => qitem.ClassId));
                    string planIds = string.Join(",", list.Select(qitem => qitem.ClassPlanId));
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassIds = classIds, QClassPlanIds = planIds });
                    guestList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel() { ClassIds = classIds });
                }
                var visitorList = new List<RB_Visitor_Reserve_Extend>();
                if (reserveList != null && reserveList.Count > 0)
                {
                    string reserveClassIds = string.Join(",", reserveList.Select(qitem => qitem.ReserveClassId));
                    visitorList = visitor_ReserveRepository.GetVisitorReserveListRepository(new Model.ViewModel.Reserve.RB_Visitor_Reserve_Extend()
                    {
                        Q_ReserveClassIds = reserveClassIds
                    });
                }

                for (var i = 0; i < days; i++)
                {
                    var ClassDate = startTime.AddDays(i);
                    List<object> subList = new List<object>();
                    var tempRoomUseLogList = roomUserLogList.Where(qitem => qitem.CreateTime.ToString("yyyy-MM-dd") == ClassDate.ToString("yyyy-MM-dd")).OrderBy(qitem => qitem.CreateTime);
                    foreach (var subItem in tempRoomUseLogList)
                    {
                        subList.Add(new
                        {
                            Id = subItem.RoomErrorId,
                            ClassType = 2,
                            ClassId = 0,
                            ClassName = subItem.ErrorTitle,
                            CourseName = subItem.ErrorContent,
                            RoomName = "",
                            TeacherName = "",
                            TimeStr = Common.ConvertHelper.GetTimeStr(subItem.CreateTime),
                            StartTime = subItem.CreateTime.ToString("HHLmm"),
                            EndTime = subItem.UpdateTime.HasValue ? subItem.UpdateTime.Value.ToString("HHLmm") : "未知",
                            CompleteProgress = -1,
                            GuestList = new List<string>(),
                            Type = subItem.ErrorType,// 0-班级，1-使用记录，2-异常记录
                        });
                    }


                    var tempList = list.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.StartTime);
                    foreach (var subItem in tempList)
                    {
                        var tempTimeList = timeList?.Where(qitem => qitem.ClassPlanId == subItem.ClassPlanId)?.ToList()?.OrderBy(qitem => qitem.StartTime);
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (tempTimeList?.FirstOrDefault()?.StartTime ?? "");
                        foreach (var item in tempTimeList)
                        {
                            subList.Add(new
                            {
                                Id = item?.ClassTimeId ?? 0,
                                ClassType = 1,
                                subItem.ClassId,
                                subItem.ClassName,
                                subItem.CourseName,
                                subItem.RoomName,
                                subItem.TeacherName,
                                subItem.CompleteProgress,
                                TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                                StartTime = item?.StartTime ?? "",
                                EndTime = item?.EndTime ?? "",
                                GuestList = guestList.Where(qitem => qitem.ClassId == subItem.ClassId && (qitem.GuestState == 1 || qitem.GuestState == 6 || ((qitem.GuestState == 5 || qitem.GuestState == 7) && qitem.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(qitem.ChangeEffectTime)) >= subItem.ClassDate)))?.Select(qitem => new { qitem.GuestName }),
                                Type = 0,// 0-班级，1-使用记录，2-异常记录
                            });
                        }

                    }

                    var tempReserveList = reserveList.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.ClassTime);
                    foreach (var subItem in tempReserveList)
                    {
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (subItem?.ClassTime ?? "");
                        subList.Add(new
                        {
                            Id = subItem.ReserveClassId,
                            ClassType = 2,
                            ClassId = subItem.ReserveClassId,
                            ClassName = "试听课",
                            CourseName = subItem.ClassContent,
                            subItem.RoomName,
                            subItem.TeacherName,
                            TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                            StartTime = subItem.ClassTime,
                            subItem.EndTime,
                            CompleteProgress = -1,
                            GuestList = visitorList?.Where(qitem => qitem.ReserveClassId == subItem.ReserveClassId)?.Select(qitem => new { GuestName = qitem.VisitorName }),
                            Type = 0,// 0-班级，1-使用记录，2-异常记录
                        });
                    }
                    var obj = new
                    {
                        ClassDate = Common.ConvertHelper.FormatDate(ClassDate),
                        WeekStr = Common.ConvertHelper.GetWeekDay(ClassDate),
                        SubList = subList
                    };
                    switch (ClassDate.DayOfWeek)
                    {
                        case DayOfWeek.Monday:
                            mondayList.Add(obj);
                            break;
                        case DayOfWeek.Tuesday:
                            tuesdayList.Add(obj);
                            break;
                        case DayOfWeek.Wednesday:
                            wednesdayList.Add(obj);
                            break;
                        case DayOfWeek.Thursday:
                            thursdayList.Add(obj);
                            break;
                        case DayOfWeek.Friday:
                            fridayList.Add(obj);
                            break;
                        case DayOfWeek.Saturday:
                            saturdayList.Add(obj);
                            break;
                        case DayOfWeek.Sunday:
                            sundayList.Add(obj);
                            break;
                    }
                }
            }
            return new { mondayList, tuesdayList, wednesdayList, thursdayList, fridayList, saturdayList, sundayList };
        }





        /// <summary>
        /// 教师-我的排课
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="ClassType">课程类型(0-全部,1-正常,2-试听课)</param>
        /// <returns></returns>
        public object GetTeacherPlanStatisticalModule(RB_Class_Plan_ViewModel query, int ClassType = 0)
        {
            List<object> mondayList = new List<object>();
            List<object> tuesdayList = new List<object>();
            List<object> wednesdayList = new List<object>();
            List<object> thursdayList = new List<object>();
            List<object> fridayList = new List<object>();
            List<object> saturdayList = new List<object>();
            List<object> sundayList = new List<object>();
            var startTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekMonday(query.StartTime));
            var endTime = Convert.ToDateTime(Common.ConvertHelper.GetWeekSunday(query.EndTime));
            TimeSpan sp = endTime.Subtract(startTime);
            var days = sp.Days + 1;

            //正常上课计划列表
            var list = new List<RB_Class_Plan_ViewModel>();
            //试听课列表
            var reserveList = new List<RB_Reserve_Class_Extend>();

            var reserveQuery = new RB_Reserve_Class_Extend()
            {
                StartClassDate = query.StartTime,
                EndClassDate = query.EndTime,
                TeacherId = query.TeacherId,
                ClassRoomId = query.ClassRoomId
            };
            if (ClassType == 0)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }
            else if (ClassType == 1)
            {
                list = class_PlanRepository.GetClassPlanStatisticalRepository(query);
            }
            else if (ClassType == 2)
            {
                reserveList = reserve_ClassRepository.GetReserveClassListRepository(reserveQuery);
            }

            if (list != null || reserveList != null)
            {
                var timeList = new List<RB_Class_Time_ViewModel>();
                var guestList = new List<RB_Order_Guest_ViewModel>();
                if (list != null && list.Count > 0)
                {
                    string classIds = string.Join(",", list.Select(qitem => qitem.ClassId));
                    string planIds = string.Join(",", list.Select(qitem => qitem.ClassPlanId));
                    timeList = class_TimeRepository.GetClassTimeListRepository(new RB_Class_Time_ViewModel() { QClassIds = classIds, QClassPlanIds = planIds });
                    guestList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel() { ClassIds = classIds });
                }
                var visitorList = new List<RB_Visitor_Reserve_Extend>();
                if (reserveList != null && reserveList.Count > 0)
                {
                    string reserveClassIds = string.Join(",", reserveList.Select(qitem => qitem.ReserveClassId));
                    visitorList = visitor_ReserveRepository.GetVisitorReserveListRepository(new Model.ViewModel.Reserve.RB_Visitor_Reserve_Extend()
                    {
                        Q_ReserveClassIds = reserveClassIds
                    });
                }

                for (var i = 0; i < days; i++)
                {
                    var ClassDate = startTime.AddDays(i);
                    List<object> subList = new List<object>();

                    var tempList = list.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.StartTime);
                    foreach (var subItem in tempList)
                    {
                        var tempTimeList = timeList?.Where(qitem => qitem.ClassPlanId == subItem.ClassPlanId)?.ToList()?.OrderBy(qitem => qitem.StartTime);
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (tempTimeList?.FirstOrDefault()?.StartTime ?? "");
                        foreach (var item in tempTimeList)
                        {
                            subList.Add(new
                            {
                                Id = item?.ClassTimeId ?? 0,
                                ClassType = 1,
                                subItem.ClassId,
                                subItem.ClassName,
                                subItem.CourseName,
                                subItem.RoomName,
                                subItem.TeacherName,
                                subItem.CompleteProgress,
                                subItem.TotalPlanNum,
                                subItem.Ranks,
                                TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                                StartTime = item?.StartTime ?? "",
                                EndTime = item?.EndTime ?? "",
                                GuestList = guestList.Where(qitem => qitem.ClassId == subItem.ClassId && (qitem.GuestState == 1 || qitem.GuestState == 6 || ((qitem.GuestState == 5 || qitem.GuestState == 7) && qitem.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(qitem.ChangeEffectTime)) >= subItem.ClassDate)))?.Select(qitem => new { qitem.GuestName })

                            });
                        }

                    }

                    var tempReserveList = reserveList.Where(qitem => qitem.ClassDate == ClassDate).OrderBy(qitem => qitem.ClassTime);
                    foreach (var subItem in tempReserveList)
                    {
                        var currentDate = Common.ConvertHelper.FormatDate(ClassDate) + " " + (subItem?.ClassTime ?? "");
                        subList.Add(new
                        {
                            Id = subItem.ReserveClassId,
                            ClassType = 2,
                            ClassId = subItem.ReserveClassId,
                            ClassName = "试听课",
                            CourseName = subItem.ClassContent,
                            subItem.RoomName,
                            subItem.TeacherName,
                            TotalPlanNum = 0,
                            Ranks = 0,
                            TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                            StartTime = subItem.ClassTime,
                            subItem.EndTime,
                            CompleteProgress = -1,
                            GuestList = visitorList?.Where(qitem => qitem.ReserveClassId == subItem.ReserveClassId)?.Select(qitem => new { GuestName = qitem.VisitorName }),

                        });
                    }
                    var obj = new
                    {
                        ClassDate = Common.ConvertHelper.FormatDate(ClassDate),
                        WeekStr = Common.ConvertHelper.GetWeekDay(ClassDate),
                        SubList = subList
                    };
                    switch (ClassDate.DayOfWeek)
                    {
                        case DayOfWeek.Monday:
                            mondayList.Add(obj);
                            break;
                        case DayOfWeek.Tuesday:
                            tuesdayList.Add(obj);
                            break;
                        case DayOfWeek.Wednesday:
                            wednesdayList.Add(obj);
                            break;
                        case DayOfWeek.Thursday:
                            thursdayList.Add(obj);
                            break;
                        case DayOfWeek.Friday:
                            fridayList.Add(obj);
                            break;
                        case DayOfWeek.Saturday:
                            saturdayList.Add(obj);
                            break;
                        case DayOfWeek.Sunday:
                            sundayList.Add(obj);
                            break;
                    }
                }
            }
            return new { mondayList, tuesdayList, wednesdayList, thursdayList, fridayList, saturdayList, sundayList };
        }



        /// <summary>
        /// 获取班级上课记录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<object> GetTeacherPlan(int pageIndex, int pageSize, out long rowsCount, RB_Class_Plan_ViewModel query)
        {
            List<object> result = new List<object>();
            var list = class_PlanRepository.GetTeacherPlan(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Any())
            {
                var guestList = new List<RB_Order_Guest_ViewModel>();
                var visitorList = new List<RB_Visitor_Reserve_Extend>();
                if (list.Any(x => x.ClassType == 1))
                {
                    string classIds = string.Join(",", list.Where(x => x.ClassType == 1).Select(qitem => qitem.ClassId));
                    guestList = order_GuestRepository.GetOrderGuestListRepository(new RB_Order_Guest_ViewModel() { ClassIds = classIds });
                }
                if (list.Any(x => x.ClassType == 2))
                {
                    string reserveClassIds = string.Join(",", list.Select(qitem => qitem.ClassTimeId));
                    visitorList = visitor_ReserveRepository.GetVisitorReserveListRepository(new Model.ViewModel.Reserve.RB_Visitor_Reserve_Extend()
                    {
                        Q_ReserveClassIds = reserveClassIds
                    });
                }
                foreach (var item in list)
                {
                    var currentDate = Common.ConvertHelper.FormatDate(item.ClassDate) + " " + (item.StartTime ?? "");
                    List<object> GuestList = new List<object>();

                    result.Add(new
                    {
                        Id = item?.ClassTimeId ?? 0,
                        item.ClassType,
                        item.ClassId,
                        item.ClassName,
                        item.CourseName,
                        item.RoomName,
                        item.TeacherName,
                        item.TotalPlanNum,
                        ClassDate = item.ClassDate.ToString("yyyy-MM-dd"),
                        item.Ranks,
                        TimeStr = Common.ConvertHelper.GetTimeStr(currentDate),
                        StartTime = item?.StartTime ?? "",
                        EndTime = item?.EndTime ?? "",
                        GuestList = item.ClassType == 1 ? guestList.Where(qitem => qitem.ClassId == item.ClassId && (qitem.GuestState == 1 || qitem.GuestState == 6 || ((qitem.GuestState == 5 || qitem.GuestState == 7) && qitem.ChangeEffectTime.HasValue && Convert.ToDateTime(Common.ConvertHelper.FormatDate(qitem.ChangeEffectTime)) >= item.ClassDate)))?.Select(qitem => new { qitem.GuestName }) : visitorList?.Where(qitem => qitem.ReserveClassId == item.ClassTimeId)?.Select(qitem => new { GuestName = qitem.VisitorName }),

                    });
                }
            }

            return result;
        }

        /// <summary>
        /// 查询指定计划的上课课时
        /// </summary>
        /// <param name="planId"></param>
        /// <returns></returns>
        public double GetCurrentPlanStudyHoursRepository(int planId)
        {
            return class_PlanRepository.GetCurrentPlanStudyHoursRepository(planId);
        }

        /// <summary>
        /// 更新历史班号
        /// </summary>
        /// <returns></returns>
        public bool UpdateHistoryClassNo()
        {
            var list = classRepository.GetClassListRepository(new RB_Class_ViewModel() { Group_Id = 100000 });
            list = list.OrderBy(x => x.OpenTime).ThenBy(x => x.ClassId).ToList();
            foreach (var model in list)
            {
                #region 生成班级号
                model.ClassNo = "";
                var classTypeModel = class_TypeRepository.GetEntity(model.ClassType);
                if (classTypeModel != null)
                {
                    model.ClassNo = classTypeModel.ClassNoPrefix;
                }
                //查询最大的字母
                int CNum = classRepository.GetMaxClassNoPrefix(model.OpenTime.ToString("yyyy-MM-dd"));
                if (CNum == 0)
                {
                    model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + "A";
                    model.ClassLetterNum = 1;
                }
                else
                {
                    string ClassNoLetterStr = Edu.Common.Config.ClassNoLetterStr;
                    model.ClassNo += model.OpenTime.ToString("yyyyMMdd") + ClassNoLetterStr.Substring(CNum, 1);
                    model.ClassLetterNum = CNum + 1;
                }
                #endregion
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_Class_ViewModel.ClassNo),model.ClassNo},
                    { nameof(RB_Class_ViewModel.ClassLetterNum),model.ClassLetterNum},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_Class_ViewModel.ClassId),
                          FiledValue=model.ClassId,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                classRepository.Update(keyValues, wheres);
            }
            return true;
        }




        #region 教师-我的班级
        /// <summary>
        /// 获取班级分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <param name="isGetStepPrice">是否查询阶梯价格</param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetTeacherClassPageList(int pageIndex, int pageSize, out long rowsCount, RB_Class_ViewModel query, bool isGetStepPrice = false)
        {
            var list = classRepository.GetClassPageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                var classTimeList = new List<RB_Class_Time_ViewModel>();
                // var classOrderList = new List<RB_Order_ViewModel>();
                var stepPriceList = new List<RB_Class_StepPrice_ViewModel>();
                var otherCourseList = new List<RB_Class_Course_Extend>();
                var maxPlanDateList = new List<RB_Class_Plan_ViewModel>();
                string ids = string.Join(",", list.Select(qitem => qitem.ClassId));
                if (!string.IsNullOrEmpty(ids))
                {
                    classTimeList = class_TimeRepository.GetClassTimeListRepository(ids);
                    // classOrderList = orderRepository.GetClassOrderPeopleNumRepository(ids);
                    otherCourseList = GetClassCourseListModule(new RB_Class_Course_Extend() { QClassIds = ids });
                    maxPlanDateList = class_PlanRepository.GetClassPlanMaxDataRepository(new RB_Class_Plan_ViewModel()
                    {
                        QClassIds = ids
                    });
                }
                if (isGetStepPrice)
                {
                    stepPriceList = class_StepPriceRepository.GetClassStepPriceListRepository(new RB_Class_StepPrice_ViewModel() { QClassIds = ids })?.ToList() ?? new List<RB_Class_StepPrice_ViewModel>();
                }

                //课程计划上课日期
                foreach (var item in list)
                {
                    var finish = maxPlanDateList?.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault()?.ClassDate;
                    item.FinishTimeStr = Common.ConvertHelper.FormatDate(finish);
                    item.NewPlanDateTime = classTimeList?.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault()?.NewPlanDateTime;

                    if (isGetStepPrice)
                    {
                        item.ClassStepPriceList = stepPriceList?.Where(qitem => qitem.ClassId == item.ClassId)?.ToList() ?? new List<RB_Class_StepPrice_ViewModel>();
                    }
                    item.OtherCourseList = otherCourseList?.Where(qitem => qitem.ClassId == item.ClassId)?.ToList() ?? new List<RB_Class_Course_Extend>();
                }
            }
            return list;
        }

        #endregion

        #region 教师-学员管理
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <param name="orderIds"></param>
        /// <returns></returns>
        public List<RB_Order_Guest_ViewModel> GetTeacherStudentPage(int pageIndex, int pageSize, out long rowsCount, RB_Order_Guest_ViewModel demodel)
        {
            var orderstudentList = order_GuestRepository.GetTeacherStudentPage(pageIndex, pageSize, out rowsCount, demodel);

            return orderstudentList;

        }

        /// <summary>
        /// 获取非本班学员的学员
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Order_Guest_ViewModel> GetNotClassStudentList(RB_Order_Guest_ViewModel demodel)
        {
            var orderstudentList = order_GuestRepository.GetNotClassStudentList(demodel);

            return orderstudentList;

        }




        #endregion


        #region 财务轧账
        /// <summary>
        /// 验证是否可以修改签到数据
        /// </summary>
        /// <param name="monthStr"></param>
        /// <param name="schoolId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public string ValidateClassCheckFinance(string monthStr, int schoolId, int group_Id)
        {
            var ralist = rollingAccountRepository.GetList(new Model.ViewModel.Finance.RB_RollingAccount_Extend() { GroupId = group_Id, Month = monthStr, State = 1 });
            if (ralist.Any())
            {
                if (ralist.Where(x => x.SchoolIds == "-1" || ("," + x.SchoolIds + ",").Contains("," + schoolId + ",")).Any())
                {
                    return "该单据所在交易日期已关帐,无法修改签到数据";
                }
            }
            return "";
        }

        #endregion

    }
}
