MdpPurchaseMigrationStore.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Microsoft.AspNetCore.Http;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  3. internal sealed class PurchaseMigrationScope
  4. {
  5. public long Id { get; set; }
  6. public long TenantId { get; set; }
  7. public long FactoryId { get; set; }
  8. public string LegacySourceCode { get; set; } = string.Empty;
  9. public string NewSourceCode { get; set; } = string.Empty;
  10. public string LogicalSourceCode { get; set; } = string.Empty;
  11. public long NewSourceId { get; set; }
  12. public long DatasetId { get; set; }
  13. public string PrimaryWriter { get; set; } = string.Empty;
  14. public bool Frozen { get; set; }
  15. public long Revision { get; set; }
  16. }
  17. internal static class MdpPurchaseMigrationStore
  18. {
  19. internal const string Legacy = "LEGACY";
  20. internal const string Unified = "UNIFIED";
  21. private const string SelectScope = """
  22. SELECT id AS Id, tenant_id AS TenantId, factory_id AS FactoryId, legacy_source_code AS LegacySourceCode,
  23. new_source_id AS NewSourceId, new_source_code AS NewSourceCode, dataset_id AS DatasetId,
  24. logical_source_code AS LogicalSourceCode, primary_writer AS PrimaryWriter, frozen AS Frozen, revision AS Revision
  25. FROM mdp_purchase_migration_scope
  26. """;
  27. internal static async Task<PurchaseMigrationScope?> LockLegacyAsync(
  28. ISqlSugarClient db, long tenantId, long factoryId, string sourceCode)
  29. {
  30. if (!await HasScopeTableAsync(db))
  31. return null;
  32. if (factoryId <= 0)
  33. {
  34. var registered = await db.Ado.SqlQuerySingleAsync<long?>(
  35. """
  36. SELECT id FROM mdp_purchase_migration_scope WHERE tenant_id=@TenantId AND legacy_source_code=@SourceCode
  37. ORDER BY id LIMIT 1 FOR UPDATE
  38. """, new { TenantId = tenantId, SourceCode = sourceCode });
  39. if (registered.HasValue)
  40. throw Conflict("PURCHASE_EXPLICIT_FACTORY_REQUIRED");
  41. return null;
  42. }
  43. return await db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
  44. SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND legacy_source_code=@SourceCode FOR UPDATE",
  45. new { TenantId = tenantId, FactoryId = factoryId, SourceCode = sourceCode });
  46. }
  47. internal static async Task<PurchaseMigrationScope?> LockNewAsync(
  48. ISqlSugarClient db, long tenantId, long factoryId, long datasetId, long sourceId, bool confirmedDelivery = false)
  49. {
  50. if (!confirmedDelivery && !await HasScopeTableAsync(db))
  51. return null;
  52. return await db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
  53. SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND dataset_id=@DatasetId AND new_source_id=@SourceId " + (confirmedDelivery ? "FOR SHARE" : "FOR UPDATE"),
  54. new { TenantId = tenantId, FactoryId = factoryId, DatasetId = datasetId, SourceId = sourceId });
  55. }
  56. internal static async Task<bool> HasFactoryOrderKeyAsync(ISqlSugarClient db) =>
  57. await db.Ado.GetIntAsync(
  58. """
  59. SELECT COUNT(*) FROM information_schema.STATISTICS WHERE table_schema=DATABASE()
  60. AND table_name='mdp_std_purchase_order' AND index_name='uk_po_line'
  61. AND non_unique=0 AND seq_in_index=2 AND column_name='factory_id'
  62. """) == 1;
  63. internal static Task<PurchaseMigrationScope?> LockByIdAsync(ISqlSugarClient db, long tenantId, long factoryId, long id) =>
  64. db.Ado.SqlQuerySingleAsync<PurchaseMigrationScope>(
  65. SelectScope + " WHERE tenant_id=@TenantId AND factory_id=@FactoryId AND id=@Id FOR UPDATE",
  66. new { TenantId = tenantId, FactoryId = factoryId, Id = id });
  67. internal static void RequireAdmission(PurchaseMigrationScope? scope, string writer)
  68. {
  69. if (scope?.Frozen == true)
  70. throw Conflict("PURCHASE_MIGRATION_FROZEN");
  71. RequireWriter(scope, writer);
  72. }
  73. internal static void RequireWriter(PurchaseMigrationScope? scope, string writer)
  74. {
  75. if (scope is not null && scope.PrimaryWriter != writer)
  76. throw Conflict("PURCHASE_MIGRATION_WRITER_CHANGED");
  77. }
  78. internal static async Task<bool> HasScopeTableAsync(ISqlSugarClient db) =>
  79. await db.Ado.GetIntAsync(
  80. "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_purchase_migration_scope'") == 1;
  81. internal static MdpIngestHttpException Conflict(string code) => new(StatusCodes.Status409Conflict, code);
  82. }