SrdRecipeViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 MECF.Framework.Common.DataCenter;
  8. using MECF.Framework.Common.RecipeCenter;
  9. using CyberX8_Core;
  10. using CyberX8_MainPages.PMs;
  11. using CyberX8_Themes.UserControls;
  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.Input;
  23. namespace CyberX8_MainPages.ViewModels
  24. {
  25. public class SrdRecipeViewModel : BindableBase
  26. {
  27. #region 常量
  28. private const string ENGINEERING = "Engineering";
  29. private const string SRD = "srd";
  30. #endregion
  31. #region 内部变量
  32. /// <summary>
  33. /// Recipe节点字典
  34. /// </summary>
  35. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  36. /// <summary>
  37. /// 属性检验结果字典
  38. /// </summary>
  39. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  40. /// <summary>
  41. /// Recipe节点集合
  42. /// </summary>
  43. private ObservableCollection<RecipeNode> _recipeNodes;
  44. /// <summary>
  45. /// uiRecipeManager
  46. /// </summary>
  47. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  48. /// <summary>
  49. /// recipe
  50. /// </summary>
  51. private SrdRecipe _recipe;
  52. /// <summary>
  53. /// 编辑可用性
  54. /// </summary>
  55. private bool _editEnable;
  56. /// <summary>
  57. /// 创建可用性
  58. /// </summary>
  59. private bool _createEnable;
  60. /// <summary>
  61. /// 当前节点
  62. /// </summary>
  63. private RecipeNode _currentNode;
  64. /// <summary>
  65. /// 是否可用
  66. /// </summary>
  67. private bool _enable = false;
  68. /// <summary>
  69. /// 是否为编辑(true-Edit,false-add)
  70. /// </summary>
  71. private bool _isEdit = false;
  72. #endregion
  73. #region 属性
  74. public ObservableCollection<RecipeNode> RecipeNodes
  75. {
  76. get { return _recipeNodes; }
  77. set { SetProperty(ref _recipeNodes, value); }
  78. }
  79. /// <summary>
  80. /// Recipe
  81. /// </summary>
  82. public SrdRecipe Recipe
  83. {
  84. get { return _recipe; }
  85. set { SetProperty(ref _recipe, value); }
  86. }
  87. /// <summary>
  88. /// 创建可用性
  89. /// </summary>
  90. public bool CreateEnable
  91. {
  92. get { return _createEnable; }
  93. set { SetProperty(ref _createEnable, value);}
  94. }
  95. /// <summary>
  96. /// 编辑可用性
  97. /// </summary>
  98. public bool EditEnable
  99. {
  100. get { return _editEnable; }
  101. set { SetProperty(ref _editEnable, value);}
  102. }
  103. /// <summary>
  104. /// 当前节点
  105. /// </summary>
  106. public RecipeNode CurrentNode
  107. {
  108. get { return _currentNode; }
  109. set { SetProperty(ref _currentNode, value);}
  110. }
  111. /// <summary>
  112. /// 可用性
  113. /// </summary>
  114. public bool Enable
  115. {
  116. get { return _enable; }
  117. set { SetProperty(ref _enable, value); }
  118. }
  119. /// <summary>
  120. /// 属性数据检验结果
  121. /// </summary>
  122. public Dictionary<string, bool> PropertyValidResultDic
  123. {
  124. get { return _propertyValidResultDic; }
  125. set { SetProperty(ref _propertyValidResultDic, value); }
  126. }
  127. #endregion
  128. #region 指令
  129. [IgnorePropertyChange]
  130. public ICommand OperationCommand
  131. {
  132. get;
  133. private set;
  134. }
  135. [IgnorePropertyChange]
  136. public ICommand CreateCommand { get; private set; }
  137. [IgnorePropertyChange]
  138. public ICommand EditCommand { get; private set; }
  139. [IgnorePropertyChange]
  140. public ICommand SaveRecipeCommand { get; private set; }
  141. [IgnorePropertyChange]
  142. public ICommand SaveAsRecipeCommand { get; private set; }
  143. #endregion
  144. /// <summary>
  145. /// 构造函数
  146. /// </summary>
  147. public SrdRecipeViewModel()
  148. {
  149. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  150. CreateCommand = new DelegateCommand<object>(CreateAction);
  151. EditCommand = new DelegateCommand<object>(EditAction);
  152. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  153. InitializeProprtyValidResultDictionary();
  154. }
  155. /// <summary>
  156. /// 初始化属性数据检验结果字典
  157. /// </summary>
  158. private void InitializeProprtyValidResultDictionary()
  159. {
  160. PropertyValidResultDic["FrontDivertTime"] = false;
  161. PropertyValidResultDic["FrontPoolTime"] = false;
  162. PropertyValidResultDic["MaxDivertPlusPoolPressure"] = false;
  163. PropertyValidResultDic["DivertPlusPoolDelay"] = false;
  164. PropertyValidResultDic["RinseSpeed"] = false;
  165. PropertyValidResultDic["FrontRinseTime"] = false;
  166. PropertyValidResultDic["BackRinseTime"] = false;
  167. PropertyValidResultDic["MaxWashPressure"] = false;
  168. PropertyValidResultDic["FlowCheckDelay"] = false;
  169. PropertyValidResultDic["DrySpeed"] = false;
  170. PropertyValidResultDic["PreN2DryTime"] = false;
  171. PropertyValidResultDic["BackN2DryTime"] = false;
  172. PropertyValidResultDic["PostN2DryTime"] = false;
  173. PropertyValidResultDic["ExhaustFanDelay"] = false;
  174. PropertyValidResultDic["MinWaterPressure"] = false;
  175. }
  176. /// <summary>
  177. /// 加载数据
  178. /// </summary>
  179. public void LoadRecipeData()
  180. {
  181. RecipeNodes = _uiRecipeManager.GetRecipesByType(SRD);
  182. _recipeNodeDic.Clear();
  183. InitializeDictionary(RecipeNodes);
  184. }
  185. /// <summary>
  186. /// 初始化字典
  187. /// </summary>
  188. /// <param name="nodes"></param>
  189. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  190. {
  191. if (nodes != null)
  192. {
  193. foreach (var node in nodes)
  194. {
  195. if (node.NodeType == RecipeNodeType.File)
  196. {
  197. _recipeNodeDic[node.Name] = node;
  198. }
  199. InitializeDictionary(node.Children);
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// 操作
  205. /// </summary>
  206. /// <param name="param"></param>
  207. private void SelectionChangedAction(object param)
  208. {
  209. if(_recipeNodeDic.ContainsKey(param.ToString()))
  210. {
  211. CurrentNode = _recipeNodeDic[param.ToString()];
  212. if(CurrentNode.NodeType==RecipeNodeType.File)
  213. {
  214. if (CurrentNode.RecipeLocation == ENGINEERING)
  215. {
  216. EditEnable = true;
  217. CreateEnable = true;
  218. }
  219. else
  220. {
  221. EditEnable= false;
  222. CreateEnable = false;
  223. }
  224. }
  225. Recipe = _uiRecipeManager.LoadRecipe<SrdRecipe>(CurrentNode.RecipeFullFileName);
  226. if(Recipe==null)
  227. {
  228. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  229. EditEnable = false;
  230. CreateEnable = false;
  231. }
  232. Enable = false;
  233. }
  234. else
  235. {
  236. if(param.ToString()==ENGINEERING)
  237. {
  238. CreateEnable= true;
  239. }
  240. else
  241. {
  242. CreateEnable = false;
  243. }
  244. CurrentNode = null;
  245. Recipe = null;
  246. EditEnable = false;
  247. Enable = false;
  248. }
  249. }
  250. #region Action
  251. /// <summary>
  252. /// 创建
  253. /// </summary>
  254. /// <param name="param"></param>
  255. private void CreateAction(object param)
  256. {
  257. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  258. if (recipeNameDialog.ShowDialog().Value)
  259. {
  260. if (!CheckNameExist(recipeNameDialog.RecipeName))
  261. {
  262. Recipe = new SrdRecipe();
  263. Recipe.CreateDate = DateTime.Now;
  264. Recipe.Ppid = recipeNameDialog.RecipeName;
  265. Recipe.Description = recipeNameDialog.RecipeDescription;
  266. Enable = true;
  267. _isEdit = false;
  268. }
  269. }
  270. }
  271. /// <summary>
  272. /// 编辑
  273. /// </summary>
  274. /// <param name="param"></param>
  275. private void EditAction(object param)
  276. {
  277. Enable = true;
  278. _isEdit= false;
  279. }
  280. /// <summary>
  281. /// 保存
  282. /// </summary>
  283. /// <param name="param"></param>
  284. private void SaveAction(object param)
  285. {
  286. if (CheckValid(_isEdit))
  287. {
  288. Recipe.SaveDate = DateTime.Now;
  289. try
  290. {
  291. _uiRecipeManager.SaveRecipe<SrdRecipe>(ENGINEERING, Recipe.Ppid,SRD, Recipe);
  292. LoadRecipeData();
  293. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  294. Enable = false;
  295. }
  296. catch (Exception ex)
  297. {
  298. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 检验合法性
  304. /// </summary>
  305. /// <param name="editType"></param>
  306. /// <returns></returns>
  307. private bool CheckValid(bool editType)
  308. {
  309. foreach(string key in _propertyValidResultDic.Keys)
  310. {
  311. bool valid = _propertyValidResultDic[key];
  312. if(!valid)
  313. {
  314. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. /// <summary>
  321. /// 检验名称是否已经存在
  322. /// </summary>
  323. /// <param name="name"></param>
  324. /// <returns></returns>
  325. private bool CheckNameExist(string name)
  326. {
  327. foreach (string item in _recipeNodeDic.Keys)
  328. {
  329. if (item == name)
  330. {
  331. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  332. return true;
  333. }
  334. }
  335. return false;
  336. }
  337. #endregion
  338. }
  339. }