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

namespace Edu.WebApi.Controllers.EduTask
{
    /// <summary>
    /// 改变流程相关申请
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class StopStudentClassController : BaseController
    {
        /// <summary>
        /// 调课处理类对象
        /// </summary>
        private readonly StopStudentClassModule stopStudentClassModule = AOP.AOPHelper.CreateAOPObject<StopStudentClassModule>();

        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = AOP.AOPHelper.CreateAOPObject<ClassModule>();
        /// <summary>
        /// 新增修改班级调课
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudentStopClass()
        {
            var model = new RB_Student_StopClass_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ClassId = base.ParmJObj.GetInt("ClassId"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                OrderGuestId = base.ParmJObj.GetInt("OrderGuestId"),
                Remarks = base.ParmJObj.GetStringValue("Remarks"),
                OldStudentStatus= base.ParmJObj.GetInt("OldStudentStatus")
            };
            var classModel = classModule.GetClassModule(model.ClassId);
            string receiptFile = string.Empty;
            try
            {
                string takeEffectTime = base.ParmJObj.GetStringValue("TakeEffectTime");
                if (string.IsNullOrWhiteSpace(takeEffectTime))
                {
                    return ApiResult.Failed("请选择生效日期");
                }
                model.TakeEffectTime = Convert.ToDateTime(takeEffectTime);
                List<string> receiptFileList = Common.Plugin.JsonHelper.DeserializeObject<List<string>>(base.ParmJObj.GetStringValue("ReceiptFileList"));
                if (receiptFileList == null || !receiptFileList.Any())
                {
                    return ApiResult.Failed("附件必须上传");
                }
                else
                {
                    receiptFile = JsonHelper.Serialize(receiptFileList);
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetStudentStopClass");
            }
            
           
            if (model.Id == 0)
            {
                model.CreateBy = base.UserInfo.Id;
                model.CreateTime = DateTime.Now;
                model.Group_Id = base.UserInfo.Group_Id;
                model.School_Id = classModel.School_Id;
            }
            model.UpdateBy = base.UserInfo.Id;
            model.UpdateTime = DateTime.Now;
            model.IsAuditThrough = 0;
            bool flag = stopStudentClassModule.SetStudentStopClassModule(model, receiptFile);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }




        //public ApiResult TestUpdateClassPlan()
        //{

        //    int id = base.ParmJObj.GetInt("Id");
        //    var result = changeClassPlanModule.UpdateChangeClassPlan(id);
        //    return ApiResult.Success("");
        //}
    }
}
