using System;
using System.Collections.Generic;
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.Module.Course;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class CourseController : BaseController
    {
        /// <summary>
        /// 课程分类处理类
        /// </summary>
        private readonly CourseCategoryModule categoryModule = new CourseCategoryModule();

        /// <summary>
        /// 课程处理类对象
        /// </summary>
        private readonly CourseModule courseModule = AOP.AOPHelper.CreateAOPObject<CourseModule>();

        #region 课程分类管理
        /// <summary>
        /// 获取课程分类分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseCategoryPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Course_Category_ViewModel()
            {
                CateName = base.ParmJObj.GetStringValue("CateName"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = categoryModule.GetCourseCategoryPageListModule(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 GetCourseCategoryList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Category_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = categoryModule.GetCourseCategoryListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取课程分类树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChildCategoryList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Category_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var cateIds = base.ParmJObj.GetStringValue("cateIds");
            var list = categoryModule.GetChildCategoryStringModule(cateIds);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取课程分类树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseCategoryTree()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Category_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = categoryModule.GetCourseCategoryTreeListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改课程分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseCategory()
        {
            var extModel = new RB_Course_Category_ViewModel()
            {
                CateId = base.ParmJObj.GetInt("CateId"),
                CateName = base.ParmJObj.GetStringValue("CateName"),
                ParentId = base.ParmJObj.GetInt("ParentId"),
                SortNum = base.ParmJObj.GetInt("SortNum"),
                Status = (DateStateEnum)base.ParmJObj.GetInt("Status")
            };
            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 = categoryModule.SetCourseCategoryModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseCategory()
        {
            var CateId = base.ParmJObj.GetInt("CateId", 0);
            var extModel = categoryModule.GetCourseCategoryModule(CateId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除课程分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourseCategory()
        {
            var CateId = base.ParmJObj.GetInt("CateId", 0);
            var flag = categoryModule.RemoveCourseCategoryModule(CateId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程管理

        /// <summary>
        /// 获取课程分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCoursePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Course_ViewModel()
            {
                CourseName = base.ParmJObj.GetStringValue("CourseName"),
                QCateIds = base.ParmJObj.GetStringValue("QCateIds"),
                Status = (DateStateEnum)base.ParmJObj.GetInt("Status"),
                IsQPrice = base.ParmJObj.GetInt("IsQPrice"),
                IsQTeacher = base.ParmJObj.GetInt("IsQTeacher"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetCoursePageListModule(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 GetCourseList()
        {
            var query = new RB_Course_ViewModel()
            {
                IsQPrice = base.ParmJObj.GetInt("IsQPrice", 0),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetCourseListModule(query);
            return ApiResult.Success(data: list.Select(qitem => new
            {
                qitem.CourseId,
                qitem.CourseName,
                qitem.CateId,
                qitem.CateName,
                qitem.CoverImg,
                qitem.TeacherIdList,
                qitem.OriginalPrice,
                qitem.SellPrice,
                qitem.SalePlatList,
                qitem.SalePlatArr,
                qitem.ClassHours,
                PreferentialList = qitem?.PreferentialList?.Select(sItem => new
                {
                    sItem.Id,
                    sItem.PriceDiscountType,
                    sItem.PriceDiscountTypeStr,
                    sItem.CourseId,
                    sItem.SaleCommissionType,
                    sItem.SaleCommissionMoney,
                    sItem.B2BCommissionType,
                    sItem.B2BCommissionMoney,
                    sItem.BuyNum,
                    sItem.SendNum,
                    sItem.PriceType,
                    sItem.PriceMoney
                })
            }));
        }


        /// <summary>
        /// 添加修改课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourse()
        {
            var extModel = new RB_Course_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                CoverImg = base.ParmJObj.GetStringValue("CoverImg"),
                CourseName = base.ParmJObj.GetStringValue("CourseName"),
                CourseIntro = base.ParmJObj.GetStringValue("CourseIntro"),
                CateId = base.ParmJObj.GetInt("CateId"),
                Teacher_Ids = base.ParmJObj.GetStringValue("Teacher_Ids"),
                CourseFeature = base.ParmJObj.GetStringValue("CourseFeature"),
                IsOpenStepPrice = base.ParmJObj.GetInt("IsOpenStepPrice"),
                IsRenew = base.ParmJObj.GetInt("IsRenew"),
                RenewOgPrice = base.ParmJObj.GetDecimal("RenewOgPrice"),
                RenewSlPrice = base.ParmJObj.GetDecimal("RenewSlPrice"),
                Saleplat = base.ParmJObj.GetStringValue("Saleplat"),
                IsKCourse = base.ParmJObj.GetInt("IsKCourse"),
                ClassHours = base.ParmJObj.GetDecimal("ClassHours")
            };
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetCourseModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourse()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            var IsGetDiscount = base.ParmJObj.GetInt("IsGetDiscount", 0);
            var extModel = courseModule.GetCourseModule(CourseId, IsGetDiscount: IsGetDiscount);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 获取销售端口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSaleplatList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(SaleplatEnum));
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 删除课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourse()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            var flag = courseModule.RemoveCourseModule(CourseId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 是否显示章节编号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseIsShowChapterNo()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            int IsShowChapterNo = base.ParmJObj.GetInt("IsShowChapterNo", 0);
            var flag = courseModule.SetCourseIsShowChapterNoModule(CourseId, IsShowChapterNo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 设置课程教师
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseTeacher()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            int Teacher_Id = base.ParmJObj.GetInt("Teacher_Id", 0);
            var flag = courseModule.SetCourseTeacherModule(CourseId, Teacher_Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 教案管理

        /// <summary>
        /// 获取课程教案分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseTeachPlanPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_TeachPlan_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetCourseTeachPlanPageListModule(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 SetTeachPlan()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_TeachPlan_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetTeachPlanModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程教案
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTeachPlan()
        {
            var PlanId = base.ParmJObj.GetInt("PlanId", 0);
            var extModel = courseModule.GetTeachPlanModule(PlanId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除课程教案
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveTeachPlan()
        {
            var PlanId = base.ParmJObj.GetInt("PlanId", 0);
            var flag = courseModule.RemoveTeachPlanModule(PlanId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 课程章节管理

        /// <summary>
        /// 添加修改课程章节
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetChapter()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetChapterModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程章节列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChapterList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetChapterListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取章节树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChapterTree()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetChapterTreeListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 删除课程章节
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveChapter()
        {
            var ChapterId = base.ParmJObj.GetInt("ChapterId", 0);
            var flag = courseModule.RemoveChapterModule(ChapterId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程章节详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChapter()
        {
            var ChapterId = base.ParmJObj.GetInt("ChapterId", 0);
            var extModel = courseModule.GetChapterModule(ChapterId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 设置章节开发状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetChapterOpenStatus()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            bool flag = courseModule.SetChapterOpenStatusModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 设置章节开发状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetChapterProgress()
        {
            var ChapterId = base.ParmJObj.GetInt("ChapterId", 0);
            var Progress = base.ParmJObj.GetInt("Progress", 0);
            bool flag = courseModule.SetChapterProgressModule(ChapterId, Progress);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程通知管理
        /// <summary>
        /// 获取课程通知分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetNoticePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Notice_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetNoticePageListModule(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 SetNotice()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Notice_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetNoticeModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetNotice()
        {
            var NoticeId = base.ParmJObj.GetInt("NoticeId", 0);
            var extModel = courseModule.GetNoticeModule(NoticeId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除课程通知
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveNotice()
        {
            var NoticeId = base.ParmJObj.GetInt("NoticeId", 0);
            var flag = courseModule.RemoveNoticeModule(NoticeId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 新增通知阅读
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult AddNoticeRevice()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_NoticeRevice_ViewModel>(RequestParm.Msg.ToString());
            var flag = courseModule.AddNoticeReviceModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程讨论管理
        /// <summary>
        /// 获取课程讨论分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDiscussPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Discuss_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetDiscussPageListModule(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 SetDiscuss()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Discuss_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetDiscussModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程讨论
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDiscuss()
        {
            var DiscussId = base.ParmJObj.GetInt("DiscussId", 0);
            var extModel = courseModule.GetDiscussModule(DiscussId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除课程讨论
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveDiscuss()
        {
            var DiscussId = base.ParmJObj.GetInt("DiscussId", 0);
            var flag = courseModule.RemoveDiscussModule(DiscussId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程配置管理

        /// <summary>
        /// 添加修改课程配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseSetting()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Setting_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetCourseSettingModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程资料管理

        /// <summary>
        /// 获取课程资料分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSourcePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Source_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetSourcePageListModule(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 SetCourseSource()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Source_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            extModel.School_Id = this.UserInfo.School_Id;
            bool flag = courseModule.SetCourseSourceModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程资料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseSource()
        {
            var SourceId = base.ParmJObj.GetInt("SourceId", 0);
            var extModel = courseModule.GetCourseSourceModule(SourceId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 更新资料共享类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseSourceShareType()
        {
            var SourceId = base.ParmJObj.GetInt("SourceId", 0);
            var ShareType = base.ParmJObj.GetInt("ShareType", 0);
            var flag = courseModule.SetCourseSourceShareTypeModule(SourceId, ShareType);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除课程资料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourseSource()
        {
            var SourceId = base.ParmJObj.GetInt("SourceId", 0);
            var flag = courseModule.RemoveCourseSourceModule(SourceId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程作业管理

        /// <summary>
        /// 获取课程分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseJobPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Course_Job_ViewModel()
            {
                JobName = base.ParmJObj.GetStringValue("JobName")
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = courseModule.GetCourseJobPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 添加修改课程作业
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseJob()
        {
            var extModel = new RB_Course_Job_ViewModel()
            {
                JobId = base.ParmJObj.GetInt("JobId"),
                JobName = base.ParmJObj.GetStringValue("JobName"),
                JobType = base.ParmJObj.GetInt("JobType"),
                ScoreType = base.ParmJObj.GetInt("ScoreType"),
                TopicType = base.ParmJObj.GetInt("TopicType")
            };
            try
            {
                extModel.JobDetailsList = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Course_JobDetails_ViewModel>>(base.ParmJObj.GetStringValue("JobDetailsList"));
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetCourseJob");
            }
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.School_Id = base.UserInfo.School_Id;
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Status = DateStateEnum.Normal;
            bool flag = courseModule.SetCourseJobModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 修改作业其他设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseJobOtherConfig()
        {
            var extModel = new RB_Course_Job_ViewModel()
            {
                JobId = base.ParmJObj.GetInt("JobId"),
                ReciveClass = base.ParmJObj.GetStringValue("ReciveClass"),
                StartTime = base.ParmJObj.GetDateTime("StartTime"),
                EndTime = base.ParmJObj.GetDateTime("EndTime"),
                IsAllowDelay = base.ParmJObj.GetInt("IsAllowDelay"),
                PassBasicScore = base.ParmJObj.GetInt("PassBasicScore"),
                IsAgain = base.ParmJObj.GetInt("IsAgain"),
                AgainTimes = base.ParmJObj.GetInt("AgainTimes"),
                IsGetHighScore = base.ParmJObj.GetInt("IsGetHighScore"),
                IsLookAnswer = base.ParmJObj.GetInt("IsLookAnswer"),
                LookAnswerType = base.ParmJObj.GetInt("LookAnswerType"),
                IsLookScore = base.ParmJObj.GetInt("IsLookScore"),
                IsLookQTypeScore = base.ParmJObj.GetInt("IsLookQTypeScore"),
                IsPaste = base.ParmJObj.GetInt("IsPaste"),
                IsAgainShowTrueFalse = base.ParmJObj.GetInt("IsAgainShowTrueFalse"),
                IsOutOrder = base.ParmJObj.GetInt("IsOutOrder"),
                IsRandom = base.ParmJObj.GetInt("IsRandom"),
                RandomNum = base.ParmJObj.GetInt("RandomNum"),
                IsSendMsg = base.ParmJObj.GetInt("IsSendMsg"),
                SendMsgHour = base.ParmJObj.GetInt("SendMsgHour"),
                IsWork = base.ParmJObj.GetInt("IsWork"),
                WorkProcess = base.ParmJObj.GetInt("WorkProcess"),
                IsBlankobj = base.ParmJObj.GetInt("IsBlankobj"),
                IsCompletionIngoreCase = base.ParmJObj.GetInt("IsCompletionIngoreCase"),
                IsBlankIgnoreComma = base.ParmJObj.GetInt("IsBlankIgnoreComma"),
                IsMultiHalfScore = base.ParmJObj.GetInt("IsMultiHalfScore"),
            };
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            bool flag = courseModule.SetCourseJobOtherConfigModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除课程作业
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourseJob()
        {
            int JobId = base.ParmJObj.GetInt("JobId");
            int Status = base.ParmJObj.GetInt("Status");
            bool flag = courseModule.RemoveCourseJobModule(JobId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 课程优惠价格设置

        /// <summary>
        /// 获取课程优惠类型列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetCoursePreferentialType()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(CoursePriceDiscountEnum));
            if (list != null && list.Count > 0)
            {
                list.Insert(0, new EnumItem()
                {
                    Id = 0,
                    Name = "请选择"
                });
            }
            return ApiResult.Success(data: list.OrderBy(qitem => qitem.Id));
        }

        /// <summary>
        /// 获取课程优惠价格列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetCoursePreferentialList()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            var list = courseModule.GetCoursePreferentialListModule(new RB_Course_Preferential_Extend() { CourseId = CourseId });
            return ApiResult.Success(data: list.OrderBy(qitem => qitem.PriceDiscountType));
        }


        /// <summary>
        /// 批量设置课程优惠价格
        /// </summary>
        /// <returns></returns>
        public ApiResult SetBeatchCoursePreferential()
        {
            var courseObj = JObject.Parse(base.ParmJObj.GetStringValue("courseObj"));
            var courseModel = new RB_Course_ViewModel()
            {
                CourseId = courseObj.GetInt("CourseId"),
                OriginalPrice = courseObj.GetDecimal("OriginalPrice"),
                SellPrice = courseObj.GetDecimal("SellPrice"),
            };
            var priceObj = base.ParmJObj.GetStringValue("priceList");
            var list = new List<RB_Course_Preferential_Extend>();
            try
            {
                list = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Course_Preferential_Extend>>(priceObj);
                if (list != null && list.Count > 0)
                {
                    foreach (var extModel in list)
                    {
                        extModel.CreateTime = DateTime.Now;
                        extModel.CreateBy = base.UserInfo.Id;
                        extModel.UpdateBy = base.UserInfo.Id;
                        extModel.UpdateTime = DateTime.Now;
                        extModel.Group_Id = this.UserInfo.Group_Id;
                        extModel.School_Id = this.UserInfo.School_Id;
                        extModel.CourseId = courseModel.CourseId;
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetBeatchCoursePreferential");
            }
            var flag = courseModule.SetCoursePreferentialListModule(courseModel, list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion
    }
}