public MineWorkViewModel(Guid id) { _id = id; this.Save = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } if (string.IsNullOrEmpty(this.Name)) { NotiCenterWindowViewModel.Current.Manager.ShowErrorMessage("作业名称是必须的"); } bool isMinerProfileChanged = false; IMineWork entity; if (NTMinerRoot.Current.MineWorkSet.TryGetMineWork(this.Id, out entity)) { string sha1 = NTMinerRoot.Current.MinerProfile.GetSha1(); if (this.Sha1 != sha1) { isMinerProfileChanged = true; } if (entity.Name != this.Name || entity.Description != this.Description) { VirtualRoot.Execute(new UpdateMineWorkCommand(this)); } CloseWindow?.Invoke(); } else { isMinerProfileChanged = true; VirtualRoot.Execute(new AddMineWorkCommand(this)); CloseWindow?.Invoke(); this.Edit.Execute(FormType.Edit); } if (isMinerProfileChanged) { Write.DevLine("检测到MinerProfile状态变更"); string localJson; string serverJson; NTMinerRoot.ExportWorkJson(new MineWorkData(this), out localJson, out serverJson); if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson)) { Server.ControlCenterService.ExportMineWorkAsync(this.Id, localJson, serverJson, callback: null); } } }); this.Edit = new DelegateCommand <FormType?>((formType) => { if (this.Id == Guid.Empty) { return; } MineWorkViewModel mineWorkVm; if (!MineWorkViewModels.Current.TryGetMineWorkVm(this.Id, out mineWorkVm)) { InputWindow.ShowDialog("作业名称", string.Empty, workName => { if (string.IsNullOrEmpty(workName)) { return("作业名称是必须的"); } return(string.Empty); }, onOk: workName => { new MineWorkViewModel(this) { Name = workName }.Save.Execute(null); }); } else { // 编辑作业前切换上下文 // 根据workId下载json保存到本地并调用LocalJson.Instance.ReInit() string json = Server.ControlCenterService.GetLocalJson(this.Id); if (!string.IsNullOrEmpty(json)) { File.WriteAllText(SpecialPath.LocalJsonFileFullName, json); } else { File.Delete(SpecialPath.LocalJsonFileFullName); } NTMinerRoot.Current.ReInitMinerProfile(); this.Sha1 = NTMinerRoot.Current.MinerProfile.GetSha1(); MineWorkEdit.ShowWindow(formType ?? FormType.Edit, new MineWorkViewModel(this)); } }, formType => { if (this == PleaseSelect) { return(false); } return(true); }); this.Remove = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } DialogWindow.ShowDialog(message: $"您确定删除吗?", title: "确认", onYes: () => { VirtualRoot.Execute(new RemoveMineWorkCommand(this.Id)); }, icon: IconConst.IconConfirm); }, () => { if (this == PleaseSelect) { return(false); } return(true); }); }
public MineWorkViewModel(Guid id) { _id = id; this.Save = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } if (string.IsNullOrEmpty(this.Name)) { VirtualRoot.Out.ShowError("作业名称是必须的"); } bool isMineWorkChanged = false; bool isMinerProfileChanged = false; MineWorkData mineWorkData = new MineWorkData(this); if (NTMinerRoot.Instance.MineWorkSet.TryGetMineWork(this.Id, out IMineWork entity)) { string sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1(); // 如果作业设置变更了则一定变更了 if (this.Sha1 != sha1) { isMinerProfileChanged = true; } else { // 如果作业设置没变更但作业引用的服务器数据库记录状态变更了则变更了 LocalJsonDb localJsonObj = new LocalJsonDb(NTMinerRoot.Instance, mineWorkData); ServerJsonDb serverJsonObj = new ServerJsonDb(NTMinerRoot.Instance, localJsonObj); var serverJson = VirtualRoot.JsonSerializer.Serialize(serverJsonObj); sha1 = HashUtil.Sha1(serverJson); if (sha1 != this.ServerJsonSha1) { isMinerProfileChanged = true; } } if (entity.Name != this.Name || entity.Description != this.Description) { isMineWorkChanged = true; } } else { isMinerProfileChanged = true; VirtualRoot.Execute(new AddMineWorkCommand(this)); this.Edit.Execute(FormType.Edit); } if (isMinerProfileChanged) { Write.DevDebug("检测到MinerProfile状态变更"); NTMinerRoot.ExportWorkJson(mineWorkData, out string localJson, out string serverJson); if (!string.IsNullOrEmpty(localJson) && !string.IsNullOrEmpty(serverJson)) { RpcRoot.Server.MineWorkService.ExportMineWorkAsync(this.Id, localJson, serverJson, callback: null); } if (mineWorkData.ServerJsonSha1 != this.ServerJsonSha1) { this.ServerJsonSha1 = mineWorkData.ServerJsonSha1; isMineWorkChanged = true; } } if (isMineWorkChanged) { VirtualRoot.Execute(new UpdateMineWorkCommand(mineWorkData)); } }); this.Edit = new DelegateCommand <FormType?>((formType) => { if (this.Id == Guid.Empty) { return; } if (!AppContext.Instance.MineWorkVms.TryGetMineWorkVm(this.Id, out MineWorkViewModel mineWorkVm)) { WpfUtil.ShowInputDialog("作业名称", string.Empty, workName => { if (string.IsNullOrEmpty(workName)) { return("作业名称是必须的"); } return(string.Empty); }, onOk: workName => { new MineWorkViewModel(this) { Name = workName }.Save.Execute(null); }); } else { // 编辑作业前切换上下文 // 根据workId下载json保存到本地并调用LocalJson.Instance.ReInit() string json = RpcRoot.Server.MineWorkService.GetLocalJson(this.Id); if (!string.IsNullOrEmpty(json)) { File.WriteAllText(SpecialPath.LocalJsonFileFullName, json); } else { File.Delete(SpecialPath.LocalJsonFileFullName); } NTMinerRoot.Instance.ReInitMinerProfile(); this.Sha1 = NTMinerRoot.Instance.MinerProfile.GetSha1(); VirtualRoot.Execute(new MineWorkEditCommand(formType ?? FormType.Edit, new MineWorkViewModel(this))); } }, formType => { if (this == PleaseSelect) { return(false); } return(true); }); this.Remove = new DelegateCommand(() => { if (this.Id == Guid.Empty) { return; } this.ShowSoftDialog(new DialogWindowViewModel(message: $"您确定删除吗?", title: "确认", onYes: () => { VirtualRoot.Execute(new RemoveMineWorkCommand(this.Id)); })); }, () => { if (this == PleaseSelect) { return(false); } return(true); }); }