| 1 | using Core.Librarys; |
| 2 | using Core.Models.Config; |
| 3 | using Core.Models.Config.Link; |
| 4 | using Core.Servicers.Instances; |
| 5 | using Core.Servicers.Interfaces; |
| 6 | using System; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Diagnostics; |
| 9 | using System.IO; |
| 10 | using System.Linq; |
| 11 | using System.Reflection; |
| 12 | using System.Text; |
| 13 | using System.Threading.Tasks; |
| 14 | using System.Windows.Forms; |
| 15 | using UI.Controls; |
| 16 | using UI.Models; |
| 17 | using UI.Servicers; |
| 18 | |
| 19 | namespace UI.ViewModels |
| 20 | { |
| 21 | public class SettingPageVM : SettingPageModel |
| 22 | { |
| 23 | private ConfigModel config; |
| 24 | private readonly IAppConfig appConfig; |
| 25 | private readonly MainViewModel mainVM; |
| 26 | private readonly IData data; |
| 27 | private readonly IWebData _webData; |
| 28 | private readonly IUIServicer _uiServicer; |
| 29 | public Command OpenURL { get; set; } |
| 30 | public Command CheckUpdate { get; set; } |
| 31 | public Command DelDataCommand { get; set; } |
| 32 | public Command ExportDataCommand { get; set; } |
| 33 | |
| 34 | public SettingPageVM(IAppConfig appConfig, MainViewModel mainVM, IData data, IWebData webData, IUIServicer uiServicer_) |
| 35 | { |
| 36 | this.appConfig = appConfig; |
| 37 | this.mainVM = mainVM; |
| 38 | this.data = data; |
| 39 | _webData = webData; |
| 40 | _uiServicer = uiServicer_; |
| 41 | |
| 42 | OpenURL = new Command(new Action<object>(OnOpenURL)); |
| 43 | CheckUpdate = new Command(new Action<object>(OnCheckUpdate)); |
| 44 | DelDataCommand = new Command(new Action<object>(OnDelData)); |
| 45 | ExportDataCommand = new Command(new Action<object>(OnExportData)); |
| 46 | |
| 47 | Init(); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | private void OnCheckUpdate(object obj) |
| 53 | { |
| 54 | try |
| 55 | { |
| 56 | CheckUpdateBtnVisibility = System.Windows.Visibility.Collapsed; |
| 57 | string updaterExePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, |
| 58 | "Updater.exe"); |
| 59 | string updaterCacheExePath = Path.Combine( |
| 60 | AppDomain.CurrentDomain.BaseDirectory, |
| 61 | "Update", |
| 62 | "Updater.exe"); |
| 63 | string updateDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update"); |
| 64 | if (!Directory.Exists(updateDirPath)) |
| 65 | { |
| 66 | Directory.CreateDirectory(updateDirPath); |
| 67 | } |
| 68 | |
| 69 | if (!File.Exists(updaterExePath)) |
| 70 | { |
| 71 | mainVM.Toast("升级程序似乎已被删除,请手动前往发布页查看新版本", Controls.Window.ToastType.Error, Controls.Base.IconTypes.None); |
| 72 | return; |
| 73 | } |
| 74 | File.Copy(updaterExePath, updaterCacheExePath, true); |
| 75 | |
| 76 | File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Newtonsoft.Json.dll"), Path.Combine( |
| 77 | AppDomain.CurrentDomain.BaseDirectory, |
| 78 | "Update", |
| 79 | "Newtonsoft.Json.dll"), true); |
| 80 | |
| 81 | ProcessHelper.Run(updaterCacheExePath, new string[] { Version }); |
| 82 | } |
| 83 | catch (Exception ex) |
| 84 | { |
| 85 | CheckUpdateBtnVisibility = System.Windows.Visibility.Visible; |
| 86 | |
| 87 | Logger.Error(ex.Message); |
| 88 | mainVM.Toast("无法正确启动检查更新程序", Controls.Window.ToastType.Error, Controls.Base.IconTypes.None); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private void OnOpenURL(object obj) |
| 93 | { |
| 94 | Process.Start(new ProcessStartInfo(obj.ToString())); |
| 95 | } |
| 96 | |
| 97 | private void Init() |
| 98 | { |
| 99 | config = appConfig.GetConfig(); |
| 100 | |
| 101 | Data = config.General; |
| 102 | |
| 103 | TabbarData = new System.Collections.ObjectModel.ObservableCollection<string>() |
| 104 | { |
| 105 | "常规","关联","行为","数据","关于" |
| 106 | }; |
| 107 | |
| 108 | PropertyChanged += SettingPageVM_PropertyChanged; |
| 109 | |
| 110 | Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); |
| 111 | |
| 112 | DelDataStartMonthDate = DateTime.Now; |
| 113 | DelDataEndMonthDate = DateTime.Now; |
| 114 | |
| 115 | ExportDataStartMonthDate = DateTime.Now; |
| 116 | ExportDataEndMonthDate = DateTime.Now; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | private void SettingPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 122 | { |
| 123 | if (e.PropertyName == nameof(Data)) |
| 124 | { |
| 125 | if (TabbarSelectedIndex == 0) |
| 126 | { |
| 127 | config.General = Data as GeneralModel; |
| 128 | } |
| 129 | else if (TabbarSelectedIndex == 1) |
| 130 | { |
| 131 | if (Data != null) |
| 132 | { |
| 133 | var newData = new List<LinkModel>(); |
| 134 | foreach (var item in Data as IEnumerable<object>) |
| 135 | { |
| 136 | newData.Add(item as LinkModel); |
| 137 | Debug.WriteLine(item.ToString()); |
| 138 | } |
| 139 | config.Links = newData; |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | else if (TabbarSelectedIndex == 2) |
| 144 | { |
| 145 | config.Behavior = Data as BehaviorModel; |
| 146 | } |
| 147 | |
| 148 | appConfig.Save(); |
| 149 | } |
| 150 | |
| 151 | if (e.PropertyName == nameof(TabbarSelectedIndex)) |
| 152 | { |
| 153 | if (TabbarSelectedIndex == 0) |
| 154 | { |
| 155 | |
| 156 | Data = config.General; |
| 157 | } |
| 158 | else if (TabbarSelectedIndex == 1) |
| 159 | { |
| 160 | |
| 161 | Data = config.Links; |
| 162 | } |
| 163 | else if (TabbarSelectedIndex == 2) |
| 164 | { |
| 165 | |
| 166 | Data = config.Behavior; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private async void OnDelData(object obj) |
| 172 | { |
| 173 | if (DelDataStartMonthDate > DelDataEndMonthDate) |
| 174 | { |
| 175 | mainVM.Toast("时间范围选择错误", Controls.Window.ToastType.Error, Controls.Base.IconTypes.IncidentTriangle); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | bool isConfirm = await _uiServicer.ShowConfirmDialogAsync("删除确认", "是否执行此操作?"); |
| 180 | if (isConfirm) |
| 181 | { |
| 182 | data.ClearRange(DelDataStartMonthDate, DelDataEndMonthDate); |
| 183 | _webData.Clear(DelDataStartMonthDate, DelDataEndMonthDate); |
| 184 | mainVM.Toast("操作已完成", Controls.Window.ToastType.Success); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void OnExportData(object obj) |
| 189 | { |
| 190 | try |
| 191 | { |
| 192 | FolderBrowserDialog dialog = new FolderBrowserDialog(); |
| 193 | dialog.Description = "请选择导出位置"; |
| 194 | if (dialog.ShowDialog() == DialogResult.OK) |
| 195 | { |
| 196 | data.ExportToExcel(dialog.SelectedPath, ExportDataStartMonthDate, ExportDataEndMonthDate); |
| 197 | _webData.Export(dialog.SelectedPath, ExportDataStartMonthDate, ExportDataEndMonthDate); |
| 198 | mainVM.Toast("导出数据完成", Controls.Window.ToastType.Success); |
| 199 | } |
| 200 | } |
| 201 | catch (Exception ec) |
| 202 | { |
| 203 | Logger.Error(ec.ToString()); |
| 204 | mainVM.Toast("导出数据失败", Controls.Window.ToastType.Error, Controls.Base.IconTypes.IncidentTriangle); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |