QdrRecipeViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 QdrRecipeViewModel : BindableBase
  26. {
  27. #region 常量
  28. private const string ENGINEERING = "Engineering";
  29. private const string QDR = "qdr";
  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 QdrRecipe _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 QdrRecipe 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 QdrRecipeViewModel()
  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["Step1DwellTimeSeconds"] = false;
  161. PropertyValidResultDic["Step1NumberOfRinse"] = false;
  162. PropertyValidResultDic["Step1NumberOfDumpToMetalDrain"] = false;
  163. PropertyValidResultDic["DumpTimeSeconds"] = false;
  164. PropertyValidResultDic["Step2DwellTimeSeconds"] = false;
  165. PropertyValidResultDic["Step2NumberOfRinse"] = false;
  166. PropertyValidResultDic["FinalRinsePulseClampTime"] = false;
  167. PropertyValidResultDic["FinalRinseSlowDrainTime"] = false;
  168. PropertyValidResultDic["ResistivityMegaOhms"] = false;
  169. PropertyValidResultDic["ResistivityDurationSeconds"] = false;
  170. PropertyValidResultDic["ResistivityStartTimeSeconds"] = false;
  171. }
  172. /// <summary>
  173. /// 加载数据
  174. /// </summary>
  175. public void LoadRecipeData()
  176. {
  177. RecipeNodes = _uiRecipeManager.GetRecipesByType(QDR);
  178. _recipeNodeDic.Clear();
  179. InitializeDictionary(RecipeNodes);
  180. }
  181. /// <summary>
  182. /// 初始化字典
  183. /// </summary>
  184. /// <param name="nodes"></param>
  185. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  186. {
  187. if (nodes != null)
  188. {
  189. foreach (var node in nodes)
  190. {
  191. if (node.NodeType == RecipeNodeType.File)
  192. {
  193. _recipeNodeDic[node.Name] = node;
  194. }
  195. InitializeDictionary(node.Children);
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 操作
  201. /// </summary>
  202. /// <param name="param"></param>
  203. private void SelectionChangedAction(object param)
  204. {
  205. if(_recipeNodeDic.ContainsKey(param.ToString()))
  206. {
  207. CurrentNode = _recipeNodeDic[param.ToString()];
  208. if(CurrentNode.NodeType==RecipeNodeType.File)
  209. {
  210. if (CurrentNode.RecipeLocation == ENGINEERING)
  211. {
  212. EditEnable = true;
  213. CreateEnable = true;
  214. }
  215. else
  216. {
  217. EditEnable= false;
  218. CreateEnable = false;
  219. }
  220. }
  221. Recipe = _uiRecipeManager.LoadRecipe<QdrRecipe>(CurrentNode.RecipeFullFileName);
  222. if(Recipe==null)
  223. {
  224. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  225. EditEnable = false;
  226. CreateEnable = false;
  227. }
  228. Enable = false;
  229. }
  230. else
  231. {
  232. if(param.ToString()==ENGINEERING)
  233. {
  234. CreateEnable= true;
  235. }
  236. else
  237. {
  238. CreateEnable = false;
  239. }
  240. CurrentNode = null;
  241. Recipe = null;
  242. EditEnable = false;
  243. Enable = false;
  244. }
  245. }
  246. #region Action
  247. /// <summary>
  248. /// 创建
  249. /// </summary>
  250. /// <param name="param"></param>
  251. private void CreateAction(object param)
  252. {
  253. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  254. if (recipeNameDialog.ShowDialog().Value)
  255. {
  256. if (!CheckNameExist(recipeNameDialog.RecipeName))
  257. {
  258. Recipe = new QdrRecipe();
  259. Recipe.CreateDate = DateTime.Now;
  260. Recipe.Ppid = recipeNameDialog.RecipeName;
  261. Recipe.Description = recipeNameDialog.RecipeDescription;
  262. Enable = true;
  263. _isEdit = false;
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// 编辑
  269. /// </summary>
  270. /// <param name="param"></param>
  271. private void EditAction(object param)
  272. {
  273. Enable = true;
  274. _isEdit= false;
  275. }
  276. /// <summary>
  277. /// 保存
  278. /// </summary>
  279. /// <param name="param"></param>
  280. private void SaveAction(object param)
  281. {
  282. if (CheckValid(_isEdit))
  283. {
  284. Recipe.SaveDate = DateTime.Now;
  285. try
  286. {
  287. _uiRecipeManager.SaveRecipe<QdrRecipe>(ENGINEERING, Recipe.Ppid,"qdr",Recipe);
  288. LoadRecipeData();
  289. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  290. Enable = false;
  291. }
  292. catch (Exception ex)
  293. {
  294. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// 检验合法性
  300. /// </summary>
  301. /// <param name="editType"></param>
  302. /// <returns></returns>
  303. private bool CheckValid(bool editType)
  304. {
  305. foreach(string key in _propertyValidResultDic.Keys)
  306. {
  307. if ("Step1NumberOfDumpToMetalDrain".Equals(key))
  308. {
  309. if(Recipe.Step1NumberOfDumpToMetalDrain > Recipe.Step1NumberOfRinse + Recipe.Step2NumberOfRinse)
  310. {
  311. MessageBox.Show($"{key} data invalid,large than total of number of rinse", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  312. return false;
  313. }
  314. }
  315. bool valid = _propertyValidResultDic[key];
  316. if(!valid)
  317. {
  318. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  319. return false;
  320. }
  321. }
  322. return true;
  323. }
  324. /// <summary>
  325. /// 检验名称是否已经存在
  326. /// </summary>
  327. /// <param name="name"></param>
  328. /// <returns></returns>
  329. private bool CheckNameExist(string name)
  330. {
  331. foreach (string item in _recipeNodeDic.Keys)
  332. {
  333. if (item == name)
  334. {
  335. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. #endregion
  342. }
  343. }