using PushPlatform.Data.MvcModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PushPlatform.Data.Bll;
using PushPlatform.Data.Criteria;
using PushPlatform.Data.Criteria;

namespace PushPlatform.Background.Areas.BackGround.Controllers
{
    public class BaseController : Controller
    {
        /// <summary>
        /// 验证权限
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var Request = filterContext.HttpContext.Request;
            CurrentUserModel currentUser = BllSysUser.GetCurrentUser();
            if (Request.RequestType == "GET")
            {
                //get请求
                if (currentUser == null)
                {
                    string loginUrl = System.Configuration.ConfigurationManager.AppSettings["backLoginUrl"];
                    filterContext.Result = new RedirectResult(loginUrl);
                    return;
                }
            }
            else
            {
                //post请求
                if (currentUser == null)
                {
                    string loginUrl = System.Configuration.ConfigurationManager.AppSettings["backLoginUrl"];
                    JsonResult jresult = new JsonResult();
                    jresult.Data = new { Result = 0, Msg = "权限已过期" };
                    filterContext.Result = jresult;
                    return;
                }
            }
            if (!string.IsNullOrEmpty(currentUser.CurrentProjectId))
            {
                string menuStr = string.Empty;
                BllSysMenu menuBll = new BllSysMenu();
                List<MenuModel> menuList = menuBll.GetMenu(currentUser.CurrentProjectId);
                if (menuList != null && menuList.Count > 0)
                {
                    menuStr = menuBll.GetMenu(menuList);
                }
                ViewBag.Menu = menuStr;
            }
            else
            {
                BllSysUser.ClearCurrentUser();
                string loginUrl = System.Configuration.ConfigurationManager.AppSettings["backLoginUrl"];
                filterContext.Result = new RedirectResult(loginUrl);
                return;
            }
            ViewBag.LoginName = currentUser == null ? null : currentUser.LoginName;
            ViewBag.RequestUrl = Request.Url.AbsoluteUri;
            ViewBag.CurrentProjectId = currentUser == null ? null : currentUser.CurrentProjectId;
        }
    }
}