using FurnaceUI.Models; using MECF.Framework.UI.Client.CenterViews.Editors.Recipe; using OpenSEMI.ClientBase; using System.Windows; using System.Linq; using MECF.Framework.Common.CommonData; using System.Collections.ObjectModel; using System; using System.Collections.Generic; using Mapster; namespace FurnaceUI.Views.Recipes { public class RecipeTableSelectDialogViewModel : FurnaceUIViewModelBase { public List Tables { get; set; } = new List(); private TableEdit _copyTable; public RecipeTable CopyTable { get; set; } public RecipeDataBase Recipe { get; set; } private int _SelectedIndex; public int SelectedIndex { get { return _SelectedIndex; } set { _SelectedIndex = value; this.NotifyOfPropertyChange(nameof(SelectedIndex)); } } public TableEdit SelectedTable => (Tables.Count > SelectedIndex && SelectedIndex >= 0) ? Tables[SelectedIndex] : null; public bool IsEditEnable { get; set; } = true; public bool IsAlarmRecipe { get; set; } protected override void OnInitialize() { for (int i = 0; i < Recipe.Tables.Count; i++) { var table = Recipe.Tables[i]; if (table != null) { Tables.Add(new TableEdit { TableIndex = table.Index,//从1开始 Name = table.Name, Steps = new ObservableCollection>(table.TableData.Steps.Select(r => Tuple.Create(r.StepNo, r.Name))), EndStatus = table.EndStatus, From = "origin", UpdateTime = DateTime.MinValue }); } else Tables.Add(new TableEdit { TableIndex = i + 1 }); } base.OnInitialize(); SelectedIndex = Recipe.TableIndex - 1; } public void Copy() { if (!DialogBox.Confirm("Copy this table?")) return; _copyTable = SelectedTable; } public void Paste() { if (!DialogBox.Confirm("Paste the copied table?")) return; if (_copyTable != null) { var table = Tables[SelectedIndex]; if (table == _copyTable) return; table.Name = _copyTable.Name; table.EndStatus = _copyTable.EndStatus; table.Steps.Clear(); foreach (var step in _copyTable.Steps) { table.Steps.Add(step); } table.From = _copyTable.From == "origin" ? _copyTable.TableIndex.ToString() : _copyTable.From; table.UpdateTime = DateTime.Now; } else { if (CopyTable != null) { var table = Tables[SelectedIndex]; table.Name = CopyTable.Name; table.EndStatus = CopyTable.EndStatus; table.Steps = new ObservableCollection>(CopyTable.TableData.Steps.Select(r => Tuple.Create(r.StepNo, r.Name))); table.From = "copy"; table.UpdateTime = DateTime.Now; } } } public void Clear() { if (SelectedIndex < 0) { DialogBox.ShowWarning("No table is selected"); return; } if (!DialogBox.Confirm("Clear this table?")) return; // SelectedTable.Name = ""; SelectedTable.EndStatus = ""; if (SelectedTable.Steps.Count > 0) { if (SelectedIndex == 0) { if (SelectedTable.Steps.Count < 2) { DialogBox.ShowInfo("Table1 have one step at least"); return; } var step = SelectedTable.Steps.First(); if (step != null) { SelectedTable.Steps.Clear(); SelectedTable.Steps.Add(step); } } else SelectedTable.Steps.Clear(); SelectedTable.From = "clear"; SelectedTable.UpdateTime = DateTime.Now; } } public void NotSelect() { SelectedIndex = -1; } public void CancelCmd() { ((Window)GetView()).DialogResult = false; } public void SaveCmd() { //判断名称是否重复 for (int i = 0; i < Tables.Count; i++) { if (Tables[i]?.Steps?.Count > 0) { var tablename = Tables[i].Name.Trim(); for (int j = i + 1; j < Tables.Count; j++) { if (Tables[j]?.Steps?.Count > 0) { if (tablename == Tables[j].Name.Trim()) { DialogBox.ShowError($"Table{i + 1} and Table{j + 1} couldn't have same table name [{tablename}]"); return; } } } } } #region 实际处理recipe var copytables = Tables.OrderBy(r => r.UpdateTime); foreach (var table in copytables) { Recipe.Tables[table.TableIndex - 1].Name = table.Name; Recipe.Tables[table.TableIndex - 1].EndStatus = table.EndStatus; if (table.From == "origin") continue; if (table.From == "clear") { if (table.Steps?.Count > 0)//复制第一步可能是1步 { var step = Recipe.Tables[0].TableData.Steps.FirstOrDefault(); Recipe.Tables[table.TableIndex - 1].TableData.Steps?.Clear(); if (step != null) { if (Recipe.Tables[table.TableIndex - 1].TableData.Steps == null) Recipe.Tables[table.TableIndex - 1].TableData.Steps = new ObservableCollection { step }; else Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(step); } } else { Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear(); } } else if (table.From == "copy") { if (CopyTable != null) { Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear(); foreach (var item in CopyTable.TableData.Steps) { Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt()); } } } else if (int.TryParse(table.From, out int no)) { if (no > 0) { Recipe.Tables[table.TableIndex - 1].TableData.Steps.Clear(); foreach (var item in Recipe.Tables[no - 1].TableData.Steps) { Recipe.Tables[table.TableIndex - 1].TableData.Steps.Add(item.Adapt()); } } } } #endregion #region 2025.8.30 工艺要求实现不同recipe的table 复制 if (_copyTable != null && _copyTable.TableIndex > 0) { CopyTable = Recipe.Tables[_copyTable.TableIndex - 1].Adapt(); } #endregion if (SelectedIndex < 0) { Recipe.TableIndex = -1; Recipe.Steps = null; } else { Recipe.TableIndex = SelectedTable.TableIndex; Recipe.Steps.Clear(); Recipe.Tables[SelectedIndex].TableData.Steps.ToList().ForEach(x => { var tempStep = Recipe.Tables[SelectedIndex].TableData.CreateStep(x); tempStep.StepNo = x.StepNo; Recipe.Steps.Add(tempStep); }); } ((Window)GetView()).DialogResult = true; } } public class TableEdit : NotifyPropertyBase { public int TableIndex { get; set; } private string _name; public string Name { get { return _name; } set { if (_name != value) { _name = value; RaisePropertyChanged(); } } } public ObservableCollection> Steps { get; set; } private string _endStatus; public string EndStatus { get { return _endStatus; } set { if (_endStatus != value) { _endStatus = value; RaisePropertyChanged(); } } } /// /// 来自哪里 /// public string From { get; set; } public DateTime UpdateTime { get; set; } } }