KpiTargetConfigStore.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Admin.NET.Plugin.AiDOP.Entity;
  2. namespace Admin.NET.Plugin.AiDOP.SmartOps;
  3. public sealed class KpiTargetConfigStore : IKpiTargetConfigStore, ITransient
  4. {
  5. private readonly ISqlSugarClient _db;
  6. public KpiTargetConfigStore(ISqlSugarClient db) => _db = db;
  7. public Task<AdoSmartOpsKpiMaster?> FindKpiAsync(long tenantId, string metricCode, CancellationToken ct = default) =>
  8. _db.Queryable<AdoSmartOpsKpiMaster>()
  9. .ClearFilter<ITenantIdFilter>()
  10. .Where(x => x.TenantId == tenantId && x.MetricCode == metricCode)
  11. .FirstAsync(ct);
  12. public Task<List<AdoSmartOpsKpiTargetConfig>> ListAsync(long tenantId, string metricCode, long? factoryId, CancellationToken ct = default)
  13. {
  14. var q = _db.Queryable<AdoSmartOpsKpiTargetConfig>()
  15. .Where(x => x.TenantId == tenantId && x.MetricCode == metricCode);
  16. if (factoryId.HasValue)
  17. q = q.Where(x => x.FactoryId == factoryId.Value);
  18. return q.OrderBy(x => x.FactoryId).OrderBy(x => x.EffectiveFrom, OrderByType.Desc).ToListAsync(ct);
  19. }
  20. public Task<List<AdoSmartOpsKpiTargetConfig>> ListActiveScopeAsync(long tenantId, long factoryId, string metricCode, CancellationToken ct = default) =>
  21. _db.Queryable<AdoSmartOpsKpiTargetConfig>()
  22. .Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.MetricCode == metricCode && x.Status == 1)
  23. .ToListAsync(ct);
  24. public Task<AdoSmartOpsKpiTargetConfig?> GetByIdAsync(long tenantId, long id, CancellationToken ct = default) =>
  25. _db.Queryable<AdoSmartOpsKpiTargetConfig>()
  26. .Where(x => x.TenantId == tenantId && x.Id == id)
  27. .FirstAsync(ct);
  28. public async Task<AdoSmartOpsKpiTargetConfig> InsertAsync(AdoSmartOpsKpiTargetConfig row, CancellationToken ct = default)
  29. {
  30. var id = await _db.Insertable(row).ExecuteReturnIdentityAsync();
  31. row.Id = id;
  32. return row;
  33. }
  34. public Task UpdateAsync(AdoSmartOpsKpiTargetConfig row, CancellationToken ct = default) =>
  35. _db.Updateable(row).ExecuteCommandAsync();
  36. public async Task<bool> IsReferencedByDailyAsync(long tenantId, long configId, CancellationToken ct = default)
  37. {
  38. foreach (var table in new[] { "ado_s9_kpi_value_l1_day", "ado_s9_kpi_value_l2_day", "ado_s9_kpi_value_l3_day", "ado_s9_kpi_value_l4_day" })
  39. {
  40. var n = await _db.Ado.GetIntAsync(
  41. $"SELECT COUNT(*) FROM {table} WHERE tenant_id=@TenantId AND target_config_id=@Id AND is_deleted=0",
  42. new List<SugarParameter> { new("@TenantId", tenantId), new("@Id", configId) });
  43. if (n > 0) return true;
  44. }
  45. return false;
  46. }
  47. public Task<int> CountCommittedHashAsync(long tenantId, string sha256, CancellationToken ct = default) =>
  48. _db.Queryable<AdoSmartOpsKpiTargetImportBatch>()
  49. .Where(x => x.TenantId == tenantId && x.FileSha256 == sha256 && x.Status == "COMMITTED")
  50. .CountAsync(ct);
  51. public async Task<AdoSmartOpsKpiTargetImportBatch> InsertBatchAsync(AdoSmartOpsKpiTargetImportBatch batch, CancellationToken ct = default)
  52. {
  53. var id = await _db.Insertable(batch).ExecuteReturnIdentityAsync();
  54. batch.Id = id;
  55. return batch;
  56. }
  57. public Task<AdoSmartOpsKpiTargetImportBatch?> GetBatchAsync(long tenantId, long batchId, CancellationToken ct = default) =>
  58. _db.Queryable<AdoSmartOpsKpiTargetImportBatch>()
  59. .Where(x => x.TenantId == tenantId && x.Id == batchId)
  60. .FirstAsync(ct);
  61. public Task UpdateBatchAsync(AdoSmartOpsKpiTargetImportBatch batch, CancellationToken ct = default) =>
  62. _db.Updateable(batch).ExecuteCommandAsync();
  63. }