| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using Microsoft.AspNetCore.Http;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- internal sealed class PurchaseMigrationScope
- {
- public long Id { get; set; }
- public long TenantId { get; set; }
- public long FactoryId { get; set; }
- public string LegacySourceCode { get; set; } = string.Empty;
- public string NewSourceCode { get; set; } = string.Empty;
- public string LogicalSourceCode { get; set; } = string.Empty;
- public long NewSourceId { get; set; }
- public long DatasetId { get; set; }
- public string PrimaryWriter { get; set; } = string.Empty;
- public bool Frozen { get; set; }
- public long Revision { get; set; }
- }
- internal static class MdpPurchaseMigrationStore
- {
- internal const string Legacy = "LEGACY";
- internal const string Unified = "UNIFIED";
- private const string SelectScope = """
- SELECT id AS Id, tenant_id AS TenantId, factory_id AS FactoryId, legacy_source_code AS LegacySourceCode,
- new_source_id AS NewSourceId, new_source_code AS NewSourceCode, dataset_id AS DatasetId,
- logical_source_code AS LogicalSourceCode, primary_writer AS PrimaryWriter, frozen AS Frozen, revision AS Revision
- FROM mdp_purchase_migration_scope
- """;
- internal static async Task<PurchaseMigrationScope?> LockLegacyAsync(
- ISqlSugarClient db, long tenantId, long factoryId, string sourceCode)
- {
- if (!await HasScopeTableAsync(db))
- return null;
- if (factoryId <= 0)
- {
- var registered = await db.Ado.SqlQuerySingleAsync<long?>(
- """
- SELECT id FROM mdp_purchase_migration_scope WHERE tenant_id=@TenantId AND legacy_source_code=@SourceCode
- ORDER BY id LIMIT 1 FOR UPDATE
- """, new { TenantId = tenantId, SourceCode = sourceCode });
- if (registered.HasValue)
- throw Conflict("PURCHASE_EXPLICIT_FACTORY_REQUIRED");
- return null;
- }
- return await db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
- SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND legacy_source_code=@SourceCode FOR UPDATE",
- new { TenantId = tenantId, FactoryId = factoryId, SourceCode = sourceCode });
- }
- internal static async Task<PurchaseMigrationScope?> LockNewAsync(
- ISqlSugarClient db, long tenantId, long factoryId, long datasetId, long sourceId, bool confirmedDelivery = false)
- {
- if (!confirmedDelivery && !await HasScopeTableAsync(db))
- return null;
- return await db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
- SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND dataset_id=@DatasetId AND new_source_id=@SourceId " + (confirmedDelivery ? "FOR SHARE" : "FOR UPDATE"),
- new { TenantId = tenantId, FactoryId = factoryId, DatasetId = datasetId, SourceId = sourceId });
- }
- internal static async Task<bool> HasFactoryOrderKeyAsync(ISqlSugarClient db) =>
- await db.Ado.GetIntAsync(
- """
- SELECT COUNT(*) FROM information_schema.STATISTICS WHERE table_schema=DATABASE()
- AND table_name='mdp_std_purchase_order' AND index_name='uk_po_line'
- AND non_unique=0 AND seq_in_index=2 AND column_name='factory_id'
- """) == 1;
- internal static Task<PurchaseMigrationScope?> LockByIdAsync(ISqlSugarClient db, long tenantId, long factoryId, long id) =>
- db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
- SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND id=@Id FOR UPDATE",
- new { TenantId = tenantId, FactoryId = factoryId, Id = id });
- internal static void RequireAdmission(PurchaseMigrationScope? scope, string writer)
- {
- if (scope?.Frozen == true)
- throw Conflict("PURCHASE_MIGRATION_FROZEN");
- RequireWriter(scope, writer);
- }
- internal static void RequireWriter(PurchaseMigrationScope? scope, string writer)
- {
- if (scope is not null && scope.PrimaryWriter != writer)
- throw Conflict("PURCHASE_MIGRATION_WRITER_CHANGED");
- }
- internal static async Task<bool> HasScopeTableAsync(ISqlSugarClient db) =>
- await db.Ado.GetIntAsync(
- "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_purchase_migration_scope'") == 1;
- internal static MdpIngestHttpException Conflict(string code) => new(StatusCodes.Status409Conflict, code);
- }
|