QdrRecipeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 CyberX8_Core;
  8. using CyberX8_MainPages.PMs;
  9. using CyberX8_Themes.UserControls;
  10. using MECF.Framework.Common.DataCenter;
  11. using MECF.Framework.Common.RecipeCenter;
  12. using MECF.Framework.Common.Utilities;
  13. using Prism.Mvvm;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.ComponentModel;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Input;
  24. namespace CyberX8_MainPages.ViewModels
  25. {
  26. public class QdrRecipeViewModel : BindableBase
  27. {
  28. #region 常量
  29. private const string ENGINEERING = "Engineering";
  30. private const string QDR = "qdr";
  31. #endregion
  32. #region 内部变量
  33. /// <summary>
  34. /// Recipe节点字典
  35. /// </summary>
  36. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  37. /// <summary>
  38. /// 属性检验结果字典
  39. /// </summary>
  40. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  41. /// <summary>
  42. /// Recipe节点集合
  43. /// </summary>
  44. private ObservableCollection<RecipeNode> _recipeNodes;
  45. /// <summary>
  46. /// uiRecipeManager
  47. /// </summary>
  48. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  49. /// <summary>
  50. /// recipe
  51. /// </summary>
  52. private QdrRecipe _recipe;
  53. /// <summary>
  54. /// 编辑可用性
  55. /// </summary>
  56. private bool _editEnable;
  57. /// <summary>
  58. /// 创建可用性
  59. /// </summary>
  60. private bool _createEnable;
  61. /// <summary>
  62. /// 当前节点
  63. /// </summary>
  64. private RecipeNode _currentNode;
  65. /// <summary>
  66. /// 是否可用
  67. /// </summary>
  68. private bool _enable = false;
  69. /// <summary>
  70. /// 是否为编辑(true-Edit,false-add)
  71. /// </summary>
  72. private bool _isEdit = false;
  73. #endregion
  74. #region 属性
  75. public ObservableCollection<RecipeNode> RecipeNodes
  76. {
  77. get { return _recipeNodes; }
  78. set { SetProperty(ref _recipeNodes, value); }
  79. }
  80. /// <summary>
  81. /// Recipe
  82. /// </summary>
  83. public QdrRecipe Recipe
  84. {
  85. get { return _recipe; }
  86. set { SetProperty(ref _recipe, value); }
  87. }
  88. /// <summary>
  89. /// 创建可用性
  90. /// </summary>
  91. public bool CreateEnable
  92. {
  93. get { return _createEnable; }
  94. set { SetProperty(ref _createEnable, value);}
  95. }
  96. /// <summary>
  97. /// 编辑可用性
  98. /// </summary>
  99. public bool EditEnable
  100. {
  101. get { return _editEnable; }
  102. set { SetProperty(ref _editEnable, value);}
  103. }
  104. /// <summary>
  105. /// 当前节点
  106. /// </summary>
  107. public RecipeNode CurrentNode
  108. {
  109. get { return _currentNode; }
  110. set { SetProperty(ref _currentNode, value);}
  111. }
  112. /// <summary>
  113. /// 可用性
  114. /// </summary>
  115. public bool Enable
  116. {
  117. get { return _enable; }
  118. set { SetProperty(ref _enable, value); }
  119. }
  120. /// <summary>
  121. /// 属性数据检验结果
  122. /// </summary>
  123. public Dictionary<string, bool> PropertyValidResultDic
  124. {
  125. get { return _propertyValidResultDic; }
  126. set { SetProperty(ref _propertyValidResultDic, value); }
  127. }
  128. #endregion
  129. #region 指令
  130. [IgnorePropertyChange]
  131. public ICommand OperationCommand
  132. {
  133. get;
  134. private set;
  135. }
  136. [IgnorePropertyChange]
  137. public ICommand CreateCommand { get; private set; }
  138. [IgnorePropertyChange]
  139. public ICommand EditCommand { get; private set; }
  140. [IgnorePropertyChange]
  141. public ICommand SaveRecipeCommand { get; private set; }
  142. [IgnorePropertyChange]
  143. public ICommand SaveAsRecipeCommand { get; private set; }
  144. #endregion
  145. /// <summary>
  146. /// 构造函数
  147. /// </summary>
  148. public QdrRecipeViewModel()
  149. {
  150. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  151. CreateCommand = new DelegateCommand<object>(CreateAction);
  152. EditCommand = new DelegateCommand<object>(EditAction);
  153. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  154. SaveAsRecipeCommand = new DelegateCommand<object>(SaveAsAction);
  155. InitializeProprtyValidResultDictionary();
  156. }
  157. /// <summary>
  158. /// 初始化属性数据检验结果字典
  159. /// </summary>
  160. private void InitializeProprtyValidResultDictionary()
  161. {
  162. PropertyValidResultDic["Step1DwellTimeSeconds"] = false;
  163. PropertyValidResultDic["Step1NumberOfRinse"] = false;
  164. PropertyValidResultDic["Step1NumberOfDumpToMetalDrain"] = false;
  165. PropertyValidResultDic["DumpTimeSeconds"] = false;
  166. PropertyValidResultDic["Step2DwellTimeSeconds"] = false;
  167. PropertyValidResultDic["Step2NumberOfRinse"] = false;
  168. PropertyValidResultDic["FinalRinsePulseClampTime"] = false;
  169. PropertyValidResultDic["FinalRinseSlowDrainTime"] = false;
  170. PropertyValidResultDic["ResistivityMegaOhms"] = false;
  171. PropertyValidResultDic["ResistivityDurationSeconds"] = false;
  172. PropertyValidResultDic["ResistivityStartTimeSeconds"] = false;
  173. }
  174. /// <summary>
  175. /// 加载数据
  176. /// </summary>
  177. public void LoadRecipeData()
  178. {
  179. RecipeNodes = _uiRecipeManager.GetRecipesByType(QDR);
  180. _recipeNodeDic.Clear();
  181. InitializeDictionary(RecipeNodes);
  182. }
  183. /// <summary>
  184. /// 初始化字典
  185. /// </summary>
  186. /// <param name="nodes"></param>
  187. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  188. {
  189. if (nodes != null)
  190. {
  191. foreach (var node in nodes)
  192. {
  193. if (node.NodeType == RecipeNodeType.File)
  194. {
  195. _recipeNodeDic[node.Name] = node;
  196. }
  197. InitializeDictionary(node.Children);
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 操作
  203. /// </summary>
  204. /// <param name="param"></param>
  205. private void SelectionChangedAction(object param)
  206. {
  207. if(_recipeNodeDic.ContainsKey(param.ToString()))
  208. {
  209. CurrentNode = _recipeNodeDic[param.ToString()];
  210. if(CurrentNode.NodeType==RecipeNodeType.File)
  211. {
  212. if (CurrentNode.RecipeLocation == ENGINEERING)
  213. {
  214. EditEnable = true;
  215. CreateEnable = true;
  216. }
  217. else
  218. {
  219. EditEnable= false;
  220. CreateEnable = false;
  221. }
  222. }
  223. Recipe = _uiRecipeManager.LoadRecipe<QdrRecipe>(CurrentNode.RecipeFullFileName);
  224. if(Recipe==null)
  225. {
  226. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  227. EditEnable = false;
  228. CreateEnable = false;
  229. }
  230. Enable = false;
  231. }
  232. else
  233. {
  234. if(param.ToString()==ENGINEERING)
  235. {
  236. CreateEnable= true;
  237. }
  238. else
  239. {
  240. CreateEnable = false;
  241. }
  242. CurrentNode = null;
  243. Recipe = null;
  244. EditEnable = false;
  245. Enable = false;
  246. }
  247. }
  248. #region Action
  249. /// <summary>
  250. /// 创建
  251. /// </summary>
  252. /// <param name="param"></param>
  253. private void CreateAction(object param)
  254. {
  255. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  256. if (recipeNameDialog.ShowDialog().Value)
  257. {
  258. if (!CheckNameExist(recipeNameDialog.RecipeName))
  259. {
  260. Recipe = new QdrRecipe();
  261. Recipe.CreateDate = DateTime.Now;
  262. Recipe.Ppid = recipeNameDialog.RecipeName;
  263. Recipe.Description = recipeNameDialog.RecipeDescription;
  264. Enable = true;
  265. _isEdit = false;
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// 编辑
  271. /// </summary>
  272. /// <param name="param"></param>
  273. private void EditAction(object param)
  274. {
  275. Enable = true;
  276. _isEdit= false;
  277. }
  278. /// <summary>
  279. /// 保存
  280. /// </summary>
  281. /// <param name="param"></param>
  282. private void SaveAction(object param)
  283. {
  284. if (CheckValid(_isEdit))
  285. {
  286. Recipe.SaveDate = DateTime.Now;
  287. try
  288. {
  289. _uiRecipeManager.SaveRecipe<QdrRecipe>(ENGINEERING, Recipe.Ppid,"qdr",Recipe);
  290. LoadRecipeData();
  291. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  292. Enable = false;
  293. }
  294. catch (Exception ex)
  295. {
  296. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// 另存为
  302. /// </summary>
  303. /// <param name="param"></param>
  304. private void SaveAsAction(object param)
  305. {
  306. if (Recipe == null)
  307. {
  308. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  309. return;
  310. }
  311. QdrRecipe recipe = new QdrRecipe();
  312. if (CheckValid(_isEdit))
  313. {
  314. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  315. if (recipeNameDialog.ShowDialog().Value)
  316. {
  317. if (!CheckNameExist(recipeNameDialog.RecipeName))
  318. {
  319. recipe = new QdrRecipe();
  320. recipe = (QdrRecipe)PropertyUtil.Clone(Recipe);
  321. //recipe.Step1DwellTimeSeconds = Recipe.Step1DwellTimeSeconds;
  322. //recipe.Step1NumberOfRinse = Recipe.Step1NumberOfRinse;
  323. //recipe.Step1N2BubbleOn = Recipe.Step1N2BubbleOn;
  324. //recipe.Step1NumberOfDumpToMetalDrain = Recipe.Step1NumberOfDumpToMetalDrain;
  325. //recipe.Step2DwellTimeSeconds = Recipe.Step2DwellTimeSeconds;
  326. //recipe.Step2N2BubbleOn = Recipe.Step2N2BubbleOn;
  327. //recipe.Step2NumberOfRinse = Recipe.Step2NumberOfRinse;
  328. //recipe.FinalRinseDry = Recipe.FinalRinseDry;
  329. //recipe.FinalRinsePulseClampTime = Recipe.FinalRinsePulseClampTime;
  330. //recipe.FinalRinseSlowDrainTime = Recipe.FinalRinseSlowDrainTime;
  331. //recipe.ResistivityDurationSeconds = Recipe.ResistivityDurationSeconds;
  332. //recipe.ResistivityStartTimeSeconds = Recipe.ResistivityStartTimeSeconds;
  333. //recipe.ResistivityMegaOhms = Recipe.ResistivityMegaOhms;
  334. //recipe.N2ChargeTimeSeconds = Recipe.N2ChargeTimeSeconds;
  335. //recipe.DumpTimeSeconds = Recipe.DumpTimeSeconds;
  336. recipe.CreateDate = DateTime.Now;
  337. recipe.Ppid = recipeNameDialog.RecipeName;
  338. recipe.Description = recipeNameDialog.RecipeDescription;
  339. }
  340. else if (recipeNameDialog.RecipeName != null)
  341. {
  342. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  343. return;
  344. }
  345. else
  346. {
  347. return;
  348. }
  349. }
  350. try
  351. {
  352. _uiRecipeManager.SaveRecipe<QdrRecipe>(ENGINEERING, recipe.Ppid, "qdr", recipe);
  353. LoadRecipeData();
  354. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  355. Enable = false;
  356. }
  357. catch (Exception ex)
  358. {
  359. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// 检验合法性
  365. /// </summary>
  366. /// <param name="editType"></param>
  367. /// <returns></returns>
  368. private bool CheckValid(bool editType)
  369. {
  370. foreach(string key in _propertyValidResultDic.Keys)
  371. {
  372. if ("Step1NumberOfDumpToMetalDrain".Equals(key))
  373. {
  374. if(Recipe.Step1NumberOfDumpToMetalDrain > Recipe.Step1NumberOfRinse + Recipe.Step2NumberOfRinse)
  375. {
  376. MessageBox.Show($"{key} data invalid,large than total of number of rinse", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  377. return false;
  378. }
  379. }
  380. bool valid = _propertyValidResultDic[key];
  381. if(!valid)
  382. {
  383. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389. /// <summary>
  390. /// 检验名称是否已经存在
  391. /// </summary>
  392. /// <param name="name"></param>
  393. /// <returns></returns>
  394. private bool CheckNameExist(string name)
  395. {
  396. foreach (string item in _recipeNodeDic.Keys)
  397. {
  398. if (item == name)
  399. {
  400. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. #endregion
  407. }
  408. }