S8NotificationLayerService.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  3. public class S8NotificationLayerService : ITransient
  4. {
  5. private readonly SqlSugarRepository<AdoS8NotificationLayer> _rep;
  6. public S8NotificationLayerService(SqlSugarRepository<AdoS8NotificationLayer> rep) => _rep = rep;
  7. public async Task<List<AdoS8NotificationLayer>> ListAsync(long tenantId, long factoryId) =>
  8. await _rep.AsQueryable()
  9. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  10. .ToListAsync();
  11. public async Task<AdoS8NotificationLayer> CreateAsync(AdoS8NotificationLayer body)
  12. {
  13. if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode))
  14. throw new S8BizException("场景、严重度、层级必填");
  15. var exists = await _rep.AsQueryable()
  16. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  17. x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode);
  18. if (exists) throw new S8BizException("同场景+严重度+层级已存在");
  19. body.Id = 0;
  20. body.CreatedAt = DateTime.Now;
  21. await _rep.InsertAsync(body);
  22. return body;
  23. }
  24. public async Task<AdoS8NotificationLayer> UpdateAsync(long id, AdoS8NotificationLayer body)
  25. {
  26. var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  27. if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode))
  28. throw new S8BizException("场景、严重度、层级必填");
  29. var exists = await _rep.AsQueryable()
  30. .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  31. x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode);
  32. if (exists) throw new S8BizException("同场景+严重度+层级已存在");
  33. body.Id = id;
  34. body.CreatedAt = e.CreatedAt;
  35. body.UpdatedAt = DateTime.Now;
  36. await _rep.UpdateAsync(body);
  37. return body;
  38. }
  39. public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id);
  40. }