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;
using Edu.Common.Enum.Course;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Grade;
using Edu.Model.ViewModel.Scroll;
using Edu.Model.ViewModel.Sell;
using Edu.Module.Course;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class ScrollController : BaseController
    {
        /// <summary>
        /// 滚动开班处理类对象
        /// </summary>
        private readonly ScrollClassModule scrollClassModule = new ScrollClassModule();

        #region 班次

        /// <summary>
        /// 获取班次列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetScrollShiftList() {
            var userInfo = base.UserInfo;
            var list = scrollClassModule.GetScrollShiftList(userInfo.Group_Id);

            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                x.StartTime,
                x.EndTime,
                x.Color,
                x.UpdateBy,
                UpdateByName = UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm")
            }));
        }

        /// <summary>
        /// 新增修改班次
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetScrollShiftInfo() {
            var userInfo = base.UserInfo;
            RB_Scroll_Shift_ViewModel demodel = JsonHelper.DeserializeObject<RB_Scroll_Shift_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name)) {
                return ApiResult.ParamIsNull("请传递班次名称");
            }
            if (string.IsNullOrEmpty(demodel.StartTime)) {
                return ApiResult.ParamIsNull("请传递开始时间");
            }
            if (string.IsNullOrEmpty(demodel.EndTime)) {
                return ApiResult.ParamIsNull("请传递结束时间");
            }

            demodel.Status = 0;
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = scrollClassModule.SetScrollShift(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 删除班次
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelScrollShiftInfo() {
            var userInfo = base.UserInfo;
            int ShiftId = ParmJObj.GetInt("ShiftId", 0);
            if (ShiftId <= 0) {
                return ApiResult.ParamIsNull();
            }

            string msg = scrollClassModule.DelScrollShift(ShiftId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }
        #endregion

        #region 排班

        /// <summary>
        /// 获取月份排班计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetScrollPlanMonthList() {
            var userIno = base.UserInfo;
            RB_Scroll_Plan_ViewModel demodel = JsonHelper.DeserializeObject<RB_Scroll_Plan_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userIno.Group_Id;
            if (string.IsNullOrEmpty(demodel.STime) || string.IsNullOrEmpty(demodel.ETime)) {
                return ApiResult.ParamIsNull("请传递开始结束时间");
            }

            var list = scrollClassModule.GetScrollPlanMonthList(demodel);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 新增修改排班
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetScrollPlanInfo() {
            var userInfo = base.UserInfo;
            RB_Scroll_Plan_ViewModel demodel = JsonHelper.DeserializeObject<RB_Scroll_Plan_ViewModel>(base.RequestParm.Msg.ToString());
            if (demodel.AccountId <= 0) {
                return ApiResult.ParamIsNull("请传递老师ID");
            }
            if (demodel.ShiftId < 0) {
                return ApiResult.ParamIsNull("请选择班次");//0表示休息吧
            }
            if (demodel.ShiftId > 0 && demodel.RoomId <= 0) {
                return ApiResult.ParamIsNull("请传递教室ID");
            }

            demodel.Status = 0;
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = scrollClassModule.SetScrollPlanInfo(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }
        #endregion

        #region 上课时段

        /// <summary>
        /// 获取上课时段列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetScrollCourseTimeList()
        {
            var userInfo = base.UserInfo;
            var list = scrollClassModule.GetScrollCourseTimeList(userInfo.Group_Id);

            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                x.TimeList,
                x.UpdateBy,
                UpdateByName = UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm")
            }));
        }

        /// <summary>
        /// 新增修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetScrollCourseTimeInfo()
        {
            var userInfo = base.UserInfo;
            RB_Scroll_CourseTime_ViewModel demodel = JsonHelper.DeserializeObject<RB_Scroll_CourseTime_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name))
            {
                return ApiResult.ParamIsNull("请传递时段名称");
            }
            if (demodel.TimeList == null || !demodel.TimeList.Any())
            {
                return ApiResult.ParamIsNull("请传递时段");
            }
            foreach (var item in demodel.TimeList) {
                item.Minutes = (int)(Convert.ToDateTime("2020-01-01 " + item.EndTime) - Convert.ToDateTime("2020-01-01 " + item.StartTime)).TotalMinutes;
            }
            demodel.Content = JsonHelper.Serialize(demodel.TimeList);
            demodel.Status = 0;
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = scrollClassModule.SetScrollCourseTime(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelScrollCourseTimeInfo()
        {
            var userInfo = base.UserInfo;
            int CourseTimeId = ParmJObj.GetInt("CourseTimeId", 0);
            if (CourseTimeId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            string msg = scrollClassModule.DelScrollCourseTime(CourseTimeId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        #endregion
    }
}
