123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace UpdateDataTool
- {
- class MainPageViewModel : INotifyPropertyChanged, IDisposable
- {
- #region realize interface
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
- }
- #endregion
- private string _originalDbHost;
- private string _newDbHost;
- private bool _isBusy;
- private DateTime _startTime;
- private DateTime _endTime;
- private DbCopyHelper _helper;
- private string _innerPort;
- private string _diskPath;
- private MainDispatcher _mainDispatcher;
- private bool _isIngnoreTime;
- public string OriginaDbHost
- {
- get => _originalDbHost;
- set
- {
- if (_originalDbHost != value)
- {
- _originalDbHost = value;
- OnPropertyChanged();
- }
- }
- }
- public string NewDbHost
- {
- get => _newDbHost;
- set
- {
- if (_newDbHost != value)
- {
- _newDbHost = value;
- OnPropertyChanged();
- }
- }
- }
- public bool IsBusy
- {
- get => _isBusy;
- set
- {
- if (_isBusy != value)
- {
- _isBusy = value;
- OnPropertyChanged();
- }
- }
- }
- public DateTime StartTime
- {
- get => _startTime;
- set
- {
- if (_startTime != value)
- {
- _startTime = value;
- OnPropertyChanged();
- }
- }
- }
- public DateTime EndTime
- {
- get => _endTime;
- set
- {
- if (_endTime != value)
- {
- _endTime = value;
- OnPropertyChanged();
- }
- }
- }
- public string InnerPort
- {
- get => _innerPort;
- set
- {
- if (_innerPort != value)
- {
- _innerPort = value;
- OnPropertyChanged();
- OnInnerPortChanged();
- }
- }
- }
- public string DiskPath
- {
- get => _diskPath;
- set
- {
- if (_diskPath != value)
- {
- _diskPath = value;
- OnPropertyChanged();
- OnDiskPathChanged();
- }
- }
- }
- public bool IsIngnoreTime
- {
- get => _isIngnoreTime;
- set
- {
- if (_isIngnoreTime != value)
- {
- _isIngnoreTime = value;
- SetIngnoreValue();
- OnPropertyChanged();
- }
- }
- }
- public ICommand CopyData { get; }
- public ICommand ConnectDb { get; }
- public ObservableCollection<string> LogMessages { get; }
- public MainPageViewModel(MainDispatcher mainDispatcher)
- {
- _mainDispatcher = mainDispatcher;
- IsIngnoreTime = true;
- _helper = new DbCopyHelper(IsIngnoreTime);
- _helper.OutMessageEvent += OnOutMessageEvent;
- OriginaDbHost = "127.0.0.1:8100";
- NewDbHost = "127.0.0.1:27017";
- StartTime = DateTime.Now;
- EndTime = DateTime.Now;
- LogMessages = new ObservableCollection<string>();
- CopyData = new RelayCommand(OnCopyData);
- InnerPort = "8097";
- DiskPath = @"c:\storage";
- ConnectDb = new RelayCommand(OnConnectDb);
- }
- public void Dispose()
- {
- _helper.OutMessageEvent -= OnOutMessageEvent;
- }
- private void SetIngnoreValue()
- {
- _helper?.SetIgnore(_isIngnoreTime);
- }
- private void OnInnerPortChanged()
- {
- _helper.ChangeInnerPort(_innerPort);
- }
- private void OnConnectDb()
- {
- IsBusy = true;
- Task.Run(() =>
- {
- _helper.Init(OriginaDbHost, NewDbHost);
- _mainDispatcher.BeginInvoke(() => IsBusy = false);
- });
- }
- private void OnDiskPathChanged()
- {
- _helper.ChangeDirectory(_diskPath);
- }
- private void OnCopyData()
- {
- IsBusy = true;
- Task.Run(() =>
- {
- try
- {
- _helper.SetTime(_startTime, _endTime);
- _helper.ImportAdmins();
- _helper.ImportOrganizations();
- _helper.ImportTerminals();
- _helper.ImportUsers();
- _helper.ImportRemarks();
- _helper.ImportFriendShips();
- _helper.ImportConversations();
- _helper.ImportChatMessages();
- _helper.ImportDiagnosisPackages();
- _helper.ImportDiagnosisItems();
- _helper.ImportTerminalRecords();
- _helper.ImportWorkOrders();
- _helper.ImportPatients();
- _helper.ImportPatientInfos();
- _helper.ImportDiagnosisResults();
- _helper.ImportFinishedTerminalRecords();
- _helper.CopyFlie();
- _helper.SaveImportTime();
- _mainDispatcher.BeginInvoke(() => IsBusy = false);
- }
- catch (Exception exception)
- {
- _mainDispatcher.BeginInvoke(() => IsBusy = false);
- OnOutMessageEvent(this, exception.Message);
- }
- });
- }
- private void OnOutMessageEvent(object sender, string message)
- {
- _mainDispatcher.BeginInvoke(() => LogMessages.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") + message));
- }
- }
- }
|