RecipeTableSelectDialogViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. table.Name = _copyTable.Name;
  68. table.EndStatus = _copyTable.EndStatus;
  69. table.Steps.Clear();
  70. foreach (var step in _copyTable.Steps)
  71. {
  72. table.Steps.Add(step);
  73. }
  74. table.From = _copyTable.From == "origin" ? _copyTable.TableIndex.ToString() : _copyTable.From;
  75. table.UpdateTime = DateTime.Now;
  76. }
  77. else
  78. {
  79. if (CopyTable != null)
  80. {
  81. var table = Tables[SelectedIndex];
  82. table.Name = CopyTable.Name;
  83. table.EndStatus = CopyTable.EndStatus;
  84. table.Steps = new ObservableCollection<Tuple<int, string>>(CopyTable.TableData.Steps.Select(r => Tuple.Create(r.StepNo, r.Name)));
  85. table.From = "copy";
  86. table.UpdateTime = DateTime.Now;
  87. }
  88. }
  89. }
  90. public void Clear()
  91. {
  92. if (SelectedIndex < 0)
  93. {
  94. DialogBox.ShowWarning("No table is selected");
  95. return;
  96. }
  97. if (!DialogBox.Confirm("Clear this table?"))
  98. return;
  99. // SelectedTable.Name = "";
  100. SelectedTable.EndStatus = "";
  101. if (SelectedTable.Steps.Count > 0)
  102. {
  103. if (SelectedIndex == 0)
  104. {
  105. if (SelectedTable.Steps.Count < 2)
  106. {
  107. DialogBox.ShowInfo("Table1 have one step at least");
  108. return;
  109. }
  110. var step = SelectedTable.Steps.First();
  111. if (step != null)
  112. {
  113. SelectedTable.Steps.Clear();
  114. SelectedTable.Steps.Add(step);
  115. }
  116. }
  117. else SelectedTable.Steps.Clear();
  118. SelectedTable.From = "clear";
  119. SelectedTable.UpdateTime = DateTime.Now;
  120. }
  121. }
  122. public void NotSelect()
  123. {
  124. SelectedIndex = -1;
  125. }
  126. public void CancelCmd()
  127. {
  128. ((Window)GetView()).DialogResult = false;
  129. }
  130. public void SaveCmd()
  131. {
  132. //判断名称是否重复
  133. for (int i = 0; i < Tables.Count; i++)
  134. {
  135. if (Tables[i]?.Steps?.Count > 0)
  136. {
  137. var tablename = Tables[i].Name.Trim();
  138. for (int j = i + 1; j < Tables.Count; j++)
  139. {
  140. if (Tables[j]?.Steps?.Count > 0)
  141. {
  142. if (tablename == Tables[j].Name.Trim())
  143. {
  144. DialogBox.ShowError($"Table{i + 1} and Table{j + 1} couldn't have same table name [{tablename}]");
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. #region 实际处理recipe
  152. var copytables = Tables.OrderBy(r => r.UpdateTime);
  153. foreach (var table in copytables)
  154. {
  155. Recipe.Tables[table.TableIndex - 1].Name = table.Name;
  156. Recipe.Tables[table.TableIndex - 1].EndStatus = table.EndStatus;
  157. if (table.From == "origin") continue;
  158. if (table.From == "clear")
  159. {
  160. if (table.Steps?.Count > 0)//复制第一步可能是1步
  161. {
  162. var step = Recipe.Tables[0].TableData.Steps.FirstOrDefault();
  163. Recipe.Tables[table.TableIndex - 1].TableData.Steps?.Clear();
  164. if (step != null)
  165. {
  166. if (Recipe.Tables[table.TableIndex - 1].TableData.Steps == null)
  167. Recipe.Tables[table.TableIndex - 1].TableData.Steps = new ObservableCollection<Step> { step };
  168. else Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(step);
  169. }
  170. }
  171. else
  172. {
  173. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  174. }
  175. }
  176. else if (table.From == "copy")
  177. {
  178. if (CopyTable != null)
  179. {
  180. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  181. foreach (var item in CopyTable.TableData.Steps)
  182. {
  183. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt<Step>());
  184. }
  185. }
  186. }
  187. else if (int.TryParse(table.From, out int no))
  188. {
  189. if (no > 0)
  190. {
  191. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear();
  192. foreach (var item in Recipe.Tables[no - 1].TableData.Steps)
  193. {
  194. Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt<Step>());
  195. }
  196. }
  197. }
  198. }
  199. #endregion
  200. #region 2025.8.30 工艺要求实现不同recipe的table 复制
  201. if (_copyTable != null && _copyTable.TableIndex > 0)
  202. {
  203. CopyTable = Recipe.Tables[_copyTable.TableIndex - 1].Adapt<RecipeTable>();
  204. }
  205. #endregion
  206. if (SelectedIndex < 0)
  207. {
  208. Recipe.TableIndex = -1;
  209. Recipe.Steps = null;
  210. }
  211. else
  212. {
  213. Recipe.TableIndex = SelectedTable.TableIndex;
  214. Recipe.Steps.Clear();
  215. Recipe.Tables[SelectedIndex].TableData.Steps.ToList().ForEach(x =>
  216. {
  217. var tempStep = Recipe.Tables[SelectedIndex].TableData.CreateStep(x);
  218. tempStep.StepNo = x.StepNo;
  219. Recipe.Steps.Add(tempStep);
  220. });
  221. }
  222. ((Window)GetView()).DialogResult = true;
  223. }
  224. }
  225. public class TableEdit : NotifyPropertyBase
  226. {
  227. public int TableIndex { get; set; }
  228. private string _name;
  229. public string Name
  230. {
  231. get { return _name; }
  232. set
  233. {
  234. if (_name != value)
  235. {
  236. _name = value;
  237. RaisePropertyChanged();
  238. }
  239. }
  240. }
  241. public ObservableCollection<Tuple<int, string>> Steps { get; set; }
  242. private string _endStatus;
  243. public string EndStatus
  244. {
  245. get { return _endStatus; }
  246. set
  247. {
  248. if (_endStatus != value)
  249. {
  250. _endStatus = value;
  251. RaisePropertyChanged();
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// 来自哪里
  257. /// </summary>
  258. public string From { get; set; }
  259. public DateTime UpdateTime { get; set; }
  260. }
  261. }