SrdRecipeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 SrdRecipeViewModel : BindableBase
  27. {
  28. #region 常量
  29. private const string ENGINEERING = "Engineering";
  30. private const string SRD = "srd";
  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 SrdRecipe _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 SrdRecipe 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 SrdRecipeViewModel()
  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["FrontDivertTime"] = false;
  163. //PropertyValidResultDic["FrontPoolTime"] = false;
  164. //PropertyValidResultDic["MaxDivertPlusPoolPressure"] = false;
  165. //PropertyValidResultDic["DivertPlusPoolDelay"] = false;
  166. PropertyValidResultDic["RinseSpeed"] = false;
  167. PropertyValidResultDic["RinseTime"] = false;
  168. //PropertyValidResultDic["FrontRinseTime"] = false;
  169. //PropertyValidResultDic["BackRinseTime"] = false;
  170. //PropertyValidResultDic["MaxWashPressure"] = false;
  171. PropertyValidResultDic["FlowCheckDelay"] = false;
  172. PropertyValidResultDic["DrySpeed"] = false;
  173. PropertyValidResultDic["DryTime"] = false;
  174. //PropertyValidResultDic["PreN2DryTime"] = false;
  175. //PropertyValidResultDic["BackN2DryTime"] = false;
  176. //PropertyValidResultDic["PostN2DryTime"] = false;
  177. //PropertyValidResultDic["ExhaustFanDelay"] = false;
  178. PropertyValidResultDic["MinWaterPressure"] = false;
  179. PropertyValidResultDic["MaxWaterPressure"] = false;
  180. PropertyValidResultDic["MinSRDWaterFlow"] = false;
  181. PropertyValidResultDic["MaxSRDWaterFlow"] = false;
  182. }
  183. /// <summary>
  184. /// 加载数据
  185. /// </summary>
  186. public void LoadRecipeData()
  187. {
  188. RecipeNodes = _uiRecipeManager.GetRecipesByType(SRD);
  189. _recipeNodeDic.Clear();
  190. InitializeDictionary(RecipeNodes);
  191. }
  192. /// <summary>
  193. /// 初始化字典
  194. /// </summary>
  195. /// <param name="nodes"></param>
  196. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  197. {
  198. if (nodes != null)
  199. {
  200. foreach (var node in nodes)
  201. {
  202. if (node.NodeType == RecipeNodeType.File)
  203. {
  204. _recipeNodeDic[node.Name] = node;
  205. }
  206. InitializeDictionary(node.Children);
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 操作
  212. /// </summary>
  213. /// <param name="param"></param>
  214. private void SelectionChangedAction(object param)
  215. {
  216. if(_recipeNodeDic.ContainsKey(param.ToString()))
  217. {
  218. CurrentNode = _recipeNodeDic[param.ToString()];
  219. if(CurrentNode.NodeType==RecipeNodeType.File)
  220. {
  221. if (CurrentNode.RecipeLocation == ENGINEERING)
  222. {
  223. EditEnable = true;
  224. CreateEnable = true;
  225. }
  226. else
  227. {
  228. EditEnable= false;
  229. CreateEnable = false;
  230. }
  231. }
  232. Recipe = _uiRecipeManager.LoadRecipe<SrdRecipe>(CurrentNode.RecipeFullFileName);
  233. if(Recipe==null)
  234. {
  235. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  236. EditEnable = false;
  237. CreateEnable = false;
  238. }
  239. Enable = false;
  240. }
  241. else
  242. {
  243. if(param.ToString()==ENGINEERING)
  244. {
  245. CreateEnable= true;
  246. }
  247. else
  248. {
  249. CreateEnable = false;
  250. }
  251. CurrentNode = null;
  252. Recipe = null;
  253. EditEnable = false;
  254. Enable = false;
  255. }
  256. }
  257. #region Action
  258. /// <summary>
  259. /// 创建
  260. /// </summary>
  261. /// <param name="param"></param>
  262. private void CreateAction(object param)
  263. {
  264. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  265. if (recipeNameDialog.ShowDialog().Value)
  266. {
  267. if (!CheckNameExist(recipeNameDialog.RecipeName))
  268. {
  269. Recipe = new SrdRecipe();
  270. Recipe.CreateDate = DateTime.Now;
  271. Recipe.Ppid = recipeNameDialog.RecipeName;
  272. Recipe.Description = recipeNameDialog.RecipeDescription;
  273. Enable = true;
  274. _isEdit = false;
  275. }
  276. }
  277. }
  278. /// <summary>
  279. /// 编辑
  280. /// </summary>
  281. /// <param name="param"></param>
  282. private void EditAction(object param)
  283. {
  284. Enable = true;
  285. _isEdit= false;
  286. }
  287. /// <summary>
  288. /// 保存
  289. /// </summary>
  290. /// <param name="param"></param>
  291. private void SaveAction(object param)
  292. {
  293. if (CheckValid(_isEdit))
  294. {
  295. Recipe.SaveDate = DateTime.Now;
  296. try
  297. {
  298. _uiRecipeManager.SaveRecipe<SrdRecipe>(ENGINEERING, Recipe.Ppid,SRD, Recipe);
  299. LoadRecipeData();
  300. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  301. Enable = false;
  302. }
  303. catch (Exception ex)
  304. {
  305. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// 另存为
  311. /// </summary>
  312. /// <param name="param"></param>
  313. private void SaveAsAction(object param)
  314. {
  315. if (Recipe == null)
  316. {
  317. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  318. return;
  319. }
  320. SrdRecipe recipe = new SrdRecipe();
  321. if (CheckValid(_isEdit))
  322. {
  323. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  324. if (recipeNameDialog.ShowDialog().Value)
  325. {
  326. if (!CheckNameExist(recipeNameDialog.RecipeName))
  327. {
  328. recipe = new SrdRecipe();
  329. //属性复制
  330. //recipe.BackN2DryTime = Recipe.BackN2DryTime;
  331. //recipe.BackRinseTime = Recipe.BackRinseTime;
  332. //recipe.DivertPlusPoolDelay = Recipe.DivertPlusPoolDelay;
  333. //recipe.DrySpeed = Recipe.DrySpeed;
  334. //recipe.ExhaustFanDelay = Recipe.ExhaustFanDelay;
  335. //recipe.FlowCheckDelay = Recipe.FlowCheckDelay;
  336. //recipe.FrontDivertTime = Recipe.FrontDivertTime;
  337. //recipe.FrontPoolTime = Recipe.FrontPoolTime;
  338. //recipe.FrontRinseTime = Recipe.FrontRinseTime;
  339. //recipe.MaxDivertPlusPoolPressure = Recipe.MaxDivertPlusPoolPressure;
  340. //recipe.MaxWashFlow = Recipe.MaxWashFlow;
  341. //recipe.MaxWashPressure = Recipe.MaxWashPressure;
  342. //recipe.MinDivertPlusPoolFlow = Recipe.MinDivertPlusPoolFlow;
  343. //recipe.MinWashFlow = Recipe.MinWashFlow;
  344. //recipe.MinWaterPressure = Recipe.MinWaterPressure;
  345. //recipe.PostN2DryTime = Recipe.PostN2DryTime;
  346. //recipe.PreN2DryTime = Recipe.PreN2DryTime;
  347. //recipe.RinseSpeed = Recipe.RinseSpeed;
  348. recipe = (SrdRecipe)PropertyUtil.Clone(Recipe);
  349. recipe.CreateDate = DateTime.Now;
  350. recipe.Ppid = recipeNameDialog.RecipeName;
  351. recipe.Description = recipeNameDialog.RecipeDescription;
  352. }
  353. else if (recipeNameDialog.RecipeName != null)
  354. {
  355. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  356. return;
  357. }
  358. else
  359. {
  360. return;
  361. }
  362. }
  363. try
  364. {
  365. _uiRecipeManager.SaveRecipe<SrdRecipe>(ENGINEERING, recipe.Ppid, "srd", recipe);
  366. LoadRecipeData();
  367. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  368. Enable = false;
  369. }
  370. catch (Exception ex)
  371. {
  372. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  373. }
  374. }
  375. }
  376. /// <summary>
  377. /// 检验合法性
  378. /// </summary>
  379. /// <param name="editType"></param>
  380. /// <returns></returns>
  381. private bool CheckValid(bool editType)
  382. {
  383. foreach(string key in _propertyValidResultDic.Keys)
  384. {
  385. bool valid = _propertyValidResultDic[key];
  386. if(!valid)
  387. {
  388. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  389. return false;
  390. }
  391. }
  392. return true;
  393. }
  394. /// <summary>
  395. /// 检验名称是否已经存在
  396. /// </summary>
  397. /// <param name="name"></param>
  398. /// <returns></returns>
  399. private bool CheckNameExist(string name)
  400. {
  401. foreach (string item in _recipeNodeDic.Keys)
  402. {
  403. if (item == name)
  404. {
  405. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. #endregion
  412. }
  413. }