using Admin.NET.Core; using Admin.NET.Plugin.AiDOP.Entity.S8; using Admin.NET.Plugin.AiDOP.Infrastructure.S8; namespace Admin.NET.Plugin.AiDOP.Service.S8; public class S8NotificationLayerService : ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _roleRep; public S8NotificationLayerService( SqlSugarRepository rep, SqlSugarRepository roleRep) { _rep = rep; _roleRep = roleRep; } public async Task> ListAsync(long tenantId, long factoryId) => await _rep.AsQueryable() .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId) .ToListAsync(); public async Task> GetRoleOptionsAsync() { var roles = await _roleRep.AsQueryable() .Where(x => (x.Id >= 1329908000101L && x.Id <= 1329908000107L) || x.Id == 1300000000101L) .OrderBy(x => x.Id) .Select(x => new { value = x.Code, label = x.Name }) .ToListAsync(); return roles.Cast().ToList(); } public async Task CreateAsync(AdoS8NotificationLayer body) { if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode)) throw new S8BizException("场景、严重度、层级必填"); if (string.IsNullOrWhiteSpace(body.TargetRoleIds)) throw new S8BizException("目标角色必填"); if (string.IsNullOrWhiteSpace(body.NotifyChannel)) throw new S8BizException("通知渠道至少选一个"); // S8-SEVERITY-FOLLOW-SERIOUS-STANDARDIZE-EXEC-1:归一为 FOLLOW/SERIOUS 后再校验/写入。 body.Severity = S8SeverityCode.Normalize(body.Severity); var exists = await _rep.AsQueryable() .AnyAsync(x => x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode); if (exists) throw new S8BizException("同场景+严重度+层级已存在"); body.Id = 0; body.CreatedAt = DateTime.Now; return await _rep.InsertReturnEntityAsync(body); } public async Task UpdateAsync(long id, AdoS8NotificationLayer body) { var e = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); if (string.IsNullOrWhiteSpace(body.SceneCode) || string.IsNullOrWhiteSpace(body.Severity) || string.IsNullOrWhiteSpace(body.LevelCode)) throw new S8BizException("场景、严重度、层级必填"); if (string.IsNullOrWhiteSpace(body.TargetRoleIds)) throw new S8BizException("目标角色必填"); if (string.IsNullOrWhiteSpace(body.NotifyChannel)) throw new S8BizException("通知渠道至少选一个"); // S8-SEVERITY-FOLLOW-SERIOUS-STANDARDIZE-EXEC-1:归一为 FOLLOW/SERIOUS 后再校验/写入。 body.Severity = S8SeverityCode.Normalize(body.Severity); var exists = await _rep.AsQueryable() .AnyAsync(x => x.Id != id && x.TenantId == body.TenantId && x.FactoryId == body.FactoryId && x.SceneCode == body.SceneCode && x.Severity == body.Severity && x.LevelCode == body.LevelCode); 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); }