S8NotificationLayerService.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Admin.NET.Core;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. public class S8NotificationLayerService : ITransient
  6. {
  7. private readonly SqlSugarRepository<AdoS8NotificationLayer> _rep;
  8. private readonly SqlSugarRepository<SysRole> _roleRep;
  9. public S8NotificationLayerService(
  10. SqlSugarRepository<AdoS8NotificationLayer> rep,
  11. SqlSugarRepository<SysRole> roleRep)
  12. {
  13. _rep = rep;
  14. _roleRep = roleRep;
  15. }
  16. public async Task<List<AdoS8NotificationLayer>> ListAsync(long tenantId, long factoryId) =>
  17. await _rep.AsQueryable()
  18. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  19. .ToListAsync();
  20. public async Task<List<object>> GetRoleOptionsAsync()
  21. {
  22. var roles = await _roleRep.AsQueryable()
  23. .Where(x => (x.Id >= 1329908000101L && x.Id <= 1329908000107L) || x.Id == 1300000000101L)
  24. .OrderBy(x => x.Id)
  25. .Select(x => new { value = x.Code, label = x.Name })
  26. .ToListAsync();
  27. return roles.Cast<object>().ToList();
  28. }
  29. public async Task<AdoS8NotificationLayer> CreateAsync(AdoS8NotificationLayer body)
  30. {
  31. if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode))
  32. throw new S8BizException("场景、严重度、层级必填");
  33. if (string.IsNullOrWhiteSpace(body.TargetRoleIds))
  34. throw new S8BizException("目标角色必填");
  35. if (string.IsNullOrWhiteSpace(body.NotifyChannel))
  36. throw new S8BizException("通知渠道至少选一个");
  37. // S8-SEVERITY-FOLLOW-SERIOUS-STANDARDIZE-EXEC-1:归一为 FOLLOW/SERIOUS 后再校验/写入。
  38. body.Severity = S8SeverityCode.Normalize(body.Severity);
  39. var exists = await _rep.AsQueryable()
  40. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  41. x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode);
  42. if (exists) throw new S8BizException("同场景+严重度+层级已存在");
  43. body.Id = 0;
  44. body.CreatedAt = DateTime.Now;
  45. return await _rep.InsertReturnEntityAsync(body);
  46. }
  47. public async Task<AdoS8NotificationLayer> UpdateAsync(long id, AdoS8NotificationLayer body)
  48. {
  49. var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  50. if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode))
  51. throw new S8BizException("场景、严重度、层级必填");
  52. if (string.IsNullOrWhiteSpace(body.TargetRoleIds))
  53. throw new S8BizException("目标角色必填");
  54. if (string.IsNullOrWhiteSpace(body.NotifyChannel))
  55. throw new S8BizException("通知渠道至少选一个");
  56. // S8-SEVERITY-FOLLOW-SERIOUS-STANDARDIZE-EXEC-1:归一为 FOLLOW/SERIOUS 后再校验/写入。
  57. body.Severity = S8SeverityCode.Normalize(body.Severity);
  58. var exists = await _rep.AsQueryable()
  59. .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  60. x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode);
  61. if (exists) throw new S8BizException("同场景+严重度+层级已存在");
  62. body.Id = id;
  63. body.CreatedAt = e.CreatedAt;
  64. body.UpdatedAt = DateTime.Now;
  65. await _rep.UpdateAsync(body);
  66. return body;
  67. }
  68. public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id);
  69. }