Sic.Framework-Nanjing-Baishi/MECF.Framework.UI.Client/RecipeEditorLib/RecipeModel/RecipeStep.cs

370 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params;
namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel
{
public class RecipeStep : ObservableCollection<Param>
{
/// <summary>
/// 步骤起始编号。
/// </summary>
public const int START_INDEX = 1;
#region Variables
private bool _isHideValue;
private bool _isProcessed;
/// <summary>
/// 当配方步骤的IsSaved属性发生变化时触发此事件。
/// </summary>
public event EventHandler<bool> SavedStateChanged;
#endregion
#region Constructors
public RecipeStep(RecipeStep previous)
{
Previous = previous;
IsSaved = true;
}
#endregion
#region Properties
/// <summary>
/// 父级配方集合。
/// </summary>
public RecipeData Parent { get; internal set; }
/// <summary>
/// 返回配方步骤序号参数。
/// </summary>
public StepParam StepNoParam => this.FirstOrDefault(x => x is StepParam) as StepParam;
public StringParam UidParam => this.FirstOrDefault(param => param.DisplayName == "Uid") as StringParam;
/// <summary>
/// 当前配方步骤的唯一识别码。
/// </summary>
public string StepUid
{
get => UidParam?.Value ?? null;
set
{
var paramUid = UidParam;
if (paramUid != null)
{
paramUid.Value = value;
}
}
}
/// <summary>
/// 返回步骤序号。
/// </summary>
public int? StepNo
{
get
{
var param = StepNoParam;
return param?.Value ?? null;
}
}
/// <summary>
/// 返回步骤执行时长。
/// </summary>
public double StepTime
{
get
{
var param = FindParamByControlName(RecipeControlNames.STEP_TIME);
if(param is DoubleParam dblParam)
return dblParam.Value;
return 0d;
}
}
/// <summary>
/// 返回当前配方步骤是否已保存。
/// </summary>
public bool IsSaved { get; private set; }
/// <summary>
/// 返回当前步骤是否已经跑完工艺。
/// </summary>
public bool IsProcessed
{
get => _isProcessed;
set
{
if (_isProcessed != value)
{
_isProcessed = value;
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsProcessed)));
}
}
}
/// <summary>
/// 返回前序配方。
/// </summary>
public RecipeStep Previous { get; private set; }
/// <summary>
/// 返回后序配方。
/// </summary>
public RecipeStep Next { get; private set; }
/// <summary>
/// 返回是否隐藏当前步骤的参数值。
/// </summary>
public bool IsHideValue
{
get => _isHideValue;
set
{
_isHideValue = value;
this.ToList().ForEach(x => x.IsHideValue = value);
}
}
#endregion
#region Methods
/// <summary>
/// 当配方中的参数的属性发生变化时,触发此事件。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ParamOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (!(sender is IParam param))
return;
if (e.PropertyName == nameof(Param.IsSaved))
{
if (param.IsSaved == false)
{
IsSaved = false;
}
else
{
IsSaved = this.ToList().FirstOrDefault(x => x.IsSaved == false) == null;
}
SavedStateChanged?.Invoke(this, IsSaved);
}
}
/// <summary>
/// 将指定的Param链接到前序Param。
/// </summary>
/// <param name="param">当前Step中的参数。</param>
private void LinkWithPreviousParam(IParam param)
{
var preParam = Previous?.FirstOrDefault(x => x.Name == param.Name);
param.Previous = preParam;
if (preParam != null)
preParam.Next = param;
}
/// <summary>
/// 将指定的Param链接到后序Param。
/// </summary>
/// <param name="param">当前Step中的参数。</param>
private void LinkWithNextParam(IParam param)
{
var nextParam = Next?.FirstOrDefault(x => x.Name == param.Name);
param.Next = nextParam;
if (nextParam != null)
nextParam.Previous = param;
}
/// <summary>
/// 在当前步骤中查找指定控制名的参数。
/// </summary>
/// <param name="controlName">
/// 参数控制的名称。
/// </param>
/// <param name="isIgnoreCase">
/// 是否区分大小写。
/// </param>
/// <returns></returns>
public IParam FindParamByControlName(string controlName, bool isIgnoreCase = true)
{
return this.FirstOrDefault(x =>
string.Equals(x.Name, controlName,
isIgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture));
}
/// <summary>
/// 校验步骤中的所有参数。
/// </summary>
public void Validate()
{
this.ToList().ForEach(x => x.Validate());
}
/// <summary>
/// 计算当前步骤的气体流量。
/// </summary>
public void CalculateGasFlow()
{
Parent?.CalculateGasFlow(this);
}
/// <summary>
/// 取消所有参数的Highlight状态。
/// </summary>
public void ResetHighlight()
{
this.ToList().ForEach(x => x.ResetHighlight());
}
/// <summary>
/// 获取高亮显示的参数。
/// </summary>
/// <returns></returns>
public List<Param> GetHighlightedParams()
{
return this.ToList().Where(p => p.IsHighlighted).ToList();
}
/// <summary>
/// 保存当前配方步骤。
/// </summary>
public void Save()
{
this.ToList().ForEach(x => x.Save());
}
/// <summary>
/// 設置前序步驟。
/// </summary>
/// <param name="step"></param>
public void SetPreviousStep(RecipeStep step)
{
Previous = step;
// 调整Step中Param的链接关系
this.ToList().ForEach(LinkWithPreviousParam);
if (Previous == null)
return;
// 将前序步骤的后序步骤链接到我自己。
Previous.Next = this;
}
public void SetNextStep(RecipeStep step)
{
Next = step;
this.ToList().ForEach(LinkWithNextParam);
}
/// <summary>
/// 检查StepNo是否为有效值。
/// </summary>
/// <param name="stepNo"></param>
/// <param name="validatedStepNo"></param>
/// <returns></returns>
public static bool ValidateStepNo(int? stepNo, out int validatedStepNo)
{
validatedStepNo = int.MinValue;
var isValid = stepNo >= START_INDEX;
if (isValid)
validatedStepNo = stepNo.Value;
return isValid;
}
#endregion
#region Override Methods
protected override void InsertItem(int index, Param item)
{
if (item != null)
{
item.Parent = this;
LinkWithPreviousParam(item);
LinkWithNextParam(item);
item.PropertyChanged += ParamOnPropertyChanged;
}
base.InsertItem(index, item);
}
protected override void SetItem(int index, Param item)
{
throw new NotSupportedException();
}
protected override void RemoveItem(int index)
{
if (Count > 0)
{
if (index >= 0 && index < Count)
{
this[index].PropertyChanged -= ParamOnPropertyChanged;
}
}
// 将前后两个Param链接起来
var preParam = this[index].Previous;
var nextParam = this[index].Next;
if (preParam != null && nextParam != null)
{
preParam.Next = nextParam;
nextParam.Previous = preParam;
}
else if (preParam == null && nextParam != null)
{
nextParam.Previous = null;
}
else if (preParam != null)
{
preParam.Next = null;
}
IsSaved = false;
SavedStateChanged?.Invoke(this, true);
base.RemoveItem(index);
}
protected override void ClearItems()
{
this.ToList().ForEach(x => x.PropertyChanged -= ParamOnPropertyChanged);
IsSaved = Count <= 0;
SavedStateChanged?.Invoke(this, true);
base.ClearItems();
}
public override string ToString()
{
return Count > 0 ? (StepNoParam == null ? "Recipe Step, Item={Count}" : StepNoParam.ToString()) : "Empty Recipe Step";
}
#endregion
}
}