PurchaseHandoffContracts.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Text.Json;
  2. using System.Text.Json.Serialization;
  3. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  4. public sealed record PurchaseHandoffLine(
  5. Guid RawEventId,
  6. string PoNo,
  7. string PoLine,
  8. string ItemCode,
  9. decimal OrderQty,
  10. string? SupplierCode,
  11. DateTime? OrderDate,
  12. DateTime? DueDate,
  13. string? Status,
  14. string SourceUpdatedAt)
  15. {
  16. public string BusinessKey => $"{PoNo}#{PoLine}";
  17. }
  18. public sealed record PurchaseHandoffPackage(
  19. Guid BatchId,
  20. string LeaseToken,
  21. [property: JsonNumberHandling(JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString)] long TenantId,
  22. [property: JsonNumberHandling(JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString)] long FactoryId,
  23. long SourceId,
  24. long DatasetId,
  25. string DatasetKey,
  26. string SourceCode,
  27. string PackageVersion,
  28. string SourceSchemaVersion,
  29. string MappingVersion,
  30. IReadOnlyList<PurchaseHandoffLine> Items)
  31. {
  32. public const string Version = "1.0.0";
  33. private static readonly JsonSerializerOptions DigestJson = new(JsonSerializerDefaults.Web);
  34. public string PackageCode => "MDP-PO-001";
  35. public string ObjectCode => "PURCHASE_ORDER";
  36. public string ComputeDigest()
  37. {
  38. var bytes = JsonSerializer.SerializeToUtf8Bytes(this with { LeaseToken = string.Empty }, DigestJson);
  39. return PurchaseOrderSchemaRegistry.ComputeSha256(bytes);
  40. }
  41. }
  42. public sealed record PurchaseQuarantine(Guid RawEventId, string Field, string Reason);
  43. public sealed record PurchaseHandoffReceipt(Guid BatchId, int AcceptedCount, IReadOnlyList<PurchaseQuarantine> Conflicts)
  44. {
  45. public bool Matches(Guid expectedBatch, IReadOnlySet<Guid> events)
  46. {
  47. if (BatchId != expectedBatch || Conflicts is null || AcceptedCount < 0 || AcceptedCount != events.Count - Conflicts.Count)
  48. return false;
  49. var seen = new HashSet<Guid>();
  50. return Conflicts.All(conflict => conflict is not null && events.Contains(conflict.RawEventId) && seen.Add(conflict.RawEventId));
  51. }
  52. }
  53. public sealed record PurchaseBatchProcessingResult(PurchaseHandoffPackage Package, IReadOnlyList<PurchaseQuarantine> Quarantines);
  54. public interface IAidopPackageHandoff
  55. {
  56. Task<PurchaseHandoffReceipt?> FindReceiptAsync(Guid batch, string digest, CancellationToken cancellationToken);
  57. Task<PurchaseHandoffReceipt> SubmitAsync(PurchaseHandoffPackage package, CancellationToken cancellationToken);
  58. }