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.Documents; using System.Windows.Input; namespace CyberX8_MainPages.ViewModels { public class RdsRecipeViewModel : BindableBase { private const string ENGINEERING = "Engineering"; #region 内部变量 private Dictionary _recipeNodeDic = new Dictionary(); private ObservableCollection _recipeNodes; private UiRecipeManager _uiRecipeManager = new UiRecipeManager(); private Dictionary _propertyValidResultDic = new Dictionary(); private RdsRecipe _recipe; private bool _editEnable; private bool _createEnable; private RecipeNode _currentNode; private bool _enable = false; private bool _isEdit = false; #endregion #region 属性 public ObservableCollection RecipeNodes { get { return _recipeNodes; } set { SetProperty(ref _recipeNodes, value); } } public Dictionary PropertyValidResultDic { get { return _propertyValidResultDic; } set { SetProperty(ref _propertyValidResultDic, value); } } public RecipeNode CurrentNode { get { return _currentNode; } set { SetProperty(ref _currentNode, value); } } public bool Enable { get { return _enable; } set { SetProperty(ref _enable, value); } } public bool EditEnable { get { return _editEnable; } set { SetProperty(ref _editEnable, value); } } public bool CreateEnable { get { return _createEnable; } set { SetProperty(ref _createEnable, value); } } public RdsRecipe Recipe { get { return _recipe; } set { SetProperty(ref _recipe, value); } } #endregion #region 指令 public ICommand OperationCommand { get; private set; } #endregion [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; } [IgnorePropertyChange] public ICommand ChemReplenEnableTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand ChemReplenEnableFalseCommand { get; private set; } [IgnorePropertyChange] public ICommand CurrentBasedTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand CurrentBasedFalseCommand { get; private set; } [IgnorePropertyChange] public ICommand TimeBasedTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand TimeBasedFalseCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoCrossDoseTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoCrossDoseFalseCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoCurrentBasedTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoCurrentBasedFalseCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoTimeBasedTrueCommand { get; private set; } [IgnorePropertyChange] public ICommand AutoTimeBasedFalseCommand { get; private set; } public RdsRecipeViewModel() { OperationCommand = new DelegateCommand(SelectionChangedAction); CreateCommand = new DelegateCommand(CreateAction); EditCommand = new DelegateCommand(EditAction); SaveRecipeCommand = new DelegateCommand(SaveAction); ChemReplenEnableTrueCommand = new DelegateCommand(ChemReplenEnableTrueAction); ChemReplenEnableFalseCommand = new DelegateCommand(ChemReplenEnableFalseAction); CurrentBasedTrueCommand = new DelegateCommand(CurrentBasedTrueAction); CurrentBasedFalseCommand = new DelegateCommand(CurrentBasedFalseAction); TimeBasedTrueCommand = new DelegateCommand(TimeBasedTrueAction); TimeBasedFalseCommand = new DelegateCommand(TimeBasedFalseAction); AutoCrossDoseTrueCommand = new DelegateCommand(AutoCrossDoseTrueAction); AutoCrossDoseFalseCommand = new DelegateCommand(AutoCrossDoseFalseAction); AutoTimeBasedTrueCommand = new DelegateCommand(AutoTimeBasedTrueAction); AutoTimeBasedFalseCommand = new DelegateCommand(AutoTimeBasedFalseAction); AutoCurrentBasedTrueCommand = new DelegateCommand(AutoCurrentBasedTrueAction); AutoCurrentBasedFalseCommand = new DelegateCommand(AutoCurrentBasedFalseAction); InitializeProprtyValidResultDictionary(); } private void ChemReplenEnableTrueAction(object param) { Recipe.ReplenEnable = true; if (Recipe.AutoDoseEnable) { Recipe.AutoDoseEnable =false; } } private void ChemReplenEnableFalseAction(object param) { Recipe.ReplenEnable = false; } private void CurrentBasedTrueAction(object param) { //基于时间和基于电流只能二选一 Recipe.ReplenCurrentBased = true; if (Recipe.ReplenTimeBased) { Recipe.ReplenTimeBased =false; } } private void CurrentBasedFalseAction(object param) { Recipe.ReplenCurrentBased = false; } private void TimeBasedTrueAction(object param) { Recipe.ReplenTimeBased = true; //基于时间和基于电流只能二选一 if (Recipe.ReplenCurrentBased) { Recipe.ReplenCurrentBased = false ; } } private void TimeBasedFalseAction(object param) { Recipe.ReplenTimeBased = false; } private void AutoCrossDoseTrueAction(object param) { Recipe.AutoDoseEnable = true; if (Recipe.ReplenEnable) { Recipe.ReplenEnable =false; } } private void AutoCrossDoseFalseAction(object param) { Recipe.AutoDoseEnable = false; } private void AutoTimeBasedTrueAction(object param) { Recipe.AutoTimeBased = true; //基于时间和基于电流只能二选一 if (Recipe.AutoCurrentBased) { Recipe.AutoCurrentBased = false; } } private void AutoTimeBasedFalseAction(object param) { Recipe.AutoTimeBased = false; } private void AutoCurrentBasedTrueAction(object param) { Recipe.AutoCurrentBased = true; //基于时间和基于电流只能二选一 if (Recipe.AutoTimeBased) { Recipe.AutoTimeBased = false; } } private void AutoCurrentBasedFalseAction(object param) { Recipe.AutoCurrentBased = false; } private void InitializeProprtyValidResultDictionary() { //需要检验的参数 PropertyValidResultDic["ReplenCurrentBasedRate"] = false; PropertyValidResultDic["ReplenNoCircFactor"] = false; PropertyValidResultDic["ReplenTimeBasedRate"] = false; } public void LoadRecipeData() { RecipeNodes = _uiRecipeManager.GetRecipesByType("rds"); _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; } } private void CreateAction(object param) { RecipeNameDialog recipeNameDialog = new RecipeNameDialog(); if (recipeNameDialog.ShowDialog().Value) { if (!CheckNameExist(recipeNameDialog.RecipeName)) { Recipe = new RdsRecipe(); 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, "rds", 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) { //在ReplenEnable false时跳过检验项 if (Recipe.ReplenEnable == false) { return true; } foreach (string key in _propertyValidResultDic.Keys) { if(Recipe.ReplenCurrentBased == false && "ReplenCurrentBasedRate".Equals(key)) { continue; } if (Recipe.ReplenTimeBased == false && "ReplenTimeBasedRate".Equals(key)) { continue; } 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; } } }