//----------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
{
///
/// 用户信息表Service
///
public partial class SysUserService : BaseServices, ISysUserService
{
IBaseRepository 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 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;
}
///
/// 获取用户角色Key
///
///
///
///
public async Task 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;
}
///
/// 根据条件分页查询用户列表
///
/// 查询条件
/// 用户信息集合信息
public async Task> SelectUserList(PageQuery pageQuery)
{
Expression> 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>(data);
userModel.data.ForEach(x =>
{
x.DeptName = deptList.Find(m => m.DeptID == x.DeptID)?.DeptName;
x.Password = string.Empty;
});
return userModel;
}
///
/// 根据条件分页查询已分配用户角色列表
///
/// 用户信息
/// 用户信息集合信息
public List SelectAllocatedList(SysUser user)
{
return null;
}
///
/// 根据条件分页查询未分配用户角色列表
///
/// 用户信息
/// 用户信息集合信息
public List SelectUnallocatedList(SysUser user)
{
return null;
}
///
/// 通过用户账号查询用户
///
/// 用户账号
/// 用户对象信息
public async Task SelectUserByLoginName(string loginName)
{
var user = await dal.QueryAsync(x => x.LoginName == loginName);
return user.FirstOrDefault();
}
///
/// 通过用户ID查询用户
///
/// 用户ID
/// 用户对象信息
public async Task SelectUserById(int userId)
{
var user = await dal.QueryByIdAsync(userId);
return user;
}
///
/// 根据ID查询
///
/// 主键
///
public async Task 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>(roles);
data.Posts = await _sysPostRepository.QueryAsync(x => x.Status == SysConst.ENABLE);
return data;
}
///
/// 根据用户编号获取详细信息
///
///
///
public async Task 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>(Roles);
userView.Posts = await _sysPostRepository.QueryAsync(x => userView.PostIds.Contains(x.PostID));
return userView;
}
///
/// 查询用户所属角色组
///
/// 用户账号
/// 结果
public async Task> SelectUserRoleGroup(string loginName)
{
var roleList = await QueryMuchAsync(
(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;
}
///
/// 查询用户所属角色组
///
/// 用户Id
/// 结果
public async Task> SelectUserRoleGroup(int userId)
{
var roleList = await QueryMuchAsync(
(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;
}
///
/// 查询用户所属岗位组
///
/// 用户名
/// 结果
public async Task> SelectUserPostGroup(string loginName)
{
var postList = await QueryMuchAsync(
(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;
}
///
/// 校验用户账号是否存在
///
/// 用户账号
/// true-存在 false-不存在
public async Task CheckLoginNameUnique(string loginName)
{
var exist = (await dal.QueryAsync(x => x.LoginName == loginName)).FirstOrDefault();
return exist.IsNotEmptyOrNull();
}
///
/// 校验用户手机号是否存在
///
/// 手机号
/// 存在数量
public async Task checkPhoneUnique(string phoneNumber)
{
var exist = await dal.QueryAsync(x => x.PhoneNumber == phoneNumber);
return exist.IsNotEmptyOrNull() ? exist.Count : 0;
}
///
/// 校验email是否存在
///
/// email
/// 存在数量
public async Task CheckEmailUnique(string email)
{
var exist = await dal.QueryAsync(x => x.Email == email);
return exist.IsNotEmptyOrNull() ? exist.Count : 0;
}
///
/// 新增保存用户信息
///
/// 用户信息
/// 结果
public async Task 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;
}
///
/// 注册用户信息
///
/// 用户信息
/// 结果
public async Task RegisterUser(SysUser user)
{
return await dal.AddAsync(user) > 0;
}
///
/// 修改保存用户信息
///
/// 用户信息
/// 结果
public async Task 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;
}
///
/// 根据用户编号获取授权角色
///
///
///
public async Task 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>(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;
}
///
/// 查询用户已分配角色列表
///
///
///
public async Task AllocatedList(int userId)
{
var sysUser = await dal.QueryByIdAsync(userId);
var roles = await _sysRoleRepository.GetUserRolesByUserId(userId);
var roleViews = _mapper.Map>(roles);
UserView userView = new UserView
{
SysUser = sysUser,
Roles = UserView.IsAdmin(userId) ? roleViews : roleViews.Where(x => x.RoleKey != SysConst.ADMIN_KEY).ToList()
};
return userView;
}
///
/// 查询用户未分配角色列表
///
///
///
public async Task 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>(roles);
UserView userView = new UserView
{
SysUser = sysUser,
Roles = UserView.IsAdmin(userId) ? roleViews : roleViews.Where(x => x.RoleKey != SysConst.ADMIN_KEY).ToList()
};
return userView;
}
///
/// 用户授权角色
///
/// 用户ID
/// 角色组
public async Task 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 userRoles = new List();
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;
}
///
/// 用户关联岗位
///
/// 用户ID
/// 岗位组
public async Task 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 userPosts = new List();
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;
}
///
/// 修改用户状态
///
/// 用户信息
/// 结果
public async Task 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);
}
///
/// 修改用户基本信息
///
/// 用户信息
/// 结果
public async Task 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);
}
///
/// 修改用户头像
///
/// 用户信息
/// 结果
public async Task 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);
}
///
/// 重置用户密码
///
/// 用户信息
/// 结果
public async Task 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);
}
///
/// 新增用户角色信息
///
/// 用户ID
/// 角色组
public async Task InsertUserRole(int userId, int?[] roleIds)
{
if (roleIds.IsNotEmptyOrNull() && roleIds.Length > 0)
{
List userRoles = new List();
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;
}
///
/// 新增用户岗位信息
///
/// 用户ID
/// 岗位组
public async Task InsertUserPost(int userId, int?[] postIds)
{
if (postIds.IsNotEmptyOrNull() && postIds.Length > 0)
{
List userPosts = new List();
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;
}
///
/// 通过用户ID删除用户
///
/// 用户ID
/// 修改者
/// 结果
public async Task 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;
}
///
/// 批量删除用户信息
///
/// 需要删除的用户ID
/// 修改者
/// 结果
public async Task 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;
}
///
/// 导入用户数据
///
/// 用户数据列表
/// 是否更新支持,如果已存在,则进行更新数据
/// 操作用户
/// 结果
public string ImportUser(List userList, bool? isUpdateSupport, string operName)
{
return string.Empty;
}
}
}
//----------SysUser结束----------