using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Log; using Aitex.Core.UI.Dialog; using Aitex.Core.UI.MVVM; using Aitex.Core.Utilities; using Caliburn.Micro.Core; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.RecipeCenter; using CyberX8_Core; using CyberX8_MainPages.PMs; using CyberX8_Themes.UserControls; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace CyberX8_MainPages.ViewModels { public class QdrRecipeViewModel : BindableBase { #region 常量 private const string ENGINEERING = "Engineering"; private const string QDR = "qdr"; #endregion #region 内部变量 /// /// Recipe节点字典 /// private Dictionary _recipeNodeDic = new Dictionary(); /// /// 属性检验结果字典 /// private Dictionary _propertyValidResultDic = new Dictionary(); /// /// Recipe节点集合 /// private ObservableCollection _recipeNodes; /// /// uiRecipeManager /// private UiRecipeManager _uiRecipeManager = new UiRecipeManager(); /// /// recipe /// private QdrRecipe _recipe; /// /// 编辑可用性 /// private bool _editEnable; /// /// 创建可用性 /// private bool _createEnable; /// /// 当前节点 /// private RecipeNode _currentNode; /// /// 是否可用 /// private bool _enable = false; /// /// 是否为编辑(true-Edit,false-add) /// private bool _isEdit = false; #endregion #region 属性 public ObservableCollection RecipeNodes { get { return _recipeNodes; } set { SetProperty(ref _recipeNodes, value); } } /// /// Recipe /// public QdrRecipe Recipe { get { return _recipe; } set { SetProperty(ref _recipe, value); } } /// /// 创建可用性 /// public bool CreateEnable { get { return _createEnable; } set { SetProperty(ref _createEnable, value);} } /// /// 编辑可用性 /// public bool EditEnable { get { return _editEnable; } set { SetProperty(ref _editEnable, value);} } /// /// 当前节点 /// public RecipeNode CurrentNode { get { return _currentNode; } set { SetProperty(ref _currentNode, value);} } /// /// 可用性 /// public bool Enable { get { return _enable; } set { SetProperty(ref _enable, value); } } /// /// 属性数据检验结果 /// public Dictionary PropertyValidResultDic { get { return _propertyValidResultDic; } set { SetProperty(ref _propertyValidResultDic, value); } } #endregion #region 指令 [IgnorePropertyChange] public ICommand OperationCommand { get; private set; } [IgnorePropertyChange] public ICommand CreateCommand { get; private set; } [IgnorePropertyChange] public ICommand EditCommand { get; private set; } [IgnorePropertyChange] public ICommand SaveRecipeCommand { get; private set; } [IgnorePropertyChange] public ICommand SaveAsRecipeCommand { get; private set; } #endregion /// /// 构造函数 /// public QdrRecipeViewModel() { OperationCommand = new DelegateCommand(SelectionChangedAction); CreateCommand = new DelegateCommand(CreateAction); EditCommand = new DelegateCommand(EditAction); SaveRecipeCommand = new DelegateCommand(SaveAction); InitializeProprtyValidResultDictionary(); } /// /// 初始化属性数据检验结果字典 /// private void InitializeProprtyValidResultDictionary() { PropertyValidResultDic["Step1DwellTimeSeconds"] = false; PropertyValidResultDic["Step1NumberOfRinse"] = false; PropertyValidResultDic["Step1NumberOfDumpToMetalDrain"] = false; PropertyValidResultDic["DumpTimeSeconds"] = false; PropertyValidResultDic["Step2DwellTimeSeconds"] = false; PropertyValidResultDic["Step2NumberOfRinse"] = false; PropertyValidResultDic["FinalRinsePulseClampTime"] = false; PropertyValidResultDic["FinalRinseSlowDrainTime"] = false; PropertyValidResultDic["ResistivityMegaOhms"] = false; PropertyValidResultDic["ResistivityDurationSeconds"] = false; PropertyValidResultDic["ResistivityStartTimeSeconds"] = false; } /// /// 加载数据 /// public void LoadRecipeData() { RecipeNodes = _uiRecipeManager.GetRecipesByType(QDR); _recipeNodeDic.Clear(); InitializeDictionary(RecipeNodes); } /// /// 初始化字典 /// /// private void InitializeDictionary(ObservableCollection nodes) { if (nodes != null) { foreach (var node in nodes) { if (node.NodeType == RecipeNodeType.File) { _recipeNodeDic[node.Name] = node; } InitializeDictionary(node.Children); } } } /// /// 操作 /// /// private void SelectionChangedAction(object param) { if(_recipeNodeDic.ContainsKey(param.ToString())) { CurrentNode = _recipeNodeDic[param.ToString()]; if(CurrentNode.NodeType==RecipeNodeType.File) { if (CurrentNode.RecipeLocation == ENGINEERING) { EditEnable = true; CreateEnable = true; } else { EditEnable= false; CreateEnable = false; } } Recipe = _uiRecipeManager.LoadRecipe(CurrentNode.RecipeFullFileName); if(Recipe==null) { MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error); EditEnable = false; CreateEnable = false; } Enable = false; } else { if(param.ToString()==ENGINEERING) { CreateEnable= true; } else { CreateEnable = false; } CurrentNode = null; Recipe = null; EditEnable = false; Enable = false; } } #region Action /// /// 创建 /// /// private void CreateAction(object param) { RecipeNameDialog recipeNameDialog = new RecipeNameDialog(); if (recipeNameDialog.ShowDialog().Value) { if (!CheckNameExist(recipeNameDialog.RecipeName)) { Recipe = new QdrRecipe(); Recipe.CreateDate = DateTime.Now; Recipe.Ppid = recipeNameDialog.RecipeName; Recipe.Description = recipeNameDialog.RecipeDescription; Enable = true; _isEdit = false; } } } /// /// 编辑 /// /// private void EditAction(object param) { Enable = true; _isEdit= false; } /// /// 保存 /// /// private void SaveAction(object param) { if (CheckValid(_isEdit)) { Recipe.SaveDate = DateTime.Now; try { _uiRecipeManager.SaveRecipe(ENGINEERING, Recipe.Ppid,"qdr",Recipe); LoadRecipeData(); MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information); Enable = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error); } } } /// /// 检验合法性 /// /// /// private bool CheckValid(bool editType) { foreach(string key in _propertyValidResultDic.Keys) { if ("Step1NumberOfDumpToMetalDrain".Equals(key)) { if(Recipe.Step1NumberOfDumpToMetalDrain > Recipe.Step1NumberOfRinse + Recipe.Step2NumberOfRinse) { MessageBox.Show($"{key} data invalid,large than total of number of rinse", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return false; } } bool valid = _propertyValidResultDic[key]; if(!valid) { MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return false; } } return true; } /// /// 检验名称是否已经存在 /// /// /// private bool CheckNameExist(string name) { foreach (string item in _recipeNodeDic.Keys) { if (item == name) { MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return true; } } return false; } #endregion } }