using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace DataRecovery { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { Thread DataRecoveryThread; public MainWindow() { InitializeComponent(); DataTable dataTable = new DataTable(); dataTable.Columns.Add("TABLENAME"); dataTable.Columns.Add("OPERATIONRESULT"); gridDataResult.AutoGenerateColumns = false; gridDataResult.SelectionMode = DataGridSelectionMode.Single; gridDataResult.ItemsSource = dataTable.DefaultView; downProgress.Visibility = Visibility.Collapsed; txtMessage.Text = "点击【开始恢复】或按【F1】开始进行营收数据恢复。\n数据恢复将清除数据库已有数据,请谨慎操作!"; } private void Button_Click(object sender, RoutedEventArgs e) { Button _Button = sender as Button; switch (_Button.Name) { case "btnReturn": Environment.Exit(0); break; case "btnStart": try { if (DataRecoveryThread == null || !DataRecoveryThread.IsAlive) { if (gridDataResult.ItemsSource is DataView dataView) { dataView.Table.Clear(); } DataRecoveryThread = new Thread(() => DataRecovery(AppDomain.CurrentDomain.BaseDirectory + "\\DataBackup", new ESSupport.Pos.DataRecovery.ProcessChange(MessageBeginInvoke))) { IsBackground = true }; DataRecoveryThread.Start(); txtMessage.Text = "正在还原数据,请稍候......"; downProgress.IsIndeterminate = true; downProgress.Visibility = Visibility.Visible; btnStart.IsEnabled = false; } } catch (Exception ex) { txtMessage.Text = "数据恢复异常:" + ex.Message; btnStart.IsEnabled = true; btnStart.Content = "开始恢复[F1]"; } break; } } /// /// 列表数据添加行 /// /// 表名 /// 返回的结果 private void TableAddRows(string TableName, string OperationResult) { if (gridDataResult.ItemsSource is DataView dataView) { DataTable dataTable = dataView.Table; DataRow dataRow = dataTable.NewRow(); dataRow["TABLENAME"] = TableName; dataRow["OPERATIONRESULT"] = OperationResult; dataTable.Rows.InsertAt(dataRow, 0); } } private void MessageBeginInvoke(ESSupport.Pos.DataRecovery.RecoveryResult recoveryResult) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { if (recoveryResult.RecEndState) { txtMessage.Text = recoveryResult.RecMessage; downProgress.Visibility = Visibility.Collapsed; btnStart.IsEnabled = true; btnStart.Content = "开始恢复[F1]"; } else { if (recoveryResult.RecTableState) { TableAddRows(recoveryResult.RecTableName, recoveryResult.RecMessage + "[" + recoveryResult.RecEndCount + "/" + recoveryResult.RecTotalCount + "]"); } else { txtMessage.Text = recoveryResult.RecMessage + (recoveryResult.RecTotalCount > 0 ? ("[" + recoveryResult.RecEndCount + "/" + recoveryResult.RecTotalCount + "]") : ""); } } }); } private void SCManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e) { e.Handled = true; } #region 备份数据类型 /// /// 备份数据类型(表) /// public enum DataBackupType { T_ENDACCOUNT = 1000, T_ENDACCOUNT_NEW = 1005, T_PERSONSELL = 2000, T_PERSONSELL_NEW = 2005, T_COMMODITYSALE = 3000, T_COMMODITYSALE_NEW = 3005, T_COMMODITYSALE_EXTAR = 3010, T_TRANSFER_SALE = 3015, T_MOBILE_PAY = 4000, T_MOBILE_PAYCHECK = 4005, T_SELLDATA_PAY = 4010, T_SELLDATA = 5000, T_SELLMASTER = 5005, T_SELLDETAILS = 5010, T_TRANSFER_SELLDATA = 5015 } #endregion #region 方法 -> 数据还原 /// /// 数据还原 /// /// 备份文件根目录路径 /// 进度报告 /// private bool DataRecovery(string backupDir, ESSupport.Pos.DataRecovery.ProcessChange processChange) { try { if (string.IsNullOrWhiteSpace(backupDir)) { backupDir = AppDomain.CurrentDomain.BaseDirectory + "\\DataBackup"; } DirectoryInfo[] _DataBackupList = new DirectoryInfo(backupDir).GetDirectories(); if (_DataBackupList != null && _DataBackupList.Count() > 0) { foreach (var _DataDir in _DataBackupList) { DirectoryInfo[] _DataDirList = new DirectoryInfo(_DataDir.FullName).GetDirectories(); if (_DataDirList != null && _DataDirList.Count() > 0) { foreach (var _DataFileDir in _DataDirList) { if (Enum.TryParse(_DataFileDir.Name, out DataBackupType dataBackupType)) { switch (dataBackupType) { case DataBackupType.T_ENDACCOUNT: ESSupport.Pos.DataRecovery.EndAccountRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_ENDACCOUNT_NEW: ESSupport.Pos.DataRecovery.EndAccountNewRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_PERSONSELL: ESSupport.Pos.DataRecovery.PersonSellRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_PERSONSELL_NEW: ESSupport.Pos.DataRecovery.PersonSellNewRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_COMMODITYSALE: ESSupport.Pos.DataRecovery.CommoditySaleRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_COMMODITYSALE_NEW: ESSupport.Pos.DataRecovery.CommoditySaleNewRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_COMMODITYSALE_EXTAR: break; case DataBackupType.T_TRANSFER_SALE: ESSupport.Pos.DataRecovery.TransferSaleRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_MOBILE_PAY: break; case DataBackupType.T_MOBILE_PAYCHECK: break; case DataBackupType.T_SELLDATA_PAY: break; case DataBackupType.T_SELLDATA: ESSupport.Pos.DataRecovery.SellDataRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_SELLMASTER: ESSupport.Pos.DataRecovery.SellMasterRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_SELLDETAILS: ESSupport.Pos.DataRecovery.SellDetailsRecovery(_DataFileDir.FullName, processChange); break; case DataBackupType.T_TRANSFER_SELLDATA: ESSupport.Pos.DataRecovery.TransferSellDataRecovery(_DataFileDir.FullName, processChange); break; } } } } } } processChange.BeginInvoke(new ESSupport.Pos.DataRecovery.RecoveryResult { RecEndState = true, RecMessage = "数据还原已完成,按【ESC】返回" }, null, null); return true; } catch (Exception ex) { processChange.BeginInvoke(new ESSupport.Pos.DataRecovery.RecoveryResult { RecEndState = true, RecMessage = "数据还原失败,请重试。\n" + ex.Message }, null, null); return false; } } #endregion } }