using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;

namespace BLL
{
    public class CommonService
    {
        /// <summary>
        /// 判断菜单是否存在
        /// </summary>
        /// <param name="postId"></param>
        /// <returns></returns>
        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;
            }
        }

        /// <summary>
        /// 根据上级菜单获取菜单ID信息
        /// </summary>
        /// <param name="superMenuId">上级菜单ID</param>
        /// <returns></returns>
        public static Model.Sys_Menu GetMenuBySuperMenuId(string superMenuId)
        {
            return Funs.DB.Sys_Menu.FirstOrDefault(e => e.SuperMenu == superMenuId);
        }

        ///// <summary>
        ///// 判断按键对应菜单是否存在
        ///// </summary>
        ///// <param name="postId"></param>
        ///// <returns></returns>
        //public static bool isExistButtonToMenu(string buttonToMenuId)
        //{
        //    Model.ButtonToMenu b = Funs.DB.ButtonToMenu.FirstOrDefault(e => e.ButtonToMenuId == buttonToMenuId);
        //    if (b != null)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        #region 获取当前人是否具有按钮操作权限
        /// <summary>
        /// 获取当前人是否具有按钮操作权限
        /// </summary>
        /// <param name="projectId">项目id</param>
        /// <param name="userId">用户id</param>
        /// <param name="menuId">菜单id</param>
        /// <param name="buttonName">按钮名称</param>
        /// <returns>是否具有权限</returns>
        public static bool GetAllButtonPowerList(string projectId, string userId, string menuId, string buttonName)
        {
            Model.HJGLDB db = Funs.DB;
            bool isPower = false;    // 定义是否具备按钮权限
            
            var pro = BLL.Base_ProjectService.GetProjectByProjectId(projectId);
            Model.Sys_Menu menu = Funs.DB.Sys_Menu.FirstOrDefault(e => e.MenuId == menuId);

            // 如项目关闭,则现场管道安装菜单不可用
            if (pro != null && pro.IsClosed == true && menu.MenuModule == Const.System_3)
            {
                isPower = false;
            }

            var user = BLL.Sys_UserService.GetUsersByUserId(userId); ////用户            
            if (user != null)
            {
                if (user.Account == Const.Gly)
                {
                    isPower = true;
                }

                // 项目不为空的时候
                if (isPower==false)
                {
                    string userRoles = user.RoleIds;
                    if (!string.IsNullOrEmpty(userRoles)) 
                    {
                        // 用户可能多个角色
                        string[] rolse = userRoles.Split('|');
                        foreach (string r in rolse)
                        {
                            var rolePower = from x in db.Sys_RolePower
                                            where x.MenuId == menuId && x.RoleId == r
                                            select x;
                           
                            if (rolePower.Count() > 0 && !string.IsNullOrEmpty(rolePower.FirstOrDefault().ButtonToMenus))
                            {
                                string buttonPowers = rolePower.FirstOrDefault().ButtonToMenus;
                                
                                // 按钮权限
                                string[] buttons= buttonPowers.Split(',');
                                foreach(string btn in buttons)
                                {
                                    var button = from x in db.Sys_ButtonToMenu where x.ButtonToMenuId == btn && x.ButtonName== buttonName select x;
                                    if (button.Count() > 0)
                                    {
                                        isPower = true;
                                        break;
                                    }
                                }
                            }
                            if (isPower == true)
                            {
                                break;
                            }
                        }
                      
                    }

                }
            }

            return isPower;
        }
        #endregion

        #region 根据登陆id和系统id得到菜单列表
        /// <summary>
        ///  根据登陆id和系统id得到菜单列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="menuModule"></param>
        /// <returns></returns>
        public static List<Model.Sys_Menu> GetMenu(string userId, string menuModule)
        {
            List<Model.Sys_Menu> reMenuList = new List<Model.Sys_Menu>(); ////要返回的菜单列表
            var user = BLL.Sys_UserService.GetUsersByUserId(userId); ////用户 
            if (user.UserId == Const.GlyId)
            {
                var sysMenu = from x in Funs.DB.Sys_Menu where x.MenuModule == menuModule && x.IsUse==true orderby x.SortIndex select x;
                reMenuList = sysMenu.ToList();
            }
            else
            {
                if (!string.IsNullOrEmpty(user.RoleIds))
                {
                    string[] userRoles = user.RoleIds.Split('|');
                    var sysMenuRole = from x in Funs.DB.Sys_Menu
                                      join y in Funs.DB.Sys_RolePower on x.MenuId equals y.MenuId
                                      where x.MenuModule == menuModule && x.IsUse==true && userRoles.Contains(y.RoleId)
                                      orderby x.SortIndex
                                      select x;
                    reMenuList = sysMenuRole.ToList();
                }
            }

            reMenuList = reMenuList.Distinct().OrderBy(x => x.SortIndex).ToList();
            ////对于菜单集合 去重复返回
            return reMenuList;
        }
        #endregion

        #region 得到首页导航菜单列表 用于空权限登陆加载
        /// <summary>
        /// 得到首页导航菜单列表 用于空权限登陆加载
        /// </summary>
        /// <returns></returns>
        public static string[] GetDeskDesktopMenuId(string menuModule)
        {
            //string[] menuList;
            //switch (menuModule)
            //{ 
            //    case Const.System_1:

            //        menuList = new string[] { Const.DeskTopSGGLSuperMenuId, Const.DeskTopSGGLMenuId };
            //        var menuSystem = from x in Funs.DB.Sys_Menu
            //                         where (x.MenuId == Const.ProjectSiteMenuId || x.MenuId == Const.ProjectColligateMenuId || x.SuperMenu == Const.ProjectSiteMenuId || x.SuperMenu == Const.ProjectColligateMenuId)
            //                         select x.MenuId;
            //        foreach (var item in menuSystem)
            //        {
            //            menuList=menuList.Concat(new string[]{item}).ToArray(); 
            //        }

            //        break;
            //    case Const.System_2:
            //        menuList = new string[] { Const.DesktopHSSEServerSuperMenuId, Const.DesktopHSSEServerMenuId };
            //        break;
            //    case Const.System_3:
            //        menuList = new string[] { Const.DesktopHSSESuperMenuId, Const.DesktopHSSEMenuId };
            //        break;
            //    case Const.System_4:
            //        menuList = new string[] { Const.DesktopCQMSServerSuperMenuId, Const.DesktopCQMSServerMenuId };
            //        break;
               
            //    default:
            //        menuList = null;
            //        break;
            //}

            return null;  //menuList;
        }
        #endregion       

        /// <summary>
        /// 抽取权限到项目
        /// </summary>
        /// <param name="projectId"></param>
        public static void ExtractionPower(string projectId)
        {
            /////增加项目上这个角色的菜单权限
            //var rolePower = from x in Funs.DB.Sys_RolePower
            //                join y in Funs.DB.Sys_Menu on x.MenuId equals y.MenuId
            //                where (y.MenuModule == Const.System_3 || y.MenuModule == Const.System_5 || y.MenuModule == Const.System_7
            //                || y.MenuModule == Const.System_9 || y.MenuModule == Const.System_11)
            //                select x;

            //if (rolePower.Count() > 0)
            //{
            //    foreach (var roleItem in rolePower)
            //    {
            //        var proRolePower = Funs.DB.Project_RolePower.FirstOrDefault(x => x.ProjectId == projectId && x.RoleId == roleItem.RoleId && x.MenuId == roleItem.MenuId);
            //        if (proRolePower == null)
            //        {
            //            Model.Project_RolePower newPower = new Model.Project_RolePower();
            //            newPower.RoleId = roleItem.RoleId;
            //            newPower.ProjectId =projectId;
            //            newPower.MenuId = roleItem.MenuId;
            //            BLL.Project_RolePowerService.SaveProject_RolePower(newPower);
            //        }
            //    }
            //}

            ///////增加项目上这个角色的按钮权限
            //var buttonPower = from x in Funs.DB.Sys_ButtonPower
            //                join y in Funs.DB.Sys_Menu on x.MenuId equals y.MenuId
            //                where (y.MenuModule == Const.System_3 || y.MenuModule == Const.System_5 || y.MenuModule == Const.System_7
            //                || y.MenuModule == Const.System_9 || y.MenuModule == Const.System_11)
            //                select x;

            //if (buttonPower.Count() > 0)
            //{
            //    foreach (var buttonItem in buttonPower)
            //    {
            //        var proButtonPower = Funs.DB.Project_RoleButtonPower.FirstOrDefault(x => x.ProjectId == projectId && x.RoleId == buttonItem.RoleId
            //            && x.MenuId == buttonItem.MenuId && x.ButtonToMenuId == buttonItem.ButtonToMenuId);
            //        if (proButtonPower == null)
            //        {
            //            Model.Project_RoleButtonPower newRoleButtonPower = new Model.Project_RoleButtonPower();
            //            newRoleButtonPower.RoleId = buttonItem.RoleId;
            //            newRoleButtonPower.ProjectId = projectId;
            //            newRoleButtonPower.MenuId = buttonItem.MenuId;
            //            newRoleButtonPower.ButtonToMenuId = buttonItem.ButtonToMenuId;
            //            BLL.Project_RoleButtonPowerService.SaveProject_RoleButtonPower(newRoleButtonPower);
            //        }
            //    }
            //}
        }
    }
}