using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Module.Course;
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 ClassController : BaseController
    {
        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = new ClassModule();

        #region 班级管理
        /// <summary>
        /// 获取班级页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            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 ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = classModule.GetClassListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClass()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.UpdateBy = UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = classModule.SetClassModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取班级实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseCategory()
        {
            var ClassId = base.ParmJObj.GetInt("ClassId", 0);
            var extModel = classModule.GetClassModule(ClassId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除班级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveClass()
        {
            var ClassId = base.ParmJObj.GetInt("ClassId", 0);
            var flag = classModule.RemoveClassModule(ClassId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion
    }
}
