using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Course;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Module.Course;
using Edu.Module.Finance;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class TeacherClassController : BaseController
    {
        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = AOP.AOPHelper.CreateAOPObject<ClassModule>();

        /// <summary>
        /// 财务处理类对象
        /// </summary>
        private readonly FinanceModule financeModule = AOP.AOPHelper.CreateAOPObject<FinanceModule>();
        #region 班级管理
        /// <summary>
        /// 获取班级页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Class_ViewModel()
            {
                ClassName = base.ParmJObj.GetStringValue("ClassName"),
                ClassStatus = (ClassStatusEnum)base.ParmJObj.GetInt("ClassStatus"),
                Teacher_Id = base.ParmJObj.GetInt("Teacher_Id"),
                CouseId = base.ParmJObj.GetInt("CouseId"),
                School_Id = base.ParmJObj.GetInt("School_Id"),
                TeacherName = base.ParmJObj.GetStringValue("TeacherName"),
                CourseName = base.ParmJObj.GetStringValue("CourseName"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            List<object> result = new List<object>();
            var list = classModule.GetClassPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.ClassId,
                    item.ClassName,
                    item.School_Id,
                    item.SchoolName,
                    item.CourseName,
                    item.Teacher_Id,
                    item.TeacherName,
                    item.ClassPersion,
                    item.TotalPlanNum,
                    item.UsePlanNum,
                    OpenTime = Common.ConvertHelper.FormatDate(item.OpenTime),
                    item.ClassStatusStr,
                    item.NewPlanDateTime,
                    item.ClassStatus,
                    item.Status,
                    item.OtherCourseList,
                    item.ClassNo,
                    item.CouseId,
                    item.FinishTimeStr,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }
        #endregion

        #region 我的课表
        /// <summary>
        /// 老师上课计划统计
        /// </summary>
        /// <returns></returns>
        public ApiResult GetClassPlanStatistical()
        {
            var startDate = base.ParmJObj.GetStringValue("StartTime");
            var endDate = base.ParmJObj.GetStringValue("EndTime");
            DateTime now = DateTime.Now;

            //获取当前月的第一天
            DateTime d1 = new DateTime(now.Year, now.Month, 1);
            //当月最后一天
            DateTime d2 = d1.AddMonths(1).AddDays(-1);
            if (string.IsNullOrEmpty(startDate))
            {
                startDate = Common.ConvertHelper.FormatDate(d1);
            }
            if (string.IsNullOrEmpty(endDate))
            {
                endDate = Common.ConvertHelper.FormatDate(d2);
            }
            else
            {
                //if (Convert.ToDateTime(endDate) > Convert.ToDateTime(Common.ConvertHelper.FormatDate(DateTime.Now)))
                //{
                //    endDate = Common.ConvertHelper.FormatDate(DateTime.Now);
                //}
            }
            RB_Class_Plan_ViewModel query = new RB_Class_Plan_ViewModel()
            {
                StartTime = startDate,
                EndTime = endDate,
                TeacherId = base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId = base.ParmJObj.GetInt("ClassRoomId"),
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
                ClassId = base.ParmJObj.GetInt("ClassId"),
            };
            //课程类型(0-全部,1-正常,2-试听课)
            int ClassType = base.ParmJObj.GetInt("ClassType");
            var obj = classModule.GetTeacherPlanStatisticalModule(query, ClassType: ClassType);
            return ApiResult.Success(data: obj);
        }



        /// <summary>
        /// 老师上课计划统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassPlanStatisticalPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var startDate = base.ParmJObj.GetStringValue("StartTime");
            var endDate = base.ParmJObj.GetStringValue("EndTime");
            DateTime now = DateTime.Now;

            //获取当前月的第一天
            DateTime d1 = new DateTime(now.Year, now.Month, 1);
            //当月最后一天
            DateTime d2 = d1.AddMonths(1).AddDays(-1);
            if (string.IsNullOrEmpty(startDate))
            {
                startDate = Common.ConvertHelper.FormatDate(d1);
            }
            if (string.IsNullOrEmpty(endDate))
            {
                endDate = Common.ConvertHelper.FormatDate(d2);
            }
            else
            {
                //if (Convert.ToDateTime(endDate) > Convert.ToDateTime(Common.ConvertHelper.FormatDate(DateTime.Now)))
                //{
                //    endDate = Common.ConvertHelper.FormatDate(DateTime.Now);
                //}
            }
            RB_Class_Plan_ViewModel query = new RB_Class_Plan_ViewModel()
            {
                StartTime = startDate,
                EndTime = endDate,
                TeacherId = base.ParmJObj.GetInt("TeacherId"),
                ClassRoomId = base.ParmJObj.GetInt("ClassRoomId"),
                Group_Id = base.UserInfo.Group_Id,
                School_Id = -1,
                ClassId = base.ParmJObj.GetInt("ClassId"),
                ClassType = base.ParmJObj.GetInt("ClassType")// //课程类型(0-全部,1-正常,2-试听课)
            };
            var result = classModule.GetTeacherPlan(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }


        #endregion


        #region 月度耗课
        /// <summary>
        /// 班级月度课耗统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassMonthStatistics()
        {
            var userInfo = base.UserInfo;
            int classId = base.ParmJObj.GetInt("ClassId", 0);

            var list = financeModule.GetTeacherClassMonthStatistics(classId, userInfo);
            return ApiResult.Success("", list);
        }

        #endregion


        #region 学员管理
        /// <summary>
        /// 获取学员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassStudent()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());

            var model = new RB_Order_Guest_ViewModel
            {
                Teacher_Id = base.ParmJObj.GetInt("Teacher_Id", 0),
                GuestName = base.ParmJObj.GetStringValue("GuestName"),
                ClassId = base.ParmJObj.GetInt("ClassId", 0),
                CourseId = base.ParmJObj.GetInt("CourseId", 0),
                EnterID = base.ParmJObj.GetInt("EnterID", 0),
            };
            model.Group_Id = base.UserInfo.Group_Id;
            var list = classModule.GetTeacherStudentPage(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, model);
            List<object> result = new List<object>();

            foreach (var item in list)
            {
                if (item.EnterID > 0)
                {
                    item.EnterName = UserReidsCache.GetUserLoginInfo(item.EnterID)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.Id,
                    item.GuestName,
                    item.Mobile,
                    item.ClassId,
                    item.ClassName,
                    item.CourseId,
                    item.CourseName,
                    item.GuestState,
                    item.GuestStateStr,
                    item.School_Id,
                    item.Teacher_Id,
                    item.TotalPlanNum,
                    item.Ranks,
                    item.EnterName,
                    item.EnterID,
                    item.IsRenewOrder,
                    item.RenewOrderId
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }




        /// <summary>
        /// 获取学员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetNotClassStudentList()
        {
            var model = new RB_Order_Guest_ViewModel
            {
                Teacher_Id = base.ParmJObj.GetInt("Teacher_Id", 0),
                GuestName = base.ParmJObj.GetStringValue("GuestName"),
                ClassId = base.ParmJObj.GetInt("ClassId", 0),
                CourseId = base.ParmJObj.GetInt("CourseId", 0),
                EnterID = base.ParmJObj.GetInt("EnterID", 0),
            };
            model.Group_Id = base.UserInfo.Group_Id;
            var list = classModule.GetNotClassStudentList(model);
            List<object> result = new List<object>();
            foreach (var item in list)
            {
                if (item.EnterID > 0)
                {
                    item.EnterName = UserReidsCache.GetUserLoginInfo(item.EnterID)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.Id,
                    item.GuestName,
                    item.Mobile,
                    item.ClassId,
                    item.ClassName,
                    item.CourseId,
                    item.CourseName,
                    item.GuestState,
                    item.GuestStateStr,
                    item.School_Id,
                    item.Teacher_Id,
                    item.TotalPlanNum,
                    item.Ranks,
                    item.EnterName,
                    item.EnterID,
                    item.IsRenewOrder,
                    item.RenewOrderId
                });
            }
            return ApiResult.Success(data: result);
        }
        #endregion
    }
}
