using Aitex.Core.UI.MVVM; using Aitex.Core.Utilities; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Equipment; using MECF.Framework.Common.RecipeCenter; using MECF.Framework.Common.Utilities; using Prism.Mvvm; using PunkHPX8_MainPages.PMs; using PunkHPX8_Themes.UserControls; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace PunkHPX8_MainPages.ViewModels { public class DqdrRecipeViewModel : BindableBase { #region 常量 private const string ENGINEERING = "Engineering"; private const string DQDR = "dqdr"; private const string DEP = "dep"; #endregion #region 内部变量 /// /// Recipe节点字典 /// private Dictionary _recipeNodeDic = new Dictionary(); /// /// 属性检验结果字典 /// private Dictionary _propertyValidResultDic = new Dictionary(); /// /// Recipe节点集合 /// private ObservableCollection _recipeNodes; /// /// DepRecipe节点集合 /// private ObservableCollection _depRecipeNodes; /// /// Plating Cell List /// private ObservableCollection _platingCellList = new ObservableCollection(); /// /// DepRecipe Name List /// private ObservableCollection _depRecipeNameList = new ObservableCollection(); /// /// uiRecipeManager /// private UiRecipeManager _uiRecipeManager = new UiRecipeManager(); /// /// recipe /// private DqdrRecipe _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); } } public ObservableCollection DepRecipeNodes { get { return _depRecipeNodes; } set { SetProperty(ref _depRecipeNodes, value); } } public ObservableCollection PlatingCellList { get { return _platingCellList; } set { SetProperty(ref _platingCellList, value); } } public ObservableCollection DepRecipeNameList { get { return _depRecipeNameList; } set { SetProperty(ref _depRecipeNameList, value); } } /// /// Recipe /// public DqdrRecipe 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; } public ICommand LinkedDepRecipeChangeCommand { get; private set; } #endregion /// /// 构造函数 /// public DqdrRecipeViewModel() { OperationCommand = new DelegateCommand(SelectionChangedAction); CreateCommand = new DelegateCommand(CreateAction); EditCommand = new DelegateCommand(EditAction); SaveRecipeCommand = new DelegateCommand(SaveAction); SaveAsRecipeCommand = new DelegateCommand(SaveAsAction); LinkedDepRecipeChangeCommand = new DelegateCommand(LinkedDepRecipeChangeAction); InitializeProprtyValidResultDictionary(); InitializePlatingCellNameList(); } /// /// 初始化属性数据检验结果字典 /// private void InitializeProprtyValidResultDictionary() { PropertyValidResultDic["ReclaimSpeed"] = false; PropertyValidResultDic["ReclaimTime"] = false; PropertyValidResultDic["RinseSpeed"] = false; PropertyValidResultDic["RinseTime"] = false; PropertyValidResultDic["DrySpeed"] = false; PropertyValidResultDic["DryTime"] = false; PropertyValidResultDic["IntervalRinseSpeed"] = false; PropertyValidResultDic["IntervalRinseTime"] = false; } /// /// 加载数据 /// public void LoadRecipeData() { RecipeNodes = _uiRecipeManager.GetRecipesByType(DQDR); DepRecipeNodes = _uiRecipeManager.GetRecipesByType(DEP); _recipeNodeDic.Clear(); InitializeDictionary(RecipeNodes); InitializeDepRecipeList(); } /// /// 初始化字典 /// /// 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); } } } /// /// 初始化DEP recipe名字列表 /// private void InitializeDepRecipeList() { DepRecipeNameList.Clear(); if (DepRecipeNodes != null && DepRecipeNodes.Count > 0) { foreach (var item in DepRecipeNodes[0].Children) { DepRecipeNameList.Add(item.FileName); } } } /// /// 初始化platingcell list /// private void InitializePlatingCellNameList() { PlatingCellList.Clear(); PlatingCellList.Add(ModuleName.PlatingCell1.ToString()); PlatingCellList.Add(ModuleName.PlatingCell2.ToString()); PlatingCellList.Add(ModuleName.PlatingCell2.ToString()); PlatingCellList.Add(ModuleName.PlatingCell4.ToString()); } /// /// 操作 /// /// 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) { Recipe = new DqdrRecipe(); 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; } /// /// 确认当前dummy rinse recipe与关联的dep recipe的时间是否相等 /// /// /// /// private void LinkedDepRecipeChangeAction(object param) { Recipe.IsIdentical = false; string linkedDepRecipeName = Recipe.LinkedDepRecipeName; string fullName = DepRecipeNodes[0].Children.FirstOrDefault(node => node.FileName == linkedDepRecipeName)?.RecipeFullFileName; DepRecipe deprecipe = _uiRecipeManager.LoadRecipe(fullName); if (deprecipe != null) { if (deprecipe.ReclaimTime == Recipe.ReclaimTime && deprecipe.RinseTime == Recipe.RinseTime && deprecipe.DryTime == Recipe.DryTime && deprecipe.RinseBeforeEntryEnable == Recipe.RinseBeforeEntryEnable && deprecipe.IntervalRinseTime == Recipe.IntervalRinseTime) { Recipe.IsIdentical = true; } } } /// /// 保存 /// /// private void SaveAction(object param) { if (CheckValid(_isEdit)) { Recipe.SaveDate = DateTime.Now; try { _uiRecipeManager.SaveRecipe(ENGINEERING, Recipe.Ppid, "dqdr", 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 void SaveAsAction(object param) { if (Recipe == null) { MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return; } DqdrRecipe recipe = new DqdrRecipe(); if (CheckValid(_isEdit)) { RecipeNameDialog recipeNameDialog = new RecipeNameDialog(); if (recipeNameDialog.ShowDialog().Value) { if (!CheckNameExist(recipeNameDialog.RecipeName)) { recipe = new DqdrRecipe(); recipe = (DqdrRecipe)PropertyUtil.Clone(Recipe); recipe.CreateDate = DateTime.Now; recipe.Ppid = recipeNameDialog.RecipeName; recipe.Description = recipeNameDialog.RecipeDescription; } else if (recipeNameDialog.RecipeName != null) { MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return; } else { return; } } try { _uiRecipeManager.SaveRecipe(ENGINEERING, recipe.Ppid, "dqdr", recipe); LoadRecipeData(); MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information); Enable = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error); } } } /// /// 检验名称是否已经存在 /// /// /// 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; } /// /// 检验合法性 /// /// /// private bool CheckValid(bool editType) { foreach (string key in _propertyValidResultDic.Keys) { bool valid = _propertyValidResultDic[key]; if (!valid) { MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error); return false; } } return true; } #endregion } }