using System.Text.Json; using System.Text.Json.Serialization; namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core; public sealed record PurchaseHandoffLine( Guid RawEventId, string PoNo, string PoLine, string ItemCode, decimal OrderQty, string? SupplierCode, DateTime? OrderDate, DateTime? DueDate, string? Status, string SourceUpdatedAt) { public string BusinessKey => $"{PoNo}#{PoLine}"; } public sealed record PurchaseHandoffPackage( Guid BatchId, string LeaseToken, [property: JsonNumberHandling(JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString)] long TenantId, [property: JsonNumberHandling(JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString)] long FactoryId, long SourceId, long DatasetId, string DatasetKey, string SourceCode, string PackageVersion, string SourceSchemaVersion, string MappingVersion, IReadOnlyList Items) { public const string Version = "1.0.0"; private static readonly JsonSerializerOptions DigestJson = new(JsonSerializerDefaults.Web); public string PackageCode => "MDP-PO-001"; public string ObjectCode => "PURCHASE_ORDER"; public string ComputeDigest() { var bytes = JsonSerializer.SerializeToUtf8Bytes(this with { LeaseToken = string.Empty }, DigestJson); return PurchaseOrderSchemaRegistry.ComputeSha256(bytes); } } public sealed record PurchaseQuarantine(Guid RawEventId, string Field, string Reason); public sealed record PurchaseHandoffReceipt(Guid BatchId, int AcceptedCount, IReadOnlyList Conflicts) { public bool Matches(Guid expectedBatch, IReadOnlySet events) { if (BatchId != expectedBatch || Conflicts is null || AcceptedCount < 0 || AcceptedCount != events.Count - Conflicts.Count) return false; var seen = new HashSet(); return Conflicts.All(conflict => conflict is not null && events.Contains(conflict.RawEventId) && seen.Add(conflict.RawEventId)); } } public sealed record PurchaseBatchProcessingResult(PurchaseHandoffPackage Package, IReadOnlyList Quarantines); public interface IAidopPackageHandoff { Task FindReceiptAsync(Guid batch, string digest, CancellationToken cancellationToken); Task SubmitAsync(PurchaseHandoffPackage package, CancellationToken cancellationToken); }