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

        #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;
            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


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