RdsRecipeViewModel.cs 13 KB

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