first commit
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Application.Dtos.SYS.Departments;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Departments;
|
||||
|
||||
internal class DepartmentReadRepository : F9RepositoryBase, IDepartmentReadRepository
|
||||
{
|
||||
public DepartmentReadRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<List<DepartmentReadDto_View>> GetAllAsync()
|
||||
=> await Orm.Select<Department>()
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ToListAsync(x => new DepartmentReadDto_View(x.Id, x.Code, x.Name, x.ParentId, x.ManageType, x.DutyType, x.Description, x.IsActive, x.SortOrder));
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Departments;
|
||||
|
||||
internal class DepartmentUserRepository : F9RepositoryBase, IDepartmentUserRepository
|
||||
{
|
||||
public DepartmentUserRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<bool> AssignAsync(DepartmentUser depUser)
|
||||
{
|
||||
depUser.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(depUser).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(long depId, long userId)
|
||||
=> await Orm.Delete<DepartmentUser>()
|
||||
.Where(x => x.DepartmentId == depId && x.UserId == userId)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Departments;
|
||||
|
||||
internal class DepartmentWriteRepository : F9RepositoryBase, IDepartmentWriteRepository
|
||||
{
|
||||
public DepartmentWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<Department> GetByIdAsync(long id, CancellationToken cancellationToken)
|
||||
=> await Orm.Select<Department>().Where(x => x.Id == id ).FirstAsync(cancellationToken);
|
||||
|
||||
public async Task<bool> AddAsync(Department dep)
|
||||
{
|
||||
dep.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(dep).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(Department dep)
|
||||
{
|
||||
dep.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<Department>().SetSource(dep).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<Department>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Where(x => x.Id == id )
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> ActiveOrDeactive(long id, bool active)
|
||||
=> await Orm.Update<Department>()
|
||||
.Set(x => x.IsActive, active)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Menus;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Menus;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Menus;
|
||||
|
||||
internal class MenuReadRepository : F9RepositoryBase, IMenuReadRepository
|
||||
{
|
||||
public MenuReadRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<MenuReadDto_Menu_Edit?> GetMenuByIdForEditAsync(long id)
|
||||
=> await Orm.Select<Menu>()
|
||||
.Where(x => x.Id == id && x.DeletedAt == null)
|
||||
.FirstAsync(x => new MenuReadDto_Menu_Edit(x.Id, x.Code, x.Name, x.IconSvg, x.MenuType, x.AssemblyName, x.TypeFullName, x.IsActive, x.ParentId, x.SortOrder));
|
||||
|
||||
public async Task<bool> ExistMenuByTypeFullnameExcludeIdAsync(string typeFullname, long? excludeId)
|
||||
{
|
||||
var query = Orm.Select<Menu>().Where(x => x.TypeFullName == typeFullname && x.DeletedAt == null);
|
||||
if (excludeId.HasValue)
|
||||
query = query.Where(x => x.Id != excludeId.Value);
|
||||
return await query.AnyAsync();
|
||||
}
|
||||
|
||||
public async Task<List<MenuReadDto_Menu_Table>> GetAllMenuAsync(string like, long? parentId)
|
||||
{
|
||||
var all = await Orm.Select<Menu>().Where(x => x.DeletedAt == null).ToListAsync();
|
||||
var dict = all.ToDictionary(x => x.Id, x => x.Name);
|
||||
|
||||
var list = all.AsEnumerable();
|
||||
if (parentId.HasValue)
|
||||
list = list.Where(x => x.ParentId == parentId.Value);
|
||||
if (!string.IsNullOrWhiteSpace(like))
|
||||
list = list.Where(x => x.Name.Contains(like) || x.Code.Contains(like));
|
||||
|
||||
return list.OrderBy(x => x.SortOrder).Select(x => ToTable(x, dict)).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<MenuReadDto_Menu_Table>> GetTreeMenuAsync()
|
||||
{
|
||||
var all = await Orm.Select<Menu>().Where(x => x.DeletedAt == null).OrderBy(x => x.SortOrder).ToListAsync();
|
||||
var dict = all.ToDictionary(x => x.Id, x => x.Name);
|
||||
return BuildTree(all, 0, dict);
|
||||
}
|
||||
|
||||
private static MenuReadDto_Menu_Table ToTable(Menu x, Dictionary<long, string> dict)
|
||||
=> new(x.Id, x.ParentId, x.Code, x.Name, x.IconSvg, x.MenuType.ToString(),
|
||||
x.AssemblyName, x.TypeFullName, dict.GetValueOrDefault(x.ParentId, ""), x.Level,
|
||||
x.IsActive ? "启用" : "禁用", x.SortOrder);
|
||||
|
||||
private static List<MenuReadDto_Menu_Table> BuildTree(List<Menu> all, long parentId, Dictionary<long, string> dict)
|
||||
=> all.Where(x => x.ParentId == parentId).OrderBy(x => x.SortOrder).Select(x =>
|
||||
{
|
||||
var children = BuildTree(all, x.Id, dict);
|
||||
return new MenuReadDto_Menu_Table(
|
||||
x.Id, x.ParentId, x.Code, x.Name, x.IconSvg, x.MenuType.ToString(),
|
||||
x.AssemblyName, x.TypeFullName, dict.GetValueOrDefault(x.ParentId, ""), x.Level,
|
||||
x.IsActive ? "启用" : "禁用", x.SortOrder, false,
|
||||
children.Count > 0 ? children.ToArray() : default);
|
||||
}).ToList();
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Menus;
|
||||
|
||||
internal class MenuWriteRepository : F9RepositoryBase, IMenuWriteRepository
|
||||
{
|
||||
public MenuWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<Menu> GetByIdAsync(long id)
|
||||
=> await Orm.Select<Menu>().Where(x => x.Id == id ).FirstAsync();
|
||||
|
||||
public async Task<bool> AddAsync(Menu menu)
|
||||
{
|
||||
menu.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(menu).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(Menu menu)
|
||||
{
|
||||
menu.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<Menu>().SetSource(menu).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<Menu>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Where(x => x.Id == id )
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> ActiveOrDeactive(long id, bool active)
|
||||
=> await Orm.Update<Menu>()
|
||||
.Set(x => x.IsActive, active)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Permissions;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Permissions;
|
||||
|
||||
internal class PermissionWriteRepository : F9RepositoryBase, IPermissionWriteRepository
|
||||
{
|
||||
public PermissionWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<Permission> GetByIdAsync(Permission_Type_Enum permissionType, Resource_Type_Enum resourceType, long resourceId)
|
||||
=> await Orm.Select<Permission>()
|
||||
.Where(x => x.PermissionType == permissionType && x.ResourceType == resourceType && x.ResourceId == resourceId )
|
||||
.FirstAsync();
|
||||
|
||||
public async Task<bool> AddRangeAsync(Permission[] entites)
|
||||
=> await Orm.Insert(entites).ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> DeleteByResourceIdAndTypeAsync(Permission_Type_Enum permissionType, Resource_Type_Enum resourceType, long resourceId)
|
||||
=> await Orm.Delete<Permission>()
|
||||
.Where(x => x.PermissionType == permissionType && x.ResourceType == resourceType && x.ResourceId == resourceId)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> DeleteByResourceIdAsync(long resourceId)
|
||||
=> await Orm.Delete<Permission>().Where(x => x.ResourceId == resourceId).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RolePermissionWriteRepository : F9RepositoryBase, IRolePermissionWriteRepository
|
||||
{
|
||||
public RolePermissionWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<RolePermission> GetByIdAsync(long id)
|
||||
=> await Orm.Select<RolePermission>().Where(x => x.Id == id).FirstAsync();
|
||||
|
||||
public async Task<bool> AssignAsync(RolePermission rolePermission)
|
||||
{
|
||||
rolePermission.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(rolePermission).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(long id)
|
||||
=> await Orm.Delete<RolePermission>().Where(x => x.Id == id).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RoleReadRepository : F9RepositoryBase, IRoleReadRepository
|
||||
{
|
||||
public RoleReadRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<RoleReadDto_Edit> GetByIdAsync(long id)
|
||||
=> await Orm.Select<Role>()
|
||||
.Where(x => x.Id == id && x.DeletedAt == null)
|
||||
.FirstAsync(x => new RoleReadDto_Edit(x.Id, x.Name, x.Code, x.SortOrder, x.Description));
|
||||
|
||||
public async Task<List<RoleReadDto_View>> GetAllAsync()
|
||||
{
|
||||
var roles = await Orm.Select<Role>()
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ToListAsync(x => new { x.Id, x.Name, x.Code, x.SortOrder, x.IsActive });
|
||||
|
||||
var roleIds = roles.Select(x => x.Id).ToList();
|
||||
var permCounts = await GetPermissionCountsAsync(roleIds);
|
||||
|
||||
return roles.Select(x =>
|
||||
{
|
||||
var c = permCounts.GetValueOrDefault(x.Id);
|
||||
return new RoleReadDto_View(x.Id, x.Name, x.Code, x.SortOrder, c.Menu, c.Report, c.Dep, x.IsActive);
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<RoleReadDto_Cache>> GetAllForCacheAsync()
|
||||
=> await Orm.Select<Role>()
|
||||
.Where(x => x.DeletedAt == null)
|
||||
.OrderBy(x => x.SortOrder)
|
||||
.ToListAsync(x => new RoleReadDto_Cache(x.Id, x.Name, x.Code, x.SortOrder));
|
||||
|
||||
public async Task<List<RolePermissionReadDto_View>> GetRolePermissionsByResourceType(long roleId, Resource_Type_Enum resourceType)
|
||||
=> await Orm.Select<RolePermission, Permission>()
|
||||
.InnerJoin((rp, p) => rp.PermissionId == p.Id && p.DeletedAt == null)
|
||||
.Where((rp, p) => rp.RoleId == roleId && p.ResourceType == resourceType)
|
||||
.ToListAsync((rp, p) => new RolePermissionReadDto_View(rp.Id, rp.RoleId, rp.PermissionId, p.ResourceId, p.PermissionType));
|
||||
|
||||
private async Task<Dictionary<long, (int Menu, int Report, int Dep)>> GetPermissionCountsAsync(List<long> roleIds)
|
||||
{
|
||||
var result = new Dictionary<long, (int, int, int)>();
|
||||
if (roleIds.Count == 0) return result;
|
||||
|
||||
var perms = await Orm.Select<RolePermission, Permission>()
|
||||
.InnerJoin((rp, p) => rp.PermissionId == p.Id && p.DeletedAt == null)
|
||||
.Where((rp, p) => roleIds.Contains(rp.RoleId))
|
||||
.ToListAsync((rp, p) => new { rp.RoleId, p.ResourceType });
|
||||
|
||||
foreach (var g in perms.GroupBy(x => x.RoleId))
|
||||
{
|
||||
result[g.Key] = (
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.菜单),
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.报表),
|
||||
g.Count(x => x.ResourceType == Resource_Type_Enum.部门));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
|
||||
|
||||
internal class RoleWriteRepository : F9RepositoryBase, IRoleWriteRepository
|
||||
{
|
||||
public RoleWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<Role> GetByIdAsync(long id)
|
||||
=> await Orm.Select<Role>().Where(x => x.Id == id ).FirstAsync();
|
||||
|
||||
public async Task<bool> AddAsync(Role role)
|
||||
{
|
||||
role.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(role).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(Role role)
|
||||
{
|
||||
role.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<Role>().SetSource(role).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<Role>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Where(x => x.Id == id )
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> ActiveOrDeactive(long id, bool active)
|
||||
=> await Orm.Update<Role>()
|
||||
.Set(x => x.IsActive, active)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Application.Dtos.Common;
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Users;
|
||||
|
||||
internal class UserReadRepository : F9RepositoryBase, IUserReadRepository
|
||||
{
|
||||
public UserReadRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<UserReadDto_Login> GetByLoginNameAsync(string loginName)
|
||||
=> await Orm.Select<User>()
|
||||
.Where(x => x.LoginName == loginName && x.DeletedAt == null)
|
||||
.FirstAsync(x => new UserReadDto_Login(x.Id, x.UserName, x.LoginName, x.PasswordHash, x.DepartmentId));
|
||||
|
||||
public async Task<UserReadDto_Login> GetByPhoneAsync(string phone)
|
||||
=> await Orm.Select<User>()
|
||||
.Where(x => x.PhoneNumber == phone && x.DeletedAt == null)
|
||||
.FirstAsync(x => new UserReadDto_Login(x.Id, x.UserName, x.LoginName, x.PasswordHash, x.DepartmentId));
|
||||
|
||||
public async Task<UserReadDto_Edit> GetByIdAsync(long id)
|
||||
=> await Orm.Select<User>()
|
||||
.Where(x => x.Id == id && x.DeletedAt == null)
|
||||
.FirstAsync(x => new UserReadDto_Edit(x.Id, x.UserName, x.PhoneNumber, x.LoginName, x.DepartmentId, x.SortOrder));
|
||||
|
||||
public async Task<List<UserReadDto_Edit>> GetByIdsAsync(List<long> ids)
|
||||
=> await Orm.Select<User>()
|
||||
.Where(x => ids.Contains(x.Id) && x.DeletedAt == null)
|
||||
.ToListAsync(x => new UserReadDto_Edit(x.Id, x.UserName, x.PhoneNumber, x.LoginName, x.DepartmentId, x.SortOrder));
|
||||
|
||||
public async Task<PagedResult<UserReadDto_View>> GetPagedAsync(long departmentId, string like, int page = 1, int pageSize = 10)
|
||||
{
|
||||
var query = Orm.Select<User, Department>()
|
||||
.LeftJoin((u, d) => u.DepartmentId == d.Id)
|
||||
.Where((u, d) => u.DeletedAt == null);
|
||||
|
||||
if (departmentId > 0)
|
||||
query = query.Where((u, d) => u.DepartmentId == departmentId);
|
||||
if (!string.IsNullOrWhiteSpace(like))
|
||||
query = query.Where((u, d) => u.UserName.Contains(like) || u.LoginName.Contains(like) || u.PhoneNumber.Contains(like));
|
||||
|
||||
var total = await query.CountAsync();
|
||||
|
||||
var list = await query
|
||||
.OrderBy((u, d) => u.SortOrder)
|
||||
.Page(page, pageSize)
|
||||
.ToListAsync((u, d) => new
|
||||
{
|
||||
u.Id, u.UserName, u.PhoneNumber, u.LoginName,
|
||||
DepartmentName = d.Name,
|
||||
u.LastLoginAt, u.SortOrder, u.IsActive
|
||||
});
|
||||
|
||||
// 批量查角色名,避免 N+1
|
||||
var userIds = list.Select(x => x.Id).ToList();
|
||||
var roleDict = await GetRoleNameDictAsync(userIds);
|
||||
|
||||
var items = list.Select(x => new UserReadDto_View(
|
||||
x.Id, x.UserName, x.PhoneNumber, x.LoginName,
|
||||
x.DepartmentName,
|
||||
roleDict.TryGetValue(x.Id, out var roleName) ? roleName : "",
|
||||
x.LastLoginAt?.ToString("yyyy-MM-dd HH:mm:ss") ?? "",
|
||||
x.SortOrder, x.IsActive)).ToList();
|
||||
|
||||
return new PagedResult<UserReadDto_View>(items, page, pageSize, total);
|
||||
}
|
||||
|
||||
public async Task<List<UserRoleReadDto_Cache>> GetUserRolesByUserId(long userId)
|
||||
=> await Orm.Select<UserRole, Role>()
|
||||
.InnerJoin((x, y) => x.RoleId == y.Id && y.DeletedAt == null)
|
||||
.Where((x, y) => x.UserId == userId)
|
||||
.ToListAsync((x, y) => new UserRoleReadDto_Cache(x.Id, x.UserId, x.RoleId, y.Name));
|
||||
|
||||
public async Task<List<UserReadDto_Edit>> GetByRoleIdsAsync(List<long> roleIds)
|
||||
=> await Orm.Select<User, UserRole, Role>()
|
||||
.InnerJoin((u, x, y) => u.Id == x.UserId)
|
||||
.InnerJoin((u, x, y) => x.RoleId == y.Id && y.DeletedAt == null)
|
||||
.Where((u, x, y) => roleIds.Contains(x.RoleId) && u.DeletedAt == null)
|
||||
.ToListAsync((u, x, y) => new UserReadDto_Edit(u.Id, u.UserName, u.PhoneNumber, u.LoginName, u.DepartmentId, u.SortOrder));
|
||||
|
||||
private async Task<Dictionary<long, string>> GetRoleNameDictAsync(List<long> userIds)
|
||||
{
|
||||
if (userIds.Count == 0) return new Dictionary<long, string>();
|
||||
|
||||
var list = await Orm.Select<UserRole, Role>()
|
||||
.InnerJoin((x, y) => x.RoleId == y.Id && y.DeletedAt == null)
|
||||
.Where((x, y) => userIds.Contains(x.UserId))
|
||||
.ToListAsync((x, y) => new { x.UserId, y.Name });
|
||||
|
||||
return list.GroupBy(x => x.UserId)
|
||||
.ToDictionary(g => g.Key, g => string.Join(",", g.Select(v => v.Name)));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Users;
|
||||
|
||||
internal class UserRoleWriteRepository : F9RepositoryBase, IUserRoleWriteRepository
|
||||
{
|
||||
public UserRoleWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<bool> AssignAsync(UserRole userRole)
|
||||
{
|
||||
userRole.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(userRole).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(long userid, long roleId)
|
||||
=> await Orm.Delete<UserRole>()
|
||||
.Where(x => x.UserId == userid && x.RoleId == roleId)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Users;
|
||||
|
||||
internal class UserWriteRepository : F9RepositoryBase, IUserWriteRepository
|
||||
{
|
||||
public UserWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<User> GetByIdAsync(long id)
|
||||
=> await Orm.Select<User>().Where(x => x.Id == id ).FirstAsync();
|
||||
|
||||
public async Task<bool> AddAsync(User user)
|
||||
{
|
||||
user.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(user).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(User user)
|
||||
{
|
||||
user.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<User>().SetSource(user).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<User>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Where(x => x.Id == id )
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> ActiveOrDeactive(long id, bool active)
|
||||
=> await Orm.Update<User>()
|
||||
.Set(x => x.IsActive, active)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> UpdatePassworkdAsync(long id, string passwordHash)
|
||||
=> await Orm.Update<User>()
|
||||
.Set(x => x.PasswordHash, passwordHash)
|
||||
.Set(x => x.UpdatedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
|
||||
public async Task<bool> UpdateLastLoginAtAsync(long id, DateTime dt)
|
||||
=> await Orm.Update<User>()
|
||||
.Set(x => x.LastLoginAt, dt)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user