RestoreViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using DBBackupTool.Data;
  4. using DBBackupTool.Helpers;
  5. using Microsoft.Win32;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. namespace DBBackupTool.ViewModels;
  17. public partial class RestoreViewModel : ObservableObject
  18. {
  19. public RestoreViewModel()
  20. {
  21. this.DbInfo = new()
  22. {
  23. ServerAddress = "localhost",
  24. Port = 5432,
  25. User = "postgres",
  26. PassWord = "123456"
  27. };
  28. this.LoadingVis = Visibility.Collapsed;
  29. }
  30. private SqlSugarHelper? _dbHelper;
  31. [ObservableProperty]
  32. private Visibility _LoadingVis;
  33. [RelayCommand]
  34. private void CheckDB()
  35. {
  36. this.Tables ??= [];
  37. this.Tables.Clear();
  38. this.TableCount = 0;
  39. if (!CheckDBInfo())
  40. {
  41. MessageBox.Show("数据库信息不完整", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  42. return;
  43. }
  44. _dbHelper = new();
  45. string dbString = $"Database={DbInfo.DataBase};Password={DbInfo.PassWord};Host={DbInfo.ServerAddress};Username={DbInfo.User};Persist Security Info=True";
  46. if (!_dbHelper.Open(dbString, SqlSugar.DbType.PostgreSQL, true) || _dbHelper.Client is null)
  47. {
  48. MessageBox.Show("数据库连接失败", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  49. return;
  50. }
  51. _dbHelper.CreateTable(this.DbInfo.DataBase!);
  52. MessageBox.Show("数据库连接成功", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  53. }
  54. [ObservableProperty]
  55. private DBInfo _dbInfo;
  56. [ObservableProperty]
  57. private ObservableCollection<DbTableInfo> _Tables = [];
  58. [ObservableProperty]
  59. private int _TableCount;
  60. [ObservableProperty]
  61. private string? _SavePath;
  62. [RelayCommand]
  63. private void Restore()
  64. {
  65. this.Tables ??= [];
  66. this.Tables.Clear();
  67. if (!CheckDBInfo())
  68. {
  69. MessageBox.Show("数据库信息不完整", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  70. return;
  71. }
  72. if (string.IsNullOrEmpty(this.SavePath))
  73. {
  74. MessageBox.Show("选择保存路径", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  75. return;
  76. }
  77. if (_dbHelper is null || _dbHelper.Client is null)
  78. {
  79. MessageBox.Show("数据库未连接", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  80. return;
  81. }
  82. PostgreSQLTool tool = new();
  83. if (!tool.Initialize())
  84. {
  85. MessageBox.Show("PostgresSQL 未安装", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  86. return;
  87. }
  88. this.LoadingVis = Visibility.Visible;
  89. Task.Factory.StartNew(() =>
  90. {
  91. Stopwatch sw = new();
  92. sw.Start();
  93. tool.Restore(this.DbInfo.ServerAddress!, this.DbInfo.Port, this.DbInfo.User!, this.DbInfo.PassWord!, this.SavePath, this.DbInfo.DataBase!);
  94. if (_dbHelper.Client.DbMaintenance.GetTableInfoList() is not IEnumerable<DbTableInfo> tables)
  95. {
  96. MessageBox.Show("数据库不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  97. return;
  98. }
  99. App.Current.Dispatcher.Invoke(() =>
  100. {
  101. this.Tables ??= [];
  102. this.Tables.Clear();
  103. foreach (DbTableInfo item in tables)
  104. this.Tables.Add(item);
  105. this.TableCount = this.Tables.Count;
  106. this.LoadingVis = Visibility.Collapsed;
  107. });
  108. sw.Stop();
  109. MessageBox.Show($"还原完成,耗时 {sw.Elapsed.Seconds}s", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  110. });
  111. }
  112. [RelayCommand]
  113. private void SelectPath()
  114. {
  115. OpenFileDialog dialog = new()
  116. {
  117. Filter = "DumpFile|*.Custom;",
  118. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  119. };
  120. if (dialog.ShowDialog() != true)
  121. return;
  122. SavePath = dialog.FileName; //获取选择的文件,或者自定义的文件名的全路径。
  123. }
  124. private bool CheckDBInfo()
  125. {
  126. if (this.DbInfo is null)
  127. return false;
  128. if (string.IsNullOrEmpty(DbInfo.ServerAddress))
  129. return false;
  130. if (string.IsNullOrEmpty(DbInfo.DataBase))
  131. return false;
  132. if (string.IsNullOrEmpty(DbInfo.User))
  133. return false;
  134. if (string.IsNullOrEmpty(DbInfo.PassWord))
  135. return false;
  136. return true;
  137. }
  138. }