S8RoleConfigService.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure;
  3. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  4. public class S8RoleConfigService : ITransient
  5. {
  6. private readonly SqlSugarRepository<AdoS8RolePermissionConfig> _rep;
  7. private readonly SqlSugarRepository<SysRole> _sysRoleRep;
  8. public S8RoleConfigService(
  9. SqlSugarRepository<AdoS8RolePermissionConfig> rep,
  10. SqlSugarRepository<SysRole> sysRoleRep)
  11. {
  12. _rep = rep;
  13. _sysRoleRep = sysRoleRep;
  14. }
  15. public async Task<List<AdoS8RolePermissionConfig>> ListAsync(long tenantId, long factoryId) =>
  16. await _rep.AsQueryable()
  17. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  18. .ToListAsync();
  19. // S8-TENANT-FACTORY-P0-CLOSURE-1:归属一律由服务端可信作用域盖章,忽略 body.TenantId / body.FactoryId。
  20. public async Task<AdoS8RolePermissionConfig> CreateAsync(AdoS8RolePermissionConfig body, S8TrustedScope scope)
  21. {
  22. if (string.IsNullOrWhiteSpace(body.RoleCode)) throw new S8BizException("角色编码必填");
  23. body.TenantId = scope.TenantId;
  24. body.FactoryId = scope.FactoryId;
  25. var exists = await _rep.AsQueryable()
  26. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.RoleCode == body.RoleCode);
  27. if (exists) throw new S8BizException("角色编码已存在");
  28. body.Id = 0;
  29. body.CreatedAt = DateTime.Now;
  30. await _rep.InsertAsync(body);
  31. return body;
  32. }
  33. // S8-TENANT-FACTORY-P0-CLOSURE-1:按 Id + 可信作用域绑行;越权 Id 视为不存在,归属不可被 body 改写。
  34. public async Task<AdoS8RolePermissionConfig> UpdateAsync(long id, AdoS8RolePermissionConfig body, S8TrustedScope scope)
  35. {
  36. var e = await LoadScopedAsync(id, scope);
  37. if (string.IsNullOrWhiteSpace(body.RoleCode)) throw new S8BizException("角色编码必填");
  38. var exists = await _rep.AsQueryable()
  39. .AnyAsync(x => x.Id != id && x.TenantId == e.TenantId && x.FactoryId == e.FactoryId && x.RoleCode == body.RoleCode);
  40. if (exists) throw new S8BizException("角色编码已存在");
  41. body.Id = id;
  42. body.TenantId = e.TenantId;
  43. body.FactoryId = e.FactoryId;
  44. body.CreatedAt = e.CreatedAt;
  45. body.UpdatedAt = DateTime.Now;
  46. await _rep.UpdateAsync(body);
  47. return body;
  48. }
  49. // S8-TENANT-FACTORY-P0-CLOSURE-1:删除必须先按可信作用域绑行,禁止裸 DeleteByIdAsync(id)。
  50. public async Task DeleteAsync(long id, S8TrustedScope scope)
  51. {
  52. var e = await LoadScopedAsync(id, scope);
  53. await _rep.DeleteByIdAsync(e.Id);
  54. }
  55. private async Task<AdoS8RolePermissionConfig> LoadScopedAsync(long id, S8TrustedScope scope) =>
  56. await _rep.AsQueryable()
  57. .Where(x => x.Id == id && x.TenantId == scope.TenantId && x.FactoryId == scope.FactoryId)
  58. .FirstAsync() ?? throw new S8NotFoundException();
  59. /// <summary>
  60. /// 从系统角色表一键导入,跳过已存在的角色编码,返回本次新增条数。
  61. /// </summary>
  62. public async Task<int> ImportFromSysRolesAsync(long tenantId, long factoryId)
  63. {
  64. var sysRoles = await _sysRoleRep.AsQueryable()
  65. .Where(r => r.TenantId == tenantId && r.Status == StatusEnum.Enable)
  66. .Where(r => !string.IsNullOrEmpty(r.Code))
  67. .Select(r => new { r.Code, r.Name })
  68. .ToListAsync();
  69. var existingCodes = (await _rep.AsQueryable()
  70. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  71. .Select(x => x.RoleCode)
  72. .ToListAsync()).ToHashSet();
  73. var toInsert = sysRoles
  74. .Where(r => !existingCodes.Contains(r.Code!))
  75. .Select(r => new AdoS8RolePermissionConfig
  76. {
  77. TenantId = tenantId,
  78. FactoryId = factoryId,
  79. RoleCode = r.Code!,
  80. PermissionCodes = "[]",
  81. CreatedAt = DateTime.Now
  82. }).ToList();
  83. if (toInsert.Count > 0)
  84. await _rep.InsertRangeAsync(toInsert);
  85. return toInsert.Count;
  86. }
  87. }