2018-11-09 09:58:17 +08:00
using Bootstrap.Security.DataAccess ;
2019-01-14 12:33:09 +08:00
using Longbow.Data ;
2018-10-19 23:09:52 +08:00
using System ;
using System.Collections.Generic ;
2018-10-21 10:02:59 +08:00
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 ; }
2018-11-09 09:58:17 +08:00
2016-10-22 16:58:31 +08:00
/// <summary>
/// 获得/设置 群组名称
/// </summary>
public string GroupName { get ; set ; }
2018-11-09 09:58:17 +08:00
2016-10-22 16:58:31 +08:00
/// <summary>
/// 获得/设置 群组描述
/// </summary>
public string Description { get ; set ; }
2018-11-09 09:58:17 +08:00
2016-10-28 21:46:48 +08:00
/// <summary>
/// 获取/设置 用户群组关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get ; set ; }
2018-11-09 09:58:17 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询所有群组信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Group > Retrieves ( ) = > DbManager . Create ( ) . Fetch < Group > ( "select * from Groups" ) ;
2018-11-09 09:58:17 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 删除群组信息
/// </summary>
/// <param name="ids"></param>
2019-01-11 23:20:28 +08:00
public virtual bool Delete ( IEnumerable < string > value )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
var ids = string . Join ( "," , value ) ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . BeginTransaction ( ) ;
db . Execute ( $"delete from UserGroup where GroupID in ({ids})" ) ;
db . Execute ( $"delete from RoleGroup where GroupID in ({ids})" ) ;
db . Execute ( $"delete from Groups where ID in ({ids})" ) ;
db . CompleteTransaction ( ) ;
ret = true ;
}
catch ( Exception e )
{
db . AbortTransaction ( ) ;
throw e ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2018-11-09 09:58:17 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存新建/更新的群组信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Save ( Group p )
2018-10-21 10:02:59 +08:00
{
2019-01-14 12:33:09 +08:00
DbManager . Create ( ) . Save ( p ) ;
2019-01-11 23:20:28 +08:00
return ! p . Id . IsNullOrEmpty ( ) ;
2018-10-21 10:02:59 +08:00
}
2018-11-09 09:58:17 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据用户查询部门信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Group > RetrievesByUserId ( string userId ) = > DbManager . Create ( ) . Fetch < Group > ( "select g.ID, g.GroupName, g.Description, case ug.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join UserGroup ug on g.ID = ug.GroupID and UserID = @0" , userId ) ;
2019-01-11 23:20:28 +08:00
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Group > RetrievesByRoleId ( string roleId ) = > DbManager . Create ( ) . Fetch < Group > ( "select g.ID, g.GroupName, g.Description, case rg.GroupID when g.ID then 'checked' else '' end Checked from Groups g left join RoleGroup rg on g.ID = rg.GroupID and RoleID = @0" , roleId ) ;
2018-11-09 09:58:17 +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>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByUserId ( string userId , IEnumerable < string > groupIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . BeginTransaction ( ) ;
//删除用户部门表中该用户所有的部门关系
db . Execute ( "delete from UserGroup where UserID = @0" , userId ) ;
2019-01-14 12:33:09 +08:00
db . InsertBatch ( "UserGroup" , groupIds . Select ( g = > new { UserID = userId , GroupID = g } ) ) ;
2019-01-11 23:20:28 +08:00
db . CompleteTransaction ( ) ;
ret = true ;
2018-10-21 10:02:59 +08:00
}
2019-01-11 23:20:28 +08:00
catch ( Exception ex )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . AbortTransaction ( ) ;
throw ex ;
2018-10-28 15:03:59 +08:00
}
2019-01-11 23:20:28 +08:00
return ret ;
2018-10-21 10:02:59 +08:00
}
2018-11-09 09:58:17 +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>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByRoleId ( string roleId , IEnumerable < string > groupIds )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
try
{
db . BeginTransaction ( ) ;
//删除角色部门表该角色所有的部门
db . Execute ( "delete from RoleGroup where RoleID = @0" , roleId ) ;
2019-01-14 12:33:09 +08:00
db . InsertBatch ( "RoleGroup" , groupIds . Select ( g = > new { RoleID = roleId , GroupID = g } ) ) ;
2019-01-11 23:20:28 +08:00
db . CompleteTransaction ( ) ;
ret = true ;
}
catch ( Exception ex )
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . AbortTransaction ( ) ;
throw ex ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2018-11-09 09:58:17 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < string > RetrievesByUserName ( string userName ) = > DbHelper . RetrieveGroupsByUserName ( userName ) ;
2016-10-22 16:58:31 +08:00
}
}