2018-10-30 13:07:29 +08:00
using Longbow.Data ;
2018-10-19 23:09:52 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
2018-10-21 10:02:59 +08:00
using System.Data.SqlClient ;
using System.Linq ;
2018-10-19 23:09:52 +08:00
namespace Bootstrap.DataAccess
2016-10-22 16:58:31 +08:00
{
/// <summary>
2017-03-30 16:15:45 +08:00
///
2016-10-22 16:58:31 +08:00
/// </summary>
public class Group
{
/// <summary>
/// 获得/设置 群组主键ID
/// </summary>
2018-10-30 13:07:29 +08:00
public string Id { get ; set ; }
2016-10-22 16:58:31 +08:00
/// <summary>
/// 获得/设置 群组名称
/// </summary>
public string GroupName { get ; set ; }
/// <summary>
/// 获得/设置 群组描述
/// </summary>
public string Description { get ; set ; }
2016-10-28 21:46:48 +08:00
/// <summary>
/// 获取/设置 用户群组关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get ; set ; }
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询所有群组信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public virtual IEnumerable < Group > RetrieveGroups ( )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
string sql = "select * from Groups" ;
List < Group > groups = new List < Group > ( ) ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
groups . Add ( new Group ( )
2018-10-21 10:02:59 +08:00
{
2018-10-30 13:07:29 +08:00
Id = reader [ 0 ] . ToString ( ) ,
2018-10-28 15:03:59 +08:00
GroupName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return groups ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 删除群组信息
/// </summary>
/// <param name="ids"></param>
2018-10-30 13:07:29 +08:00
public virtual bool DeleteGroup ( IEnumerable < string > value )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
var ids = string . Join ( "," , value ) ;
2018-11-02 19:09:31 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-21 10:02:59 +08:00
{
2018-11-02 19:09:31 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , $"delete from UserGroup where GroupID in ({ids})" ) )
{
try
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from RoleGroup where GroupID in ({ids})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from Groups where ID in ({ids})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
transaction . CommitTransaction ( ) ;
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存新建/更新的群组信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual bool SaveGroup ( Group p )
{
bool ret = false ;
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 ) ;
2018-10-30 13:07:29 +08:00
string sql = string . IsNullOrEmpty ( p . Id ) ?
2018-10-21 10:02:59 +08:00
"Insert Into Groups (GroupName, Description) Values (@GroupName, @Description)" :
"Update Groups set GroupName = @GroupName, Description = @Description where ID = @ID" ;
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@ID" , p . Id ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@GroupName" , p . GroupName ) ) ;
2018-10-21 21:08:35 +08:00
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@Description" , DbAdapterManager . ToDBValue ( p . Description ) ) ) ;
2018-10-21 10:02:59 +08:00
ret = DbAccessManager . DBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据用户查询部门信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public virtual IEnumerable < Group > RetrieveGroupsByUserId ( string userId )
2018-10-21 10:02:59 +08:00
{
2018-11-02 12:43:21 +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" ;
2018-10-28 15:03:59 +08:00
List < Group > groups = new List < Group > ( ) ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@UserID" , userId ) ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
groups . Add ( new Group ( )
2018-10-21 10:02:59 +08:00
{
2018-10-30 13:07:29 +08:00
Id = reader [ 0 ] . ToString ( ) ,
2018-10-28 15:03:59 +08:00
GroupName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return groups ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存用户部门关系
/// </summary>
2018-10-23 14:24:22 +08:00
/// <param name="userId"></param>
2018-10-19 23:09:52 +08:00
/// <param name="groupIds"></param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public virtual bool SaveGroupsByUserId ( string userId , IEnumerable < string > groupIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
//判断用户是否选定角色
2018-10-23 14:24:22 +08:00
groupIds . ToList ( ) . ForEach ( groupId = > dt . Rows . Add ( userId , groupId ) ) ;
2018-10-21 10:02:59 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
{
try
{
//删除用户部门表中该用户所有的部门关系
2018-10-23 14:24:22 +08:00
string sql = $"delete from UserGroup where UserID = {userId}" ;
2018-10-21 10:02:59 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
// insert batch data into config table
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . BatchSize = 1000 ;
bulk . DestinationTableName = "UserGroup" ;
bulk . ColumnMappings . Add ( "UserID" , "UserID" ) ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public virtual IEnumerable < Group > RetrieveGroupsByRoleId ( string roleId )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
List < Group > groups = new List < Group > ( ) ;
2018-11-02 12:43:21 +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" ;
2018-10-28 15:03:59 +08:00
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@RoleID" , roleId ) ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
groups . Add ( new Group ( )
2018-10-21 10:02:59 +08:00
{
2018-10-30 13:07:29 +08:00
Id = reader [ 0 ] . ToString ( ) ,
2018-10-28 15:03:59 +08:00
GroupName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return groups ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据角色ID以及选定的部门ID, 保到角色部门表
/// </summary>
2018-10-23 14:24:22 +08:00
/// <param name="roleId"></param>
2018-10-19 23:09:52 +08:00
/// <param name="groupIds"></param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public virtual bool SaveGroupsByRoleId ( string roleId , IEnumerable < string > groupIds )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
2018-10-23 14:24:22 +08:00
groupIds . ToList ( ) . ForEach ( groupId = > dt . Rows . Add ( groupId , roleId ) ) ;
2018-10-21 10:02:59 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
{
try
{
//删除角色部门表该角色所有的部门
2018-10-23 14:24:22 +08:00
string sql = $"delete from RoleGroup where RoleID = {roleId}" ;
2018-10-21 10:02:59 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
//批插入角色部门表
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . BatchSize = 1000 ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
bulk . DestinationTableName = "RoleGroup" ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public virtual IEnumerable < string > RetrieveGroupsByUserName ( string userName )
{
2018-10-28 15:03:59 +08:00
var entities = new List < string > ( ) ;
var db = DbAccessManager . DBAccess ;
2018-11-02 12:43:21 +08:00
using ( DbCommand cmd = db . CreateCommand ( CommandType . Text , "select g.GroupName, g.Description from Groups g inner join UserGroup ug on g.ID = ug.GroupID inner join Users u on ug.UserID = u.ID where UserName = @UserName" ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
cmd . Parameters . Add ( db . CreateParameter ( "@UserName" , userName ) ) ;
using ( DbDataReader reader = db . ExecuteReader ( cmd ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
entities . Add ( ( string ) reader [ 0 ] ) ;
2018-10-19 23:09:52 +08:00
}
}
2018-10-28 15:03:59 +08:00
}
return entities ;
2018-10-19 23:09:52 +08:00
}
2016-10-22 16:58:31 +08:00
}
}