S8AlertRuleService.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 S8AlertRuleService : ITransient
  5. {
  6. private readonly SqlSugarRepository<AdoS8AlertRule> _rep;
  7. public S8AlertRuleService(SqlSugarRepository<AdoS8AlertRule> rep) => _rep = rep;
  8. public async Task<List<AdoS8AlertRule>> ListAsync(long tenantId, long factoryId) =>
  9. await _rep.AsQueryable()
  10. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
  11. .ToListAsync();
  12. // S8-TENANT-FACTORY-P0-CLOSURE-1:归属一律由服务端可信作用域盖章,忽略 body.TenantId / body.FactoryId。
  13. public async Task<AdoS8AlertRule> CreateAsync(AdoS8AlertRule body, S8TrustedScope scope)
  14. {
  15. if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
  16. throw new S8BizException("规则编码和场景编码必填");
  17. body.TenantId = scope.TenantId;
  18. body.FactoryId = scope.FactoryId;
  19. var exists = await _rep.AsQueryable()
  20. .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
  21. (x.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
  22. if (exists) throw new S8BizException("相同场景下规则编码或触发条件重复");
  23. body.Id = 0;
  24. body.CreatedAt = DateTime.Now;
  25. await _rep.InsertAsync(body);
  26. return body;
  27. }
  28. // S8-TENANT-FACTORY-P0-CLOSURE-1:按 Id + 可信作用域绑行;越权 Id 视为不存在,归属不可被 body 改写。
  29. public async Task<AdoS8AlertRule> UpdateAsync(long id, AdoS8AlertRule body, S8TrustedScope scope)
  30. {
  31. var e = await LoadScopedAsync(id, scope);
  32. if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
  33. throw new S8BizException("规则编码和场景编码必填");
  34. var exists = await _rep.AsQueryable()
  35. .AnyAsync(x => x.Id != id && x.TenantId == e.TenantId && x.FactoryId == e.FactoryId &&
  36. (x.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
  37. if (exists) throw new S8BizException("相同场景下规则编码或触发条件重复");
  38. body.Id = id;
  39. body.TenantId = e.TenantId;
  40. body.FactoryId = e.FactoryId;
  41. body.CreatedAt = e.CreatedAt;
  42. body.UpdatedAt = DateTime.Now;
  43. await _rep.UpdateAsync(body);
  44. return body;
  45. }
  46. // S8-TENANT-FACTORY-P0-CLOSURE-1:删除必须先按可信作用域绑行,禁止裸 DeleteByIdAsync(id)。
  47. public async Task DeleteAsync(long id, S8TrustedScope scope)
  48. {
  49. var e = await LoadScopedAsync(id, scope);
  50. await _rep.DeleteByIdAsync(e.Id);
  51. }
  52. private async Task<AdoS8AlertRule> LoadScopedAsync(long id, S8TrustedScope scope) =>
  53. await _rep.AsQueryable()
  54. .Where(x => x.Id == id && x.TenantId == scope.TenantId && x.FactoryId == scope.FactoryId)
  55. .FirstAsync() ?? throw new S8NotFoundException();
  56. }