FakeKpiTargetStore.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Admin.NET.Plugin.AiDOP.Entity;
  2. using Admin.NET.Plugin.AiDOP.SmartOps;
  3. namespace Admin.NET.Plugin.AiDOP.Tests.SmartOps;
  4. internal sealed class FakeKpiTargetStore : IKpiTargetConfigStore
  5. {
  6. public List<AdoSmartOpsKpiMaster> Kpis { get; } = new();
  7. public List<AdoSmartOpsKpiTargetConfig> Configs { get; } = new();
  8. public List<AdoSmartOpsKpiTargetImportBatch> Batches { get; } = new();
  9. public HashSet<long> ReferencedIds { get; } = new();
  10. private long _id = 1;
  11. private long _batchId = 1;
  12. public Task<AdoSmartOpsKpiMaster?> FindKpiAsync(long tenantId, string metricCode, CancellationToken ct = default) =>
  13. Task.FromResult(Kpis.FirstOrDefault(x => x.TenantId == tenantId && x.MetricCode == metricCode));
  14. public Task<List<AdoSmartOpsKpiTargetConfig>> ListAsync(long tenantId, string metricCode, long? factoryId, CancellationToken ct = default) =>
  15. Task.FromResult(Configs.Where(x => x.TenantId == tenantId && x.MetricCode == metricCode && (!factoryId.HasValue || x.FactoryId == factoryId.Value)).ToList());
  16. public Task<List<AdoSmartOpsKpiTargetConfig>> ListActiveScopeAsync(long tenantId, long factoryId, string metricCode, CancellationToken ct = default) =>
  17. Task.FromResult(Configs.Where(x => x.TenantId == tenantId && x.FactoryId == factoryId && x.MetricCode == metricCode && x.Status == 1).ToList());
  18. public Task<AdoSmartOpsKpiTargetConfig?> GetByIdAsync(long tenantId, long id, CancellationToken ct = default) =>
  19. Task.FromResult(Configs.FirstOrDefault(x => x.TenantId == tenantId && x.Id == id));
  20. public Task<AdoSmartOpsKpiTargetConfig> InsertAsync(AdoSmartOpsKpiTargetConfig row, CancellationToken ct = default)
  21. {
  22. row.Id = _id++;
  23. Configs.Add(row);
  24. return Task.FromResult(row);
  25. }
  26. public Task UpdateAsync(AdoSmartOpsKpiTargetConfig row, CancellationToken ct = default) => Task.CompletedTask;
  27. public Task<bool> IsReferencedByDailyAsync(long tenantId, long configId, CancellationToken ct = default) =>
  28. Task.FromResult(ReferencedIds.Contains(configId));
  29. public Task<int> CountCommittedHashAsync(long tenantId, string sha256, CancellationToken ct = default) =>
  30. Task.FromResult(Batches.Count(x => x.TenantId == tenantId && x.FileSha256 == sha256 && x.Status == "COMMITTED"));
  31. public Task<AdoSmartOpsKpiTargetImportBatch> InsertBatchAsync(AdoSmartOpsKpiTargetImportBatch batch, CancellationToken ct = default)
  32. {
  33. batch.Id = _batchId++;
  34. Batches.Add(batch);
  35. return Task.FromResult(batch);
  36. }
  37. public Task<AdoSmartOpsKpiTargetImportBatch?> GetBatchAsync(long tenantId, long batchId, CancellationToken ct = default) =>
  38. Task.FromResult(Batches.FirstOrDefault(x => x.TenantId == tenantId && x.Id == batchId));
  39. public Task UpdateBatchAsync(AdoSmartOpsKpiTargetImportBatch batch, CancellationToken ct = default) => Task.CompletedTask;
  40. }
  41. internal sealed class FakeDailyReader : IKpiDailyTargetReader
  42. {
  43. public decimal? Current { get; set; }
  44. public decimal? Prior { get; set; }
  45. public Task<decimal?> GetCurrentTargetAsync(long tenantId, long factoryId, string moduleCode, string metricCode, DateTime bizDate, CancellationToken ct = default)
  46. => Task.FromResult(Current);
  47. public Task<decimal?> GetPriorTargetAsync(long tenantId, long factoryId, string moduleCode, string metricCode, DateTime bizDate, CancellationToken ct = default)
  48. => Task.FromResult(Prior);
  49. }