using Caliburn.Micro; using Caliburn.Micro.Core; using FurnaceUI.Controls.Common; using FurnaceUI.Views.Editors; using MECF.Framework.Common.CommonData; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.OperationCenter; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; using MECF.Framework.UI.Client.ClientBase; using OpenSEMI.ClientBase; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Input; namespace FurnaceUI.Views.Recipes { public class RecipeHistoryViewModel : ModuleUiViewModelBase { public List RecipeHistories; private RecipeProvider _recipeProvider = new RecipeProvider(); public ObservableCollection ObservableRecipeHistory { get; set; } = new ObservableCollection(); public ShowRecipeHistory SelectedItemShowRecipeHistory { get; set; } public string FilePath { get; set; } = ""; public RecipeDataBase CurrentRecipe { get; set; } public RecipeHistoryViewModel() { } protected override void OnViewLoaded(object view) { base.OnViewLoaded(view); GetRecipeHistoryByDB(FilePath, 20); ShowRecipeHistory(); } private void GetRecipeHistoryByDB(string RecipePath, int row = 20) { RecipeHistories = QueryDataClient.Instance.Service.QueryByRecipePathHistory(RecipePath, row); } private void ShowRecipeHistory() { ObservableRecipeHistory.Clear(); if (RecipeHistories != null && RecipeHistories.Count > 0) { RecipeHistories.OrderByDescending(x => x.LastRevisionTime).ToList().ForEach(x => { ObservableRecipeHistory.Add(new ShowRecipeHistory(x)); }); } } public void RecipeViewClick() { if (SelectedItemShowRecipeHistory != null) { CGlobal.RecipeProcessEditViewEnable = false; MECF.Framework.UI.Client.CenterViews.Editors.Recipe.CGlobal.RecipeProcessEditViewEnable = false; var wm = IoC.Get(); //var viewProcessRecipe = new RecipeProcessEditViewModel(SelectedItemShowRecipeHistory); //viewProcessRecipe.RecipeType = "process"; //(wm as WindowManager)?.ShowDialogWithTitle(viewProcessRecipe, null, "View Process Recpie"); //CGlobal.RecipeProcessEditViewEnable = true; //MECF.Framework.UI.Client.CenterViews.Editors.Recipe.CGlobal.RecipeProcessEditViewEnable = true; } } RelayCommand _recipeCompareCommand; public ICommand RecipeCompareCommand { get { if (_recipeCompareCommand == null) { _recipeCompareCommand = new RelayCommand(param => this.RecipeCompareClick(), param => this.CanRecipeCompareClick()); } return _recipeCompareCommand; } } private bool CanRecipeCompareClick() { bool isEnable = false; if (ObservableRecipeHistory != null) { int count = ObservableRecipeHistory.Count(p => p.IsSelected == true); if (0 < count && count <= 2) { isEnable = true; } } return isEnable; } private void RecipeCompareClick() { if (ObservableRecipeHistory != null) { var selectedItems = ObservableRecipeHistory.Where(p => p.IsSelected == true).ToList(); var wm = IoC.Get(); ShowRecipeHistory showRecipeHistoryA = null; ShowRecipeHistory showRecipeHistoryB = null; if (selectedItems.Count == 1) { showRecipeHistoryA = selectedItems.FirstOrDefault(); showRecipeHistoryB = ObservableRecipeHistory.OrderBy(p => p.LastRevisionTime).LastOrDefault(); } if (selectedItems.Count == 2) { showRecipeHistoryA = selectedItems.LastOrDefault(); showRecipeHistoryB = selectedItems.FirstOrDefault(); } if (showRecipeHistoryA != null && showRecipeHistoryB != null) { var recipesHistoryCompareViewModel = new RecipesCompareTwoViewModel(showRecipeHistoryA, showRecipeHistoryB); recipesHistoryCompareViewModel.DisplayName = "Compare Recipes History"; IDictionary settings = new Dictionary() { { "Height", 900 }, { "Width", 1200 }, { "WindowStartupLocation", WindowStartupLocation.CenterScreen }, { "SizeToContent", System.Windows.SizeToContent.Manual } }; (wm as WindowManager)?.ShowDialog(recipesHistoryCompareViewModel, null, settings); } } } RelayCommand _recipeRollbackClickCommand; public ICommand RecipeRollbackClickCommand { get { if (_recipeRollbackClickCommand == null) { _recipeRollbackClickCommand = new RelayCommand(param => this.RecipeRollbackClick(), param => this.CanRecipeRollbackClick()); } return _recipeRollbackClickCommand; } } private bool CanRecipeRollbackClick() { bool isEnable = false; if (ObservableRecipeHistory != null) { int count = ObservableRecipeHistory.Count(p => p.IsSelected == true); if (count == 1) { isEnable = true; } } return isEnable; } private void RecipeRollbackClick() { if (SelectedItemShowRecipeHistory != null) { if (DialogBox.ShowDialog(DialogButton.Yes | DialogButton.No, DialogType.CONFIRM, $"Recipe {CurrentRecipe.Name} content is changed, do you want to save it?") == DialogButton.Yes) { InvokeClient.Instance.Service.DoOperation("SetRecipeEditHistory", this.CurrentRecipe.GetRecipeHistory()); var result = this._recipeProvider.SaveRecipe(CurrentRecipe.PrefixPath, CurrentRecipe.Name, SelectedItemShowRecipeHistory.Recipe_Content); } } } } }