RecipeTableSelectDialogViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using FurnaceUI.Models;
  2. using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
  3. using OpenSEMI.ClientBase;
  4. using System.Windows;
  5. using System.Linq;
  6. using MECF.Framework.Common.CommonData;
  7. using System.Collections.ObjectModel;
  8. using System;
  9. using System.Collections.Generic;
  10. using Mapster;
  11. namespace FurnaceUI.Views.Recipes
  12. {
  13. public class RecipeTableSelectDialogViewModel : FurnaceUIViewModelBase
  14. {
  15. public List<TableEdit> Tables { get; set; } = new List<TableEdit>();
  16. private TableEdit _copyTable;
  17. public RecipeTable CopyTable { get; set; }
  18. public RecipeDataBase Recipe { get; set; }
  19. private int _SelectedIndex;
  20. public int SelectedIndex
  21. {
  22. get { return _SelectedIndex; }
  23. set
  24. {
  25. _SelectedIndex = value;
  26. this.NotifyOfPropertyChange(nameof(SelectedIndex));
  27. }
  28. }
  29. public TableEdit SelectedTable => (Tables.Count > SelectedIndex && SelectedIndex >= 0) ? Tables[SelectedIndex] : null;
  30. public bool IsEditEnable { get; set; } = true;
  31. public bool IsAlarmRecipe { get; set; }
  32. protected override void OnInitialize()
  33. {
  34. for (int i = 0; i < Recipe.Tables.Count; i++)
  35. {
  36. var table = Recipe.Tables[i];
  37. if (table != null)
  38. {
  39. Tables.Add(new TableEdit
  40. {
  41. TableIndex = table.Index,//从1开始
  42. Name = table.Name,
  43. Steps = new ObservableCollection<Tuple<int, string>>(table.TableData.Steps.Select(r => Tuple.Create(r.StepNo, r.Name))),
  44. EndStatus = table.EndStatus,
  45. From = "origin",
  46. UpdateTime = DateTime.MinValue
  47. });
  48. }
  49. else Tables.Add(new TableEdit { TableIndex = i + 1 });
  50. }
  51. base.OnInitialize();
  52. SelectedIndex = Recipe.TableIndex - 1;
  53. }
  54. public void Copy()
  55. {
  56. if (!DialogBox.Confirm("Copy this table?"))
  57. return;
  58. _copyTable = SelectedTable;
  59. }
  60. public void Paste()
  61. {
  62. if (!DialogBox.Confirm("Paste the copied table?"))
  63. return;
  64. if (_copyTable != null)
  65. {
  66. var table = Tables[SelectedIndex];
  67. if (table == _copyTable) return;
  68. table.Name = _copyTable.Name;
  69. table.EndStatus = _copyTable.EndStatus;
  70. table.Steps.Clear();
  71. foreach (var step in _copyTable.Steps)
  72. {
  73. table.Steps.Add(step);
  74. }
  75. table.From = _copyTable.From == "origin" ? _copyTable.TableIndex.ToString() : _copyTable.From;
  76. table.UpdateTime = DateTime.Now;
  77. }
  78. else
  79. {
  80. if (CopyTable != null)
  81. {
  82. var table = Tables[SelectedIndex];
  83. table.Name = CopyTable.Name;
  84. table.EndStatus = CopyTable.EndStatus;
  85. table.Steps = new ObservableCollection<Tuple<int, string>>(CopyTable.TableData.Steps.Select(r => Tuple.Create(r.StepNo, r.Name)));
  86. table.From = "copy";
  87. table.UpdateTime = DateTime.Now;
  88. }
  89. }
  90. }
  91. public void Clear()
  92. {
  93. if (SelectedIndex < 0)
  94. {
  95. DialogBox.ShowWarning("No table is selected");
  96. return;
  97. }
  98. if (!DialogBox.Confirm("Clear this table?"))
  99. return;
  100. // SelectedTable.Name = "";
  101. SelectedTable.EndStatus = "";
  102. if (SelectedTable.Steps.Count > 0)
  103. {
  104. if (SelectedIndex == 0)
  105. {
  106. if (SelectedTable.Steps.Count < 2)
  107. {
  108. DialogBox.ShowInfo("Table1 have one step at least");
  109. return;
  110. }
  111. var step = SelectedTable.Steps.First();
  112. if (step != null)
  113. {
  114. SelectedTable.Steps.Clear();
  115. SelectedTable.Steps.Add(step);
  116. }
  117. }
  118. else SelectedTable.Steps.Clear();
  119. SelectedTable.From = "clear";
  120. SelectedTable.UpdateTime = DateTime.Now;
  121. }
  122. }
  123. public void NotSelect()
  124. {
  125. SelectedIndex = -1;
  126. }
  127. public void CancelCmd()
  128. {
  129. ((Window)GetView()).DialogResult = false;
  130. }
  131. public void SaveCmd()
  132. {
  133. //判断名称是否重复
  134. for (int i = 0; i < Tables.Count; i++)
  135. {
  136. if (Tables[i]?.Steps?.Count > 0)
  137. {
  138. var tablename = Tables[i].Name.Trim();
  139. for (int j = i + 1; j < Tables.Count; j++)
  140. {
  141. if (Tables[j]?.Steps?.Count > 0)
  142. {
  143. if (tablename == Tables[j].Name.Trim())
  144. {
  145. DialogBox.ShowError($"Table{i + 1} and Table{j + 1} couldn't have same table name [{tablename}]");
  146. return;
  147. }
  148. }
  149. }
  150. }
  151. }
  152. #region 实际处理recipe
  153. var copytables = Tables.OrderBy(r => r.UpdateTime);
  154. foreach (var table in copytables)
  155. {
  156. Recipe.Tables[table.TableIndex - 1].Name = table.Name;
  157. Recipe.Tables[table.TableIndex - 1].EndStatus = table.EndStatus;
  158. if (table.From == "origin") continue;
  159. if (table.From == "clear")
  160. {
  161. if (table.Steps?.Count > 0)//复制第一步可能是1步
  162. {
  163. var step = Recipe.Tables[0].TableData.Steps.FirstOrDefault();
  164. Recipe.Tables[table.TableIndex - 1].TableData.Steps?.Clear();
  165. if (step != null)
  166. {
  167. if (Recipe.Tables[table.TableIndex - 1].TableData.Steps == null)
  168. Recipe.Tables[table.TableIndex - 1].TableData.Steps = new ObservableCollection<Step> { step };
  169. else Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(step);
  170. }
  171. }
  172. else
  173. {
  174. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  175. }
  176. }
  177. else if (table.From == "copy")
  178. {
  179. if (CopyTable != null)
  180. {
  181. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  182. foreach (var item in CopyTable.TableData.Steps)
  183. {
  184. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt<Step>());
  185. }
  186. }
  187. }
  188. else if (int.TryParse(table.From, out int no))
  189. {
  190. if (no > 0)
  191. {
  192. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  193. foreach (var item in Recipe.Tables[no - 1].TableData.Steps)
  194. {
  195. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt<Step>());
  196. }
  197. }
  198. }
  199. }
  200. #endregion
  201. #region 2025.8.30 工艺要求实现不同recipe的table 复制
  202. if (_copyTable != null && _copyTable.TableIndex > 0)
  203. {
  204. CopyTable = Recipe.Tables[_copyTable.TableIndex - 1].Adapt<RecipeTable>();
  205. }
  206. #endregion
  207. if (SelectedIndex < 0)
  208. {
  209. Recipe.TableIndex = -1;
  210. Recipe.Steps = null;
  211. }
  212. else
  213. {
  214. Recipe.TableIndex = SelectedTable.TableIndex;
  215. Recipe.Steps.Clear();
  216. Recipe.Tables[SelectedIndex].TableData.Steps.ToList().ForEach(x =>
  217. {
  218. var tempStep = Recipe.Tables[SelectedIndex].TableData.CreateStep(x);
  219. tempStep.StepNo = x.StepNo;
  220. Recipe.Steps.Add(tempStep);
  221. });
  222. }
  223. ((Window)GetView()).DialogResult = true;
  224. }
  225. }
  226. public class TableEdit : NotifyPropertyBase
  227. {
  228. public int TableIndex { get; set; }
  229. private string _name;
  230. public string Name
  231. {
  232. get { return _name; }
  233. set
  234. {
  235. if (_name != value)
  236. {
  237. _name = value;
  238. RaisePropertyChanged();
  239. }
  240. }
  241. }
  242. public ObservableCollection<Tuple<int, string>> Steps { get; set; }
  243. private string _endStatus;
  244. public string EndStatus
  245. {
  246. get { return _endStatus; }
  247. set
  248. {
  249. if (_endStatus != value)
  250. {
  251. _endStatus = value;
  252. RaisePropertyChanged();
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// 来自哪里
  258. /// </summary>
  259. public string From { get; set; }
  260. public DateTime UpdateTime { get; set; }
  261. }
  262. }