2018-10-19 23:09:52 +08:00
using Bootstrap.Security ;
using Longbow.Data ;
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
using System.Linq ;
namespace Bootstrap.DataAccess.SQLite
{
/// <summary>
/// 用户表实体类
/// </summary>
public class User : DataAccess . User
{
/// <summary>
/// 通过角色ID保存当前授权用户( 插入)
/// </summary>
2018-10-23 15:37:42 +08:00
/// <param name="roleId">角色ID</param>
2018-10-19 23:09:52 +08:00
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public override bool SaveUsersByRoleId ( string roleId , IEnumerable < string > userIds )
2018-10-19 23:09:52 +08:00
{
bool ret = false ;
2018-10-20 22:25:53 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
try
{
//删除用户角色表该角色所有的用户
2018-10-23 15:37:42 +08:00
string sql = $"delete from UserRole where RoleID = {roleId}" ;
2018-10-20 22:25:53 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
2018-10-19 23:09:52 +08:00
{
2018-10-20 22:25:53 +08:00
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2018-10-19 23:09:52 +08:00
//批插入用户角色表
2018-10-23 15:37:42 +08:00
userIds . ToList ( ) . ForEach ( uId = >
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
cmd . CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {uId}, {roleId})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
} ) ;
transaction . CommitTransaction ( ) ;
2018-10-19 23:09:52 +08:00
}
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
/// <summary>
/// 通过部门ID保存当前授权用户( 插入)
/// </summary>
2018-10-23 15:37:42 +08:00
/// <param name="groupId">GroupID</param>
2018-10-19 23:09:52 +08:00
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
2018-10-30 13:07:29 +08:00
public override bool SaveUsersByGroupId ( string groupId , IEnumerable < string > userIds )
2018-10-19 23:09:52 +08:00
{
bool ret = false ;
2018-10-20 22:25:53 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
try
{
//删除用户角色表该角色所有的用户
2018-10-23 15:37:42 +08:00
string sql = $"delete from UserGroup where GroupID = {groupId}" ;
2018-10-20 22:25:53 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
2018-10-19 23:09:52 +08:00
{
2018-10-20 22:25:53 +08:00
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2018-10-19 23:09:52 +08:00
//批插入用户角色表
2018-10-23 15:37:42 +08:00
userIds . ToList ( ) . ForEach ( uId = >
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
cmd . CommandText = $"Insert Into UserGroup (UserID, GroupID) Values ( {uId}, {groupId})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
} ) ;
transaction . CommitTransaction ( ) ;
2018-10-19 23:09:52 +08:00
}
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override BootstrapUser RetrieveUserByUserName ( string userName )
{
2018-10-28 15:03:59 +08:00
BootstrapUser user = null ;
var sql = "select UserName, DisplayName, case ifnull(d.Code, '') when '' then '~/images/uploader/' else d.Code end || ifnull(Icon, 'default.jpg') Icon, u.Css from Users u left join Dicts d on d.Define = '0' and d.Category = '头像地址' and Name = '头像路径' where ApprovedTime is not null and UserName = @UserName" ;
var db = DbAccessManager . DBAccess ;
var cmd = db . CreateCommand ( CommandType . Text , sql ) ;
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
if ( reader . Read ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
user = new BootstrapUser
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
UserName = ( string ) reader [ 0 ] ,
DisplayName = ( string ) reader [ 1 ] ,
Icon = ( string ) reader [ 2 ] ,
Css = reader . IsDBNull ( 3 ) ? string . Empty : ( string ) reader [ 3 ]
} ;
2018-10-19 23:09:52 +08:00
}
2018-10-28 15:03:59 +08:00
}
return user ;
2018-10-19 23:09:52 +08:00
}
}
}