AidopTenantMigration.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Diagnostics;
  2. using Admin.NET.Core;
  3. using SqlSugar;
  4. namespace Admin.NET.Plugin.AiDOP.Infrastructure;
  5. /// <summary>
  6. /// 一次性迁移:将历史 TenantId=1 的 AiDOP 业务数据更新为框架默认租户。
  7. /// 幂等——若已无 TenantId=1 的行则不做任何事。
  8. /// </summary>
  9. public static class AidopTenantMigration
  10. {
  11. private static readonly (string Table, string TenantColumn)[] Tables =
  12. {
  13. ("ado_smart_ops_kpi_master", "TenantId"),
  14. ("ado_smart_ops_layout_item", "TenantId"),
  15. ("ado_smart_ops_home_module", "TenantId"),
  16. ("ado_s9_kpi_value_l1_day", "tenant_id"),
  17. ("ado_s9_kpi_value_l2_day", "tenant_id"),
  18. ("ado_s9_kpi_value_l3_day", "tenant_id"),
  19. ("ado_s9_kpi_value_l4_day", "tenant_id")
  20. };
  21. public static void MigrateOldTenantId(ISqlSugarClient db)
  22. {
  23. EnsureKpiValueL4Table(db);
  24. var target = SqlSugarConst.DefaultTenantId;
  25. if (target == 1) return;
  26. foreach (var (table, tenantColumn) in Tables)
  27. {
  28. try
  29. {
  30. db.Ado.ExecuteCommand(
  31. $"UPDATE `{table}` SET `{tenantColumn}` = @target WHERE `{tenantColumn}` = 1",
  32. new { target });
  33. }
  34. catch (Exception ex)
  35. {
  36. Trace.TraceWarning($"AidopTenantMigration [{table}]: {ex.Message}");
  37. }
  38. }
  39. DropOldUniqueIndex(db);
  40. BackfillThresholds(db);
  41. }
  42. public static void EnsureKpiValueL4Table(ISqlSugarClient db)
  43. {
  44. try
  45. {
  46. db.Ado.ExecuteCommand(
  47. "CREATE TABLE IF NOT EXISTS `ado_s9_kpi_value_l4_day` LIKE `ado_s9_kpi_value_l3_day`");
  48. }
  49. catch (Exception ex)
  50. {
  51. Trace.TraceWarning($"AidopTenantMigration ensure L4 value table: {ex.Message}");
  52. }
  53. }
  54. private static void BackfillThresholds(ISqlSugarClient db)
  55. {
  56. try
  57. {
  58. db.Ado.ExecuteCommand(
  59. "UPDATE ado_smart_ops_kpi_master SET YellowThreshold=95, RedThreshold=80 " +
  60. "WHERE YellowThreshold IS NULL AND Direction='higher_is_better'");
  61. db.Ado.ExecuteCommand(
  62. "UPDATE ado_smart_ops_kpi_master SET YellowThreshold=110, RedThreshold=120 " +
  63. "WHERE YellowThreshold IS NULL AND Direction='lower_is_better'");
  64. }
  65. catch (Exception ex)
  66. {
  67. Trace.TraceWarning($"AidopTenantMigration backfill thresholds: {ex.Message}");
  68. }
  69. }
  70. private static void DropOldUniqueIndex(ISqlSugarClient db)
  71. {
  72. try
  73. {
  74. var rows = db.Ado.SqlQuery<dynamic>(
  75. "SHOW INDEX FROM ado_smart_ops_kpi_master WHERE Key_name = 'uk_kpi_master_code'");
  76. if (rows != null && rows.Count > 0)
  77. db.Ado.ExecuteCommand("ALTER TABLE ado_smart_ops_kpi_master DROP INDEX uk_kpi_master_code");
  78. }
  79. catch (Exception ex)
  80. {
  81. Trace.TraceWarning($"AidopTenantMigration drop old index: {ex.Message}");
  82. }
  83. }
  84. }