2016-10-26 14:02:40 +08:00
using Longbow ;
using Longbow.Caching ;
using Longbow.Caching.Configuration ;
2016-10-26 21:51:38 +08:00
using Longbow.Data ;
2016-10-26 14:02:40 +08:00
using Longbow.ExceptionManagement ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
using System.Globalization ;
using System.Linq ;
namespace Bootstrap.DataAccess
{
public static class MenuHelper
{
2016-11-05 12:11:16 +08:00
internal const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus" ;
2016-11-04 16:06:40 +08:00
internal const string RetrieveMenusByUserIDDataKey = "MenuHelper-RetrieveMenusByUserId" ;
2016-10-26 14:02:40 +08:00
/// <summary>
/// 查询所有菜单信息
/// </summary>
/// <param name="tId"></param>
/// <returns></returns>
2016-11-05 23:15:07 +08:00
public static IEnumerable < Menu > RetrieveMenus ( )
2016-10-26 14:02:40 +08:00
{
2016-11-05 23:15:07 +08:00
return CacheManager . GetOrAdd ( RetrieveMenusDataKey , CacheSection . RetrieveIntervalByKey ( RetrieveMenusDataKey ) , key = >
2016-10-26 14:02:40 +08:00
{
2016-11-05 23:15:07 +08:00
string sql = "select n.*, d.Name as CategoryName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = N'菜单' and d.Define = 0" ;
2016-10-26 14:02:40 +08:00
List < Menu > Menus = new List < Menu > ( ) ;
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
try
{
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
Menus . Add ( new Menu ( )
{
ID = ( int ) reader [ 0 ] ,
ParentId = ( int ) reader [ 1 ] ,
Name = ( string ) reader [ 2 ] ,
Order = ( int ) reader [ 3 ] ,
2016-10-26 21:51:38 +08:00
Icon = LgbConvert . ReadValue ( reader [ 4 ] , string . Empty ) ,
Url = LgbConvert . ReadValue ( reader [ 5 ] , string . Empty ) ,
2016-11-03 21:29:32 +08:00
Category = ( string ) reader [ 6 ] ,
CategoryName = ( string ) reader [ 7 ]
2016-10-26 14:02:40 +08:00
} ) ;
}
}
}
catch ( Exception ex ) { ExceptionManager . Publish ( ex ) ; }
return Menus ;
2016-11-04 16:06:40 +08:00
} , CacheSection . RetrieveDescByKey ( RetrieveMenusDataKey ) ) ;
2016-10-26 14:02:40 +08:00
}
/// <summary>
2016-11-02 17:00:49 +08:00
/// 查询某个用户所配置的菜单
2016-10-26 14:02:40 +08:00
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static IEnumerable < Menu > RetrieveMenusByUserId ( int userId )
{
2016-11-04 16:06:40 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveMenusByUserIDDataKey , userId ) ;
return CacheManager . GetOrAdd ( key , CacheSection . RetrieveIntervalByKey ( RetrieveMenusByUserIDDataKey ) , k = >
2016-11-02 17:00:49 +08:00
{
2016-11-05 23:15:07 +08:00
string sql = "select n.* from Navigations n inner join NavigationRole nr on n.ID = nr.NavigationID inner join UserRole ur on nr.RoleID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.ID = @UserID union select n.* from Navigations n inner join NavigationRole nr on n.ID = nr.NavigationID inner join RoleGroup rg on nr.RoleID = rg.RoleID inner join UserGroup ur on rg.GroupID = ur.GroupID inner join Users u on ur.UserID = u.ID where u.ID = @UserID" ;
2016-11-02 17:00:49 +08:00
List < Menu > Menus = new List < Menu > ( ) ;
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
try
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserID" , userId , ParameterDirection . Input ) ) ;
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
Menus . Add ( new Menu ( )
{
ID = ( int ) reader [ 0 ] ,
ParentId = ( int ) reader [ 1 ] ,
Name = ( string ) reader [ 2 ] ,
Order = ( int ) reader [ 3 ] ,
Icon = LgbConvert . ReadValue ( reader [ 4 ] , string . Empty ) ,
Url = LgbConvert . ReadValue ( reader [ 5 ] , string . Empty ) ,
Category = ( string ) reader [ 6 ]
} ) ;
}
}
}
catch ( Exception ex ) { ExceptionManager . Publish ( ex ) ; }
return Menus ;
2016-11-04 16:06:40 +08:00
} , CacheSection . RetrieveDescByKey ( RetrieveMenusByUserIDDataKey ) ) ;
2016-10-26 14:02:40 +08:00
}
/// <summary>
2016-11-05 23:15:07 +08:00
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static IEnumerable < Menu > RetrieveNavigationsByUserId ( int userId )
{
var navs = ( userId = = 0 ? RetrieveMenus ( ) : RetrieveMenusByUserId ( userId ) ) . Where ( m = > m . Category = = "0" ) ;
2016-11-06 00:32:55 +08:00
var root = navs . Where ( m = > m . ParentId = = 0 ) . OrderBy ( m = > m . Order ) ;
2016-11-05 23:15:07 +08:00
CascadeMenu ( navs , root ) ;
return root ;
}
private static void CascadeMenu ( IEnumerable < Menu > navs , IEnumerable < Menu > level )
{
level . ToList ( ) . ForEach ( m = >
{
2016-11-06 00:32:55 +08:00
m . Menus = navs . Where ( sub = > sub . ParentId = = m . ID ) . OrderBy ( sub = > sub . Order ) ;
2016-11-05 23:15:07 +08:00
CascadeMenu ( navs , m . Menus ) ;
} ) ;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static IEnumerable < Menu > RetrieveLinksByUserId ( int userId )
{
var navs = ( userId = = 0 ? RetrieveMenus ( ) : RetrieveMenusByUserId ( userId ) ) . Where ( m = > m . Category = = "1" ) ;
2016-11-06 00:32:55 +08:00
var root = navs . Where ( m = > m . ParentId = = 0 ) . OrderBy ( m = > m . Order ) ;
2016-11-05 23:15:07 +08:00
CascadeMenu ( navs , root ) ;
return root ;
}
/// <summary>
2016-10-26 14:02:40 +08:00
/// 删除菜单信息
/// </summary>
/// <param name="ids"></param>
public static bool DeleteMenu ( string ids )
{
bool ret = false ;
if ( string . IsNullOrEmpty ( ids ) | | ids . Contains ( "'" ) ) return ret ;
try
{
string sql = string . Format ( CultureInfo . InvariantCulture , "Delete from Navigations where ID in ({0})" , ids ) ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) ;
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( menuIds : ids ) ;
2016-10-26 14:02:40 +08:00
ret = true ;
}
catch ( Exception ex )
{
ExceptionManager . Publish ( ex ) ;
}
return ret ;
}
/// <summary>
/// 保存新建/更新的菜单信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveMenu ( Menu p )
{
if ( p = = null ) throw new ArgumentNullException ( "p" ) ;
bool ret = false ;
2016-10-26 21:51:38 +08:00
if ( string . IsNullOrEmpty ( p . Name ) ) return ret ;
2016-10-26 14:02:40 +08:00
if ( p . Name . Length > 50 ) p . Name . Substring ( 0 , 50 ) ;
2016-10-26 21:51:38 +08:00
if ( p . Icon ! = null & & p . Icon . Length > 50 ) p . Icon . Substring ( 0 , 50 ) ;
if ( p . Url ! = null & & p . Url . Length > 50 ) p . Url . Substring ( 0 , 50 ) ;
2016-10-26 14:02:40 +08:00
string sql = p . ID = = 0 ?
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category)" :
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category where ID = @ID" ;
try
{
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ID" , p . ID , ParameterDirection . Input ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ParentId" , p . ParentId , ParameterDirection . Input ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Name" , p . Name , ParameterDirection . Input ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Order" , p . Order , ParameterDirection . Input ) ) ;
2016-10-26 21:51:38 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Icon" , DBAccess . ToDBValue ( p . Icon ) , ParameterDirection . Input ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Url" , DBAccess . ToDBValue ( p . Url ) , ParameterDirection . Input ) ) ;
2016-10-26 14:02:40 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Category" , p . Category , ParameterDirection . Input ) ) ;
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) ;
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( menuIds : p . ID = = 0 ? "" : p . ID . ToString ( ) ) ;
2016-10-26 14:02:40 +08:00
ret = true ;
}
catch ( DbException ex )
{
ExceptionManager . Publish ( ex ) ;
}
return ret ;
}
}
}