HvdRecipeViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 HvdRecipeViewModel : BindableBase
  27. {
  28. #region 常量
  29. private const string ENGINEERING = "Engineering";
  30. private const string HVD = "hvd";
  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 HvdRecipe _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 HvdRecipe 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 HvdRecipeViewModel()
  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["DryTime"] = false;
  163. }
  164. /// <summary>
  165. /// 加载数据
  166. /// </summary>
  167. public void LoadRecipeData()
  168. {
  169. RecipeNodes = _uiRecipeManager.GetRecipesByType(HVD);
  170. _recipeNodeDic.Clear();
  171. InitializeDictionary(RecipeNodes);
  172. }
  173. /// <summary>
  174. /// 初始化字典
  175. /// </summary>
  176. /// <param name="nodes"></param>
  177. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  178. {
  179. if (nodes != null)
  180. {
  181. foreach (var node in nodes)
  182. {
  183. if (node.NodeType == RecipeNodeType.File)
  184. {
  185. _recipeNodeDic[node.Name] = node;
  186. }
  187. InitializeDictionary(node.Children);
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 操作
  193. /// </summary>
  194. /// <param name="param"></param>
  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<HvdRecipe>(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. #region Action
  239. /// <summary>
  240. /// 创建
  241. /// </summary>
  242. /// <param name="param"></param>
  243. private void CreateAction(object param)
  244. {
  245. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  246. if (recipeNameDialog.ShowDialog().Value)
  247. {
  248. if (!CheckNameExist(recipeNameDialog.RecipeName))
  249. {
  250. Recipe = new HvdRecipe();
  251. Recipe.CreateDate = DateTime.Now;
  252. Recipe.Ppid = recipeNameDialog.RecipeName;
  253. Recipe.Description = recipeNameDialog.RecipeDescription;
  254. Enable = true;
  255. _isEdit = false;
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 编辑
  261. /// </summary>
  262. /// <param name="param"></param>
  263. private void EditAction(object param)
  264. {
  265. Enable = true;
  266. _isEdit= false;
  267. }
  268. /// <summary>
  269. /// 保存
  270. /// </summary>
  271. /// <param name="param"></param>
  272. private void SaveAction(object param)
  273. {
  274. if (CheckValid(_isEdit))
  275. {
  276. Recipe.SaveDate = DateTime.Now;
  277. try
  278. {
  279. _uiRecipeManager.SaveRecipe<HvdRecipe>(ENGINEERING, Recipe.Ppid,"hvd", Recipe);
  280. LoadRecipeData();
  281. MessageBox.Show("Save Recipe Success", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  282. Enable = false;
  283. }
  284. catch (Exception ex)
  285. {
  286. MessageBox.Show(ex.Message, "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// 另存为
  292. /// </summary>
  293. /// <param name="param"></param>
  294. private void SaveAsAction(object param)
  295. {
  296. if (Recipe == null)
  297. {
  298. MessageBox.Show("Select a Recipe first", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  299. return;
  300. }
  301. HvdRecipe recipe = new HvdRecipe();
  302. if (CheckValid(_isEdit))
  303. {
  304. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  305. if (recipeNameDialog.ShowDialog().Value)
  306. {
  307. if (!CheckNameExist(recipeNameDialog.RecipeName))
  308. {
  309. recipe = new HvdRecipe();
  310. recipe = (HvdRecipe)PropertyUtil.Clone(Recipe);
  311. //recipe.DryTime = Recipe.DryTime;
  312. recipe.CreateDate = DateTime.Now;
  313. recipe.Ppid = recipeNameDialog.RecipeName;
  314. recipe.Description = recipeNameDialog.RecipeDescription;
  315. Enable = true;
  316. _isEdit = false;
  317. }
  318. else if (recipeNameDialog.RecipeName != null)
  319. {
  320. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  321. return;
  322. }
  323. else
  324. {
  325. return;
  326. }
  327. }
  328. try
  329. {
  330. _uiRecipeManager.SaveRecipe<HvdRecipe>(ENGINEERING, recipe.Ppid, "hvd", recipe);
  331. LoadRecipeData();
  332. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  333. Enable = false;
  334. }
  335. catch (Exception ex)
  336. {
  337. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  338. }
  339. }
  340. }
  341. /// <summary>
  342. /// 检验合法性
  343. /// </summary>
  344. /// <param name="editType"></param>
  345. /// <returns></returns>
  346. private bool CheckValid(bool editType)
  347. {
  348. foreach(string key in _propertyValidResultDic.Keys)
  349. {
  350. bool valid = _propertyValidResultDic[key];
  351. if(!valid)
  352. {
  353. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  354. return false;
  355. }
  356. }
  357. return true;
  358. }
  359. /// <summary>
  360. /// 检验名称是否已经存在
  361. /// </summary>
  362. /// <param name="name"></param>
  363. /// <returns></returns>
  364. private bool CheckNameExist(string name)
  365. {
  366. foreach (string item in _recipeNodeDic.Keys)
  367. {
  368. if (item == name)
  369. {
  370. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  371. return true;
  372. }
  373. }
  374. return false;
  375. }
  376. #endregion
  377. }
  378. }