using PageAdmin.BLL;
using PageAdmin.Utils;
using System;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    public class DepartmentPermissionsController : SetDataBaseController
    {
        public DepartmentPermissionsController() : base("pa_department_permissions", "User/Department/")
        {
            base.IndexViewExecuting = new Action(OnGetDataing);
            base.EditExecuting = SetMenuIds;
        }

        /// <summary>
        /// 给菜单自动添加上级menuIds
        /// </summary>
        private void SetMenuIds()
        {
            string menuIds = base.MvcContext.Data["MenuIds"];
            MemberMenuBLL memberMenuBLL = new MemberMenuBLL();
            menuIds = memberMenuBLL.GetParentIdsAdnSelf(menuIds);
            base.MvcContext.Data["MenuIds"] = menuIds;
        }

        private void OnGetDataing()
        {
            int departmentId = StringHelper.Format<int>(Request.QueryString["departmentId"]);
            if (departmentId == 0)
            {
                throw new Exception("无效的departmentId参数！");
            }
            DepartmentBLL departmentBLL = new DepartmentBLL();
            if (!departmentBLL.IsExists(departmentId))
            {
                throw new Exception("departmentId参数值错误！");
            }
            base.GetDataWhere = "departmentId=" + departmentId;
            base.AddDbKeyValueList.Add("departmentId", departmentId);
        }

    }
}