RdsRecipeViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.UI.Dialog;
  3. using Aitex.Core.UI.MVVM;
  4. using Aitex.Core.Utilities;
  5. using Caliburn.Micro.Core;
  6. using CyberX8_Core;
  7. using CyberX8_MainPages.PMs;
  8. using CyberX8_Themes.UserControls;
  9. using MECF.Framework.Common.DataCenter;
  10. using MECF.Framework.Common.RecipeCenter;
  11. using MECF.Framework.Common.Utilities;
  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.Documents;
  23. using System.Windows.Input;
  24. namespace CyberX8_MainPages.ViewModels
  25. {
  26. public class RdsRecipeViewModel : BindableBase
  27. {
  28. private const string ENGINEERING = "Engineering";
  29. #region 内部变量
  30. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  31. private ObservableCollection<RecipeNode> _recipeNodes;
  32. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  33. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  34. private RdsRecipe _recipe;
  35. private bool _editEnable;
  36. private bool _createEnable;
  37. private RecipeNode _currentNode;
  38. private bool _enable = false;
  39. private bool _isEdit = false;
  40. #endregion
  41. #region 属性
  42. public ObservableCollection<RecipeNode> RecipeNodes
  43. {
  44. get { return _recipeNodes; }
  45. set { SetProperty(ref _recipeNodes, value); }
  46. }
  47. public Dictionary<string, bool> PropertyValidResultDic
  48. {
  49. get { return _propertyValidResultDic; }
  50. set { SetProperty(ref _propertyValidResultDic, value); }
  51. }
  52. public RecipeNode CurrentNode
  53. {
  54. get { return _currentNode; }
  55. set { SetProperty(ref _currentNode, value); }
  56. }
  57. public bool Enable
  58. {
  59. get { return _enable; }
  60. set { SetProperty(ref _enable, value); }
  61. }
  62. public bool EditEnable
  63. {
  64. get { return _editEnable; }
  65. set { SetProperty(ref _editEnable, value); }
  66. }
  67. public bool CreateEnable
  68. {
  69. get { return _createEnable; }
  70. set { SetProperty(ref _createEnable, value); }
  71. }
  72. public RdsRecipe Recipe
  73. {
  74. get { return _recipe; }
  75. set { SetProperty(ref _recipe, value); }
  76. }
  77. #endregion
  78. #region 指令
  79. public ICommand OperationCommand
  80. {
  81. get;
  82. private set;
  83. }
  84. #endregion
  85. [IgnorePropertyChange]
  86. public ICommand CreateCommand { get; private set; }
  87. [IgnorePropertyChange]
  88. public ICommand EditCommand { get; private set; }
  89. [IgnorePropertyChange]
  90. public ICommand SaveRecipeCommand { get; private set; }
  91. [IgnorePropertyChange]
  92. public ICommand SaveAsRecipeCommand { get; private set; }
  93. [IgnorePropertyChange]
  94. public ICommand ChemReplenEnableTrueCommand { get; private set; }
  95. [IgnorePropertyChange]
  96. public ICommand ChemReplenEnableFalseCommand { get; private set; }
  97. [IgnorePropertyChange]
  98. public ICommand CurrentBasedTrueCommand { get; private set; }
  99. [IgnorePropertyChange]
  100. public ICommand CurrentBasedFalseCommand { get; private set; }
  101. [IgnorePropertyChange]
  102. public ICommand TimeBasedTrueCommand { get; private set; }
  103. [IgnorePropertyChange]
  104. public ICommand TimeBasedFalseCommand { get; private set; }
  105. [IgnorePropertyChange]
  106. public ICommand AutoCrossDoseTrueCommand { get; private set; }
  107. [IgnorePropertyChange]
  108. public ICommand AutoCrossDoseFalseCommand { get; private set; }
  109. [IgnorePropertyChange]
  110. public ICommand AutoCurrentBasedTrueCommand { get; private set; }
  111. [IgnorePropertyChange]
  112. public ICommand AutoCurrentBasedFalseCommand { get; private set; }
  113. [IgnorePropertyChange]
  114. public ICommand AutoTimeBasedTrueCommand { get; private set; }
  115. [IgnorePropertyChange]
  116. public ICommand AutoTimeBasedFalseCommand { get; private set; }
  117. public RdsRecipeViewModel()
  118. {
  119. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  120. CreateCommand = new DelegateCommand<object>(CreateAction);
  121. EditCommand = new DelegateCommand<object>(EditAction);
  122. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  123. SaveAsRecipeCommand = new DelegateCommand<object>(SaveAsAction);
  124. ChemReplenEnableTrueCommand = new DelegateCommand<object>(ChemReplenEnableTrueAction);
  125. ChemReplenEnableFalseCommand = new DelegateCommand<object>(ChemReplenEnableFalseAction);
  126. CurrentBasedTrueCommand = new DelegateCommand<object>(CurrentBasedTrueAction);
  127. CurrentBasedFalseCommand = new DelegateCommand<object>(CurrentBasedFalseAction);
  128. TimeBasedTrueCommand = new DelegateCommand<object>(TimeBasedTrueAction);
  129. TimeBasedFalseCommand = new DelegateCommand<object>(TimeBasedFalseAction);
  130. AutoCrossDoseTrueCommand = new DelegateCommand<object>(AutoCrossDoseTrueAction);
  131. AutoCrossDoseFalseCommand = new DelegateCommand<object>(AutoCrossDoseFalseAction);
  132. AutoTimeBasedTrueCommand = new DelegateCommand<object>(AutoTimeBasedTrueAction);
  133. AutoTimeBasedFalseCommand = new DelegateCommand<object>(AutoTimeBasedFalseAction);
  134. AutoCurrentBasedTrueCommand = new DelegateCommand<object>(AutoCurrentBasedTrueAction);
  135. AutoCurrentBasedFalseCommand = new DelegateCommand<object>(AutoCurrentBasedFalseAction);
  136. InitializeProprtyValidResultDictionary();
  137. }
  138. private void ChemReplenEnableTrueAction(object param)
  139. {
  140. Recipe.ReplenEnable = true;
  141. if (Recipe.AutoDoseEnable)
  142. {
  143. Recipe.AutoDoseEnable =false;
  144. }
  145. }
  146. private void ChemReplenEnableFalseAction(object param)
  147. {
  148. Recipe.ReplenEnable = false;
  149. }
  150. private void CurrentBasedTrueAction(object param)
  151. {
  152. //基于时间和基于电流只能二选一
  153. Recipe.ReplenCurrentBased = true;
  154. if (Recipe.ReplenTimeBased)
  155. {
  156. Recipe.ReplenTimeBased =false;
  157. }
  158. }
  159. private void CurrentBasedFalseAction(object param)
  160. {
  161. Recipe.ReplenCurrentBased = false;
  162. }
  163. private void TimeBasedTrueAction(object param)
  164. {
  165. Recipe.ReplenTimeBased = true;
  166. //基于时间和基于电流只能二选一
  167. if (Recipe.ReplenCurrentBased)
  168. {
  169. Recipe.ReplenCurrentBased = false ;
  170. }
  171. }
  172. private void TimeBasedFalseAction(object param)
  173. {
  174. Recipe.ReplenTimeBased = false;
  175. }
  176. private void AutoCrossDoseTrueAction(object param)
  177. {
  178. Recipe.AutoDoseEnable = true;
  179. if (Recipe.ReplenEnable)
  180. {
  181. Recipe.ReplenEnable =false;
  182. }
  183. }
  184. private void AutoCrossDoseFalseAction(object param)
  185. {
  186. Recipe.AutoDoseEnable = false;
  187. }
  188. private void AutoTimeBasedTrueAction(object param)
  189. {
  190. Recipe.AutoTimeBased = true;
  191. //基于时间和基于电流只能二选一
  192. if (Recipe.AutoCurrentBased)
  193. {
  194. Recipe.AutoCurrentBased = false;
  195. }
  196. }
  197. private void AutoTimeBasedFalseAction(object param)
  198. {
  199. Recipe.AutoTimeBased = false;
  200. }
  201. private void AutoCurrentBasedTrueAction(object param)
  202. {
  203. Recipe.AutoCurrentBased = true;
  204. //基于时间和基于电流只能二选一
  205. if (Recipe.AutoTimeBased)
  206. {
  207. Recipe.AutoTimeBased = false;
  208. }
  209. }
  210. private void AutoCurrentBasedFalseAction(object param)
  211. {
  212. Recipe.AutoCurrentBased = false;
  213. }
  214. private void InitializeProprtyValidResultDictionary()
  215. {
  216. //需要检验的参数
  217. PropertyValidResultDic["ReplenCurrentBasedRate"] = false;
  218. PropertyValidResultDic["ReplenNoCircFactor"] = false;
  219. PropertyValidResultDic["ReplenTimeBasedRate"] = false;
  220. }
  221. public void LoadRecipeData()
  222. {
  223. RecipeNodes = _uiRecipeManager.GetRecipesByType("rds");
  224. _recipeNodeDic.Clear();
  225. InitializeDictionary(RecipeNodes);
  226. }
  227. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  228. {
  229. if (nodes != null)
  230. {
  231. foreach (var node in nodes)
  232. {
  233. if (node.NodeType == RecipeNodeType.File)
  234. {
  235. _recipeNodeDic[node.Name] = node;
  236. }
  237. InitializeDictionary(node.Children);
  238. }
  239. }
  240. }
  241. private void SelectionChangedAction(object param)
  242. {
  243. if (_recipeNodeDic.ContainsKey(param.ToString()))
  244. {
  245. CurrentNode = _recipeNodeDic[param.ToString()];
  246. if (CurrentNode.NodeType == RecipeNodeType.File)
  247. {
  248. if (CurrentNode.RecipeLocation == ENGINEERING)
  249. {
  250. EditEnable = true;
  251. CreateEnable = true;
  252. }
  253. else
  254. {
  255. EditEnable = false;
  256. CreateEnable = false;
  257. }
  258. }
  259. Recipe = _uiRecipeManager.LoadRecipe<RdsRecipe>(CurrentNode.RecipeFullFileName);
  260. if (Recipe == null)
  261. {
  262. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  263. EditEnable = false;
  264. CreateEnable = false;
  265. }
  266. Enable = false;
  267. }
  268. else
  269. {
  270. if (param.ToString() == ENGINEERING)
  271. {
  272. CreateEnable = true;
  273. }
  274. else
  275. {
  276. CreateEnable = false;
  277. }
  278. CurrentNode = null;
  279. Recipe = null;
  280. EditEnable = false;
  281. Enable = false;
  282. }
  283. }
  284. private void CreateAction(object param)
  285. {
  286. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  287. if (recipeNameDialog.ShowDialog().Value)
  288. {
  289. if (!CheckNameExist(recipeNameDialog.RecipeName))
  290. {
  291. Recipe = new RdsRecipe();
  292. Recipe.CreateDate = DateTime.Now;
  293. Recipe.Ppid = recipeNameDialog.RecipeName;
  294. Recipe.Description = recipeNameDialog.RecipeDescription;
  295. Enable = true;
  296. _isEdit = false;
  297. }
  298. }
  299. }
  300. private void EditAction(object param)
  301. {
  302. Enable = true;
  303. _isEdit = false;
  304. }
  305. private void SaveAction(object param)
  306. {
  307. if (CheckValid(_isEdit))
  308. {
  309. Recipe.SaveDate = DateTime.Now;
  310. try
  311. {
  312. _uiRecipeManager.SaveRecipe<RdsRecipe>(ENGINEERING, Recipe.Ppid, "rds", Recipe);
  313. LoadRecipeData();
  314. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  315. Enable = false;
  316. }
  317. catch (Exception ex)
  318. {
  319. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// 另存为
  325. /// </summary>
  326. /// <param name="param"></param>
  327. private void SaveAsAction(object param)
  328. {
  329. if (Recipe == null)
  330. {
  331. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  332. return;
  333. }
  334. RdsRecipe recipe = new RdsRecipe();
  335. if (CheckValid(_isEdit))
  336. {
  337. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  338. if (recipeNameDialog.ShowDialog().Value)
  339. {
  340. if (!CheckNameExist(recipeNameDialog.RecipeName))
  341. {
  342. recipe = new RdsRecipe();
  343. //复制属性
  344. //recipe.AutoDoseEnable = Recipe.AutoDoseEnable;
  345. //recipe.AutoCurrentBased = Recipe.AutoCurrentBased;
  346. //recipe.AutoDoseFrequency = Recipe.AutoDoseFrequency;
  347. //recipe.AutoTimeBased = Recipe.AutoTimeBased;
  348. //recipe.AutoDoseIdleStartTime = Recipe.AutoDoseIdleStartTime;
  349. //recipe.ReplenCurrentBased = Recipe.ReplenCurrentBased;
  350. //recipe.ReplenCurrentBasedRate = Recipe.ReplenCurrentBasedRate;
  351. //recipe.ReplenEnable = Recipe.ReplenEnable;
  352. //recipe.ReplenNoCircFactor = Recipe.ReplenNoCircFactor;
  353. //recipe.ReplenTimeBased = Recipe.ReplenTimeBased;
  354. //recipe.ReplenTimeBasedRate = Recipe.ReplenTimeBasedRate;
  355. recipe = (RdsRecipe)PropertyUtil.Clone(Recipe);
  356. recipe.CreateDate = DateTime.Now;
  357. recipe.Ppid = recipeNameDialog.RecipeName;
  358. recipe.Description = recipeNameDialog.RecipeDescription;
  359. }
  360. else if (recipeNameDialog.RecipeName != null)
  361. {
  362. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  363. return;
  364. }
  365. else
  366. {
  367. return;
  368. }
  369. }
  370. try
  371. {
  372. _uiRecipeManager.SaveRecipe<RdsRecipe>(ENGINEERING, recipe.Ppid, "rds", recipe);
  373. LoadRecipeData();
  374. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  375. Enable = false;
  376. }
  377. catch (Exception ex)
  378. {
  379. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  380. }
  381. }
  382. }
  383. private bool CheckValid(bool editType)
  384. {
  385. //在ReplenEnable false时跳过检验项
  386. if (Recipe.ReplenEnable == false)
  387. {
  388. return true;
  389. }
  390. foreach (string key in _propertyValidResultDic.Keys)
  391. {
  392. if(Recipe.ReplenCurrentBased == false && "ReplenCurrentBasedRate".Equals(key))
  393. {
  394. continue;
  395. }
  396. if (Recipe.ReplenTimeBased == false && "ReplenTimeBasedRate".Equals(key))
  397. {
  398. continue;
  399. }
  400. bool valid = _propertyValidResultDic[key];
  401. if (!valid)
  402. {
  403. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409. private bool CheckNameExist(string name)
  410. {
  411. foreach (string item in _recipeNodeDic.Keys)
  412. {
  413. if (item == name)
  414. {
  415. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  416. return true;
  417. }
  418. }
  419. return false;
  420. }
  421. }
  422. }