using Edu.Common;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Edu.Model.CacheModel;
using Edu.Cache.User;
using Edu.Module.System;

namespace Edu.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiFilter]
    [EnableCors("AllowCors")]
    public class BaseController : ControllerBase
    {
        /// <summary>
        /// 菜单处理类对象
        /// </summary>
        private readonly MenuModule menuModule = new MenuModule();

        /// <summary>
        /// 整理前端传递的post参数
        /// </summary>
        /// <param name="requestMsg"></param>
        /// <returns></returns>
        public RequestParm RequestParm
        {
            get
            {
                var requestParm = new RequestParm();
                #region 读取post参数
                var requestMsg = Request.HttpContext.Items[GlobalKey.UserPostInfo];
                if (requestMsg != null)
                {
                    requestParm = JsonConvert.DeserializeObject<RequestParm>(requestMsg.ToString());
                    if (Request.HttpContext.Items[GlobalKey.TokenUserInfo] != null)
                    {
                        JObject parms = JObject.Parse(Request.HttpContext.Items[GlobalKey.TokenUserInfo].ToString());
                        requestParm.Uid = parms.GetStringValue("uid");
                    }
                }
                #endregion
                //根据token 获取uid
                return requestParm;
            }
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        public JObject ParmJObj
        {
            get
            {
                if (this.RequestParm != null && this.RequestParm.Msg != null)
                {
                    return JObject.Parse(this.RequestParm.Msg.ToString());
                }
                return new JObject();
            }
        }

        /// <summary>
        /// 用户缓存
        /// </summary>
        public UserInfo UserInfo
        {
            get
            {
                var parm = this.RequestParm;
                UserInfo userInfo = UserReidsCache.GetUserLoginInfo(parm.Uid);
                return userInfo;
            }
        }

        /// <summary>
        /// 根据Id获取用户信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo(object Id)
        {
            UserInfo userInfo = UserReidsCache.GetUserLoginInfo(Id);
            return userInfo;
        }

        /// <summary>
        /// 判断用户是否有功能权限
        /// </summary>
        /// <param name="actionCode">权限编码</param>
        /// <returns></returns>
        public bool CheckUserActionAuth(string actionCode)
        {
            bool flag = false;
            if (this.UserInfo != null && this.UserInfo.PostId > 0)
            {
                flag = menuModule.CheckUserFunctionModule(actionCode, this.UserInfo.PostId.ToString());
            }
            return flag;
        }
    }
}