publicvirtualIEnumerable<User>Retrieves()=>DbManager.Create().Fetch<User>("select u.ID, u.UserName, u.DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description, ru.IsReset from Users u left join (select 1 as IsReset, UserName from ResetUsers group by UserName) ru on u.UserName = ru.UserName Where ApprovedTime is not null");
/// <summary>
/// 查询所有的新注册用户
/// </summary>
/// <returns></returns>
publicvirtualIEnumerable<User>RetrieveNewUsers()=>DbManager.Create().Fetch<User>("select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc");
/// <summary>
/// 删除用户
/// </summary>
/// <param name="value"></param>
publicvirtualboolDelete(IEnumerable<string>value)
{
if(!value.Any())returntrue;
boolret=false;
vardb=DbManager.Create();
try
{
varids=string.Join(",",value);
db.BeginTransaction();
db.Execute($"Delete from UserRole where UserID in ({ids})");
db.Execute($"delete from UserGroup where UserID in ({ids})");
db.Execute($"delete from Users where ID in ({ids})");
db.Execute("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @0",user.UserName);
returnDbManager.Create().Update<User>("set Password = @1, PassSalt = @2, DisplayName = @3 where ID = @0",id,newPassword,passSalt,displayName)==1;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
publicvirtualboolApprove(stringid,stringapprovedBy)=>DbManager.Create().Update<User>("set ApprovedTime = @1, ApprovedBy = @2 where ID = @0",id,DateTime.Now,approvedBy)==1;
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="rejectBy"></param>
/// <returns></returns>
publicvirtualboolReject(stringid,stringrejectBy)
{
varret=false;
vardb=DbManager.Create();
try
{
db.BeginTransaction();
db.Execute("insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) select UserName, DisplayName, Registertime, @1, @2, @3 from Users where ID = @0",id,rejectBy,DateTime.Now,"未填写");
db.Execute("delete from UserRole where UserId = @0",id);
db.Execute("delete from UserGroup where UserId = @0",id);
db.Execute("delete from users where ID = @0",id);
db.CompleteTransaction();
ret=true;
}
catch(Exceptionex)
{
db.AbortTransaction();
throwex;
}
returnret;
}
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
publicvirtualIEnumerable<User>RetrievesByRoleId(stringroleId)=>DbManager.Create().Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @0 where u.ApprovedTime is not null",roleId);
publicvirtualIEnumerable<User>RetrievesByGroupId(stringgroupId)=>DbManager.Create().Fetch<User>("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID = @0 where u.ApprovedTime is not null",groupId);