using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    public static class RolePowerService
    {        
        /// 
        /// 增加角色权限
        /// 
        /// 
        public static void SaveRolePower(Model.Sys_RolePower power)
        {
            Model.SGGLDB db = Funs.DB;
            string newRolePower = SQLHelper.GetNewID();
            Model.Sys_RolePower newPower = new Model.Sys_RolePower
            {
                RolePowerId = newRolePower,
                RoleId = power.RoleId,
                MenuId = power.MenuId,
                MenuType=power.MenuType,
                IsOffice=power.IsOffice,
            };
            db.Sys_RolePower.InsertOnSubmit(newPower);
            db.SubmitChanges();
        }
        /// 
        /// 判断菜单是否存在
        /// 
        /// 
        /// 
        public static bool IsExistMenu(string menuId)
        {
            Model.Sys_Menu m = Funs.DB.Sys_Menu.FirstOrDefault(e => e.MenuId == menuId);
            if (m != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 删除权限
        /// 
        /// 
        public static void DeleteRolePowerByRoleIdMenuType(string roleId, string menuType)
        {
            Model.SGGLDB db = Funs.DB;
            var q = from x in db.Sys_RolePower
                    join y in db.Sys_Menu on x.MenuId equals y.MenuId
                    where x.RoleId == roleId && y.MenuType == menuType
                    select x;
            if (q.Count() > 0)
            {
                db.Sys_RolePower.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }
        /// 
        /// 获取角色权限
        /// 
        /// 
        public static List GetRolePower(string roleId)
        {
            List powerList = Funs.DB.Sys_RolePower.Where(e => e.RoleId == roleId).ToList();
            return powerList;
        }
        /// 
        /// 根据角色Id、菜单id查询是否有权限
        /// 
        /// 
        /// 
        public static bool IsHavePowerByRoleIdMenuId(string roleId,string menuId)
        {
            bool isHave = false;
            var q = Funs.DB.Sys_RolePower.FirstOrDefault(x => x.RoleId == roleId && x.MenuId == menuId);
            if (q != null)
            {
                isHave = true;
            }
            return isHave;
        }
        /// 
        /// 根据角色主键获得角色权限数量
        /// 
        /// 角色
        /// 
        public static int GetPostPowerCountByRoleId(string roleId)
        {
            var q = (from x in Funs.DB.Sys_RolePower where x.RoleId == roleId select x).ToList();
            return q.Count();
        }
        /// 
        /// 根据角色主键获得对应的菜单权限
        /// 
        /// 角色主键
        /// 菜单ID数组
        public static string[] GetMenuIdByRoleId(string roleId)
        {
            var q = Funs.DB.Sys_RolePower.Where(e => e.RoleId == roleId);
            string[] menuId = new string[q.Count()];
            if (q.Count() > 0)
            {
                int i = 0;
                foreach (var menu in q)
                {
                    menuId[i] = menu.MenuId;
                    i++;
                }
                return menuId;
            }
            else
            {
                return null;
            }
        }
    }
}