AdoSmartOpsKpiCalcConfigService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using Admin.NET.Core;
  2. using Admin.NET.Plugin.AiDOP.Dto.SmartOps;
  3. using Admin.NET.Plugin.AiDOP.Entity;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. using SqlSugar;
  6. namespace Admin.NET.Plugin.AiDOP.SmartOps;
  7. /// <summary>
  8. /// KPI 计算配置服务:CRUD + 校验 + 试算 + 发布/停用/激活(发布状态机,单生效版本事务保证)。
  9. /// 租户显式控制(ClearFilter):配置落在"分发器运行时查找的租户"= KPI 数据租户(S5=AidopSourceTenantMap 解析),
  10. /// 而非 API 调用者 JWT 租户;否则运行时找不到配置。SqlScript 属高危配置,接口须登录鉴权(Controller 层)。
  11. /// </summary>
  12. public sealed class AdoSmartOpsKpiCalcConfigService : ITransient
  13. {
  14. private const string EngineLegacyCode = "LEGACY_CODE";
  15. private const string EngineConfigSql = "CONFIG_SQL";
  16. private const string EngineLegacyTvf = "LEGACY_TVF";
  17. private const string StatusDraft = "DRAFT";
  18. private const string StatusPublished = "PUBLISHED";
  19. private const string StatusRetired = "RETIRED";
  20. private static readonly string[] TenantScopedModules = { "S5", "S6", "S7" };
  21. private readonly ISqlSugarClient _db;
  22. private readonly KpiSqlReadOnlyExecutor _executor;
  23. private readonly AdoSmartOpsKpiBusinessInputService _businessInput;
  24. public AdoSmartOpsKpiCalcConfigService(ISqlSugarClient db, KpiSqlReadOnlyExecutor executor, AdoSmartOpsKpiBusinessInputService businessInput)
  25. {
  26. _db = db;
  27. _executor = executor;
  28. _businessInput = businessInput;
  29. }
  30. /// <summary>
  31. /// CONFIG_SQL 业务门禁:仅 BusinessInputStatus=READY_FOR_CONFIG 放行 试算/发布/激活。
  32. /// 未就绪则明确拒绝——不执行 SQL、不写运行日志、不改版本状态/IsCurrent。
  33. /// </summary>
  34. private async Task EnsureBusinessReadyAsync(long tenantId, string metricCode, string action)
  35. {
  36. var status = await _businessInput.GetStatusAsync(tenantId, metricCode);
  37. if (status != AdoSmartOpsKpiBusinessInputService.StatusReady)
  38. throw Oops.Bah($"业务来源未就绪(当前 {status}):{action} 需先在「业务来源登记」补齐来源系统/表/字段/SQL来源并置为 READY_FOR_CONFIG");
  39. }
  40. /// <summary>解析配置应归属的租户(= 运行时 KPI 落库租户)。S5/S6/S7 走 T8 账套映射(pbxfxp→AIDOP)。</summary>
  41. public static long ResolveKpiTenantId(string moduleCode)
  42. {
  43. if (TenantScopedModules.Contains((moduleCode ?? "").Trim().ToUpperInvariant()))
  44. return AidopSourceTenantMap.ResolveTenantId("pbxfxp");
  45. return SqlSugarConst.DefaultTenantId;
  46. }
  47. private ISugarQueryable<AdoSmartOpsKpiCalcConfig> Query() =>
  48. _db.Queryable<AdoSmartOpsKpiCalcConfig>().ClearFilter<ITenantIdFilter>();
  49. /// <summary>某 KPI 的全部版本(按版本号倒序)。</summary>
  50. public async Task<List<KpiCalcConfigDto>> GetByMetricAsync(string metricCode, string moduleCode)
  51. {
  52. var tenantId = ResolveKpiTenantId(moduleCode);
  53. var list = await Query()
  54. .Where(x => x.MetricCode == metricCode && x.TenantId == tenantId)
  55. .OrderBy(x => x.VersionNo, OrderByType.Desc)
  56. .ToListAsync();
  57. return list.Select(ToDto).ToList();
  58. }
  59. /// <summary>当前生效配置(PUBLISHED + IsCurrent),无则 null(= 走 legacy)。</summary>
  60. public async Task<AdoSmartOpsKpiCalcConfig?> GetActiveAsync(long tenantId, string metricCode)
  61. {
  62. return await Query()
  63. .Where(x => x.MetricCode == metricCode && x.TenantId == tenantId
  64. && x.IsCurrent && x.PublishStatus == StatusPublished)
  65. .FirstAsync();
  66. }
  67. /// <summary>新增草稿(VersionNo = 现有最大 + 1)。</summary>
  68. public async Task<KpiCalcConfigDto> CreateDraftAsync(KpiCalcConfigUpsertDto dto, string operatorName)
  69. {
  70. ValidateEngineType(dto.CalcEngineType);
  71. var tenantId = ResolveKpiTenantId(dto.ModuleCode);
  72. var maxVer = await Query()
  73. .Where(x => x.MetricCode == dto.MetricCode && x.TenantId == tenantId)
  74. .MaxAsync(x => (int?)x.VersionNo) ?? 0;
  75. var entity = new AdoSmartOpsKpiCalcConfig
  76. {
  77. TenantId = tenantId,
  78. MetricCode = dto.MetricCode,
  79. ModuleCode = dto.ModuleCode,
  80. VersionNo = maxVer + 1,
  81. CalcEngineType = dto.CalcEngineType,
  82. DataSourceCode = dto.DataSourceCode,
  83. SqlScript = dto.SqlScript,
  84. SqlParametersJson = dto.SqlParametersJson,
  85. TimeoutSeconds = NormalizeTimeout(dto.TimeoutSeconds),
  86. PublishStatus = StatusDraft,
  87. IsCurrent = false,
  88. Remark = dto.Remark,
  89. CreatedBy = operatorName,
  90. CreatedAt = DateTime.Now,
  91. };
  92. entity.Id = await _db.Insertable(entity).ExecuteReturnBigIdentityAsync();
  93. return ToDto(entity);
  94. }
  95. /// <summary>编辑草稿(仅 DRAFT 可改;已发布/停用版本不可改,须新建版本)。</summary>
  96. public async Task UpdateDraftAsync(long id, KpiCalcConfigUpsertDto dto, string operatorName)
  97. {
  98. var entity = await Query().Where(x => x.Id == id).FirstAsync()
  99. ?? throw Oops.Bah("配置版本不存在");
  100. if (entity.PublishStatus != StatusDraft)
  101. throw Oops.Bah("仅草稿(DRAFT)可编辑;已发布/停用版本请新建版本");
  102. ValidateEngineType(dto.CalcEngineType);
  103. entity.CalcEngineType = dto.CalcEngineType;
  104. entity.DataSourceCode = dto.DataSourceCode;
  105. entity.SqlScript = dto.SqlScript;
  106. entity.SqlParametersJson = dto.SqlParametersJson;
  107. entity.TimeoutSeconds = NormalizeTimeout(dto.TimeoutSeconds);
  108. entity.Remark = dto.Remark;
  109. entity.UpdatedBy = operatorName;
  110. entity.UpdatedAt = DateTime.Now;
  111. await _db.Updateable(entity).ExecuteCommandAsync();
  112. }
  113. /// <summary>删除草稿(仅 DRAFT)。</summary>
  114. public async Task DeleteAsync(long id)
  115. {
  116. var entity = await Query().Where(x => x.Id == id).FirstAsync()
  117. ?? throw Oops.Bah("配置版本不存在");
  118. if (entity.PublishStatus != StatusDraft)
  119. throw Oops.Bah("仅草稿(DRAFT)可删除;已发布版本请用停用");
  120. await _db.Deleteable<AdoSmartOpsKpiCalcConfig>().Where(x => x.Id == id).ExecuteCommandAsync();
  121. }
  122. /// <summary>SQL 安全校验(多层非正则)。</summary>
  123. public KpiSqlValidateResultDto Validate(string? sql)
  124. {
  125. var r = KpiSqlSecurityValidator.Validate(sql);
  126. return new KpiSqlValidateResultDto
  127. {
  128. Ok = r.Ok,
  129. ValidationStatus = r.Ok ? "VALID" : "INVALID",
  130. ErrorCode = r.ErrorCode,
  131. ErrorMessage = r.ErrorMessage,
  132. ReferencedTables = r.ReferencedTables,
  133. };
  134. }
  135. /// <summary>试算(服务端重校验 + 只读执行;不写正式 KPI 值)。</summary>
  136. public async Task<KpiSqlPreviewResultDto> PreviewAsync(KpiSqlPreviewDto dto, CancellationToken ct)
  137. {
  138. var bizDate = (dto.BizDate ?? DateTime.Today.AddDays(-1)).Date;
  139. var res = new KpiSqlPreviewResultDto
  140. {
  141. MetricCode = dto.MetricCode,
  142. EngineType = EngineConfigSql,
  143. DataSourceCode = dto.DataSourceCode,
  144. BizDate = bizDate.ToString("yyyy-MM-dd"),
  145. };
  146. var val = KpiSqlSecurityValidator.Validate(dto.SqlScript);
  147. res.ValidationStatus = val.Ok ? "VALID" : "INVALID";
  148. if (!val.Ok)
  149. {
  150. res.Ok = false;
  151. res.ResultStatus = "FAILED";
  152. res.ErrorCode = val.ErrorCode;
  153. res.ErrorMessage = val.ErrorMessage;
  154. return res;
  155. }
  156. var tenantId = ResolveKpiTenantId(dto.ModuleCode);
  157. // 业务门禁:试算是真实数据只读执行,未 READY 直接拒绝(不落只读事务、不占连接)。
  158. await EnsureBusinessReadyAsync(tenantId, dto.MetricCode, "试算");
  159. var pars = new KpiSqlRunParams
  160. {
  161. TenantId = tenantId,
  162. FactoryId = 1,
  163. ModuleCode = dto.ModuleCode,
  164. MetricCode = dto.MetricCode,
  165. BizDate = bizDate,
  166. PeriodStart = bizDate,
  167. PeriodEnd = bizDate.AddDays(1).AddSeconds(-1),
  168. SourceZtid = "pbxfxp",
  169. };
  170. var exec = await _executor.ExecuteAsync(dto.DataSourceCode, dto.SqlScript!, dto.TimeoutSeconds, true, pars, ct);
  171. res.Ok = exec.Status != "FAILED";
  172. res.ResultStatus = exec.Status;
  173. res.DurationMs = exec.DurationMs;
  174. res.RowCount = exec.RowCount;
  175. res.MetricValue = exec.MetricValue;
  176. res.NumeratorValue = exec.NumeratorValue;
  177. res.DenominatorValue = exec.DenominatorValue;
  178. res.ResultMessage = exec.ResultMessage;
  179. res.ErrorCode = exec.ErrorCode;
  180. res.ErrorMessage = exec.ErrorMessage;
  181. res.ExecutedParameters =
  182. $"tenant_id={pars.TenantId}, factory_id={pars.FactoryId}, module_code={pars.ModuleCode}, " +
  183. $"metric_code={pars.MetricCode}, biz_date={pars.BizDate:yyyy-MM-dd}, ztid={pars.SourceZtid}";
  184. return res;
  185. }
  186. /// <summary>发布:事务内校验→旧 current 置 0→本版 PUBLISHED+IsCurrent=1(保证单生效版本)。</summary>
  187. public async Task PublishAsync(long id, string operatorName)
  188. {
  189. var entity = await Query().Where(x => x.Id == id).FirstAsync()
  190. ?? throw Oops.Bah("配置版本不存在");
  191. if (entity.PublishStatus == StatusRetired)
  192. throw Oops.Bah("已停用版本不可发布,请新建版本");
  193. // CONFIG_SQL 发布前必须再过一次安全校验
  194. if (entity.CalcEngineType == EngineConfigSql)
  195. {
  196. var val = KpiSqlSecurityValidator.Validate(entity.SqlScript);
  197. if (!val.Ok) throw Oops.Bah($"SQL 安全校验未通过:{val.ErrorCode} {val.ErrorMessage}");
  198. if (string.IsNullOrWhiteSpace(entity.DataSourceCode))
  199. throw Oops.Bah("CONFIG_SQL 必须指定数据源");
  200. await EnsureBusinessReadyAsync(entity.TenantId ?? 0, entity.MetricCode, "发布");
  201. }
  202. var tenantId = entity.TenantId;
  203. var tran = await _db.AsTenant().UseTranAsync(async () =>
  204. {
  205. await _db.Updateable<AdoSmartOpsKpiCalcConfig>()
  206. .SetColumns(x => new AdoSmartOpsKpiCalcConfig { IsCurrent = false })
  207. .Where(x => x.MetricCode == entity.MetricCode && x.TenantId == tenantId && x.Id != id && x.IsCurrent)
  208. .ExecuteCommandAsync();
  209. await _db.Updateable<AdoSmartOpsKpiCalcConfig>()
  210. .SetColumns(x => new AdoSmartOpsKpiCalcConfig
  211. {
  212. PublishStatus = StatusPublished, IsCurrent = true,
  213. PublishedBy = operatorName, PublishedAt = DateTime.Now,
  214. })
  215. .Where(x => x.Id == id)
  216. .ExecuteCommandAsync();
  217. });
  218. if (!tran.IsSuccess) throw tran.ErrorException;
  219. }
  220. /// <summary>激活/回滚:把某历史版本设为当前生效(其它同 KPI 版本 IsCurrent 置 0),事务内。仅对已发布版本。</summary>
  221. public async Task ActivateAsync(long id, string operatorName)
  222. {
  223. var entity = await Query().Where(x => x.Id == id).FirstAsync()
  224. ?? throw Oops.Bah("配置版本不存在");
  225. if (entity.PublishStatus != StatusPublished)
  226. throw Oops.Bah("只能激活已发布(PUBLISHED)版本;回滚是激活历史已发布版本,不是复制重发");
  227. if (entity.CalcEngineType == EngineConfigSql)
  228. await EnsureBusinessReadyAsync(entity.TenantId ?? 0, entity.MetricCode, "激活");
  229. var tenantId = entity.TenantId;
  230. var tran = await _db.AsTenant().UseTranAsync(async () =>
  231. {
  232. await _db.Updateable<AdoSmartOpsKpiCalcConfig>()
  233. .SetColumns(x => new AdoSmartOpsKpiCalcConfig { IsCurrent = false })
  234. .Where(x => x.MetricCode == entity.MetricCode && x.TenantId == tenantId && x.Id != id && x.IsCurrent)
  235. .ExecuteCommandAsync();
  236. await _db.Updateable<AdoSmartOpsKpiCalcConfig>()
  237. .SetColumns(x => new AdoSmartOpsKpiCalcConfig { IsCurrent = true, UpdatedBy = operatorName, UpdatedAt = DateTime.Now })
  238. .Where(x => x.Id == id)
  239. .ExecuteCommandAsync();
  240. });
  241. if (!tran.IsSuccess) throw tran.ErrorException;
  242. }
  243. /// <summary>停用(RETIRED,IsCurrent=0)。停用当前生效版本后无 current → 运行时回落 legacy。</summary>
  244. public async Task RetireAsync(long id, string operatorName)
  245. {
  246. var entity = await Query().Where(x => x.Id == id).FirstAsync()
  247. ?? throw Oops.Bah("配置版本不存在");
  248. await _db.Updateable<AdoSmartOpsKpiCalcConfig>()
  249. .SetColumns(x => new AdoSmartOpsKpiCalcConfig
  250. {
  251. PublishStatus = StatusRetired, IsCurrent = false,
  252. RetiredBy = operatorName, RetiredAt = DateTime.Now,
  253. })
  254. .Where(x => x.Id == id)
  255. .ExecuteCommandAsync();
  256. }
  257. /// <summary>已登记数据源列表(第一版仅本地中台库)。</summary>
  258. public List<object> ListDataSources() => new()
  259. {
  260. new { code = KpiSqlReadOnlyExecutor.LocalDataSourceCode, name = "本地中台库(aidopdev / mdp_std_* / dwd_*)", readOnly = true },
  261. };
  262. private static void ValidateEngineType(string engine)
  263. {
  264. if (engine != EngineLegacyCode && engine != EngineConfigSql && engine != EngineLegacyTvf)
  265. throw Oops.Bah($"非法引擎类型:{engine}(LEGACY_CODE/CONFIG_SQL/LEGACY_TVF)");
  266. }
  267. private static int NormalizeTimeout(int t) =>
  268. t <= 0 ? 30 : Math.Min(t, KpiSqlReadOnlyExecutor.SystemMaxTimeoutSeconds);
  269. private static KpiCalcConfigDto ToDto(AdoSmartOpsKpiCalcConfig e) => new()
  270. {
  271. Id = e.Id, TenantId = e.TenantId, MetricCode = e.MetricCode, ModuleCode = e.ModuleCode,
  272. VersionNo = e.VersionNo, CalcEngineType = e.CalcEngineType, DataSourceCode = e.DataSourceCode,
  273. SqlScript = e.SqlScript, SqlParametersJson = e.SqlParametersJson, TimeoutSeconds = e.TimeoutSeconds,
  274. PublishStatus = e.PublishStatus, IsCurrent = e.IsCurrent, Remark = e.Remark,
  275. CreatedBy = e.CreatedBy, CreatedAt = e.CreatedAt, PublishedBy = e.PublishedBy, PublishedAt = e.PublishedAt,
  276. };
  277. }