| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- using Admin.NET.Plugin.AiDOP.Infrastructure;
- namespace Admin.NET.Plugin.AiDOP.Service.S8;
- public class S8AlertRuleService : ITransient
- {
- private readonly SqlSugarRepository<AdoS8AlertRule> _rep;
- public S8AlertRuleService(SqlSugarRepository<AdoS8AlertRule> rep) => _rep = rep;
- public async Task<List<AdoS8AlertRule>> ListAsync(long tenantId, long factoryId) =>
- await _rep.AsQueryable()
- .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
- .ToListAsync();
- // S8-TENANT-FACTORY-P0-CLOSURE-1:归属一律由服务端可信作用域盖章,忽略 body.TenantId / body.FactoryId。
- public async Task<AdoS8AlertRule> CreateAsync(AdoS8AlertRule body, S8TrustedScope scope)
- {
- if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
- throw new S8BizException("规则编码和场景编码必填");
- body.TenantId = scope.TenantId;
- body.FactoryId = scope.FactoryId;
- var exists = await _rep.AsQueryable()
- .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId &&
- (x.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
- if (exists) throw new S8BizException("相同场景下规则编码或触发条件重复");
- body.Id = 0;
- body.CreatedAt = DateTime.Now;
- await _rep.InsertAsync(body);
- return body;
- }
- // S8-TENANT-FACTORY-P0-CLOSURE-1:按 Id + 可信作用域绑行;越权 Id 视为不存在,归属不可被 body 改写。
- public async Task<AdoS8AlertRule> UpdateAsync(long id, AdoS8AlertRule body, S8TrustedScope scope)
- {
- var e = await LoadScopedAsync(id, scope);
- if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
- throw new S8BizException("规则编码和场景编码必填");
- var exists = await _rep.AsQueryable()
- .AnyAsync(x => x.Id != id && x.TenantId == e.TenantId && x.FactoryId == e.FactoryId &&
- (x.RuleCode == body.RuleCode || (x.SceneCode == body.SceneCode && x.TriggerCondition == body.TriggerCondition)));
- if (exists) throw new S8BizException("相同场景下规则编码或触发条件重复");
- body.Id = id;
- body.TenantId = e.TenantId;
- body.FactoryId = e.FactoryId;
- body.CreatedAt = e.CreatedAt;
- body.UpdatedAt = DateTime.Now;
- await _rep.UpdateAsync(body);
- return body;
- }
- // S8-TENANT-FACTORY-P0-CLOSURE-1:删除必须先按可信作用域绑行,禁止裸 DeleteByIdAsync(id)。
- public async Task DeleteAsync(long id, S8TrustedScope scope)
- {
- var e = await LoadScopedAsync(id, scope);
- await _rep.DeleteByIdAsync(e.Id);
- }
- private async Task<AdoS8AlertRule> LoadScopedAsync(long id, S8TrustedScope scope) =>
- await _rep.AsQueryable()
- .Where(x => x.Id == id && x.TenantId == scope.TenantId && x.FactoryId == scope.FactoryId)
- .FirstAsync() ?? throw new S8NotFoundException();
- }
|