DqdrRecipeViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using Aitex.Core.UI.MVVM;
  2. using Aitex.Core.Utilities;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.RecipeCenter;
  5. using MECF.Framework.Common.Utilities;
  6. using Prism.Mvvm;
  7. using PunkHPX8_MainPages.PMs;
  8. using PunkHPX8_Themes.UserControls;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Input;
  17. namespace PunkHPX8_MainPages.ViewModels
  18. {
  19. public class DqdrRecipeViewModel : BindableBase
  20. {
  21. #region 常量
  22. private const string ENGINEERING = "Engineering";
  23. private const string DQDR = "dqdr";
  24. #endregion
  25. #region 内部变量
  26. /// <summary>
  27. /// Recipe节点字典
  28. /// </summary>
  29. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  30. /// <summary>
  31. /// 属性检验结果字典
  32. /// </summary>
  33. private Dictionary<string, bool> _propertyValidResultDic = new Dictionary<string, bool>();
  34. /// <summary>
  35. /// Recipe节点集合
  36. /// </summary>
  37. private ObservableCollection<RecipeNode> _recipeNodes;
  38. /// <summary>
  39. /// Plating Cell List
  40. /// </summary>
  41. private ObservableCollection<string> _platingCellList;
  42. /// <summary>
  43. /// DepRecipe Name List
  44. /// </summary>
  45. private ObservableCollection<string> _depRecipeNameList;
  46. /// <summary>
  47. /// uiRecipeManager
  48. /// </summary>
  49. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  50. /// <summary>
  51. /// recipe
  52. /// </summary>
  53. private DqdrRecipe _recipe;
  54. /// <summary>
  55. /// 编辑可用性
  56. /// </summary>
  57. private bool _editEnable;
  58. /// <summary>
  59. /// 创建可用性
  60. /// </summary>
  61. private bool _createEnable;
  62. /// <summary>
  63. /// 当前节点
  64. /// </summary>
  65. private RecipeNode _currentNode;
  66. /// <summary>
  67. /// 是否可用
  68. /// </summary>
  69. private bool _enable = false;
  70. /// <summary>
  71. /// 是否为编辑(true-Edit,false-add)
  72. /// </summary>
  73. private bool _isEdit = false;
  74. #endregion
  75. #region 属性
  76. public ObservableCollection<RecipeNode> RecipeNodes
  77. {
  78. get { return _recipeNodes; }
  79. set { SetProperty(ref _recipeNodes, value); }
  80. }
  81. public ObservableCollection<string> PlatingCellList
  82. {
  83. get { return _platingCellList; }
  84. set { SetProperty(ref _platingCellList, value); }
  85. }
  86. public ObservableCollection<string> DepRecipeNameList
  87. {
  88. get { return _depRecipeNameList; }
  89. set { SetProperty(ref _depRecipeNameList, value); }
  90. }
  91. /// <summary>
  92. /// Recipe
  93. /// </summary>
  94. public DqdrRecipe Recipe
  95. {
  96. get { return _recipe; }
  97. set { SetProperty(ref _recipe, value); }
  98. }
  99. /// <summary>
  100. /// 创建可用性
  101. /// </summary>
  102. public bool CreateEnable
  103. {
  104. get { return _createEnable; }
  105. set { SetProperty(ref _createEnable, value); }
  106. }
  107. /// <summary>
  108. /// 编辑可用性
  109. /// </summary>
  110. public bool EditEnable
  111. {
  112. get { return _editEnable; }
  113. set { SetProperty(ref _editEnable, value); }
  114. }
  115. /// <summary>
  116. /// 当前节点
  117. /// </summary>
  118. public RecipeNode CurrentNode
  119. {
  120. get { return _currentNode; }
  121. set { SetProperty(ref _currentNode, value); }
  122. }
  123. /// <summary>
  124. /// 可用性
  125. /// </summary>
  126. public bool Enable
  127. {
  128. get { return _enable; }
  129. set { SetProperty(ref _enable, value); }
  130. }
  131. /// <summary>
  132. /// 属性数据检验结果
  133. /// </summary>
  134. public Dictionary<string, bool> PropertyValidResultDic
  135. {
  136. get { return _propertyValidResultDic; }
  137. set { SetProperty(ref _propertyValidResultDic, value); }
  138. }
  139. #endregion
  140. #region 指令
  141. [IgnorePropertyChange]
  142. public ICommand OperationCommand
  143. {
  144. get;
  145. private set;
  146. }
  147. [IgnorePropertyChange]
  148. public ICommand CreateCommand { get; private set; }
  149. [IgnorePropertyChange]
  150. public ICommand EditCommand { get; private set; }
  151. [IgnorePropertyChange]
  152. public ICommand SaveRecipeCommand { get; private set; }
  153. [IgnorePropertyChange]
  154. public ICommand SaveAsRecipeCommand { get; private set; }
  155. #endregion
  156. /// <summary>
  157. /// 构造函数
  158. /// </summary>
  159. public DqdrRecipeViewModel()
  160. {
  161. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  162. CreateCommand = new DelegateCommand<object>(CreateAction);
  163. EditCommand = new DelegateCommand<object>(EditAction);
  164. SaveRecipeCommand = new DelegateCommand<object>(SaveAction);
  165. SaveAsRecipeCommand = new DelegateCommand<object>(SaveAsAction);
  166. InitializeProprtyValidResultDictionary();
  167. }
  168. /// <summary>
  169. /// 初始化属性数据检验结果字典
  170. /// </summary>
  171. private void InitializeProprtyValidResultDictionary()
  172. {
  173. PropertyValidResultDic["ReclaimSpeed"] = false;
  174. PropertyValidResultDic["ReclaimTime"] = false;
  175. PropertyValidResultDic["ReclaimZoffset"] = false;
  176. PropertyValidResultDic["RinseSpeed"] = false;
  177. PropertyValidResultDic["RinseTime"] = false;
  178. PropertyValidResultDic["RinseZoffset"] = false;
  179. PropertyValidResultDic["DrySpeed"] = false;
  180. PropertyValidResultDic["DryTime"] = false;
  181. PropertyValidResultDic["DryZoffset"] = false;
  182. PropertyValidResultDic["IntervalRinseSpeed"] = false;
  183. PropertyValidResultDic["IntervalRinseTime"] = false;
  184. PropertyValidResultDic["IntervalRinseZoffset"] = false;
  185. }
  186. /// <summary>
  187. /// 加载数据
  188. /// </summary>
  189. public void LoadRecipeData()
  190. {
  191. RecipeNodes = _uiRecipeManager.GetRecipesByType(DQDR);
  192. _recipeNodeDic.Clear();
  193. InitializeDictionary(RecipeNodes);
  194. }
  195. /// <summary>
  196. /// 初始化字典
  197. /// </summary>
  198. /// <param name="nodes"></param>
  199. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  200. {
  201. if (nodes != null)
  202. {
  203. foreach (var node in nodes)
  204. {
  205. if (node.NodeType == RecipeNodeType.File)
  206. {
  207. _recipeNodeDic[node.Name] = node;
  208. }
  209. InitializeDictionary(node.Children);
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 操作
  215. /// </summary>
  216. /// <param name="param"></param>
  217. private void SelectionChangedAction(object param)
  218. {
  219. if (_recipeNodeDic.ContainsKey(param.ToString()))
  220. {
  221. CurrentNode = _recipeNodeDic[param.ToString()];
  222. if (CurrentNode.NodeType == RecipeNodeType.File)
  223. {
  224. if (CurrentNode.RecipeLocation == ENGINEERING)
  225. {
  226. EditEnable = true;
  227. CreateEnable = true;
  228. }
  229. else
  230. {
  231. EditEnable = false;
  232. CreateEnable = false;
  233. }
  234. }
  235. Recipe = _uiRecipeManager.LoadRecipe<DqdrRecipe>(CurrentNode.RecipeFullFileName);
  236. if (Recipe == null)
  237. {
  238. MessageBox.Show("Invalid Recipe", "Load Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  239. EditEnable = false;
  240. CreateEnable = false;
  241. }
  242. Enable = false;
  243. }
  244. else
  245. {
  246. if (param.ToString() == ENGINEERING)
  247. {
  248. CreateEnable = true;
  249. }
  250. else
  251. {
  252. CreateEnable = false;
  253. }
  254. CurrentNode = null;
  255. Recipe = null;
  256. EditEnable = false;
  257. Enable = false;
  258. }
  259. }
  260. #region Action
  261. /// <summary>
  262. /// 创建
  263. /// </summary>
  264. /// <param name="param"></param>
  265. private void CreateAction(object param)
  266. {
  267. RecipeNameDialog recipeNameDialog = new RecipeNameDialog();
  268. if (recipeNameDialog.ShowDialog().Value)
  269. {
  270. Recipe = new DqdrRecipe();
  271. Recipe.CreateDate = DateTime.Now;
  272. Recipe.Ppid = recipeNameDialog.RecipeName;
  273. Recipe.Description = recipeNameDialog.RecipeDescription;
  274. Enable = true;
  275. _isEdit = false;
  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<DqdrRecipe>(ENGINEERING, Recipe.Ppid, "dqdr", 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. DqdrRecipe recipe = new DqdrRecipe();
  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 DqdrRecipe();
  329. recipe = (DqdrRecipe)PropertyUtil.Clone(Recipe);
  330. recipe.CreateDate = DateTime.Now;
  331. recipe.Ppid = recipeNameDialog.RecipeName;
  332. recipe.Description = recipeNameDialog.RecipeDescription;
  333. }
  334. else if (recipeNameDialog.RecipeName != null)
  335. {
  336. MessageBox.Show("Name can not be Null", "Create Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  337. return;
  338. }
  339. else
  340. {
  341. return;
  342. }
  343. }
  344. try
  345. {
  346. _uiRecipeManager.SaveRecipe<DqdrRecipe>(ENGINEERING, recipe.Ppid, "dqdr", recipe);
  347. LoadRecipeData();
  348. MessageBox.Show("Save As Recipe Success", "SaveAs Recipe", MessageBoxButton.OK, MessageBoxImage.Information);
  349. Enable = false;
  350. }
  351. catch (Exception ex)
  352. {
  353. MessageBox.Show(ex.Message, "Save As Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  354. }
  355. }
  356. }
  357. /// <summary>
  358. /// 检验名称是否已经存在
  359. /// </summary>
  360. /// <param name="name"></param>
  361. /// <returns></returns>
  362. private bool CheckNameExist(string name)
  363. {
  364. foreach (string item in _recipeNodeDic.Keys)
  365. {
  366. if (item == name)
  367. {
  368. MessageBox.Show($"{name} is exsit", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  369. return true;
  370. }
  371. }
  372. return false;
  373. }
  374. /// <summary>
  375. /// 检验合法性
  376. /// </summary>
  377. /// <param name="editType"></param>
  378. /// <returns></returns>
  379. private bool CheckValid(bool editType)
  380. {
  381. foreach (string key in _propertyValidResultDic.Keys)
  382. {
  383. bool valid = _propertyValidResultDic[key];
  384. if (!valid)
  385. {
  386. MessageBox.Show($"{key} data invalid", "Save Recipe", MessageBoxButton.OK, MessageBoxImage.Error);
  387. return false;
  388. }
  389. }
  390. return true;
  391. }
  392. #endregion
  393. }
  394. }