S8WatchRuleService.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  3. public class S8WatchRuleService : ITransient
  4. {
  5. private readonly SqlSugarRepository<AdoS8WatchRule> _rep;
  6. private readonly SqlSugarRepository<AdoS8DataSource> _dataSourceRep;
  7. private readonly SqlSugarRepository<AdoS8SceneConfig> _sceneRep;
  8. public S8WatchRuleService(
  9. SqlSugarRepository<AdoS8WatchRule> rep,
  10. SqlSugarRepository<AdoS8DataSource> dataSourceRep,
  11. SqlSugarRepository<AdoS8SceneConfig> sceneRep)
  12. {
  13. _rep = rep;
  14. _dataSourceRep = dataSourceRep;
  15. _sceneRep = sceneRep;
  16. }
  17. public async Task<List<AdoS8WatchRule>> ListAsync(long tenantId, long factoryId) =>
  18. await _rep.AsQueryable()
  19. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  20. .ToListAsync();
  21. public async Task<AdoS8WatchRule> CreateAsync(AdoS8WatchRule body)
  22. {
  23. await ValidateAsync(body);
  24. body.Id = 0;
  25. body.CreatedAt = DateTime.Now;
  26. await _rep.InsertAsync(body);
  27. return body;
  28. }
  29. public async Task<AdoS8WatchRule> UpdateAsync(long id, AdoS8WatchRule body)
  30. {
  31. var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  32. await ValidateAsync(body, id);
  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. public async Task<object> TestAsync(long id)
  41. {
  42. var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
  43. await ValidateAsync(entity, id);
  44. return new { id, success = true, message = "规则基础校验通过", pollIntervalSeconds = entity.PollIntervalSeconds };
  45. }
  46. private async Task ValidateAsync(AdoS8WatchRule body, long? id = null)
  47. {
  48. if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
  49. throw new S8BizException("规则编码和场景编码必填");
  50. var exists = await _rep.AsQueryable()
  51. .AnyAsync(x => x.Id != (id ?? 0) && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.RuleCode == body.RuleCode);
  52. if (exists) throw new S8BizException("监视规则编码已存在");
  53. var dataSource = await _dataSourceRep.GetFirstAsync(x => x.Id == body.DataSourceId)
  54. ?? throw new S8BizException("关联数据源不存在");
  55. if (!dataSource.Enabled) throw new S8BizException("关联数据源未启用");
  56. var scene = await _sceneRep.GetFirstAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.SceneCode == body.SceneCode)
  57. ?? throw new S8BizException("关联场景不存在");
  58. if (!scene.Enabled) throw new S8BizException("关联场景未启用");
  59. if (body.PollIntervalSeconds <= 0) throw new S8BizException("轮询间隔必须大于 0");
  60. }
  61. }