VpwRecipeViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using Aitex.Core.UI.MVVM;
  2. using Aitex.Core.Utilities;
  3. using MECF.Framework.Common.RecipeCenter;
  4. using MECF.Framework.Common.Utilities;
  5. using Prism.Mvvm;
  6. using PunkHPX8_MainPages.PMs;
  7. using PunkHPX8_Themes.UserControls;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Input;
  16. namespace PunkHPX8_MainPages.ViewModels
  17. {
  18. public class VpwRecipeViewModel : BindableBase
  19. {
  20. private const string ENGINEERING = "Engineering";
  21. #region 内部变量
  22. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  23. private ObservableCollection<RecipeNode> _recipeNodes;
  24. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  25. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  26. private VpwRecipe _recipe;
  27. private bool _editEnable;
  28. private bool _createEnable;
  29. private RecipeNode _currentNode;
  30. private bool _enable = false;
  31. private bool _isEdit = false;
  32. #endregion
  33. #region 属性
  34. public ObservableCollection<RecipeNode> RecipeNodes
  35. {
  36. get { return _recipeNodes; }
  37. set { SetProperty(ref _recipeNodes, value); }
  38. }
  39. public Dictionary<string, bool> PropertyValidResultDic
  40. {
  41. get { return _propertyValidResultDic; }
  42. set { SetProperty(ref _propertyValidResultDic, value); }
  43. }
  44. public RecipeNode CurrentNode
  45. {
  46. get { return _currentNode; }
  47. set { SetProperty(ref _currentNode, value); }
  48. }
  49. public bool Enable
  50. {
  51. get { return _enable; }
  52. set { SetProperty(ref _enable, value); }
  53. }
  54. public bool EditEnable
  55. {
  56. get { return _editEnable; }
  57. set { SetProperty(ref _editEnable, value); }
  58. }
  59. public bool CreateEnable
  60. {
  61. get { return _createEnable; }
  62. set { SetProperty(ref _createEnable, value); }
  63. }
  64. public VpwRecipe Recipe
  65. {
  66. get { return _recipe; }
  67. set { SetProperty(ref _recipe, value); }
  68. }
  69. #region 指令
  70. public ICommand OperationCommand
  71. {
  72. get;
  73. private set;
  74. }
  75. [IgnorePropertyChange]
  76. public ICommand CreateCommand { get; private set; }
  77. [IgnorePropertyChange]
  78. public ICommand EditCommand { get; private set; }
  79. [IgnorePropertyChange]
  80. public ICommand SaveRecipeCommand { get; private set; }
  81. [IgnorePropertyChange]
  82. public ICommand SaveAsRecipeCommand { get; private set; }
  83. #endregion
  84. public VpwRecipeViewModel()
  85. {
  86. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  87. CreateCommand = new DelegateCommand<object>(CreateAction);
  88. EditCommand = new DelegateCommand<object>(EditAction);
  89. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  90. SaveAsRecipeCommand = new DelegateCommand<object>(SaveAsAction);
  91. }
  92. #endregion
  93. public void LoadRecipeData()
  94. {
  95. RecipeNodes = _uiRecipeManager.GetRecipesByType("vpw");
  96. _recipeNodeDic.Clear();
  97. InitializeDictionary(RecipeNodes);
  98. }
  99. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  100. {
  101. if (nodes != null)
  102. {
  103. foreach (var node in nodes)
  104. {
  105. if (node.NodeType == RecipeNodeType.File)
  106. {
  107. _recipeNodeDic[node.Name] = node;
  108. }
  109. InitializeDictionary(node.Children);
  110. }
  111. }
  112. }
  113. private void SelectionChangedAction(object param)
  114. {
  115. if (_recipeNodeDic.ContainsKey(param.ToString()))
  116. {
  117. CurrentNode = _recipeNodeDic[param.ToString()];
  118. if (CurrentNode.NodeType == RecipeNodeType.File)
  119. {
  120. if (CurrentNode.RecipeLocation == ENGINEERING)
  121. {
  122. EditEnable = true;
  123. CreateEnable = true;
  124. }
  125. else
  126. {
  127. EditEnable = false;
  128. CreateEnable = false;
  129. }
  130. }
  131. Recipe = _uiRecipeManager.LoadRecipe<VpwRecipe>(CurrentNode.RecipeFullFileName);
  132. if (Recipe == null)
  133. {
  134. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  135. EditEnable = false;
  136. CreateEnable = false;
  137. }
  138. Enable = false;
  139. }
  140. else
  141. {
  142. if (param.ToString() == ENGINEERING)
  143. {
  144. CreateEnable = true;
  145. }
  146. else
  147. {
  148. CreateEnable = false;
  149. }
  150. CurrentNode = null;
  151. Recipe = null;
  152. EditEnable = false;
  153. Enable = false;
  154. }
  155. }
  156. private void CreateAction(object param)
  157. {
  158. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  159. if (recipeNameDialog.ShowDialog().Value)
  160. {
  161. if (!CheckNameExist(recipeNameDialog.RecipeName))
  162. {
  163. Recipe = new VpwRecipe();
  164. Recipe.CreateDate = DateTime.Now;
  165. Recipe.Ppid = recipeNameDialog.RecipeName;
  166. Recipe.Description = recipeNameDialog.RecipeDescription;
  167. Enable = true;
  168. _isEdit = false;
  169. }
  170. }
  171. }
  172. private void EditAction(object param)
  173. {
  174. Enable = true;
  175. _isEdit = false;
  176. }
  177. private void SaveAction(object param)
  178. {
  179. if (CheckValid(_isEdit))
  180. {
  181. Recipe.SaveDate = DateTime.Now;
  182. try
  183. {
  184. _uiRecipeManager.SaveRecipe<VpwRecipe>(ENGINEERING, Recipe.Ppid, "vpw", Recipe);
  185. LoadRecipeData();
  186. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  187. Enable = false;
  188. }
  189. catch (Exception ex)
  190. {
  191. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 另存为
  197. /// </summary>
  198. /// <param name="param"></param>
  199. private void SaveAsAction(object param)
  200. {
  201. if (Recipe == null)
  202. {
  203. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  204. return;
  205. }
  206. VpwRecipe recipe = new VpwRecipe();
  207. if (CheckValid(_isEdit))
  208. {
  209. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  210. if (recipeNameDialog.ShowDialog().Value)
  211. {
  212. if (!CheckNameExist(recipeNameDialog.RecipeName))
  213. {
  214. recipe = new VpwRecipe();
  215. recipe = (VpwRecipe)PropertyUtil.Clone(Recipe);
  216. recipe.CreateDate = DateTime.Now;
  217. recipe.Ppid = recipeNameDialog.RecipeName;
  218. recipe.Description = recipeNameDialog.RecipeDescription;
  219. }
  220. else if (recipeNameDialog.RecipeName != null)
  221. {
  222. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  223. return;
  224. }
  225. else
  226. {
  227. return;
  228. }
  229. }
  230. try
  231. {
  232. _uiRecipeManager.SaveRecipe<RdsRecipe>(ENGINEERING, recipe.Ppid, "vpw", recipe);
  233. LoadRecipeData();
  234. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  235. Enable = false;
  236. }
  237. catch (Exception ex)
  238. {
  239. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  240. }
  241. }
  242. }
  243. private bool CheckNameExist(string name)
  244. {
  245. foreach (string item in _recipeNodeDic.Keys)
  246. {
  247. if (item == name)
  248. {
  249. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  250. return true;
  251. }
  252. }
  253. return false;
  254. }
  255. private bool CheckValid(bool editType)
  256. {
  257. foreach (string key in _propertyValidResultDic.Keys)
  258. {
  259. bool valid = _propertyValidResultDic[key];
  260. if (!valid)
  261. {
  262. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. }
  269. }