2018-06-07 00:45:47 +08:00
using Longbow.Cache ;
2016-10-28 21:46:48 +08:00
using Longbow.Data ;
2018-06-07 00:45:47 +08:00
using Longbow.Logging ;
2016-10-22 16:58:31 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
2016-10-28 21:46:48 +08:00
using System.Data.SqlClient ;
2016-10-22 16:58:31 +08:00
using System.Linq ;
namespace Bootstrap.DataAccess
{
/// <summary>
/// author:liuchun
/// date:2016.10.22
/// </summary>
public static class GroupHelper
{
2016-11-05 12:11:16 +08:00
internal const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups" ;
2017-03-30 16:15:45 +08:00
internal const string RetrieveGroupsByUserIdDataKey = "GroupHelper-RetrieveGroupsByUserId" ;
internal const string RetrieveGroupsByRoleIdDataKey = "GroupHelper-RetrieveGroupsByRoleId" ;
2016-10-22 16:58:31 +08:00
/// <summary>
/// 查询所有群组信息
/// </summary>
2016-11-10 12:20:26 +08:00
/// <param name="id"></param>
2016-10-22 16:58:31 +08:00
/// <returns></returns>
2016-11-10 12:20:26 +08:00
public static IEnumerable < Group > RetrieveGroups ( int id = 0 )
2016-10-22 16:58:31 +08:00
{
2017-04-05 11:54:21 +08:00
var ret = CacheManager . GetOrAdd ( RetrieveGroupsDataKey , key = >
2016-10-22 16:58:31 +08:00
{
2016-11-04 16:06:40 +08:00
string sql = "select * from Groups" ;
2017-03-30 16:15:45 +08:00
List < Group > groups = new List < Group > ( ) ;
2016-10-22 16:58:31 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
try
{
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
2017-03-30 16:15:45 +08:00
groups . Add ( new Group ( )
2016-10-22 16:58:31 +08:00
{
2017-03-30 16:15:45 +08:00
Id = ( int ) reader [ 0 ] ,
2016-10-22 16:58:31 +08:00
GroupName = ( string ) reader [ 1 ] ,
2016-12-26 12:19:47 +08:00
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ]
2016-10-22 16:58:31 +08:00
} ) ;
}
}
}
catch ( Exception ex ) { ExceptionManager . Publish ( ex ) ; }
2017-03-30 16:15:45 +08:00
return groups ;
2017-04-05 11:54:21 +08:00
} ) ;
2017-03-30 16:15:45 +08:00
return id = = 0 ? ret : ret . Where ( t = > id = = t . Id ) ;
2016-10-22 16:58:31 +08:00
}
/// <summary>
/// 删除群组信息
/// </summary>
/// <param name="ids"></param>
2018-06-08 12:54:05 +08:00
public static bool DeleteGroup ( IEnumerable < int > value )
2016-10-22 16:58:31 +08:00
{
2016-11-07 15:17:10 +08:00
bool ret = false ;
try
2016-10-24 20:53:05 +08:00
{
2018-06-08 12:54:05 +08:00
var ids = string . Join ( "," , value ) ;
2016-11-07 15:17:10 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_DeleteGroups" ) )
2016-10-24 20:53:05 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ids" , ids ) ) ;
2016-11-07 15:17:10 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) ;
2016-10-24 20:53:05 +08:00
}
2016-11-07 15:17:10 +08:00
CacheCleanUtility . ClearCache ( groupIds : ids ) ;
ret = true ;
}
catch ( Exception ex )
{
ExceptionManager . Publish ( ex ) ;
2016-10-24 20:53:05 +08:00
}
2016-11-07 15:17:10 +08:00
return ret ;
2016-10-22 16:58:31 +08:00
}
/// <summary>
/// 保存新建/更新的群组信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveGroup ( Group p )
{
bool ret = false ;
2017-03-30 16:15:45 +08:00
if ( p . GroupName . Length > 50 ) p . GroupName = p . GroupName . Substring ( 0 , 50 ) ;
if ( ! string . IsNullOrEmpty ( p . Description ) & & p . Description . Length > 500 ) p . Description = p . Description . Substring ( 0 , 500 ) ;
string sql = p . Id = = 0 ?
2016-10-22 16:58:31 +08:00
"Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" :
"Update Groups set GroupName = @GroupName, Description = @Description where ID = @ID" ;
try
{
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ID" , p . Id ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@GroupName" , p . GroupName ) ) ;
2018-06-07 00:45:47 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Description" , DBAccessFactory . ToDBValue ( p . Description ) ) ) ;
2016-10-22 16:58:31 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) ;
}
2017-03-30 16:15:45 +08:00
CacheCleanUtility . ClearCache ( groupIds : p . Id = = 0 ? string . Empty : p . Id . ToString ( ) ) ;
2016-10-22 16:58:31 +08:00
ret = true ;
}
catch ( DbException ex )
{
ExceptionManager . Publish ( ex ) ;
}
return ret ;
}
2016-10-28 21:46:48 +08:00
/// <summary>
/// 根据用户查询部门信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public static IEnumerable < Group > RetrieveGroupsByUserId ( int userId )
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveGroupsByUserIdDataKey , userId ) ;
2017-04-05 11:54:21 +08:00
var ret = CacheManager . GetOrAdd ( key , k = >
2016-10-28 21:46:48 +08:00
{
2016-11-04 16:06:40 +08:00
string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID" ;
2017-03-30 16:15:45 +08:00
List < Group > groups = new List < Group > ( ) ;
2016-10-28 21:46:48 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserID" , userId ) ) ;
2016-10-28 21:46:48 +08:00
try
{
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
2017-03-30 16:15:45 +08:00
groups . Add ( new Group ( )
2016-10-28 21:46:48 +08:00
{
2017-03-30 16:15:45 +08:00
Id = ( int ) reader [ 0 ] ,
2016-10-28 21:46:48 +08:00
GroupName = ( string ) reader [ 1 ] ,
2016-12-26 12:19:47 +08:00
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
2016-10-28 21:46:48 +08:00
Checked = ( string ) reader [ 3 ]
} ) ;
}
}
}
catch ( Exception ex ) { ExceptionManager . Publish ( ex ) ; }
2017-03-30 16:15:45 +08:00
return groups ;
2017-04-05 11:54:21 +08:00
} , RetrieveGroupsByUserIdDataKey ) ;
2016-10-28 21:46:48 +08:00
return ret ;
}
/// <summary>
/// 保存用户部门关系
/// </summary>
/// <param name="id"></param>
2017-03-30 16:15:45 +08:00
/// <param name="groupIds"></param>
2016-10-28 21:46:48 +08:00
/// <returns></returns>
2016-10-31 15:55:31 +08:00
public static bool SaveGroupsByUserId ( int id , string groupIds )
2016-10-28 21:46:48 +08:00
{
2016-10-31 15:55:31 +08:00
var ret = false ;
2016-10-28 21:46:48 +08:00
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
//判断用户是否选定角色
2016-11-02 12:12:34 +08:00
if ( ! string . IsNullOrEmpty ( groupIds ) ) groupIds . Split ( ',' ) . ToList ( ) . ForEach ( groupId = > dt . Rows . Add ( id , groupId ) ) ;
2016-10-31 15:55:31 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
2016-10-28 21:46:48 +08:00
{
2016-10-31 15:55:31 +08:00
try
2016-10-28 21:46:48 +08:00
{
2016-10-31 15:55:31 +08:00
//删除用户部门表中该用户所有的部门关系
string sql = "delete from UserGroup where UserID=@UserID;" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-28 21:46:48 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserID" , id ) ) ;
2016-10-31 15:55:31 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2016-10-28 21:46:48 +08:00
2016-10-31 15:55:31 +08:00
// insert batch data into config table
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
2016-10-28 21:46:48 +08:00
{
2016-10-31 15:55:31 +08:00
bulk . BatchSize = 1000 ;
bulk . DestinationTableName = "UserGroup" ;
bulk . ColumnMappings . Add ( "UserID" , "UserID" ) ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
2016-10-28 21:46:48 +08:00
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( groupIds : groupIds , userIds : id . ToString ( ) ) ;
2016-10-31 15:55:31 +08:00
ret = true ;
}
catch ( Exception ex )
{
ExceptionManager . Publish ( ex ) ;
transaction . RollbackTransaction ( ) ;
2016-10-28 21:46:48 +08:00
}
}
2016-10-31 15:55:31 +08:00
return ret ;
2016-10-28 21:46:48 +08:00
}
2016-10-31 15:55:31 +08:00
/// <summary>
2016-10-29 09:24:55 +08:00
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public static IEnumerable < Group > RetrieveGroupsByRoleId ( int roleId )
{
2017-03-30 16:15:45 +08:00
string k = string . Format ( "{0}-{1}" , RetrieveGroupsByRoleIdDataKey , roleId ) ;
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( k , key = >
2016-10-29 09:24:55 +08:00
{
2017-03-30 16:15:45 +08:00
List < Group > groups = new List < Group > ( ) ;
2016-10-29 09:24:55 +08:00
string sql = "select g.ID,g.GroupName,g.[Description],case rg.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join RoleGroup rg on g.ID=rg.GroupID and RoleID=@RoleID" ;
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@RoleID" , roleId ) ) ;
2016-10-29 09:24:55 +08:00
try
{
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
{
while ( reader . Read ( ) )
{
2017-03-30 16:15:45 +08:00
groups . Add ( new Group ( )
2016-10-29 09:24:55 +08:00
{
2017-03-30 16:15:45 +08:00
Id = ( int ) reader [ 0 ] ,
2016-10-31 15:55:31 +08:00
GroupName = ( string ) reader [ 1 ] ,
2016-12-26 12:19:47 +08:00
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
2016-10-29 09:24:55 +08:00
Checked = ( string ) reader [ 3 ]
} ) ;
}
}
}
catch ( Exception ex ) { ExceptionManager . Publish ( ex ) ; }
2017-03-30 16:15:45 +08:00
return groups ;
2017-04-05 11:54:21 +08:00
} , RetrieveGroupsByRoleIdDataKey ) ;
2016-10-29 09:24:55 +08:00
}
/// <summary>
/// 根据角色ID以及选定的部门ID, 保到角色部门表
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="id"></param>
/// <param name="groupIds"></param>
2016-10-29 09:24:55 +08:00
/// <returns></returns>
public static bool SaveGroupsByRoleId ( int id , string groupIds )
{
bool ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
2016-11-02 12:12:34 +08:00
if ( ! string . IsNullOrEmpty ( groupIds ) ) groupIds . Split ( ',' ) . ToList ( ) . ForEach ( groupId = > dt . Rows . Add ( groupId , id ) ) ;
2016-10-31 15:55:31 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
{
2016-10-29 09:24:55 +08:00
try
{
//删除角色部门表该角色所有的部门
string sql = "delete from RoleGroup where RoleID=@RoleID" ;
2016-10-31 15:55:31 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-29 09:24:55 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@RoleID" , id ) ) ;
2016-10-29 09:24:55 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
//批插入角色部门表
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
2016-10-31 15:55:31 +08:00
bulk . BatchSize = 1000 ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
2016-10-29 09:24:55 +08:00
bulk . DestinationTableName = "RoleGroup" ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( groupIds : groupIds , roleIds : id . ToString ( ) ) ;
2016-10-29 09:24:55 +08:00
ret = true ;
}
2016-10-31 15:55:31 +08:00
catch ( Exception ex )
{
2016-10-29 09:24:55 +08:00
ExceptionManager . Publish ( ex ) ;
transaction . RollbackTransaction ( ) ;
}
}
return ret ;
2016-10-22 16:58:31 +08:00
}
}
}