Files
6098/Cowain.Bake.UI/Home/ViewModels/ProcessParametersViewModel.cs

329 lines
13 KiB
C#
Raw Normal View History

using Cowain.Bake.BLL;
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Common.Enums;
using Cowain.Bake.Model;
using Cowain.Bake.Model.Entity;
using HandyControl.Controls;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Regions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Unity;
using static Cowain.Bake.Common.Models.MESModel;
namespace Cowain.Bake.UI.Home.ViewModels
{
public class ProcessParametersViewModel : ViewModelBase, INavigationAware
{
private List<string> processParamNames;
public List<string> ProcessParamNames
{
get => processParamNames;
set => SetProperty(ref processParamNames, value);
}
private string selectedProcessParamName;
public string SelectedProcessParamName
{
get => selectedProcessParamName;
//set => SetProperty(ref selectedProcessParam, value);
set
{
SetProperty(ref selectedProcessParamName, value);
SelectionProcessParamChanged(value); //选择了配方,参数也要跟着变
}
}
private string newProcessParamName;
public string NewProcessParamName
{
get => newProcessParamName;
set => SetProperty(ref newProcessParamName, value);
}
private bool isAllowed;
public bool IsAllowed
{
get => isAllowed;
set => SetProperty(ref isAllowed, value);
}
public event PropertyChangedEventHandler NotifyPropertyChanged;
public void OnPropertyChanged(string propName)
{
if (NotifyPropertyChanged != null)
{
NotifyPropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
//参数列表
private ObservableCollection<EqptParameterModel> paramList;
public ObservableCollection<EqptParameterModel> ParamList
{
get => paramList ?? (paramList = new ObservableCollection<EqptParameterModel>());
set
{
if (value != paramList)
{
paramList = value;
OnPropertyChanged(nameof(ParamList));
}
}
}
private ObservableCollection<WorkStepEntity> workStepList;
public ObservableCollection<WorkStepEntity> WorkStepList
{
get => workStepList ?? (workStepList = new ObservableCollection<WorkStepEntity>());
set
{
if (value != workStepList)
{
workStepList = value;
OnPropertyChanged(nameof(WorkStepList));
}
}
}
TProcessParameter SelectProcessParam = null;
List<EqptParameterModel> OperNodes = null;
public ProcessParametersViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
{
this.PageTitle = "配方参数";
Refresh();
isAllowed = unityContainer.Resolve<UserService>().CheckPermission(ERole.Mantainer);
}
private void SelectionProcessParamChanged(string processParamName)
{
SelectProcessParam = _unityContainer.Resolve<ProcessParamService>().QueryFormula(processParamName).FirstOrDefault();
if (null != SelectProcessParam)
{
ParamList.Clear();
WorkStepList.Clear();
OperNodes = JsonConvert.DeserializeObject<List<EqptParameterModel>>(SelectProcessParam.Parameters);
// Application.Current.Dispatcher.Invoke(() =>
//{
ParamList.AddRange(OperNodes); //现场耗时0.3S本机耗时2S
//});
List<WorkStepEntity> workSteps = GetWorkStepData(OperNodes);
WorkStepList.AddRange(workSteps);
}
}
List<WorkStepEntity> GetWorkStepData(List<EqptParameterModel> ops)
{
List<WorkStepEntity> workSteps = new List<WorkStepEntity>();
workSteps.Add(SetSingleData(EStovePLC.HeatingEnabled.ToString(), EStovePLC.HeatingEnabled.GetDescription(), ops) );
workSteps.Add(SetSingleData(EStovePLC.VacuumEnabled.ToString().ToString(), EStovePLC.VacuumEnabled.GetDescription(), ops));
workSteps.Add(SetSingleData(EStovePLC.NitrogenEnabled.ToString(), EStovePLC.NitrogenEnabled.GetDescription(), ops));
workSteps.Add(SetSingleData(EStovePLC.StepWorkTime.ToString(), EStovePLC.StepWorkTime.GetDescription(), ops));
return workSteps;
}
WorkStepEntity SetSingleData(string key, string desc, List<EqptParameterModel> ops)
{
WorkStepEntity wse = new WorkStepEntity();
wse.WorkStepName = desc.Substring(3, desc.Length - 3);
foreach (var item in ops)
{
if (item.ParameterCode.ToLower().StartsWith(key.ToLower())) //在JOSN里面数组一定要按顺序排或者顺序乱了
{
string pattern = @"\d+";
Match match = Regex.Match(item.ParameterCode, pattern);
int value = System.Convert.ToInt32(item.TargetValue);
PropertyInfo propertyInfo = typeof(WorkStepEntity).GetProperty("WorkStep" + match.Value);
propertyInfo.SetValue(wse, value);
}
}
return wse;
}
public override void Refresh()
{
List<TProcessParameter> ProcessParams = _unityContainer.Resolve<ProcessParamService>().GetAllFormula().ToList();
ProcessParamNames = ProcessParams.Select(x => x.ProcessParamName).ToList();
if (0 != ProcessParamNames.Count)
{
var currentJobNum = _unityContainer.Resolve<ProductionInformationService>().GetCurrentProductInfo();
var processParams = _unityContainer.Resolve<ProcessParamService>().Get(currentJobNum.ProcessParamId);
SelectedProcessParamName = processParams.ProcessParamName; //赋值给当前配方
}
}
public DelegateCommand<object> AddCommand => new DelegateCommand<object>((x) =>
{
if (string.IsNullOrEmpty(NewProcessParamName))
{
Growl.Error("配方名称不能为空!");
return;
}
if (!_unityContainer.Resolve<ProcessParamService>().CreateNewProcessParam(newProcessParamName))
{
Growl.Error("配方名称重复!");
return;
}
Refresh();
NewProcessParamName = "";
Growl.Info("新增配方成功!");
});
public DelegateCommand<object> DeleteCommand => new DelegateCommand<object>((x) =>
{
if (0 == WorkStepList.Count)
{
LogHelper.Instance.Error("没有工步数据,修改失败");
}
if (!string.IsNullOrEmpty(SelectedProcessParamName))
{
var currentProductInfo = _unityContainer.Resolve<ProductionInformationService>().GetCurrentProductInfo();
if (SelectProcessParam.Id == currentProductInfo.ProcessParamId)
{
HandyControl.Controls.MessageBox.Fatal($@"此配方有工单正在使用,请修改当前使用工单后重试", "操作提示");
return;
}
var result1 = HandyControl.Controls.MessageBox.Ask($@"确定删除此配方?该操作会删除所有绑定此配方的工单", "操作提示");
if (result1 == System.Windows.MessageBoxResult.Cancel)
{
return;
}
_unityContainer.Resolve<ProcessParamService>().Delete(SelectProcessParam); //删除配方
_unityContainer.Resolve<ProductionInformationService>().Delete(SelectProcessParam.Id); //删除TProductionInformation里的对应配方
_unityContainer.Resolve<LogService>().AddLog($"删除配方:{SelectProcessParam.ProcessParamName}" , E_LogType.Operate.ToString());
Refresh();
}
});
bool IsTypeMatching(string type, string value, string desc) //数据类型不匹配
{
string regular = "";
if (BasicFramework.Instance.RegularDic.ContainsKey(type))
{
regular = BasicFramework.Instance.RegularDic[type];
}
else
{
regular = BasicFramework.Instance.RegularDic["Int32"];
}
Regex regex = new Regex(regular);
if (!regex.IsMatch(value))
{
LogHelper.Instance.Error($"数据类型:{type},值:{value},节点:{desc},数据类型与值不匹配!", true);
return false;
}
return true;
}
//ParamList
bool IsMatchingBaseData()
{
if (ParamList.Count != OperNodes.Count
|| OperNodes.Count < 28) //正常是107个参数
{
return false;
}
for (int i = 0; i <= 28; i++)
{
if (!IsTypeMatching(ParamList[i].ParameterType, ParamList[i].TargetValue, ParamList[i].Description))
{
return false;
}
}
return true;
}
public DelegateCommand<object> ModifyCommand => new DelegateCommand<object>((x) =>
{
if (!IsMatchingBaseData())
{
return;
}
if (0 == WorkStepList.Count)
{
LogHelper.Instance.Error("没有工步数据,修改失败", true);
return;
}
if (SetWorkStep(WorkStepList[0], EStovePLC.HeatingEnabled.ToString())
&& SetWorkStep(WorkStepList[1], EStovePLC.VacuumEnabled.ToString())
&& SetWorkStep(WorkStepList[2], EStovePLC.NitrogenEnabled.ToString())
&& SetWorkStep(WorkStepList[3], EStovePLC.StepWorkTime.ToString()))
{
SelectProcessParam.Parameters = JsonConvert.SerializeObject(ParamList); //JsonConvert.SerializeObject(OperNodes);
if (0 == _unityContainer.Resolve<ProcessParamService>().Update<TProcessParameter>(SelectProcessParam))
{
LogHelper.Instance.Error("修改配方参数失败!", true);
return;
}
HandyControl.Controls.MessageBox.Success("修改配方参数成功!");
}
});
bool SetWorkStep(WorkStepEntity wse, string key) //WorkStepList->OperNodes赋值
{
foreach (var item in ParamList)
{
if (item.ParameterCode.ToLower().StartsWith(key.ToLower()))
{
string pattern = @"\d+";
Match match = Regex.Match(item.ParameterCode, pattern);
string value = GetValue(wse, match.Value);
if (!IsTypeMatching(item.ParameterType, value, item.Description))
{
return false ;
}
PropertyInfo propertyInfo = typeof(EqptParameterModel).GetProperty("TargetValue");
propertyInfo.SetValue(item, value);
}
}
return true;
}
string GetValue(WorkStepEntity wse, string number)
{
PropertyInfo propertyInfo = typeof(WorkStepEntity).GetProperty("WorkStep" + number);
return propertyInfo.GetValue(wse).ToString();
}
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
// 导航到当前页面时的处理逻辑,先执行构造方法,再执行本方法
}
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
{
return false; //false:表示每次导航都创建新实例,不重用旧实例; true:用旧实例,不会执行构造方法
}
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
//相当于析构方法,退出时(切换时)再执行。
// 在这里添加释放旧视图实例资源的代码
}
}
}