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;
using Edu.Model.ViewModel.Mall;
using Edu.Model.ViewModel.Grade;

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>();

        /// <summary>
        /// 试听课处理类对象
        /// </summary>
        private readonly TrialLessonModule trialLessonModule = AOP.AOPHelper.CreateAOPObject<TrialLessonModule>();

        /// <summary>
        /// 滚动开班
        /// </summary>
        private readonly ScrollClassCommonModule scrollClassCommon = new ScrollClassCommonModule();


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

        /// <summary>
        /// 添加修改课程科目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseSubject()
        {
            var extModel = new RB_Course_Subject_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                SubjectName = base.ParmJObj.GetStringValue("SubjectName"),
                SubjectIcon = base.ParmJObj.GetStringValue("SubjectIcon"),
            };
            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.SetCourseSubjectModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程科目详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseSubject()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = categoryModule.GetCourseSubjectModule(Id);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除课程科目
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourseSubject()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var flag = categoryModule.RemoveCourseSubjectModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程重点枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseEmphasisEnumList() {
            var list = EnumHelper.EnumToList(typeof(CourseEmphasisEnum));
            return ApiResult.Success("", list.OrderBy(x => x.Id).Select(x => new
            {
                x.Name,
                x.Id
            }));
        }

        #endregion

        #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"),
                CourseSubject = base.ParmJObj.GetInt("CourseSubject"),
            };
            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"),
                CourseSubject=base.ParmJObj.GetInt("CourseSubject"),
            };
            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 课程管理

        [HttpPost]
        public ApiResult GetAllCourseChapterCount()
        {
            int groupId = base.UserInfo.Group_Id;
            int courseId = base.ParmJObj.GetInt("CourseId", 0);
            var list = courseModule.GetAllCourseChapterCountModule(groupId, courseId);
            return ApiResult.Success(data: list.Where(x => x.UnitCount > 0).Select(x => new
            {
                x.CourseId,
                x.CourseName,
                x.UnitCount,
                x.ChapterCount
            }));
        }

        /// <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"),
                Saleplat = base.ParmJObj.GetStringValue("Saleplat"),
                CourseSubject = base.ParmJObj.GetInt("CourseSubject"),
                Q_OrderBy = base.ParmJObj.GetInt("Q_OrderBy")
            };
            query.Group_Id = base.UserInfo.Group_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),
                CourseSubject=base.ParmJObj.GetInt("CourseSubject"),
                IsQuerySalePlat=base.ParmJObj.GetInt("IsQuerySalePlat"),
            };
        
            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"),
                CourseRate = (CourseRateEnum)base.ParmJObj.GetInt("CourseRate"),
                CourseSubject = base.ParmJObj.GetInt("CourseSubject"),
                B2BIcon = base.ParmJObj.GetStringValue("B2BIcon"),
                B2BBackground = base.ParmJObj.GetStringValue("B2BBackground"),
                CourseEmphasis = base.ParmJObj.GetStringValue("CourseEmphasis"),
                ContractInfo=base.ParmJObj.GetStringValue("ContractInfo"),
                IsScrollClass = base.ParmJObj.GetInt("IsScrollClass"),
                ScrollMinNum = base.ParmJObj.GetInt("ScrollMinNum"),
                ScrollMaxNum = base.ParmJObj.GetInt("ScrollMaxNum"),
                CourseTimeId = base.ParmJObj.GetInt("CourseTimeId"),
            };
            try
            {
                extModel.CategoryList = JsonHelper.DeserializeObject<List<RB_Goods_Category_Extend>>(base.ParmJObj.GetStringValue("CategoryList"));
                if (extModel.SalePlatList != null && extModel.SalePlatList.Any(x => x == 4))//有小程序的上架平台
                {
                    if (extModel.CategoryList == null || !extModel.CategoryList.Any())
                    {
                        return ApiResult.Failed("上架小程序必须选择小程序对应的分类");
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Plugin.LogHelper.Write(ex, "SetCourse");
            }
            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;
            if (extModel.CourseId > 0)
            {
                var oldModel = courseModule.GetCourseModule(extModel.CourseId, IsGetDiscount: 1);
                extModel.MallGoodsId = oldModel.MallGoodsId;
                extModel.PreferentialList = oldModel.PreferentialList;
                extModel.SellPrice = oldModel.SellPrice;
                extModel.OriginalPrice = oldModel.OriginalPrice;
            }
            bool flag = courseModule.SetCourseModule(extModel, out int courseId);
            if (flag) {
                if (extModel.IsScrollClass == 1) {
                    try
                    {
                        //创建当月虚拟班级
                        scrollClassCommon.CreateScrollClass(courseId.ToString(), this.UserInfo.Group_Id);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Write(ex, "CreateScrollClass失败");
                    }
                }
            }
            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);
            int Status = base.ParmJObj.GetInt("Status", 1);
            var flag = courseModule.RemoveCourseModule(CourseId, Status, out string message);
            return flag ? ApiResult.Success(message: message) : ApiResult.Failed(message: message);
        }

        /// <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 SetImportCourseChapter()
        {
            var query = new RB_Course_Chapter_ViewModel()
            {
                CourseIds = base.ParmJObj.GetStringValue("CourseIds"),
                NewCourseId = base.ParmJObj.GetInt("NewCourseId"),
                MaxLength = base.ParmJObj.GetInt("MaxLength")
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            query.CreateBy = base.UserInfo.Id;
            var flag = courseModule.ImportChapterModule(query);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 添加修改课程章节
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetChapter()
        {
            var extModel = new RB_Course_Chapter_ViewModel()
            {
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                ChapterNo = base.ParmJObj.GetStringValue("ChapterNo"),
                ChapterName = base.ParmJObj.GetStringValue("ChapterName"),
                ChapterContent = base.ParmJObj.GetStringValue("ChapterContent"),
                ParentId = base.ParmJObj.GetInt("ParentId"),
                OpenStatus = base.ParmJObj.GetInt("OpenStatus"),
                Progress = base.ParmJObj.GetInt("Progress"),
                StudyHours = base.ParmJObj.GetInt("StudyHours"),
                StudyMinutes = base.ParmJObj.GetInt("StudyMinutes"),
                Objectives = base.ParmJObj.GetStringValue("Objectives"),
                Requirement = base.ParmJObj.GetStringValue("Requirement"),
                CourseRate = (CourseRateEnum)base.ParmJObj.GetInt("CourseRate"),
                //CurrentHours=base.ParmJObj.GetInt("CurrentHours"),
            };

            extModel.Status = DateStateEnum.Normal;
            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(data: extModel) : 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 BatchUpdateChapterCurrentHours()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            var list = courseModule.BatchUpdateChapterCurrentHoursModule(CourseId);
            return ApiResult.Success(data: list.Select(qitem => new
            {
                qitem.ChapterId,
                qitem.ChapterNo,
                qitem.ChapterName,
                qitem.CurrentHours,
            }));
        }

        /// <summary>
        /// 获取章节树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChapterTree()
        {
            var query = new RB_Course_Chapter_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
            };
            if (query.CourseId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请传递课程编号!");
            }
            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 GetCourseRateEnumList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(CourseRateEnum)).OrderBy(qitem=>qitem.Id);
            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 BatchRemoveChapter()
        {
            var extModel = new RB_Course_Chapter_ViewModel()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                ChapterNo = base.ParmJObj.GetStringValue("ChapterNo"),
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
            };
            bool flag = courseModule.BatchRemoveChapterModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 批量更新课程等级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult BatchSetChapterRate()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            bool flag = courseModule.BatchUpdateChapterRateModule(extModel);
            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 SetChapterName()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Chapter_ViewModel>(RequestParm.Msg.ToString());
            bool flag = courseModule.SetChapterNameModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 批量设置章节编号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowRepeatAttribute]
        public ApiResult SetBatchChapterNo()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Course_Chapter_ViewModel>>(RequestParm.Msg.ToString());
            bool flag = courseModule.SetBatchChapterNoModule(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>
        [HttpPost]
        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>
        [HttpPost]
        public ApiResult GetCourseRebateSetList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(CourseRebateSetEnum));
            return ApiResult.Success(data: list);
        }

        /// <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"),
                B2CRatio = courseObj.GetDecimal("B2CRatio"),
                B2CReNewRatio = courseObj.GetDecimal("B2CReNewRatio"),

                B2BRebateRatio = courseObj.GetDecimal("B2BRebateRatio"),
                B2BRebateRatioType= (CourseRebateSetEnum)courseObj.GetInt("B2BRebateRatioType"),
                B2BRebateRatioValue= courseObj.GetDecimal("B2BRebateRatioValue"),

                B2BReNewRatio = courseObj.GetDecimal("B2BReNewRatio"),
                B2BReNewRatioType = (CourseRebateSetEnum)courseObj.GetInt("B2BReNewRatioType"),
                B2BReNewRatioValue= courseObj.GetDecimal("B2BReNewRatioValue"),

                SchoolRebateRatio = courseObj.GetDecimal("SchoolRebateRatio"),
                SchoolRebateRatioType= (CourseRebateSetEnum)courseObj.GetInt("SchoolRebateRatioType"),
                SchoolRebateRatioValue= courseObj.GetDecimal("SchoolRebateRatioValue"),

                SchoolReNewRatio = courseObj.GetDecimal("SchoolReNewRatio"),
                SchoolReNewRatioType = (CourseRebateSetEnum)courseObj.GetInt("SchoolReNewRatioType"),
                SchoolReNewRatioValue = courseObj.GetDecimal("SchoolReNewRatioValue"),

                InnerRecommendRatio =courseObj.GetDecimal("InnerRecommendRatio"),
                InnerRecommendRatioType = (CourseRebateSetEnum)courseObj.GetInt("InnerRecommendRatioType"),
                InnerRecommendRatioValue= courseObj.GetDecimal("InnerRecommendRatioValue"),

                InnerRecommendReNewRatio =courseObj.GetDecimal("InnerRecommendReNewRatio"),
                InnerRecommendReNewRatioType = (CourseRebateSetEnum)courseObj.GetInt("InnerRecommendReNewRatioType"),
                InnerRecommendReNewRatioValue= courseObj.GetDecimal("InnerRecommendReNewRatioValue"),

                TransIntroductceRatio =courseObj.GetDecimal("TransIntroductceRatio"),
                TransIntroductceRatioType = (CourseRebateSetEnum)courseObj.GetInt("TransIntroductceRatioType"),
                TransIntroductceRatioValue = courseObj.GetDecimal("TransIntroductceRatioValue"),

                TransIntroductceReNewRatio =courseObj.GetDecimal("TransIntroductceReNewRatio"),
                TransIntroductceReNewRatioType = (CourseRebateSetEnum)courseObj.GetInt("TransIntroductceReNewRatioType"),
                TransIntroductceReNewRatioValue = courseObj.GetDecimal("TransIntroductceReNewRatioValue"),

                CommissionReType = courseObj.GetInt("CommissionReType"),  
                
                B2CRbRatio=courseObj.GetDecimal("B2CRbRatio"),
                B2CRbRatioType=(CourseRebateSetEnum)courseObj.GetInt("B2CRbRatioType"),
                B2CRbRatioValue=courseObj.GetDecimal("B2CRbRatioValue"),

                B2CRNRatio=courseObj.GetDecimal("B2CRNRatio"),
                B2CRNRatioType= (CourseRebateSetEnum)courseObj.GetInt("B2CRNRatioType"),
                B2CRNRatioValue=courseObj.GetDecimal("B2CRNRatioValue"),
            };
            var priceObj = base.ParmJObj.GetStringValue("priceList");
            var list = new List<RB_Course_Preferential_Extend>();
            JArray jArray = JArray.Parse(priceObj);
            if (jArray != null && jArray.Count > 0)
            {
                foreach (var item in jArray)
                {
                    JObject param = JObject.Parse(item.ToString());
                    int priceDiscountType = param.GetInt("PriceDiscountType");
                    if (priceDiscountType <= 0)
                    {
                        return ApiResult.ParamIsNull(message: "请选择【优惠类型】");
                    }
                    RB_Course_Preferential_Extend model = new RB_Course_Preferential_Extend()
                    {
                        Id = param.GetInt("Id"),
                        B2BCommissionMoney = param.GetDecimal("B2BCommissionMoney"),
                        B2BCommissionType = param.GetInt("B2BCommissionType"),
                        BuyNum = param.GetInt("BuyNum"),
                        EduCommissionMoney = param.GetDecimal("EduCommissionMoney"),
                        EduCommissionType = param.GetInt("EduCommissionType"),
                        PreferentialTerms = param.GetStringValue("PreferentialTerms"),
                        PriceDiscountType = (CoursePriceDiscountEnum)priceDiscountType,
                        PriceMoney = param.GetDecimal("PriceMoney"),
                        PriceType = param.GetInt("PriceType"),
                        SaleCommissionMoney = param.GetDecimal("SaleCommissionMoney"),
                        SaleCommissionType = param.GetInt("SaleCommissionType"),
                        SendNum = param.GetInt("SendNum"),
                        CreateTime = DateTime.Now,
                        CreateBy = base.UserInfo.Id,
                        UpdateBy = base.UserInfo.Id,
                        UpdateTime = DateTime.Now,
                        Group_Id = this.UserInfo.Group_Id,
                        School_Id = this.UserInfo.School_Id,
                        CourseId = courseModel.CourseId,
                        Status = DateStateEnum.Normal
                    };
                    list.Add(model);
                }
            }
            if (list != null && list.Count > 0)
            {
                var groupList = list.GroupBy(qitem => new { qitem.PriceDiscountType, qitem.PriceDiscountTypeStr });
                foreach (var item in groupList)
                {
                    var tempCount = list?.Where(qitem => qitem.PriceDiscountType == item.Key.PriceDiscountType)?.Count() ?? 0;
                    if (tempCount > 1)
                    {
                        return ApiResult.ParamIsNull(message: string.Format("已存在【{0}】的优惠类型,请重新选择!", item.Key.PriceDiscountTypeStr));
                    }
                }
            }
            var extModel = courseModule.GetCourseModule(courseModel.CourseId, IsGetDiscount: 0);
            extModel.OriginalPrice = courseModel.OriginalPrice;
            extModel.SellPrice = courseModel.SellPrice;
            extModel.B2CRatio = courseModel.B2CRatio;
            extModel.B2CReNewRatio = courseModel.B2CReNewRatio;

            extModel.B2BRebateRatio = courseModel.B2BRebateRatio;
            extModel.B2BRebateRatioType = courseModel.B2BRebateRatioType;
            extModel.B2BRebateRatioValue = courseModel.B2BRebateRatioValue;
          
            extModel.B2BReNewRatio = courseModel.B2BReNewRatio;
            extModel.B2BReNewRatioType = courseModel.B2BReNewRatioType;
            extModel.B2BReNewRatioValue = courseModel.B2BReNewRatioValue;

            extModel.SchoolRebateRatio = courseModel.SchoolRebateRatio;
            extModel.SchoolRebateRatioType = courseModel.SchoolRebateRatioType;
            extModel.SchoolRebateRatioValue = courseModel.SchoolRebateRatioValue;

            extModel.SchoolReNewRatio = courseModel.SchoolReNewRatio;
            extModel.SchoolReNewRatioType = courseModel.SchoolReNewRatioType;
            extModel.SchoolReNewRatioValue = courseModel.SchoolReNewRatioValue;

            extModel.InnerRecommendRatio = courseModel.InnerRecommendRatio;
            extModel.InnerRecommendRatioType = courseModel.InnerRecommendRatioType;
            extModel.InnerRecommendRatioValue= courseModel.InnerRecommendRatioValue;

            extModel.InnerRecommendReNewRatio = courseModel.InnerRecommendReNewRatio;
            extModel.InnerRecommendReNewRatioType = courseModel.InnerRecommendReNewRatioType;
            extModel.InnerRecommendReNewRatioValue = courseModel.InnerRecommendReNewRatioValue;

            extModel.TransIntroductceRatio = courseModel.TransIntroductceRatio;
            extModel.TransIntroductceRatioType = courseModel.TransIntroductceRatioType;
            extModel.TransIntroductceRatioValue = courseModel.TransIntroductceRatioValue;

            extModel.TransIntroductceReNewRatio = courseModel.TransIntroductceReNewRatio;
            extModel.TransIntroductceReNewRatioType = courseModel.TransIntroductceReNewRatioType;
            extModel.TransIntroductceReNewRatioValue = courseModel.TransIntroductceReNewRatioValue;

            extModel.CommissionReType = courseModel.CommissionReType;

            extModel.B2CRbRatio = courseModel.B2CRbRatio;
            extModel.B2CRbRatioType = courseModel.B2CRbRatioType;
            extModel.B2CRbRatioValue = courseModel.B2CRbRatioValue;

            extModel.B2CRNRatio = courseModel.B2CRNRatio;
            extModel.B2CRNRatioType = courseModel.B2CRNRatioType;
            extModel.B2CRNRatioValue = courseModel.B2CRNRatioValue;

            var flag = courseModule.SetCoursePreferentialListModule(extModel, list);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 非法用户管理

        /// <summary>
        /// 保存非法用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetUnKnowUser()
        {
            RB_Class_Time_UnKnowUser_ViewModel model = JsonHelper.DeserializeObject<RB_Class_Time_UnKnowUser_ViewModel>(RequestParm.Msg.ToString());
            model.CreateBy = base.UserInfo.Id;
            model.CreateDate = DateTime.Now;
            var flag = courseModule.SetUnKnownUserModule(model);

            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 跟进课程编号查询非法用户
        /// </summary>
        /// <returns></returns>
        public ApiResult GetUnKnowUserByClassId()
        {
            int classId = ParmJObj.GetInt("classId", 0);
            var list = courseModule.GetUnKnowUserByClassTimeModule(classId);
            var result = list.Select(x => new
            {
                x.Id,
                x.UserAvatar,
                x.CreateTimeStr,
                x.FaceId
            });
            return ApiResult.Success(data: result);
        }

        #endregion

        #region 试听课程关联

        /// <summary>
        /// 获取试听课程分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTrialLessonPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_TrialLesson_Extend()
            {
                LessonName = base.ParmJObj.GetStringValue("LessonName"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = trialLessonModule.GetTrialLessonPageModule(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 GetTrialLessonList()
        {
            var query = new RB_TrialLesson_Extend()
            {
                LessonName = base.ParmJObj.GetStringValue("LessonName"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var list = trialLessonModule.GetTrialLessonListModule(query);
            return ApiResult.Success(data: list);
        }


        /// <summary>
        /// 添加修改试听课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetTrialLesson()
        {
            var extModel = new RB_TrialLesson_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                LessonName = base.ParmJObj.GetStringValue("LessonName"),
                TeacherId = base.ParmJObj.GetInt("TeacherId"),
                ClassHourFee=base.ParmJObj.GetDecimal("ClassHourFee"),
                CalculateType=base.ParmJObj.GetInt("CalculateType"),
            };
            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;
            extModel.Status = DateStateEnum.Normal;
            bool flag = trialLessonModule.SetTrialLessonModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 根据编号获取试听课程信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTrialLesson()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = trialLessonModule.GetTrialLessonModule(Id);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 根据编号删除试听课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveTrialLesson()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var flag = trialLessonModule.RemoveTrialLessonModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion
    }
}