S8AlertRuleService.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  3. public class S8AlertRuleService : ITransient
  4. {
  5. private readonly SqlSugarRepository<AdoS8AlertRule> _rep;
  6. public S8AlertRuleService(SqlSugarRepository<AdoS8AlertRule> rep) => _rep = rep;
  7. public async Task<List<AdoS8AlertRule>> ListAsync(long tenantId, long factoryId) =>
  8. await _rep.AsQueryable()
  9. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  10. .ToListAsync();
  11. public async Task<AdoS8AlertRule> CreateAsync(AdoS8AlertRule body)
  12. {
  13. if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
  14. throw new S8BizException("规则编码和场景编码必填");
  15. var exists = await _rep.AsQueryable()
  16. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  17. (x.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
  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<AdoS8AlertRule> UpdateAsync(long id, AdoS8AlertRule body)
  25. {
  26. var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  27. if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
  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.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
  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. }