using System.Text.Json; using Admin.NET.Plugin.AiDOP.Entity.S8; using Admin.NET.Plugin.AiDOP.Service.S8.Rules; namespace Admin.NET.Plugin.AiDOP.Service.S8; public class S8WatchRuleService : ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _dataSourceRep; private readonly SqlSugarRepository _sceneRep; public S8WatchRuleService( SqlSugarRepository rep, SqlSugarRepository dataSourceRep, SqlSugarRepository sceneRep) { _rep = rep; _dataSourceRep = dataSourceRep; _sceneRep = sceneRep; } public async Task> ListAsync(long tenantId, long factoryId) => await _rep.AsQueryable() .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId) .ToListAsync(); public async Task CreateAsync(AdoS8WatchRule body) { await ValidateAsync(body); body.Id = 0; body.CreatedAt = DateTime.Now; await _rep.InsertAsync(body); return body; } public async Task 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); /// /// R4 安全更新:只更新 params_json 与 enabled。expression / rule_code / data_source_id / /// scene_code / watch_object_type / rule_type / source_object_type 一律不通过此路径修改。 /// 当 RuleType 非空时,按对应 evaluator 的 Params.Parse 进行 schema 校验,解析失败抛 S8BizException。 /// public async Task UpdateParamsAsync(long id, S8WatchRuleParamsPayload payload) { var entity = await _rep.GetByIdAsync(id) ?? throw new S8BizException("记录不存在"); var paramsJson = payload.ParamsJson?.Trim(); if (!string.IsNullOrEmpty(paramsJson)) { ValidateParamsJsonByRuleType(entity.RuleType, paramsJson); } entity.ParamsJson = string.IsNullOrEmpty(paramsJson) ? null : paramsJson; entity.Enabled = payload.Enabled; entity.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(entity); return entity; } private static void ValidateParamsJsonByRuleType(string? ruleType, string paramsJson) { try { switch (ruleType) { case S8TimeoutRuleEvaluator.RuleTypeCode: { var p = S8TimeoutParams.Parse(paramsJson); if (string.IsNullOrWhiteSpace(p.DueAtField) || string.IsNullOrWhiteSpace(p.StatusField) || string.IsNullOrWhiteSpace(p.ExceptionTypeCode)) throw new S8BizException("TIMEOUT params 缺少必填字段:dueAtField / statusField / exceptionTypeCode"); break; } case S8ShortageRuleEvaluator.RuleTypeCode: { var p = S8ShortageParams.Parse(paramsJson); if (string.IsNullOrWhiteSpace(p.TargetQtyField) || string.IsNullOrWhiteSpace(p.ActualQtyField) || string.IsNullOrWhiteSpace(p.ExceptionTypeCode)) throw new S8BizException("SHORTAGE params 缺少必填字段:targetQtyField / actualQtyField / exceptionTypeCode"); break; } case S8OutOfRangeRuleEvaluator.RuleTypeCode: { var p = S8OutOfRangeParams.Parse(paramsJson); if (string.IsNullOrWhiteSpace(p.MeasuredValueField)) throw new S8BizException("OUT_OF_RANGE params 缺少必填字段:measuredValueField"); if (p.LowerBound == null && p.UpperBound == null && string.IsNullOrWhiteSpace(p.LowerBoundField) && string.IsNullOrWhiteSpace(p.UpperBoundField)) throw new S8BizException("OUT_OF_RANGE params 必须提供 upperBound / lowerBound 或对应行内字段之一"); break; } default: // RuleType 为空或非三类已知值:仅做 JSON 合法性校验,避免阻塞历史数据。 using (JsonDocument.Parse(paramsJson)) { } break; } } catch (JsonException ex) { throw new S8BizException($"params_json 不是合法 JSON:{ex.Message}"); } } public async Task 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"); } }