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

namespace BLL
{
   public static class SysMenuService
    {
       public static Model.SGGLDB db = Funs.DB;

       /// <summary>
       /// 根据MenuId获取菜单名称项
       /// </summary>
       /// <param name="menuId"></param>
       /// <returns></returns>
       public static List<Model.Sys_Menu> GetSupMenuListBySuperMenu(string superMenu)
       {
           var list = (from x in Funs.DB.Sys_Menu where x.SuperMenu == superMenu orderby x.SortIndex select x).ToList();          
           return list;
       }
       public static Model.Sys_Menu GetSys_MenuById(string MenuId)

       {
           return Funs.DB.Sys_Menu.FirstOrDefault(e => e.MenuId == MenuId);
       }
        /// <summary>
        /// 根据MenuId获取菜单名称项
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static Model.Sys_Menu GetSupMenuBySuperMenu(string superMenu)
       {
           return Funs.DB.Sys_Menu.FirstOrDefault(x => x.SuperMenu == superMenu);    
       }

       /// <summary>
       /// 根据MenuId获取菜单名称项
       /// </summary>
       /// <param name="menuId"></param>
       /// <returns></returns>
       public static Model.Sys_Menu GetSysMenuByMenuId(string menuId)
       {
           return Funs.DB.Sys_Menu.FirstOrDefault(x => x.MenuId == menuId);
       }

       /// <summary>
       /// 根据MenuType获取菜单集合
       /// </summary>
       /// <param name="menuId"></param>
       /// <returns></returns>
       public static List<Model.Sys_Menu> GetMenuListByMenuType(string menuType)
       {
           var list = (from x in Funs.DB.Sys_Menu where x.MenuType == menuType orderby x.SortIndex select x).ToList();
           return list;
       }

        /// <summary>
        /// 根据MenuType获取菜单集合
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static List<Model.Sys_Menu> GetIsUsedMenuListByMenuType(string menuType)
        {
            var list = (from x in Funs.DB.Sys_Menu
                        where (x.MenuType == menuType || menuType == null) && x.IsUsed == true
                        orderby x.SortIndex
                        select x).Distinct().ToList();
            return list;
        }

        /// <summary>
        /// 根据MenuType获取菜单集合
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public static List<Model.Sys_Menu> GetIsUsedMenuListBySupType(string menuType)
        {
            List<Model.Sys_Menu> lists = new List<Model.Sys_Menu>();

            if (menuType == "MenuType_S")
            {
                lists = (from x in Funs.DB.Sys_Menu
                         where x.IsOffice == true && x.IsUsed == true
                         orderby x.SortIndex
                         select x).Distinct().ToList();
            }
            else
            {
                lists = (from x in Funs.DB.Sys_Menu
                         where x.IsOffice == false && x.IsUsed == true
                         orderby x.SortIndex
                         select x).Distinct().ToList();
            }
            
            return lists;
        }
        public static void UpdateSys_Menu(Model.Sys_Menu newtable)
        {
            Model.Sys_Menu table = Funs.DB.Sys_Menu.FirstOrDefault(e => e.MenuId == newtable.MenuId);

            if (table != null)
            {
                table.MenuId = newtable.MenuId;
                table.IsUsed = newtable.IsUsed;
                table.MenuName = newtable.MenuName;
                table.Icon = newtable.Icon;
                table.Url = newtable.Url;
                table.SortIndex = newtable.SortIndex;
                table.SuperMenu = newtable.SuperMenu;
                table.MenuType = newtable.MenuType;
                table.IsOffice = newtable.IsOffice;
                table.IsEnd = newtable.IsEnd;
                Funs.DB.SubmitChanges();
            }

        }
        public static void SetAllIsUsed(string MenuType)
        {
            var q = from x in Funs.DB.Sys_Menu
                where x.MenuType == MenuType
                select x;
            foreach (var p in q)
            {
                p.IsUsed = false;
            }
            db.SubmitChanges();
        }
    }
}