PwtRecipeViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.Dialog;
  4. using Aitex.Core.UI.MVVM;
  5. using Aitex.Core.Utilities;
  6. using Caliburn.Micro.Core;
  7. using MECF.Framework.Common.DataCenter;
  8. using MECF.Framework.Common.RecipeCenter;
  9. using CyberX8_Core;
  10. using CyberX8_MainPages.PMs;
  11. using CyberX8_Themes.UserControls;
  12. using Prism.Mvvm;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.ComponentModel;
  17. using System.Linq;
  18. using System.Reflection;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Input;
  23. namespace CyberX8_MainPages.ViewModels
  24. {
  25. public class PwtRecipeViewModel : BindableBase
  26. {
  27. #region 常量
  28. private const string ENGINEERING = "Engineering";
  29. private const string PWT = "pwt";
  30. #endregion
  31. #region 内部变量
  32. /// <summary>
  33. /// Recipe节点字典
  34. /// </summary>
  35. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  36. /// <summary>
  37. /// 属性检验结果字典
  38. /// </summary>
  39. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  40. /// <summary>
  41. /// Recipe节点集合
  42. /// </summary>
  43. private ObservableCollection<RecipeNode> _recipeNodes;
  44. /// <summary>
  45. /// uiRecipeManager
  46. /// </summary>
  47. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  48. /// <summary>
  49. /// recipe
  50. /// </summary>
  51. private PwtRecipe _recipe;
  52. /// <summary>
  53. /// 编辑可用性
  54. /// </summary>
  55. private bool _editEnable;
  56. /// <summary>
  57. /// 创建可用性
  58. /// </summary>
  59. private bool _createEnable;
  60. /// <summary>
  61. /// 当前节点
  62. /// </summary>
  63. private RecipeNode _currentNode;
  64. /// <summary>
  65. /// 是否可用
  66. /// </summary>
  67. private bool _enable = false;
  68. /// <summary>
  69. /// 是否为编辑(true-Edit,false-add)
  70. /// </summary>
  71. private bool _isEdit = false;
  72. #endregion
  73. #region 属性
  74. public ObservableCollection<RecipeNode> RecipeNodes
  75. {
  76. get { return _recipeNodes; }
  77. set { SetProperty(ref _recipeNodes, value); }
  78. }
  79. /// <summary>
  80. /// Recipe
  81. /// </summary>
  82. public PwtRecipe Recipe
  83. {
  84. get { return _recipe; }
  85. set { SetProperty(ref _recipe, value); }
  86. }
  87. /// <summary>
  88. /// 创建可用性
  89. /// </summary>
  90. public bool CreateEnable
  91. {
  92. get { return _createEnable; }
  93. set { SetProperty(ref _createEnable, value);}
  94. }
  95. /// <summary>
  96. /// 编辑可用性
  97. /// </summary>
  98. public bool EditEnable
  99. {
  100. get { return _editEnable; }
  101. set { SetProperty(ref _editEnable, value);}
  102. }
  103. /// <summary>
  104. /// 当前节点
  105. /// </summary>
  106. public RecipeNode CurrentNode
  107. {
  108. get { return _currentNode; }
  109. set { SetProperty(ref _currentNode, value);}
  110. }
  111. /// <summary>
  112. /// 可用性
  113. /// </summary>
  114. public bool Enable
  115. {
  116. get { return _enable; }
  117. set { SetProperty(ref _enable, value); }
  118. }
  119. /// <summary>
  120. /// 属性数据检验结果
  121. /// </summary>
  122. public Dictionary<string, bool> PropertyValidResultDic
  123. {
  124. get { return _propertyValidResultDic; }
  125. set { SetProperty(ref _propertyValidResultDic, value); }
  126. }
  127. #endregion
  128. #region 指令
  129. [IgnorePropertyChange]
  130. public ICommand OperationCommand
  131. {
  132. get;
  133. private set;
  134. }
  135. [IgnorePropertyChange]
  136. public ICommand CreateCommand { get; private set; }
  137. [IgnorePropertyChange]
  138. public ICommand EditCommand { get; private set; }
  139. [IgnorePropertyChange]
  140. public ICommand SaveRecipeCommand { get; private set; }
  141. [IgnorePropertyChange]
  142. public ICommand SaveAsRecipeCommand { get; private set; }
  143. #endregion
  144. /// <summary>
  145. /// 构造函数
  146. /// </summary>
  147. public PwtRecipeViewModel()
  148. {
  149. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  150. CreateCommand = new DelegateCommand<object>(CreateAction);
  151. EditCommand = new DelegateCommand<object>(EditAction);
  152. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  153. InitializeProprtyValidResultDictionary();
  154. }
  155. /// <summary>
  156. /// 初始化属性数据检验结果字典
  157. /// </summary>
  158. private void InitializeProprtyValidResultDictionary()
  159. {
  160. PropertyValidResultDic["NumberOfScans"] = false;
  161. }
  162. /// <summary>
  163. /// 加载数据
  164. /// </summary>
  165. public void LoadRecipeData()
  166. {
  167. RecipeNodes = _uiRecipeManager.GetRecipesByType(PWT);
  168. _recipeNodeDic.Clear();
  169. InitializeDictionary(RecipeNodes);
  170. }
  171. /// <summary>
  172. /// 初始化字典
  173. /// </summary>
  174. /// <param name="nodes"></param>
  175. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  176. {
  177. if (nodes != null)
  178. {
  179. foreach (var node in nodes)
  180. {
  181. if (node.NodeType == RecipeNodeType.File)
  182. {
  183. _recipeNodeDic[node.Name] = node;
  184. }
  185. InitializeDictionary(node.Children);
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 操作
  191. /// </summary>
  192. /// <param name="param"></param>
  193. private void SelectionChangedAction(object param)
  194. {
  195. if(_recipeNodeDic.ContainsKey(param.ToString()))
  196. {
  197. CurrentNode = _recipeNodeDic[param.ToString()];
  198. if(CurrentNode.NodeType==RecipeNodeType.File)
  199. {
  200. if (CurrentNode.RecipeLocation == ENGINEERING)
  201. {
  202. EditEnable = true;
  203. CreateEnable = true;
  204. }
  205. else
  206. {
  207. EditEnable= false;
  208. CreateEnable = false;
  209. }
  210. }
  211. Recipe = _uiRecipeManager.LoadRecipe<PwtRecipe>(CurrentNode.RecipeFullFileName);
  212. if(Recipe==null)
  213. {
  214. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  215. EditEnable = false;
  216. CreateEnable = false;
  217. }
  218. Enable = false;
  219. }
  220. else
  221. {
  222. if(param.ToString()==ENGINEERING)
  223. {
  224. CreateEnable= true;
  225. }
  226. else
  227. {
  228. CreateEnable = false;
  229. }
  230. CurrentNode = null;
  231. Recipe = null;
  232. EditEnable = false;
  233. Enable = false;
  234. }
  235. }
  236. #region Action
  237. /// <summary>
  238. /// 创建
  239. /// </summary>
  240. /// <param name="param"></param>
  241. private void CreateAction(object param)
  242. {
  243. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  244. if (recipeNameDialog.ShowDialog().Value)
  245. {
  246. Recipe = new PwtRecipe();
  247. Recipe.CreateDate = DateTime.Now;
  248. Recipe.Ppid = recipeNameDialog.RecipeName;
  249. Recipe.Description = recipeNameDialog.RecipeDescription;
  250. Enable = true;
  251. _isEdit = false;
  252. }
  253. }
  254. /// <summary>
  255. /// 编辑
  256. /// </summary>
  257. /// <param name="param"></param>
  258. private void EditAction(object param)
  259. {
  260. Enable = true;
  261. _isEdit= false;
  262. }
  263. /// <summary>
  264. /// 保存
  265. /// </summary>
  266. /// <param name="param"></param>
  267. private void SaveAction(object param)
  268. {
  269. if (CheckValid(_isEdit))
  270. {
  271. Recipe.SaveDate = DateTime.Now;
  272. try
  273. {
  274. _uiRecipeManager.SaveRecipe<PwtRecipe>(ENGINEERING, Recipe.Ppid,"pwt", Recipe);
  275. LoadRecipeData();
  276. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  277. Enable = false;
  278. }
  279. catch (Exception ex)
  280. {
  281. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  282. }
  283. }
  284. }
  285. /// <summary>
  286. /// 检验合法性
  287. /// </summary>
  288. /// <param name="editType"></param>
  289. /// <returns></returns>
  290. private bool CheckValid(bool editType)
  291. {
  292. foreach(string key in _propertyValidResultDic.Keys)
  293. {
  294. bool valid = _propertyValidResultDic[key];
  295. if(!valid)
  296. {
  297. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. #endregion
  304. }
  305. }