MainPageViewModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. namespace UpdateDataTool
  8. {
  9. class MainPageViewModel : INotifyPropertyChanged, IDisposable
  10. {
  11. #region realize interface
  12. public event PropertyChangedEventHandler PropertyChanged;
  13. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  14. {
  15. PropertyChangedEventHandler handler = PropertyChanged;
  16. if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  17. }
  18. #endregion
  19. private string _originalDbHost;
  20. private string _newDbHost;
  21. private bool _isBusy;
  22. private DateTime _startTime;
  23. private DateTime _endTime;
  24. private DbCopyHelper _helper;
  25. private string _innerPort;
  26. private string _diskPath;
  27. private MainDispatcher _mainDispatcher;
  28. private bool _isIngnoreTime;
  29. public string OriginaDbHost
  30. {
  31. get => _originalDbHost;
  32. set
  33. {
  34. if (_originalDbHost != value)
  35. {
  36. _originalDbHost = value;
  37. OnPropertyChanged();
  38. }
  39. }
  40. }
  41. public string NewDbHost
  42. {
  43. get => _newDbHost;
  44. set
  45. {
  46. if (_newDbHost != value)
  47. {
  48. _newDbHost = value;
  49. OnPropertyChanged();
  50. }
  51. }
  52. }
  53. public bool IsBusy
  54. {
  55. get => _isBusy;
  56. set
  57. {
  58. if (_isBusy != value)
  59. {
  60. _isBusy = value;
  61. OnPropertyChanged();
  62. }
  63. }
  64. }
  65. public DateTime StartTime
  66. {
  67. get => _startTime;
  68. set
  69. {
  70. if (_startTime != value)
  71. {
  72. _startTime = value;
  73. OnPropertyChanged();
  74. }
  75. }
  76. }
  77. public DateTime EndTime
  78. {
  79. get => _endTime;
  80. set
  81. {
  82. if (_endTime != value)
  83. {
  84. _endTime = value;
  85. OnPropertyChanged();
  86. }
  87. }
  88. }
  89. public string InnerPort
  90. {
  91. get => _innerPort;
  92. set
  93. {
  94. if (_innerPort != value)
  95. {
  96. _innerPort = value;
  97. OnPropertyChanged();
  98. OnInnerPortChanged();
  99. }
  100. }
  101. }
  102. public string DiskPath
  103. {
  104. get => _diskPath;
  105. set
  106. {
  107. if (_diskPath != value)
  108. {
  109. _diskPath = value;
  110. OnPropertyChanged();
  111. OnDiskPathChanged();
  112. }
  113. }
  114. }
  115. public bool IsIngnoreTime
  116. {
  117. get => _isIngnoreTime;
  118. set
  119. {
  120. if (_isIngnoreTime != value)
  121. {
  122. _isIngnoreTime = value;
  123. SetIngnoreValue();
  124. OnPropertyChanged();
  125. }
  126. }
  127. }
  128. public ICommand CopyData { get; }
  129. public ICommand ConnectDb { get; }
  130. public ObservableCollection<string> LogMessages { get; }
  131. public MainPageViewModel(MainDispatcher mainDispatcher)
  132. {
  133. _mainDispatcher = mainDispatcher;
  134. IsIngnoreTime = true;
  135. _helper = new DbCopyHelper(IsIngnoreTime);
  136. _helper.OutMessageEvent += OnOutMessageEvent;
  137. OriginaDbHost = "127.0.0.1:8100";
  138. NewDbHost = "127.0.0.1:27017";
  139. StartTime = DateTime.Now;
  140. EndTime = DateTime.Now;
  141. LogMessages = new ObservableCollection<string>();
  142. CopyData = new RelayCommand(OnCopyData);
  143. InnerPort = "8097";
  144. DiskPath = @"c:\storage";
  145. ConnectDb = new RelayCommand(OnConnectDb);
  146. }
  147. public void Dispose()
  148. {
  149. _helper.OutMessageEvent -= OnOutMessageEvent;
  150. }
  151. private void SetIngnoreValue()
  152. {
  153. _helper?.SetIgnore(_isIngnoreTime);
  154. }
  155. private void OnInnerPortChanged()
  156. {
  157. _helper.ChangeInnerPort(_innerPort);
  158. }
  159. private void OnConnectDb()
  160. {
  161. IsBusy = true;
  162. Task.Run(() =>
  163. {
  164. _helper.Init(OriginaDbHost, NewDbHost);
  165. _mainDispatcher.BeginInvoke(() => IsBusy = false);
  166. });
  167. }
  168. private void OnDiskPathChanged()
  169. {
  170. _helper.ChangeDirectory(_diskPath);
  171. }
  172. private void OnCopyData()
  173. {
  174. IsBusy = true;
  175. Task.Run(() =>
  176. {
  177. try
  178. {
  179. _helper.SetTime(_startTime, _endTime);
  180. _helper.ImportAdmins();
  181. _helper.ImportOrganizations();
  182. _helper.ImportTerminals();
  183. _helper.ImportUsers();
  184. _helper.ImportRemarks();
  185. _helper.ImportFriendShips();
  186. _helper.ImportConversations();
  187. _helper.ImportChatMessages();
  188. _helper.ImportDiagnosisPackages();
  189. _helper.ImportDiagnosisItems();
  190. _helper.ImportTerminalRecords();
  191. _helper.ImportWorkOrders();
  192. _helper.ImportPatients();
  193. _helper.ImportPatientInfos();
  194. _helper.ImportDiagnosisResults();
  195. _helper.ImportFinishedTerminalRecords();
  196. _helper.CopyFlie();
  197. _helper.SaveImportTime();
  198. _mainDispatcher.BeginInvoke(() => IsBusy = false);
  199. }
  200. catch (Exception exception)
  201. {
  202. _mainDispatcher.BeginInvoke(() => IsBusy = false);
  203. OnOutMessageEvent(this, exception.Message);
  204. }
  205. });
  206. }
  207. private void OnOutMessageEvent(object sender, string message)
  208. {
  209. _mainDispatcher.BeginInvoke(() => LogMessages.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") + message));
  210. }
  211. }
  212. }