| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- 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();
- public async Task<AdoS8AlertRule> CreateAsync(AdoS8AlertRule body)
- {
- if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
- throw new S8BizException("规则编码和场景编码必填");
- 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;
- }
- public async Task<AdoS8AlertRule> UpdateAsync(long id, AdoS8AlertRule body)
- {
- var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
- if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
- throw new S8BizException("规则编码和场景编码必填");
- var exists = await _rep.AsQueryable()
- .AnyAsync(x => x.Id != id && 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 = id;
- body.CreatedAt = e.CreatedAt;
- body.UpdatedAt = DateTime.Now;
- await _rep.UpdateAsync(body);
- return body;
- }
- public async Task DeleteAsync(long id) => await _rep.DeleteByIdAsync(id);
- }
|