using Edu.AOP;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum;
using Edu.Common.Enum.Web;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Grade;
using Edu.Model.ViewModel.StudyAbroad;
using Edu.Model.ViewModel.Web;
using Edu.Module.Course;
using Edu.Module.StudyAbroad;
using Edu.Module.User;
using Edu.Module.Web;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.Web
{
    /// <summary>
    /// 网站前端接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class WebController : BaseController
    {
        /// <summary>
        /// 网站导航处理类对象
        /// </summary>
        private readonly WebNavModule navModule = new WebNavModule();

        /// <summary>
        /// 集团处理类对象
        /// </summary>
        private readonly GroupModule groupModule = AOPHelper.CreateAOPObject<GroupModule>();

        /// <summary>
        /// 网站菜单管理处理类对象
        /// </summary>
        private readonly WebMenuModule menuModule = new WebMenuModule();

        /// <summary>
        /// 网站新闻管理处理类对象
        /// </summary>
        private readonly WebNewsModule newsModule = new WebNewsModule();

        /// <summary>
        /// 课程系列处理类
        /// </summary>
        private readonly CourseCategoryModule categoryModule = new CourseCategoryModule();

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

        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private readonly OrderModule orderModule = AOP.AOPHelper.CreateAOPObject<OrderModule>();

        /// <summary>
        /// 教师团队管理处理类对象
        /// </summary>
        private readonly WebTeacherModule teacherModule = new WebTeacherModule();

        /// <summary>
        /// 留学就业处理对象
        /// </summary>
        private readonly StudyAbroadModule studyAbroadModule = AOP.AOPHelper.CreateAOPObject<StudyAbroadModule>();

        /// <summary>
        /// 咨询管理处理类对象
        /// </summary>
        private readonly WebAdvisoryModule advisoryModule = new WebAdvisoryModule();

        /// <summary>
        /// 招聘管理
        /// </summary>
        private readonly WebRecruitmentModule webRecruitmentModule = new WebRecruitmentModule();

        /// <summary>
        /// 获取网站配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebSiteConfig()
        {
            string Domain = base.ParmJObj.GetStringValue("Domain");
            int groupId = groupModule.GetGroupIdByDomainModule(Domain);
            var menuList = menuModule.GetMenuTreeModule(new RB_Web_Menu_ViewModel() { Group_Id = groupId });
            var obj = new
            {
                groupId,
                menuList
            };
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 获取网站导航列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebNavList()
        {
            var query = new RB_Web_Nav_ViewModel()
            {
                NavTitle = base.ParmJObj.GetStringValue("NavTitle"),
                SubTitle = base.ParmJObj.GetStringValue("SubTitle"),
                NavType = (NavTypeEnum)base.ParmJObj.GetInt("NavType"),
                Group_Id = base.ParmJObj.GetInt("Group_Id"),
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = navModule.GetWebNavListModule(query);
            return ApiResult.Success(data: list);
        }

        #region 新闻相关接口
        /// <summary>
        /// 获取网站新闻类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebNewsTypeList()
        {
            var query = new RB_Web_NewsType_ViewModel()
            {
                TypeName = base.ParmJObj.GetStringValue("TypeName"),
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = newsModule.GetWebNewsTypeListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取新闻分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebNewsPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Web_News_ViewModel()
            {
                Title = base.ParmJObj.GetStringValue("Title"),
                TypeId = base.ParmJObj.GetInt("TypeId"),
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            query.Status = DateStateEnum.Normal;
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = newsModule.GetWebNewsPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            List<object> resultList = new List<object>();
            foreach (var item in list)
            {
                string CreateByName = "甲鹤教育";
                if (item.CreateBy > 0)
                {
                    CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                resultList.Add(new
                {
                    item.Id,
                    item.Title,
                    item.Content,
                    item.Img,
                    CreateByName,
                    CreateTime = Common.Plugin.StringHelper.DateFormatToString(item.CreateDate)
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = resultList;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取网站新闻详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebNews()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = newsModule.GetWebNewsModule(Id);
            object obj = new object();
            if (extModel != null && extModel.Id > 0)
            {
                string CreateByName = "甲鹤教育";
                if (extModel.CreateBy > 0)
                {
                    CreateByName = UserReidsCache.GetUserLoginInfo(extModel.CreateBy)?.AccountName ?? "";
                }
                obj = new
                {
                    extModel.Id,
                    extModel.Title,
                    extModel.Img,
                    extModel.Content,
                    CreateByName,
                    CreateTime = Common.Plugin.StringHelper.DateFormatToString(extModel.CreateDate)
                };
            }
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 获取品牌卡通分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebCartoonPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Web_Cartoon_ViewModel()
            {
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = newsModule.GetWebCartoonPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list.Select(qitem => new
            {
                qitem.Id,
                qitem.Name,
                qitem.LinkUrl,
                CreateDate = Common.ConvertHelper.FormatDate(qitem.CreateDate)
            });
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取品牌月刊分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebMonthlyPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Web_Monthly_ViewModel()
            {
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = newsModule.GetWebMonthlyPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list.Select(qitem => new
            {
                qitem.Id,
                qitem.Name,
                qitem.LinkUrl,
                qitem.CoverImg,
            });
            return ApiResult.Success(data: pageModel);
        }

        #endregion

        #region 课程相关接口
        /// <summary>
        /// 获取课程分类列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetCourseCategoryList()
        {
            var query = new RB_Course_Category_ViewModel()
            {
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = categoryModule.GetCourseCategoryListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取课程分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        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"),
                Group_Id = base.ParmJObj.GetInt("Group_Id")
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            query.Saleplat = "2,3";
            var list = courseModule.GetCoursePageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            List<object> resultList = new List<object>();
            foreach (var item in list)
            {
                resultList.Add(new
                {
                    item.CourseId,
                    item.CourseName,
                    item.CoverImg,
                    item.CourseIntro,
                    item.CourseFeature,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = resultList;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 获取课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetCourse()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId", 0);
            var Group_Id = base.ParmJObj.GetInt("Group_Id");
            if (Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                Group_Id = groupId;
            }
            var extModel = courseModule.GetCourseModule(CourseId);
            object obj = new object();
            if (extModel != null && extModel.CourseId > 0)
            {
                obj = new
                {
                    extModel.CourseId,
                    extModel.CourseName,
                    extModel.CourseIntro,
                    extModel.CourseFeature,
                    extModel.CoverImg,
                };
            }
            //获取【教育相关产品】列表
            var RandList = courseModule.GetCourseListModule(new RB_Course_ViewModel()
            {
                CourseId = CourseId,
                IsRand = true,
                RandNum = 6,
                Group_Id = Group_Id,
                CateId=extModel.CateId,
            });
            var result = new
            {
                CourseObj = obj,
                RandList = RandList.Select(qitem => new { qitem.CourseId, qitem.CourseName, qitem.CoverImg, qitem.CourseIntro, qitem.CourseFeature })
            };
            return ApiResult.Success(data: result);
        }

        /// <summary>
        /// 获取课程列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetCourseList()
        {
            var query = new RB_Course_ViewModel()
            {
                CateId = base.ParmJObj.GetInt("CateId", 0),
                Group_Id = base.ParmJObj.GetInt("Group_Id"),
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            query.Saleplat = "2,3";
            var list = courseModule.GetCourseListModule(query);
            return ApiResult.Success(data: list.Select(qitem => new
            {
                qitem.CourseId,
                qitem.CourseName,
            }));
        }


        /// <summary>
        /// 官网开班计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetClassPruduct()
        {
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = new RB_Class_ViewModel()
            {
                ClassName = base.ParmJObj.GetStringValue("ClassName"),
                CouseId = base.ParmJObj.GetInt("CouseId"),
                StartTime = base.ParmJObj.GetStringValue("StartTime"),
                EndTime = base.ParmJObj.GetStringValue("EndTime"),
                JoinEndTime = base.ParmJObj.GetStringValue("JoinEndTime"),
                JoinStartTime = base.ParmJObj.GetStringValue("JoinStartTime"),
                Q_CanApply = base.ParmJObj.GetInt("Q_CanApply"),
                Teacher_Id = base.ParmJObj.GetInt("Teacher_Id"),
                Group_Id = base.ParmJObj.GetInt("Group_Id"),
                CateId = base.ParmJObj.GetInt("CateId"),
                School_Id = -1,
            };
            if (dmodel.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                dmodel.Group_Id = groupId;
            }
            dmodel.Saleplat = "2,3";
            var list = orderModule.GetClassPruductListModule(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.ClassId,
                x.ClassName,
                x.CourseName,
                x.ClassHours,
                OpenTime = x.OpenTime.ToString("yyyy年MM月dd日"),
                EndOrderTime = x.EndOrderTime.HasValue ? x.EndOrderTime.Value.ToString("yyyy年MM月dd日") : "",
                IsCanApply = x.EndOrderTime >= Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")) && x.ClassPersion > x.OrderStudentCount ? 1 : 0,
                x.ClassPersion,
                x.OrderStudentCount,
                SurplusNum = x.ClassPersion - x.OrderStudentCount,
                x.SchoolName,
            });
            return ApiResult.Success("", pageModel);
        }
        #endregion


        /// <summary>
        /// 获取教师团队列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebTeacherList()
        {
            var query = new RB_Web_Teacher_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                Type = base.ParmJObj.GetInt("Type"),
                Group_Id = base.ParmJObj.GetInt("Group_Id"),
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = teacherModule.GetWebTeacherListModule(query);
            return ApiResult.Success(data: list?.Select(qitem => new
            {
                qitem.Name,
                qitem.Intro,
                qitem.Icon,
                qitem.Sex,
                qitem.SexStr
            }));
        }


        /// <summary>
        /// 获取留学就业列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetStudyAbroadList()
        {
            var query = new RB_StudyAbroad_ViewModel()
            {
                Name = base.ParmJObj.GetStringValue("Name"),
                SaleState = Common.Enum.Sale.SaleStateEnum.Sell,
                Group_Id = base.ParmJObj.GetInt("Group_Id"),
                Type = base.ParmJObj.GetInt("Type"),
            };
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = studyAbroadModule.GetStudyAbroadListModule(query);
            return ApiResult.Success(data: list?.Select(qitem => new
            {
                qitem.Name,
                qitem.Remark,
                qitem.ImgCover,
            }));
        }

        /// <summary>
        /// 新增咨询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult AddWebAdvisory()
        {
            var extModel = new RB_Web_Advisory_ViewModel()
            {
                AdvType = base.ParmJObj.GetInt("AdvType"),
                SourceId = base.ParmJObj.GetInt("SourceId"),
                LinkMan = base.ParmJObj.GetStringValue("LinkMan"),
                LinkTel = base.ParmJObj.GetStringValue("LinkTel"),
                Remark = base.ParmJObj.GetStringValue("Remark"),
            };
            extModel.Group_Id = base.ParmJObj.GetInt("Group_Id");
            if (extModel.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                extModel.Group_Id = groupId;
            }
            extModel.School_Id = 0;
            extModel.CreateDate = DateTime.Now;
            extModel.Status = DateStateEnum.Normal;
            extModel.DealStatus = 0;
            bool flag = advisoryModule.SetWebAdvisoryModule(extModel);
            return flag ? ApiResult.Success(data: extModel) : ApiResult.Failed();
        }


        /// <summary>
        /// 获取招聘类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebRecruitmentTypeList()
        {
            var query = new RB_Web_RecruitmentType_ViewModel()
            {
                TypeName = base.ParmJObj.GetStringValue("TypeName"),
            };
            query.Group_Id = base.ParmJObj.GetInt("Group_Id");
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            var list = webRecruitmentModule.GetWebRecruitmentTypeListModule(query);
            return ApiResult.Success(data: list.Select(qitem => new { qitem.Id, qitem.TypeName, qitem.CheckImg,qitem.ImgCover }));
        }

        /// <summary>
        /// 获取招聘信息分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebRecruitmentPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Web_Recruitment_ViewModel()
            {
                PositionName = base.ParmJObj.GetStringValue("PositionName"),
                TypeId = base.ParmJObj.GetInt("TypeId")
            };
            query.Group_Id = base.ParmJObj.GetInt("Group_Id");
            if (query.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                query.Group_Id = groupId;
            }
            List<object> result = new List<object>();
            var list = webRecruitmentModule.GetWebRecruitmentPageModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                result.Add(new
                {
                    item.Id,
                    Salary=item.SalaryStr,
                    item.PositionName,
                    item.Experience,
                    item.Education,
                    item.PublishTimeStr,
                    item.WorkPlace,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 根据编号获取招聘详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetWebRecruitment()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = webRecruitmentModule.GetWebRecruitmentModule(Id);
            var obj = new object();
            if (extModel != null && extModel.Id > 0)
            {
                obj = new
                {
                    extModel.Id,
                    Salary = extModel.SalaryStr,
                    extModel.PositionName,
                    extModel.Experience,
                    extModel.Education,
                    extModel.PublishTimeStr,
                    extModel.WorkPlace,
                    extModel.PositionDesc
                };
            }
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 新增招聘申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ApiResult AddWebJobApply()
        {
            var extModel = new RB_Web_JobApply_ViewModel()
            {
                LinkMan = base.ParmJObj.GetStringValue("LinkMan"),
                LinkTel = base.ParmJObj.GetStringValue("LinkTel"),
                Remark = base.ParmJObj.GetStringValue("Remark"),
                RecruitmentId=base.ParmJObj.GetInt("RecruitmentId"),
            };
            extModel.Group_Id = base.ParmJObj.GetInt("Group_Id");
            if (extModel.Group_Id <= 0)
            {
                string Domain = base.ParmJObj.GetStringValue("Domain");
                int groupId = groupModule.GetGroupIdByDomainModule(Domain);
                extModel.Group_Id = groupId;
            }
            extModel.School_Id = 0;
            extModel.CreateDate = DateTime.Now;
            extModel.Status = DateStateEnum.Normal;
            bool flag = webRecruitmentModule.AddWebJobApplyModule(extModel);
            return flag ? ApiResult.Success(data: extModel) : ApiResult.Failed();
        }
    }
}