| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- namespace Admin.NET.Plugin.AiDOP.Service.S8;
- public class S8WatchRuleService : ITransient
- {
- private readonly SqlSugarRepository<AdoS8WatchRule> _rep;
- private readonly SqlSugarRepository<AdoS8DataSource> _dataSourceRep;
- private readonly SqlSugarRepository<AdoS8SceneConfig> _sceneRep;
- public S8WatchRuleService(
- SqlSugarRepository<AdoS8WatchRule> rep,
- SqlSugarRepository<AdoS8DataSource> dataSourceRep,
- SqlSugarRepository<AdoS8SceneConfig> sceneRep)
- {
- _rep = rep;
- _dataSourceRep = dataSourceRep;
- _sceneRep = sceneRep;
- }
- public async Task<List<AdoS8WatchRule>> ListAsync(long tenantId, long factoryId) =>
- await _rep.AsQueryable()
- .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId)
- .ToListAsync();
- public async Task<AdoS8WatchRule> CreateAsync(AdoS8WatchRule body)
- {
- await ValidateAsync(body);
- body.Id = 0;
- body.CreatedAt = DateTime.Now;
- await _rep.InsertAsync(body);
- return body;
- }
- public async Task<AdoS8WatchRule> UpdateAsync(long id, AdoS8WatchRule body)
- {
- var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
- await ValidateAsync(body, id);
- 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);
- public async Task<object> TestAsync(long id)
- {
- var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在");
- await ValidateAsync(entity, id);
- return new { id, success = true, message = "规则基础校验通过", pollIntervalSeconds = entity.PollIntervalSeconds };
- }
- private async Task ValidateAsync(AdoS8WatchRule body, long? id = null)
- {
- if (string.IsNullOrWhiteSpace(body.RuleCode) || string.IsNullOrWhiteSpace(body.SceneCode))
- throw new S8BizException("规则编码和场景编码必填");
- var exists = await _rep.AsQueryable()
- .AnyAsync(x => x.Id != (id ?? 0) && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.RuleCode == body.RuleCode);
- if (exists) throw new S8BizException("监视规则编码已存在");
- var dataSource = await _dataSourceRep.GetFirstAsync(x => x.Id == body.DataSourceId)
- ?? throw new S8BizException("关联数据源不存在");
- if (!dataSource.Enabled) throw new S8BizException("关联数据源未启用");
- var scene = await _sceneRep.GetFirstAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.SceneCode == body.SceneCode)
- ?? throw new S8BizException("关联场景不存在");
- if (!scene.Enabled) throw new S8BizException("关联场景未启用");
- if (body.PollIntervalSeconds <= 0) throw new S8BizException("轮询间隔必须大于 0");
- }
- }
|