Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs

299 lines
11 KiB
C#
Raw Normal View History

2022-04-06 22:22:45 +08:00
using SqlSugar;
2022-04-07 22:48:10 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2022-04-07 22:48:10 +08:00
using System.Threading;
using System.Threading.Tasks;
2022-09-09 19:22:14 +08:00
using Yi.Framework.Common.Enum;
2022-05-01 18:31:06 +08:00
using Yi.Framework.Common.Helper;
2022-09-09 15:53:11 +08:00
using Yi.Framework.Common.Models;
2022-04-30 21:48:18 +08:00
using Yi.Framework.DTOModel;
2022-04-06 22:22:45 +08:00
using Yi.Framework.Interface;
2021-10-11 21:50:50 +08:00
using Yi.Framework.Model.Models;
2022-04-02 17:44:50 +08:00
using Yi.Framework.Repository;
2021-10-11 21:50:50 +08:00
namespace Yi.Framework.Service
{
2022-04-06 22:22:45 +08:00
public partial class UserService
2021-10-11 21:50:50 +08:00
{
2022-09-11 02:39:33 +08:00
public async Task<List<UserEntity>> GetListInRole()
{
return await _repository._DbQueryable.Includes(u => u.Roles).ToListAsync();
}
public async Task<List<UserEntity>> DbTest()
{
return await _repository._Db.Queryable<UserEntity>().ToListAsync();
}
public async Task<bool> Exist(long id, Action<UserEntity> userAction = null)
2022-04-06 22:22:45 +08:00
{
2022-04-08 23:44:25 +08:00
var user = await _repository.GetByIdAsync(id);
2022-04-07 22:48:10 +08:00
userAction.Invoke(user);
if (user == null)
{
return false;
}
return true;
}
public async Task<bool> Exist(string userName, Action<UserEntity> userAction = null)
{
2022-09-09 15:53:11 +08:00
var user = await _repository.GetFirstAsync(u => u.UserName == userName && u.IsDeleted == false);
2022-04-07 22:48:10 +08:00
if (userAction != null)
{
userAction.Invoke(user);
}
if (user == null)
{
return false;
}
return true;
}
2022-04-26 01:31:14 +08:00
public async Task<bool> Login(string userName, string password, Action<UserEntity> userAction = null)
2022-04-07 22:48:10 +08:00
{
2022-04-26 01:31:14 +08:00
var user = new UserEntity();
2022-04-07 22:48:10 +08:00
if (await Exist(userName, o => user = o))
{
userAction.Invoke(user);
2022-04-26 01:31:14 +08:00
if (user.Password == Common.Helper.MD5Helper.SHA2Encode(password, user.Salt))
2022-04-07 22:48:10 +08:00
{
return true;
}
}
2022-04-26 01:31:14 +08:00
return false;
2022-04-07 22:48:10 +08:00
}
public async Task<bool> Register(UserEntity userEntity, Action<UserEntity> userAction = null)
{
var user = new UserEntity();
2022-04-09 00:54:13 +08:00
if (!await Exist(userEntity.UserName))
2022-04-07 22:48:10 +08:00
{
2022-04-26 01:31:14 +08:00
user.UserName = userEntity.UserName;
user.BuildPassword();
2022-09-11 02:39:33 +08:00
userAction.Invoke(await _repository.InsertReturnEntityAsync(user));
2022-04-07 22:48:10 +08:00
return true;
}
return false;
2022-04-06 22:22:45 +08:00
}
2022-04-16 00:01:00 +08:00
2022-04-26 01:31:14 +08:00
public async Task<bool> GiveUserSetRole(List<long> userIds, List<long> roleIds)
{
var _repositoryUserRole = _repository.ChangeRepository<Repository<UserRoleEntity>>();
//多次操作,需要事务确保原子性
return await _repositoryUserRole.UseTranAsync(async () =>
{
2022-09-11 12:39:22 +08:00
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
await _repositoryUserRole.DeleteAsync(u => userIds.Contains((long)u.UserId));
2022-04-26 01:31:14 +08:00
if (roleIds is not null)
{
//遍历用户
foreach (var userId in userIds)
2022-04-26 01:31:14 +08:00
{
//添加新的关系
List<UserRoleEntity> userRoleEntities = new();
foreach (var roleId in roleIds)
{
userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
}
2022-04-16 00:01:00 +08:00
//一次性批量添加
await _repositoryUserRole.InsertReturnSnowflakeIdAsync(userRoleEntities);
}
2022-04-26 01:31:14 +08:00
}
2022-04-26 01:31:14 +08:00
});
2022-04-16 00:01:00 +08:00
}
2022-09-13 18:15:01 +08:00
public async Task<bool> GiveUserSetPost(List<long> userIds, List<long> postIds)
{
var _repositoryUserPost = _repository.ChangeRepository<Repository<UserPostEntity>>();
//多次操作,需要事务确保原子性
return await _repositoryUserPost.UseTranAsync(async () =>
{
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
await _repositoryUserPost.DeleteAsync(u => userIds.Contains((long)u.UserId));
if (postIds is not null)
2022-09-13 18:15:01 +08:00
{
//遍历用户
foreach (var userId in userIds)
2022-09-13 18:15:01 +08:00
{
//添加新的关系
List<UserPostEntity> userPostEntities = new();
foreach (var post in postIds)
{
userPostEntities.Add(new UserPostEntity() { UserId = userId, PostId = post });
}
//一次性批量添加
await _repositoryUserPost.InsertReturnSnowflakeIdAsync(userPostEntities);
2022-09-13 18:15:01 +08:00
}
}
});
}
2022-09-11 02:39:33 +08:00
public async Task<UserEntity> GetInfoById(long userId)
{
var data = await _repository._DbQueryable.Includes(u => u.Roles).Includes(u => u.Posts).Includes(u => u.Dept).InSingleAsync(userId);
data.Password = null;
data.Salt = null;
return data;
}
2022-04-30 21:48:18 +08:00
public async Task<UserRoleMenuDto> GetUserAllInfo(long userId)
{
var userRoleMenu = new UserRoleMenuDto();
//首先获取到该用户全部信息,导航到角色、菜单,(菜单需要去重,完全交给Set来处理即可)
//得到用户
2022-09-10 13:36:01 +08:00
var user = await _repository._DbQueryable.Includes(u => u.Roles.Where(r => r.IsDeleted == false).ToList(), r => r.Menus.Where(m => m.IsDeleted == false).ToList()).InSingleAsync(userId);
user.Password = null;
user.Salt = null;
2022-04-30 21:48:18 +08:00
//得到角色集合
var roleList = user.Roles;
//得到菜单集合
foreach (var role in roleList)
{
userRoleMenu.RoleCodes.Add(role.RoleCode);
2022-09-09 18:15:10 +08:00
if (role.Menus.IsNotNull())
2022-04-30 21:48:18 +08:00
{
2022-09-09 18:15:10 +08:00
foreach (var menu in role.Menus)
{
2022-09-09 18:15:10 +08:00
2022-09-09 19:22:14 +08:00
if (!string.IsNullOrEmpty(menu.PermissionCode))
{
userRoleMenu.PermissionCodes.Add(menu.PermissionCode);
2022-09-13 19:39:16 +08:00
2022-09-09 19:22:14 +08:00
}
2022-09-11 16:49:40 +08:00
userRoleMenu.Menus.Add(menu);
}
2022-04-30 21:48:18 +08:00
}
2022-09-09 18:15:10 +08:00
2022-04-30 21:48:18 +08:00
//刚好可以去除一下多余的导航属性
role.Menus = null;
userRoleMenu.Roles.Add(role);
}
user.Roles = null;
userRoleMenu.User = user;
2022-09-09 15:53:11 +08:00
2022-04-30 21:48:18 +08:00
return userRoleMenu;
2022-05-01 18:31:06 +08:00
}
2022-04-30 21:48:18 +08:00
2022-09-11 12:39:22 +08:00
2022-09-09 15:53:11 +08:00
2022-09-13 19:39:16 +08:00
public async Task<PageModel<List<UserEntity>>> SelctPageList(UserEntity user, PageParModel page, long? deptId)
2022-09-09 15:53:11 +08:00
{
2022-09-13 19:39:16 +08:00
2022-09-09 15:53:11 +08:00
RefAsync<int> total = 0;
2022-09-13 19:39:16 +08:00
List<UserEntity> data = null;
var query = _repository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(user.UserName), u => u.UserName.Contains(user.UserName))
.WhereIF(!string.IsNullOrEmpty(user.Name), u => u.Name.Contains(user.Name))
.WhereIF(!string.IsNullOrEmpty(user.Phone), u => u.Phone.Contains(user.Phone))
.WhereIF(page.StartTime.IsNotNull() && page.EndTime.IsNotNull(), u => u.CreateTime >= page.StartTime && u.CreateTime <= page.EndTime)
.WhereIF(user.IsDeleted.IsNotNull(), u => u.IsDeleted == user.IsDeleted)
.Includes(u => u.Roles)
.Includes(u => u.Posts)
.Includes(u => u.Dept);
if (deptId is not null)
{
//如果deptId不为空部门id以下及自己都可以
List<long> deptIds = (await _repository._Db.Queryable<DeptEntity>().ToChildListAsync(it => it.ParentId, deptId)).Select(d => d.Id).ToList();
query = query.Where(u => u.DeptId != null && deptIds.Contains((long)u.DeptId));
2022-09-13 19:39:16 +08:00
}
2022-09-09 15:53:11 +08:00
2022-09-13 19:39:16 +08:00
data = await query.OrderBy(u => u.OrderNum, OrderByType.Desc)
.ToPageListAsync(page.PageNum, page.PageSize, total);
data.ForEach(u => { u.Password = null; u.Salt = null; });
2022-09-09 15:53:11 +08:00
return new PageModel<List<UserEntity>>(data, total);
}
2022-09-09 19:22:14 +08:00
2022-09-11 12:39:22 +08:00
public async Task<bool> UpdateInfo(UserInfoDto userDto)
{
//未填写密码,可不更新
userDto.User.Salt = null;
if (userDto.User.Password.IsNotNull())
{
userDto.User.BuildPassword();
}
2022-09-13 18:15:01 +08:00
userDto.User.DeptId = userDto.DeptId;
2022-09-11 12:39:22 +08:00
var res1 = await _repository.UpdateIgnoreNullAsync(userDto.User);
var res2 = await GiveUserSetRole(new List<long> { userDto.User.Id }, userDto.RoleIds);
2022-09-13 18:15:01 +08:00
var res3 = await GiveUserSetPost(new List<long> { userDto.User.Id }, userDto.PostIds);
2022-09-13 19:39:16 +08:00
return res1 && res2 && res3;
2022-09-11 12:39:22 +08:00
}
public async Task<bool> AddInfo(UserInfoDto userDto)
{
userDto.User.BuildPassword();
var res1 = await _repository.InsertReturnSnowflakeIdAsync(userDto.User);
var res2 = await GiveUserSetRole(new List<long> { res1 }, userDto.RoleIds);
2022-09-13 18:15:01 +08:00
var res3 = await GiveUserSetPost(new List<long> { res1 }, userDto.PostIds);
2022-09-13 19:39:16 +08:00
return !0.Equals(res1) && res2 && res3;
2022-09-11 12:39:22 +08:00
}
2022-09-11 13:16:24 +08:00
public async Task<bool> RestPassword(long userId, string password)
{
var user = new UserEntity();
user.Id = userId;
user.Password = password;
user.BuildPassword();
return await _repository.UpdateIgnoreNullAsync(user);
}
public async Task<bool> UpdatePassword(UpdatePasswordDto dto, long userId)
{
var user = await _repository.GetByIdAsync(userId);
if (dto.OldPassword.Equals(dto.NewPassword))
{
return false;
}
if (!user.JudgePassword(dto.OldPassword))
{
return false;
}
var newUser = new UserEntity();
newUser.Password = dto.NewPassword;
newUser.Id = userId;
newUser.BuildPassword();
return await _repository.UpdateIgnoreNullAsync(newUser);
}
public async Task<bool> UpdateProfile(UserInfoDto userDto)
{
userDto.User.Salt = null;
userDto.User.Password = null;
userDto.User.DeptId = null;
return await _repository.UpdateIgnoreNullAsync(userDto.User);
}
2021-10-22 16:48:03 +08:00
}
2021-10-11 21:50:50 +08:00
}