ResRecipeViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 PunkHPX8_Core;
  7. using PunkHPX8_MainPages.PMs;
  8. using PunkHPX8_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 PunkHPX8_MainPages.ViewModels
  25. {
  26. public class ResRecipeViewModel: 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 ResRecipe _recipe;
  35. private bool _editEnable;
  36. private bool _createEnable;
  37. private RecipeNode _currentNode;
  38. private bool _enable = false;
  39. private bool _isEdit = false;
  40. /// <summary>
  41. /// 化学液集合
  42. /// </summary>
  43. private List<string> _chemistryLst = new List<string>();
  44. #endregion
  45. #region 属性
  46. /// <summary>
  47. /// Recipe Nodes
  48. /// </summary>
  49. public ObservableCollection<RecipeNode> RecipeNodes
  50. {
  51. get { return _recipeNodes; }
  52. set { SetProperty(ref _recipeNodes, value); }
  53. }
  54. /// <summary>
  55. /// PropertyValidResultDic
  56. /// </summary>
  57. public Dictionary<string, bool> PropertyValidResultDic
  58. {
  59. get { return _propertyValidResultDic; }
  60. set { SetProperty(ref _propertyValidResultDic, value); }
  61. }
  62. /// <summary>
  63. /// 当前Recipe节点
  64. /// </summary>
  65. public RecipeNode CurrentNode
  66. {
  67. get { return _currentNode; }
  68. set { SetProperty(ref _currentNode, value); }
  69. }
  70. public bool Enable
  71. {
  72. get { return _enable; }
  73. set { SetProperty(ref _enable, value); }
  74. }
  75. public bool EditEnable
  76. {
  77. get { return _editEnable; }
  78. set { SetProperty(ref _editEnable, value); }
  79. }
  80. public bool CreateEnable
  81. {
  82. get { return _createEnable; }
  83. set { SetProperty(ref _createEnable, value); }
  84. }
  85. public ResRecipe Recipe
  86. {
  87. get { return _recipe; }
  88. set { SetProperty(ref _recipe, value); }
  89. }
  90. /// <summary>
  91. /// 化学液集合
  92. /// </summary>
  93. public List<string> ChemistryLst
  94. {
  95. get { return _chemistryLst; }
  96. set { SetProperty(ref _chemistryLst, value); }
  97. }
  98. #endregion
  99. #region 指令
  100. public ICommand OperationCommand
  101. {
  102. get;
  103. private set;
  104. }
  105. #endregion
  106. [IgnorePropertyChange]
  107. public ICommand CreateCommand { get; private set; }
  108. [IgnorePropertyChange]
  109. public ICommand EditCommand { get; private set; }
  110. [IgnorePropertyChange]
  111. public ICommand SaveRecipeCommand { get; private set; }
  112. [IgnorePropertyChange]
  113. public ICommand SaveAsRecipeCommand { get; private set; }
  114. [IgnorePropertyChange]
  115. public ICommand DIReplenEnableTrueCommand { get; private set; }
  116. [IgnorePropertyChange]
  117. public ICommand DIReplenEnableFalseCommand { get; private set; }
  118. [IgnorePropertyChange]
  119. public ICommand ANDIReplenEnableTrueCommand { get; private set; }
  120. [IgnorePropertyChange]
  121. public ICommand ANDIReplenEnableFalseCommand { get; private set; }
  122. [IgnorePropertyChange]
  123. public ICommand TimeBasedTrueCommand { get; private set; }
  124. [IgnorePropertyChange]
  125. public ICommand TimeBasedFalseCommand { get; private set; }
  126. [IgnorePropertyChange]
  127. public ICommand CurrentBasedTrueCommand { get; private set; }
  128. [IgnorePropertyChange]
  129. public ICommand CurrentBasedFalseCommand { get; private set; }
  130. public ResRecipeViewModel()
  131. {
  132. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  133. CreateCommand = new DelegateCommand<object>(CreateAction);
  134. EditCommand = new DelegateCommand<object>(EditAction);
  135. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  136. SaveAsRecipeCommand = new DelegateCommand<object>(SaveAsAction);
  137. DIReplenEnableTrueCommand = new DelegateCommand<object>(EnableTrueAction);
  138. DIReplenEnableFalseCommand = new DelegateCommand<object>(EnableFalseAction);
  139. ANDIReplenEnableTrueCommand = new DelegateCommand<object>(ANEnableTrueAction);
  140. ANDIReplenEnableFalseCommand = new DelegateCommand<object>(ANEnableFalseAction);
  141. TimeBasedTrueCommand = new DelegateCommand<object>(TimeBasedTrueAction);
  142. TimeBasedFalseCommand = new DelegateCommand<object>(TimeBasedFalseAction);
  143. CurrentBasedTrueCommand = new DelegateCommand<object>(CurrentBasedTrueAction);
  144. CurrentBasedFalseCommand = new DelegateCommand<object>(CurrentBasedFalseAction);
  145. InitializeProprtyValidResultDictionary();
  146. string chemistryContent = QueryDataClient.Instance.Service.GetConfig($"System.ChemistryList").ToString();
  147. if (!string.IsNullOrEmpty(chemistryContent))
  148. {
  149. ChemistryLst = chemistryContent.Split(',').ToList();
  150. }
  151. }
  152. private void InitializeProprtyValidResultDictionary()
  153. {
  154. PropertyValidResultDic["DIReplenTimeRate"] = false;
  155. PropertyValidResultDic["DIReplenCurrentRate"] = false;
  156. PropertyValidResultDic["PHErrorLow"] = false;
  157. PropertyValidResultDic["PHErrorHigh"] = false;
  158. PropertyValidResultDic["CALevelErrorHigh"] = false;
  159. PropertyValidResultDic["CALevelErrorLow"] = false;
  160. PropertyValidResultDic["ReservoirCALevel"] = false;
  161. PropertyValidResultDic["CAFlowSetPoint"] = false;
  162. PropertyValidResultDic["CAFlowRateWarningLow"] = false;
  163. PropertyValidResultDic["CAFlowRateErrorLow"] = false;
  164. PropertyValidResultDic["TemperatureErrorHigh"] = false;
  165. PropertyValidResultDic["TemperatureErrorLow"] = false;
  166. PropertyValidResultDic["TemperatureSetPoint"] = false;
  167. PropertyValidResultDic["TemperatureWarningHigh"] = false;
  168. PropertyValidResultDic["TemperatureWarningLow"] = false;
  169. PropertyValidResultDic["ANDIReplenCurrentRate"] = false;
  170. PropertyValidResultDic["ANDIReplenTimeRate"] = false;
  171. PropertyValidResultDic["ANFlowRateErrorLow"] = false;
  172. PropertyValidResultDic["ANFlowRateWarningLow"] = false;
  173. PropertyValidResultDic["ANFlowSetPoint"] = false;
  174. }
  175. public void LoadRecipeData()
  176. {
  177. RecipeNodes = _uiRecipeManager.GetRecipesByType("res");
  178. _recipeNodeDic.Clear();
  179. InitializeDictionary(RecipeNodes);
  180. }
  181. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  182. {
  183. if (nodes != null)
  184. {
  185. foreach (var node in nodes)
  186. {
  187. if (node.NodeType == RecipeNodeType.File)
  188. {
  189. _recipeNodeDic[node.Name] = node;
  190. }
  191. InitializeDictionary(node.Children);
  192. }
  193. }
  194. }
  195. private void SelectionChangedAction(object param)
  196. {
  197. if (_recipeNodeDic.ContainsKey(param.ToString()))
  198. {
  199. CurrentNode = _recipeNodeDic[param.ToString()];
  200. if (CurrentNode.NodeType == RecipeNodeType.File)
  201. {
  202. if (CurrentNode.RecipeLocation == ENGINEERING)
  203. {
  204. EditEnable = true;
  205. CreateEnable = true;
  206. }
  207. else
  208. {
  209. EditEnable = false;
  210. CreateEnable = false;
  211. }
  212. }
  213. Recipe = _uiRecipeManager.LoadRecipe<ResRecipe>(CurrentNode.RecipeFullFileName);
  214. if (Recipe == null)
  215. {
  216. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  217. EditEnable = false;
  218. CreateEnable = false;
  219. }
  220. Enable = false;
  221. }
  222. else
  223. {
  224. if (param.ToString() == ENGINEERING)
  225. {
  226. CreateEnable = true;
  227. }
  228. else
  229. {
  230. CreateEnable = false;
  231. }
  232. CurrentNode = null;
  233. Recipe = null;
  234. EditEnable = false;
  235. Enable = false;
  236. }
  237. }
  238. private void CreateAction(object param)
  239. {
  240. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  241. if (recipeNameDialog.ShowDialog().Value)
  242. {
  243. if (!CheckNameExist(recipeNameDialog.RecipeName))
  244. {
  245. Recipe = new ResRecipe();
  246. Recipe.CreateDate = DateTime.Now;
  247. Recipe.Ppid = recipeNameDialog.RecipeName;
  248. Recipe.Description = recipeNameDialog.RecipeDescription;
  249. Enable = true;
  250. _isEdit = false;
  251. }
  252. }
  253. }
  254. private void EditAction(object param)
  255. {
  256. Enable = true;
  257. _isEdit = false;
  258. }
  259. private void SaveAction(object param)
  260. {
  261. if (CheckValid(_isEdit))
  262. {
  263. Recipe.SaveDate = DateTime.Now;
  264. try
  265. {
  266. _uiRecipeManager.SaveRecipe<ResRecipe>(ENGINEERING, Recipe.Ppid,"res", Recipe);
  267. LoadRecipeData();
  268. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  269. Enable = false;
  270. }
  271. catch (Exception ex)
  272. {
  273. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// 另存为
  279. /// </summary>
  280. /// <param name="param"></param>
  281. private void SaveAsAction(object param)
  282. {
  283. if (Recipe == null)
  284. {
  285. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  286. return;
  287. }
  288. ResRecipe recipe = new ResRecipe();
  289. if (CheckValid(_isEdit))
  290. {
  291. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  292. if (recipeNameDialog.ShowDialog().Value)
  293. {
  294. if (!CheckNameExist(recipeNameDialog.RecipeName))
  295. {
  296. recipe = new ResRecipe();
  297. recipe = (ResRecipe)PropertyUtil.Clone(Recipe);
  298. recipe.CreateDate = DateTime.Now;
  299. recipe.Ppid = recipeNameDialog.RecipeName;
  300. recipe.Description = recipeNameDialog.RecipeDescription;
  301. }
  302. else if (recipeNameDialog.RecipeName != null)
  303. {
  304. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  305. return;
  306. }
  307. else
  308. {
  309. return;
  310. }
  311. }
  312. try
  313. {
  314. _uiRecipeManager.SaveRecipe<ResRecipe>(ENGINEERING, recipe.Ppid, "res", recipe);
  315. LoadRecipeData();
  316. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  317. Enable = false;
  318. }
  319. catch (Exception ex)
  320. {
  321. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  322. }
  323. }
  324. }
  325. private void EnableTrueAction(object param)
  326. {
  327. Recipe.DIReplenEnable = true;
  328. }
  329. private void EnableFalseAction(object param)
  330. {
  331. Recipe.DIReplenEnable = false;
  332. }
  333. private void ANEnableTrueAction(object param)
  334. {
  335. Recipe.ANDIReplenEnable = true;
  336. }
  337. private void ANEnableFalseAction(object param)
  338. {
  339. Recipe.ANDIReplenEnable = false;
  340. }
  341. private bool CheckValid(bool editType)
  342. {
  343. foreach (string key in _propertyValidResultDic.Keys)
  344. {
  345. //在DIReplenEnable时跳过检验项
  346. if(Recipe.ANDIReplenEnable == false)
  347. {
  348. bool result = false;
  349. switch (key)
  350. {
  351. case "ANFlowRateErrorLow":
  352. result = true; break;
  353. case "ANFlowRateWarningLow":
  354. result = true; break;
  355. case "ANFlowSetPoint":
  356. result = true; break;
  357. case "ANDIReplenTimeRate":
  358. result = true; break;
  359. case "ANDIReplenCurrentRate":
  360. result = true; break;
  361. }
  362. if (result)
  363. {
  364. continue;
  365. }
  366. }
  367. //在ReplenEnable时跳过检验项
  368. if (Recipe.DIReplenEnable == false)
  369. {
  370. bool result = false;
  371. switch (key)
  372. {
  373. case "CALevelErrorHigh":
  374. result = true; break;
  375. case "CALevelErrorLow":
  376. result = true; break;
  377. case "ReservoirCALevel":
  378. result = true; break;
  379. case "DIReplenTimeRate":
  380. result = true; break;
  381. case "DIReplenCurrentRate":
  382. result = true; break;
  383. }
  384. if (result)
  385. {
  386. continue;
  387. }
  388. }
  389. bool valid = _propertyValidResultDic[key];
  390. if (!valid)
  391. {
  392. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  393. return false;
  394. }
  395. }
  396. return true;
  397. }
  398. private bool CheckNameExist(string name)
  399. {
  400. foreach (string item in _recipeNodeDic.Keys)
  401. {
  402. if (item == name)
  403. {
  404. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  405. return true;
  406. }
  407. }
  408. return false;
  409. }
  410. private void CurrentBasedTrueAction(object param)
  411. {
  412. //基于时间和基于电流只能二选一
  413. Recipe.CurrentBased = true;
  414. if (Recipe.TimeBased)
  415. {
  416. Recipe.TimeBased = false;
  417. }
  418. }
  419. private void CurrentBasedFalseAction(object param)
  420. {
  421. Recipe.CurrentBased = false;
  422. }
  423. private void TimeBasedTrueAction(object param)
  424. {
  425. Recipe.TimeBased = true;
  426. //基于时间和基于电流只能二选一
  427. if (Recipe.CurrentBased)
  428. {
  429. Recipe.CurrentBased = false;
  430. }
  431. }
  432. private void TimeBasedFalseAction(object param)
  433. {
  434. Recipe.TimeBased = false;
  435. }
  436. }
  437. }