You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
754 lines
29 KiB
C#
754 lines
29 KiB
C#
|
|
|
|
//----------SysUser开始----------
|
|
|
|
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using Admin.Core.Common;
|
|
using Admin.Core.IRepository;
|
|
using Admin.Core.IRepository.ISys;
|
|
using Admin.Core.IService.ISys;
|
|
using Admin.Core.Model;
|
|
using Admin.Core.Model.Sys;
|
|
using AutoMapper;
|
|
using SqlSugar;
|
|
|
|
namespace Admin.Core.Service.Sys
|
|
{
|
|
/// <summary>
|
|
/// 用户信息表Service
|
|
/// </summary>
|
|
public partial class SysUserService : BaseServices<SysUser>, ISysUserService
|
|
{
|
|
IBaseRepository<SysUser> dal;
|
|
private readonly ISysUserRoleRepository _sysUserRoleRepository;
|
|
private readonly ISysRoleRepository _sysRoleRepository;
|
|
private readonly ISysPostRepository _sysPostRepository;
|
|
private readonly ISysUserPostRepository _sysUserPostRepository;
|
|
private readonly ISysDeptRepository _sysDeptRepository;
|
|
private readonly IMapper _mapper;
|
|
public SysUserService(IBaseRepository<SysUser> dal, ISysUserRoleRepository sysUserRoleRepository, ISysRoleRepository sysRoleRepository, ISysPostRepository sysPostRepository,
|
|
ISysUserPostRepository sysUserPostRepository, ISysDeptRepository sysDeptRepository, IMapper mapper)
|
|
{
|
|
this.dal = dal;
|
|
BaseDal = dal;
|
|
_sysUserRoleRepository = sysUserRoleRepository;
|
|
_sysRoleRepository = sysRoleRepository;
|
|
_sysPostRepository = sysPostRepository;
|
|
_sysUserPostRepository = sysUserPostRepository;
|
|
_sysDeptRepository = sysDeptRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户角色Key
|
|
/// </summary>
|
|
/// <param name="loginName"></param>
|
|
/// <param name="loginPwd"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetUserRoleNameStr(string loginName, string loginPwd)
|
|
{
|
|
string roleName = "";
|
|
var user = (await QueryAsync(a => a.LoginName == loginName && a.Password == loginPwd)).FirstOrDefault();
|
|
var roleList = await _sysRoleRepository.QueryAsync(a => a.DelFlag == false);
|
|
if (user != null)
|
|
{
|
|
var userRoles = await _sysUserRoleRepository.QueryAsync(ur => ur.UserID == user.UserID);
|
|
if (userRoles.Count > 0)
|
|
{
|
|
var arr = userRoles.Select(ur => ur.UserID).ToList();
|
|
var roles = roleList.Where(d => arr.Contains(d.RoleID));
|
|
|
|
roleName = string.Join(',', roles.Select(r => r.RoleKey).ToArray());
|
|
}
|
|
}
|
|
return roleName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件分页查询用户列表
|
|
/// </summary>
|
|
/// <param name="pageQuery"> 查询条件 </param>
|
|
/// <returns> 用户信息集合信息 </returns>
|
|
public async Task<PageModel<UserModel>> SelectUserList(PageQuery<UserQuery> pageQuery)
|
|
{
|
|
Expression<Func<SysUser, bool>> whereExpression = x => (bool)x.DelFlag == false;
|
|
if (pageQuery.Query.LoginName.IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.LoginName.Contains(pageQuery.Query.LoginName));
|
|
}
|
|
if (pageQuery.Query.UserName.IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.UserName.Contains(pageQuery.Query.UserName));
|
|
}
|
|
if (pageQuery.Query.PhoneNumber.IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.PhoneNumber.Contains(pageQuery.Query.PhoneNumber));
|
|
}
|
|
if (pageQuery.Query.Status.IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.Status == pageQuery.Query.Status);
|
|
}
|
|
if (pageQuery.Query.DateRange.IsNotEmptyOrNull() && pageQuery.Query.DateRange.Count > 0)
|
|
{
|
|
if (pageQuery.Query.DateRange[0].IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.CreateTime >= pageQuery.Query.DateRange[0]);
|
|
}
|
|
if (pageQuery.Query.DateRange.Count > 1 && pageQuery.Query.DateRange[1].IsNotEmptyOrNull())
|
|
{
|
|
whereExpression = whereExpression.And(x => x.CreateTime <= pageQuery.Query.DateRange[1]);
|
|
}
|
|
}
|
|
//部门相关查询
|
|
if (pageQuery.Query.DeptID.IsNotEmptyOrNull())
|
|
{
|
|
var depts = await _sysDeptRepository.SelectNormalChildrenIDsById((int)pageQuery.Query.DeptID);
|
|
whereExpression = whereExpression.And(x => depts.Contains((int)x.DeptID));
|
|
}
|
|
//角色相关查询
|
|
if (pageQuery.Query.RoleQuery)
|
|
{
|
|
if (pageQuery.Query.RoleID.IsNotEmptyOrNull() && pageQuery.Query.RoleID > 0 && pageQuery.Query.ContainsUser)
|
|
{
|
|
var userRoles = await _sysUserRoleRepository.QueryAsync(x => x.RoleID == pageQuery.Query.RoleID);
|
|
var userIds = userRoles.Select(x => x.UserID).ToList();
|
|
whereExpression = whereExpression.And(x => userIds.Contains(x.UserID));
|
|
}
|
|
else if (pageQuery.Query.RoleID.IsNotEmptyOrNull() && pageQuery.Query.RoleID > 0 && !pageQuery.Query.ContainsUser)
|
|
{
|
|
var userRoles = await _sysUserRoleRepository.QueryAsync(x => x.RoleID == pageQuery.Query.RoleID);
|
|
var userIds = userRoles.Select(x => x.UserID).ToList();
|
|
whereExpression = whereExpression.And(x => !userIds.Contains(x.UserID));
|
|
}
|
|
}
|
|
|
|
var data = await dal.QueryPageAsync(whereExpression, pageQuery.Page, pageQuery.PageSize, "UpdateTime desc");
|
|
var deptList = await _sysDeptRepository.QueryAsync();
|
|
var userModel = _mapper.Map<PageModel<UserModel>>(data);
|
|
userModel.data.ForEach(x =>
|
|
{
|
|
x.DeptName = deptList.Find(m => m.DeptID == x.DeptID)?.DeptName;
|
|
x.Password = string.Empty;
|
|
});
|
|
|
|
return userModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件分页查询已分配用户角色列表
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 用户信息集合信息 </returns>
|
|
public List<SysUser> SelectAllocatedList(SysUser user)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件分页查询未分配用户角色列表
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 用户信息集合信息 </returns>
|
|
public List<SysUser> SelectUnallocatedList(SysUser user)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过用户账号查询用户
|
|
/// </summary>
|
|
/// <param name="loginName"> 用户账号 </param>
|
|
/// <returns> 用户对象信息 </returns>
|
|
public async Task<SysUser> SelectUserByLoginName(string loginName)
|
|
{
|
|
var user = await dal.QueryAsync(x => x.LoginName == loginName);
|
|
return user.FirstOrDefault();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过用户ID查询用户
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <returns> 用户对象信息 </returns>
|
|
public async Task<SysUser> SelectUserById(int userId)
|
|
{
|
|
var user = await dal.QueryByIdAsync(userId);
|
|
return user;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID查询
|
|
/// </summary>
|
|
/// <param name="id">主键</param>
|
|
/// <returns></returns>
|
|
public async Task<UserView> GetByID(int id)
|
|
{
|
|
var data = await GetInfo(id);
|
|
var roles = await _sysRoleRepository.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE && x.RoleID != RoleView.Admin());
|
|
data.Roles = _mapper.Map<List<RoleModel>>(roles);
|
|
data.Posts = await _sysPostRepository.QueryAsync(x => x.Status == SysConst.ENABLE);
|
|
|
|
return data;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据用户编号获取详细信息
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserView> GetInfo(int userId)
|
|
{
|
|
var user = await SelectUserById(userId);
|
|
if (user.IsNotEmptyOrNull())
|
|
{
|
|
user.Password = string.Empty;
|
|
}
|
|
var userPosts = await _sysUserPostRepository.QueryAsync(x => x.UserID == userId);
|
|
var userRoles = await _sysUserRoleRepository.QueryAsync(x => x.UserID == userId);
|
|
UserView userView = new UserView
|
|
{
|
|
SysUser = user,
|
|
PostIds = userPosts.Select(x => x.PostID).ToArray(),
|
|
RoleIds = userRoles.Select(x => x.RoleID).ToArray(),
|
|
Dept = await _sysDeptRepository.QueryByIdAsync(user.IsNotEmptyOrNull() ? user.DeptID ?? 0 : 0),
|
|
};
|
|
var Roles = await _sysRoleRepository.QueryAsync(x => userView.RoleIds.Contains(x.RoleID));
|
|
userView.Roles = _mapper.Map<List<RoleModel>>(Roles);
|
|
userView.Posts = await _sysPostRepository.QueryAsync(x => userView.PostIds.Contains(x.PostID));
|
|
return userView;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询用户所属角色组
|
|
/// </summary>
|
|
/// <param name="loginName"> 用户账号 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<List<SysRole>> SelectUserRoleGroup(string loginName)
|
|
{
|
|
var roleList = await QueryMuchAsync<SysUser, SysUserRole, SysRole, SysRole>(
|
|
(a, b, c) => new object[] {
|
|
JoinType.Inner, a.UserID == b.UserID,
|
|
JoinType.Inner, b.RoleID == c.RoleID
|
|
},
|
|
(a, b, c) => new SysRole
|
|
{
|
|
RoleID = c.RoleID,
|
|
RoleName = c.RoleName,
|
|
RoleKey = c.RoleKey,
|
|
RoleSort = c.RoleSort,
|
|
DataScope = c.DataScope,
|
|
MenuCheckStrictly = c.MenuCheckStrictly,
|
|
DeptCheckStrictly = c.DeptCheckStrictly,
|
|
Status = c.Status,
|
|
DelFlag = c.DelFlag,
|
|
Remark = c.Remark,
|
|
CreateBy = c.CreateBy,
|
|
CreateTime = c.CreateTime,
|
|
UpdateBy = c.UpdateBy,
|
|
UpdateTime = c.UpdateTime
|
|
},
|
|
(a, b, c) => c.Status == SysConst.ENABLE && c.DelFlag == false && a.LoginName == loginName
|
|
);
|
|
|
|
return roleList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询用户所属角色组
|
|
/// </summary>
|
|
/// <param name="userId"> 用户Id </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<List<SysRole>> SelectUserRoleGroup(int userId)
|
|
{
|
|
var roleList = await QueryMuchAsync<SysUser, SysUserRole, SysRole, SysRole>(
|
|
(a, b, c) => new object[] {
|
|
JoinType.Inner, a.UserID == b.UserID,
|
|
JoinType.Inner, b.RoleID == c.RoleID
|
|
},
|
|
(a, b, c) => new SysRole
|
|
{
|
|
RoleID = c.RoleID,
|
|
RoleName = c.RoleName,
|
|
RoleKey = c.RoleKey,
|
|
RoleSort = c.RoleSort,
|
|
DataScope = c.DataScope,
|
|
MenuCheckStrictly = c.MenuCheckStrictly,
|
|
DeptCheckStrictly = c.DeptCheckStrictly,
|
|
Status = c.Status,
|
|
DelFlag = c.DelFlag,
|
|
Remark = c.Remark,
|
|
CreateBy = c.CreateBy,
|
|
CreateTime = c.CreateTime,
|
|
UpdateBy = c.UpdateBy,
|
|
UpdateTime = c.UpdateTime
|
|
},
|
|
(a, b, c) => c.Status == SysConst.ENABLE && c.DelFlag == false && a.UserID == userId
|
|
);
|
|
|
|
return roleList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询用户所属岗位组
|
|
/// </summary>
|
|
/// <param name="loginName"> 用户名 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<List<SysPost>> SelectUserPostGroup(string loginName)
|
|
{
|
|
var postList = await QueryMuchAsync<SysUser, SysUserPost, SysPost, SysPost>(
|
|
(a, b, c) => new object[] {
|
|
JoinType.Inner, a.UserID == b.UserID,
|
|
JoinType.Inner, b.PostID == c.PostID
|
|
},
|
|
(a, b, c) => new SysPost
|
|
{
|
|
PostID = c.PostID,
|
|
PostCode = c.PostCode,
|
|
PostName = c.PostName,
|
|
PostSort = c.PostSort,
|
|
Status = c.Status,
|
|
Remark = c.Remark,
|
|
CreateBy = c.CreateBy,
|
|
CreateTime = c.CreateTime,
|
|
UpdateBy = c.UpdateBy,
|
|
UpdateTime = c.UpdateTime
|
|
},
|
|
(a, b, c) => c.Status == SysConst.ENABLE && a.LoginName == loginName
|
|
);
|
|
|
|
return postList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验用户账号是否存在
|
|
/// </summary>
|
|
/// <param name="loginName"> 用户账号 </param>
|
|
/// <returns> true-存在 false-不存在 </returns>
|
|
public async Task<bool> CheckLoginNameUnique(string loginName)
|
|
{
|
|
var exist = (await dal.QueryAsync(x => x.LoginName == loginName)).FirstOrDefault();
|
|
return exist.IsNotEmptyOrNull();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验用户手机号是否存在
|
|
/// </summary>
|
|
/// <param name="phoneNumber"> 手机号
|
|
/// <returns> 存在数量 </returns>
|
|
public async Task<int> checkPhoneUnique(string phoneNumber)
|
|
{
|
|
var exist = await dal.QueryAsync(x => x.PhoneNumber == phoneNumber);
|
|
return exist.IsNotEmptyOrNull() ? exist.Count : 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验email是否存在
|
|
/// </summary>
|
|
/// <param name="email"> email
|
|
/// <returns> 存在数量 </returns>
|
|
public async Task<int> CheckEmailUnique(string email)
|
|
{
|
|
var exist = await dal.QueryAsync(x => x.Email == email);
|
|
return exist.IsNotEmptyOrNull() ? exist.Count : 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增保存用户信息
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<int> InsertUser(UserView user)
|
|
{
|
|
user.SysUser.Password = MD5Helper.MD5Encrypt32(user.SysUser.Password);
|
|
user.SysUser.UserType = "00";
|
|
user.SysUser.DelFlag = false;
|
|
user.SysUser.CreateTime = DateTime.Now;
|
|
user.SysUser.UpdateTime = DateTime.Now;
|
|
// 新增用户信息
|
|
int userId = await dal.AddAsync(user.SysUser);
|
|
if (userId > 0)
|
|
{
|
|
// 新增用户岗位关联
|
|
await InsertUserPost(userId, user.PostIds);
|
|
// 新增用户与角色管理
|
|
await InsertUserRole(userId, user.RoleIds);
|
|
}
|
|
return userId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册用户信息
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> RegisterUser(SysUser user)
|
|
{
|
|
return await dal.AddAsync(user) > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改保存用户信息
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> UpdateUser(UserView user)
|
|
{
|
|
// 更新用户与角色关联
|
|
var URole = await InsertUserAuth(user.SysUser.UserID, user.RoleIds);
|
|
// 更新用户与岗位关联
|
|
var UPost = await UpdateUserPost(user.SysUser.UserID, user.PostIds);
|
|
//更新用户基础信息
|
|
if (URole && UPost)
|
|
{
|
|
return await UpdateUserProfile(user.SysUser);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据用户编号获取授权角色
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserView> AuthRole(int userId)
|
|
{
|
|
var user = await dal.QueryByIdAsync(userId);
|
|
var roleIds = (await _sysRoleRepository.GetUserRolesByUserId(userId)).Select(x => x.RoleID).ToList();
|
|
var roles = await _sysRoleRepository.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE);
|
|
var roleViews = _mapper.Map<List<RoleModel>>(roles);
|
|
|
|
roleViews.ForEach(x => x.Flag = roleIds.Contains(x.RoleID));
|
|
|
|
UserView userView = new UserView
|
|
{
|
|
SysUser = user,
|
|
Roles = UserView.IsAdmin(userId) ? roleViews : roleViews.Where(x => x.RoleKey != SysConst.ADMIN_KEY).ToList()
|
|
};
|
|
|
|
return userView;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询用户已分配角色列表
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserView> AllocatedList(int userId)
|
|
{
|
|
var sysUser = await dal.QueryByIdAsync(userId);
|
|
var roles = await _sysRoleRepository.GetUserRolesByUserId(userId);
|
|
|
|
var roleViews = _mapper.Map<List<RoleModel>>(roles);
|
|
UserView userView = new UserView
|
|
{
|
|
SysUser = sysUser,
|
|
Roles = UserView.IsAdmin(userId) ? roleViews : roleViews.Where(x => x.RoleKey != SysConst.ADMIN_KEY).ToList()
|
|
};
|
|
|
|
return userView;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询用户未分配角色列表
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public async Task<UserView> UnallocatedList(int userId)
|
|
{
|
|
var sysUser = await dal.QueryByIdAsync(userId);
|
|
var roleIds = (await _sysRoleRepository.GetUserRolesByUserId(userId)).Select(x => x.RoleID).ToList();
|
|
var roles = await _sysRoleRepository.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE && !roleIds.Contains(x.RoleID));
|
|
|
|
var roleViews = _mapper.Map<List<RoleModel>>(roles);
|
|
UserView userView = new UserView
|
|
{
|
|
SysUser = sysUser,
|
|
Roles = UserView.IsAdmin(userId) ? roleViews : roleViews.Where(x => x.RoleKey != SysConst.ADMIN_KEY).ToList()
|
|
};
|
|
|
|
return userView;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户授权角色
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <param name="roleIds"> 角色组 </param>
|
|
public async Task<bool> InsertUserAuth(int userId, int?[] roleIds)
|
|
{
|
|
var DUserRoles = true;
|
|
var RUserRoles = await _sysUserRoleRepository.QueryAsync(x => x.UserID == userId);
|
|
|
|
if (RUserRoles.Count > 0)
|
|
{
|
|
DUserRoles = await _sysUserRoleRepository.DeletesAsync(RUserRoles);
|
|
}
|
|
if (DUserRoles && roleIds.IsNotEmptyOrNull() && roleIds.Length > 0)
|
|
{
|
|
List<SysUserRole> userRoles = new List<SysUserRole>();
|
|
foreach (var item in roleIds)
|
|
{
|
|
SysUserRole userRole = new SysUserRole()
|
|
{
|
|
UserID = userId,
|
|
RoleID = item
|
|
};
|
|
userRoles.Add(userRole);
|
|
}
|
|
var R = await _sysUserRoleRepository.AddAsync(userRoles);
|
|
return R > 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户关联岗位
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <param name="postIds"> 岗位组 </param>
|
|
public async Task<bool> UpdateUserPost(int userId, int?[] postIds)
|
|
{
|
|
var DUserPosts = true;
|
|
var RUserPosts = await _sysUserPostRepository.QueryAsync(x => x.UserID == userId);
|
|
|
|
if (RUserPosts.Count > 0)
|
|
{
|
|
DUserPosts = await _sysUserPostRepository.DeletesAsync(RUserPosts);
|
|
}
|
|
if (DUserPosts && postIds.IsNotEmptyOrNull() && postIds.Length > 0)
|
|
{
|
|
List<SysUserPost> userPosts = new List<SysUserPost>();
|
|
foreach (var item in postIds)
|
|
{
|
|
SysUserPost userPost = new SysUserPost()
|
|
{
|
|
UserID = userId,
|
|
PostID = item
|
|
};
|
|
userPosts.Add(userPost);
|
|
}
|
|
var R = await _sysUserPostRepository.AddAsync(userPosts);
|
|
return R > 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改用户状态
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> UpdateUserStatus(SysUser user)
|
|
{
|
|
var sysUser = await dal.QueryByIdAsync(user.UserID);
|
|
sysUser.Status = user.Status;
|
|
sysUser.UpdateBy = user.UpdateBy;
|
|
sysUser.UpdateTime = DateTime.Now;
|
|
return await dal.UpdateAsync(sysUser);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改用户基本信息
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> UpdateUserProfile(SysUser user)
|
|
{
|
|
var sysUser = await dal.QueryByIdAsync(user.UserID);
|
|
if (!sysUser.IsNotEmptyOrNull())
|
|
{
|
|
return false;
|
|
}
|
|
user.Password = sysUser.Password;
|
|
user.UpdateTime = DateTime.Now;
|
|
return await dal.UpdateAsync(user);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改用户头像
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> UpdateUserAvatar(SysUser user)
|
|
{
|
|
var sysUser = await SelectUserByLoginName(user.LoginName);
|
|
sysUser.Avatar = user.Avatar;
|
|
sysUser.UpdateBy = user.UpdateBy;
|
|
sysUser.UpdateTime = DateTime.Now;
|
|
return await dal.UpdateAsync(sysUser);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置用户密码
|
|
/// </summary>
|
|
/// <param name="user"> 用户信息 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> ResetPwd(SysUser user)
|
|
{
|
|
var sysUser = await SelectUserById(user.UserID);
|
|
sysUser.Password = MD5Helper.MD5Encrypt32(user.Password);
|
|
sysUser.UpdateBy = user.UpdateBy;
|
|
sysUser.UpdateTime = DateTime.Now;
|
|
return await dal.UpdateAsync(sysUser);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增用户角色信息
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <param name="roleIds"> 角色组 </param>
|
|
public async Task<bool> InsertUserRole(int userId, int?[] roleIds)
|
|
{
|
|
if (roleIds.IsNotEmptyOrNull() && roleIds.Length > 0)
|
|
{
|
|
List<SysUserRole> userRoles = new List<SysUserRole>();
|
|
foreach (var item in roleIds)
|
|
{
|
|
SysUserRole userRole = new SysUserRole()
|
|
{
|
|
UserID = userId,
|
|
RoleID = item
|
|
};
|
|
userRoles.Add(userRole);
|
|
}
|
|
var R = await _sysUserRoleRepository.AddAsync(userRoles);
|
|
return R > 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增用户岗位信息
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <param name="postIds"> 岗位组 </param>
|
|
public async Task<bool> InsertUserPost(int userId, int?[] postIds)
|
|
{
|
|
if (postIds.IsNotEmptyOrNull() && postIds.Length > 0)
|
|
{
|
|
List<SysUserPost> userPosts = new List<SysUserPost>();
|
|
foreach (var item in postIds)
|
|
{
|
|
SysUserPost userPost = new SysUserPost()
|
|
{
|
|
UserID = userId,
|
|
PostID = item
|
|
};
|
|
userPosts.Add(userPost);
|
|
}
|
|
var R = await _sysUserPostRepository.AddAsync(userPosts);
|
|
return R > 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过用户ID删除用户
|
|
/// </summary>
|
|
/// <param name="userId"> 用户ID </param>
|
|
/// <param name="updateBy"> 修改者 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> DeleteUserById(int userId, string updateBy = "")
|
|
{
|
|
var DUserRoles = true;
|
|
var DUserPosts = true;
|
|
var DUser = true;
|
|
|
|
// 删除用户与角色关联
|
|
var RUserRoles = await _sysUserRoleRepository.QueryAsync(x => x.UserID == userId);
|
|
if (RUserRoles.Count > 0)
|
|
{
|
|
DUserRoles = await _sysUserRoleRepository.DeletesAsync(RUserRoles);
|
|
}
|
|
// 删除用户与岗位表
|
|
var RUserPosts = await _sysUserPostRepository.QueryAsync(x => x.UserID == userId);
|
|
if (RUserPosts.Count > 0)
|
|
{
|
|
DUserPosts = await _sysUserPostRepository.DeletesAsync(RUserPosts);
|
|
}
|
|
//删除用户:逻辑删除
|
|
var sysUser = await dal.QueryByIdAsync(userId);
|
|
sysUser.DelFlag = true;
|
|
sysUser.UpdateBy = updateBy;
|
|
sysUser.UpdateTime = DateTime.Now;
|
|
|
|
if (DUserRoles && DUserPosts)
|
|
{
|
|
DUser = await dal.UpdateAsync(sysUser);
|
|
}
|
|
|
|
return DUser;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除用户信息
|
|
/// </summary>
|
|
/// <param name="userIds"> 需要删除的用户ID </param>
|
|
/// <param name="updateBy"> 修改者 </param>
|
|
/// <returns> 结果 </returns>
|
|
public async Task<bool> DeleteUserByIds(int[] userIds, string updateBy = "")
|
|
{
|
|
var DUserRoles = true;
|
|
var DUserPosts = true;
|
|
var DUser = true;
|
|
|
|
foreach (var userId in userIds)
|
|
{
|
|
if (UserView.IsAdmin(userId))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
// 删除用户与角色关联
|
|
var RUserRoles = await _sysUserRoleRepository.QueryAsync(x => userIds.ToList().Contains((int)x.UserID));
|
|
if (RUserRoles.Count > 0)
|
|
{
|
|
DUserRoles = await _sysUserRoleRepository.DeletesAsync(RUserRoles);
|
|
}
|
|
// 删除用户与岗位表
|
|
var RUserPosts = await _sysUserPostRepository.QueryAsync(x => userIds.ToList().Contains((int)x.UserID));
|
|
if (RUserPosts.Count > 0)
|
|
{
|
|
DUserPosts = await _sysUserPostRepository.DeletesAsync(RUserPosts);
|
|
}
|
|
//删除用户:逻辑删除
|
|
var sysUsers = await dal.QueryAsync(x => userIds.ToList().Contains(x.UserID));
|
|
sysUsers.ForEach(x =>
|
|
{
|
|
x.DelFlag = true;
|
|
x.UpdateBy = updateBy;
|
|
x.UpdateTime = DateTime.Now;
|
|
});
|
|
|
|
if (DUserRoles && DUserPosts)
|
|
{
|
|
DUser = await dal.UpdateAsync(sysUsers);
|
|
}
|
|
|
|
return DUser;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导入用户数据
|
|
/// </summary>
|
|
/// <param name="userList"> 用户数据列表 </param>
|
|
/// <param name="isUpdateSupport"> 是否更新支持,如果已存在,则进行更新数据 </param>
|
|
/// <param name="operName"> 操作用户 </param>
|
|
/// <returns> 结果 </returns>
|
|
public string ImportUser(List<SysUser> userList, bool? isUpdateSupport, string operName)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//----------SysUser结束----------
|
|
|