using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.Public;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.System;
using Edu.Module.Log;
using Edu.Module.Public;
using Edu.Module.System;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Public
{
    /// <summary>
    /// 系统公用接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class PublicController : BaseController
    {
        /// <summary>
        /// 省市区处理类对象
        /// </summary>
        private readonly DestinationModule destinationModule = new DestinationModule();

        /// <summary>
        /// 菜单处理类对象
        /// </summary>
        private readonly MenuModule menuModule = new MenuModule();

        /// <summary>
        /// 角色处理类对象
        /// </summary>
        private readonly RoleModule roleModule = AOP.AOPHelper.CreateAOPObject<RoleModule>();

        /// <summary>
        /// 公共处理类对象
        /// </summary>
        private readonly PublicModule publicModule = new PublicModule();

        /// <summary>
        /// 班级基础设置处理类对象
        /// </summary>
        private readonly ClassConfigModule classConfigModule = new ClassConfigModule();

        /// <summary>
        /// 日志处理类对象
        /// </summary>
        private readonly LogModule logModule = AOP.AOPHelper.CreateAOPObject<LogModule>();

        #region 省市区

        /// <summary>
        /// 获取省市区树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAreaTree()
        {
            var list = destinationModule.GetAreaTreeModule();
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取省市区列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAreaList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Destination_ViewModel>(RequestParm.Msg.ToString());
            var list = destinationModule.GetAreaListModule(query);
            return ApiResult.Success(data: list);
        }

        #endregion 

        #region 菜单管理
        /// <summary>
        /// 获取菜单分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMenuPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Menu_ViewModel>(RequestParm.Msg.ToString());
            var list = menuModule.GetMenuPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMenuList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Menu_ViewModel>(RequestParm.Msg.ToString());
            var list = menuModule.GetMenuListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMenu()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Menu_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.TeamType = extModel.TeamType == 0 ? 1 : extModel.TeamType;
            bool flag = menuModule.SetMenuModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取菜单实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMenu()
        {
            var MenuId = base.ParmJObj.GetInt("MenuId", 0);
            var extModel = menuModule.GetMenuModule(MenuId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改菜单状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMenuStatus()
        {
            var MenuId = base.ParmJObj.GetInt("MenuId", 0);
            int Status = base.ParmJObj.GetInt("Status", 0);
            var flag = menuModule.SetMenuStatusModule(MenuId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 菜单功能权限

        /// <summary>
        /// 获取菜单功能权限分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMenuFunctionPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Menu_Function_ViewModel>(RequestParm.Msg.ToString());
            var list = menuModule.GetMenuFunctionPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 添加修改菜单功能权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMenuFunction()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Menu_Function_ViewModel>(RequestParm.Msg.ToString());
            extModel.Status = Common.Enum.DateStateEnum.Normal;
            bool flag = menuModule.SetMenuFunctionModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取菜单功能权限实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMenuFunction()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = menuModule.GetMenuFunctionModule(Id);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改菜单功能权限状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetMenuFunctionStatus()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            int Status = base.ParmJObj.GetInt("Status", 0);
            var flag = menuModule.SetMenuFunctionStatusModule(Id, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 角色管理
        /// <summary>
        /// 获取角色分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRolePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Role_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = roleModule.GetRolePageListModule(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 GetRoleList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Role_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = roleModule.GetRoleListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetRole()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Role_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateDate = DateTime.Now;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.School_Id = base.UserInfo.School_Id;
            bool flag = roleModule.SetRoleModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取角色实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRole()
        {
            var RoleId = base.ParmJObj.GetInt("RoleId", 0);
            var extModel = roleModule.GetRoleModule(RoleId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 权限预览
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult ViewRolePermission()
        {
            var RoleId = base.ParmJObj.GetInt("RoleId", 0);
            var extModel = roleModule.ViewRolePermissionModule(RoleId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetRoleStatus()
        {
            var RoleId = base.ParmJObj.GetInt("RoleId", 0);
            var Status = base.ParmJObj.GetInt("Status", 0);
            var flag = roleModule.SetRoleStatusModule(RoleId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 上传设置
        public ApiResult GetFileStoreList()
        {
            var query = new RB_File_Store
            {
                Group_Id = base.UserInfo.Group_Id,
                //School_Id = base.UserInfo.School_Id
            };
            var list = publicModule.GetFileStoreList(query);
            return ApiResult.Success("获取成功", list);
        }

        /// <summary>
        /// 新增/修改上传设置
        /// </summary>
        /// <returns></returns>
        public ApiResult AddOrUpdateFileStore()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_File_Store>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;

            if (query == null)
            {
                return ApiResult.Failed("请传入上传设置信息");
            }
            else
            {
                if ((int)query.StoreType == 0)
                {
                    return ApiResult.Failed("请选择存储位置");
                }
                if (query.ID == 0)
                {
                    query.CreateDate = System.DateTime.Now;
                }
                query.UpdateDate = System.DateTime.Now;
                bool result = publicModule.AddOrUpdateFileStore(query);
                if (result)
                {
                    return ApiResult.Success("上传设置保存成功");
                }
                else
                {
                    return ApiResult.Failed("上传设置保存失败");
                }
            }
        }


        /// <summary>
        /// 获取上传信息的详情
        /// </summary>
        /// <returns></returns>
        public ApiResult GetFileStoreDetail()
        {
            var query = new RB_File_Store
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.UserInfo.School_Id
            };
            var model = publicModule.GetFileStoreList(query).FirstOrDefault();
            if (model == null)
            {
                model = new RB_File_Store();
            }
            return ApiResult.Success("获取成功", model);
        }



        /// <summary>
        /// 获取触发事件枚举
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStoreTypeEnumList()
        {
            int type = base.ParmJObj.GetInt("Type", 0);
            var list = EnumHelper.EnumToList(typeof(Common.Enum.Public.StoreTypeEnum));
            if (type == 1)
            {
                return ApiResult.Success("", list.Where(x => x.Id != 3));
            }
            else
            {
                return ApiResult.Success("", list);

            }
        }

        /// <summary>
        /// 获取默认上传信息的详情
        /// </summary>
        /// <returns></returns>
        public ApiResult GetDefaultFileStore()
        {
            var query = new RB_File_Store
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.UserInfo.School_Id
            };
            var model = publicModule.GetFileStoreList(query).Where(x => x.IsDefault == 1).FirstOrDefault();
            if (model == null)
            {
                model = new RB_File_Store();
            }
            return ApiResult.Success("获取成功", model);
        }

        #endregion

        #region 角色权限
        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRolePermission()
        {
            var roleId = base.ParmJObj.GetInt("RoleId");
            var list = roleModule.GetRolePermissionModule(roleId);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <returns></returns>
        public ApiResult SetRolePermission()
        {
            var roleMenu = base.ParmJObj.GetStringValue("roleMenu");
            var roleFunction = base.ParmJObj.GetStringValue("roleFunction");
            var rolePermissionList = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Role_Permission_ViewModel>>(roleMenu);
            var roleFunctionList = Common.Plugin.JsonHelper.DeserializeObject<List<RB_Role_FunctionPermission_ViewModel>>(roleFunction);
            bool flag = roleModule.SetRolePermissionModule(rolePermissionList, roleFunctionList);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 班级设置
        /// <summary>
        /// 获取班级基础设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassConfig()
        {
            var query = new RB_Class_Config_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                ConfigId = base.ParmJObj.GetInt("ConfigId")
            };
            var extModel = classConfigModule.GetClassConfigModule(query);
            return ApiResult.Success(data: extModel);
        }




        /// <summary>
        /// 获取班级基础设置的管理员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Microsoft.AspNetCore.Authorization.AllowAnonymous]
        public ApiResult GetClassConfigAdmini()
        {
            var query = new RB_Class_Config_ViewModel()
            {
                Group_Id = base.ParmJObj.GetInt("Group_Id", 100000),
                ConfigId = base.ParmJObj.GetInt("ConfigId")
            };
            var extModel = classConfigModule.GetClassConfigModule(query);
            return ApiResult.Success(data: extModel.AdminiIdList);
        }

        /// <summary>
        /// 新增修改班级基础设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClassConfig()
        {
            var extModel = new RB_Class_Config_ViewModel()
            {
                ConfigId = base.ParmJObj.GetInt("ConfigId"),
                BasicHourFee = base.ParmJObj.GetDecimal("BasicHourFee"),
                BasicClassNum = base.ParmJObj.GetInt("BasicClassNum"),
                BasicMinutes = base.ParmJObj.GetInt("BasicMinutes"),
                BasicHours = base.ParmJObj.GetInt("BasicHours"),
            };
            List<int> AdminiIds = Common.ConvertHelper.StringToList(base.ParmJObj.GetStringValue("AdminiIds"));
            if (AdminiIds != null && AdminiIds.Count > 0)
            {
                extModel.AdminiIds = string.Join(",", AdminiIds);
            }
            else
            {
                extModel.AdminiIds = "";
            }
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            var flag = classConfigModule.SetClassConfigModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取班级类型费用分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassTypePageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Type_ViewModel>(RequestParm.Msg.ToString());
            var list = classConfigModule.GetClassTypePageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取班级类型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassTypeList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Type_ViewModel>(RequestParm.Msg.ToString());
            var list = classConfigModule.GetClassTypeListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改班级费用类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClassType()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Type_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.CreateBy = base.UserInfo.Id;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            bool flag = classConfigModule.SetClassTypeModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 根据编号获取班级费用类型实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassTypeModule()
        {
            var CTypeId = base.ParmJObj.GetInt("CTypeId", 0);
            var extModel = classConfigModule.GetClassTypeModule(CTypeId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改班级费用类型状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveClassType()
        {
            var CTypeId = base.ParmJObj.GetInt("CTypeId", 0);
            var flag = classConfigModule.RemoveClassTypeModule(CTypeId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 日志管理

        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMsgLogList()
        {
            var query = new Model.Entity.Log.RB_MsgLog()
            {
                AppId = Common.Config.PushAppId,
                IsRead = 0,
                ReceiveId = base.UserInfo.Id.ToString(),
                Platform = 2
            };
            var list = logModule.GetMsgLogListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 修改班级费用类型状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult BeatchReadMsgLog()
        {
            List<int> Ids = Common.Plugin.JsonHelper.DeserializeObject<List<int>>(base.ParmJObj.GetStringValue("Ids"));
            var flag = logModule.SetMsgReadModule(Ids);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

    }
}