|
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.MorkCL.Model.Json;
- using BPASmartClient.MorkCL.Model.Recipe;
- using System.Windows.Controls;
- using System.Windows.Media.Animation;
- using System.Windows.Media;
-
- namespace BPASmartClient.MorkCL.ViewModel
- {
- public class EditRecipeViewModel : NotifyBase
- {
- private EFunc lastFuncName = EFunc.搅拌启动;
- public EditRecipeViewModel()
- {
- //AddTestData();
-
- Functions = new();
-
-
-
- ActionManage.GetInstance.Register((object o) =>
- {
- if (o != null && o is ControlData recipe)
- {
- this.Name = recipe.Name;
- this.ID = recipe.Id;
- this.DishType = recipe.DishType;
- int step = 0;
- Functions.Clear();
- foreach (var func in recipe.ControlFuncs)
- {
- step++;
- Functions.Add(new FuncModel() { eFunc = func.eFunc, funcPars = func.funcPars });
- }
- }
- }, "OpenRecipeEditView", true);
-
- AllFunc = new ObservableCollection<string>();
- foreach (var item in Enum.GetNames(typeof(EFunc)))
- {
- AllFunc.Add(item);
- }
-
- AllDishType = new ObservableCollection<string>();
- foreach (var item in Enum.GetNames(typeof(EDishType)))
- {
- AllDishType.Add(item);
- }
-
- SaveParamCommand = new BPARelayCommand(() =>
- {
- if (!MessageNotify.GetInstance.ShowDialog("请确认是否保存该配方?"))
- {
- return;
- }
-
- #region 数据验证
-
- if (String.IsNullOrEmpty(Name) || Name.Length <= 0)
- {
- MessageNotify.GetInstance.ShowDialog("配方名称不可为空,请重新输入后重试!", DialogType.Error);
- return;
- }
- if (Name.Equals("模板配方"))
- {
- MessageNotify.GetInstance.ShowDialog("配方名称不可为【模板配方】,请修改!", DialogType.Error);
- return;
- }
- //if (!int.TryParse(ID,out int Id))
- //{
- // MessageNotify.GetInstance.ShowDialog("配方ID不是数字,请重新输入后重试!");
- // return;
- //}
- if (Functions == null || Functions.Count <= 0)
- {
- MessageNotify.GetInstance.ShowDialog("没有可保存的配方功能配置,请验证后重试!", DialogType.Error);
- return;
- }
- foreach (FuncModel item in Functions)
- {
- for (int i = 0; i < item.funcPars.Count; i++)
- {
- var argument = item.funcPars[i].ParValue;
- if (argument == null || String.IsNullOrEmpty(argument.ToString()) || argument.ToString().Length <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的参数项未设置,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- switch (item.eFunc)
- {
- case EFunc.搅拌启动:
- if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int agitateSpeed))
- {
- if (agitateSpeed > 60 || agitateSpeed <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的搅拌速度限值0--60HZ,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的搅拌速度不是整数,请修改验证后重试!", DialogType.Error);
- return;
- }
- break;
-
- case EFunc.搅拌停止:
- break;
-
- case EFunc.加热启动:
- if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int heatGear))
- {
- if (heatGear > 8 || heatGear <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的加热档位限值1--8,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的加热档位不是整数,请修改验证后重试!", DialogType.Error);
- return;
- }
-
- break;
-
- case EFunc.加热停止:
- break;
-
- case EFunc.添加调料:
- if (double.TryParse(item.funcPars[1].ParValue?.ToString(), out double seasoningWeight))
- {
- if (seasoningWeight <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的调料需求值小于0,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的调料需求值不是纯数字,请修改验证后重试!", DialogType.Error);
- return;
- }
-
- if (!ValidateStirFryAndTime(item.funcPars[2].ParValue?.ToString(), item.funcPars[3].ParValue?.ToString()))
- {
- return;
- }
- break;
-
- case EFunc.添加主料:
- if (double.TryParse(item.funcPars[1].ParValue?.ToString(), out double ingredientsWeight))
- {
- if (ingredientsWeight <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的主料份量小于0,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的主料份量不是纯数字,请修改验证后重试!", DialogType.Error);
- return;
- }
-
- if (!ValidateStirFryAndTime(item.funcPars[2].ParValue?.ToString(), item.funcPars[3].ParValue?.ToString()))
- {
- return;
- }
-
- break;
-
- case EFunc.添加辅料:
- if (!ValidateStirFryAndTime(item.funcPars[1].ParValue?.ToString(), item.funcPars[2].ParValue?.ToString()))
- {
- return;
- }
- break;
-
- case EFunc.炒锅回原点位:
- break;
-
- case EFunc.出餐启动:
- break;
-
-
- case EFunc.炒锅清洗:
- break;
-
- //case EFunc.炒锅回调料投料位置:
- // break;
-
- case EFunc.去指定炒制位:
- if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int fryingPanLoc))
- {
- if (fryingPanLoc > 4 || fryingPanLoc <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制位限值1--4,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制不是整数,请修改验证后重试!", DialogType.Error);
- return;
- }
- break;
-
- case EFunc.炒制:
- if (int.TryParse(item.funcPars[0].ParValue.ToString(), out int stirFryTime))
- {
- if (stirFryTime <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间设置小于0,请修改验证后重试!", DialogType.Error);
- return;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间不是纯数字,请修改验证后重试!", DialogType.Error);
- return;
- }
- break;
-
- default:
- break;
- }
- //校验炒制位置和时间
- bool ValidateStirFryAndTime(string loc, string time)
- {
- if (int.TryParse(loc, out int fryingPanLoc))
- {
- if (fryingPanLoc > 4 || fryingPanLoc <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制位限值1--4,请修改验证后重试!", DialogType.Error);
- return false;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制不是整数,请修改验证后重试!", DialogType.Error);
- return false;
- }
-
- if (int.TryParse(time, out int stirFryTime))
- {
- if (stirFryTime <= 0)
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间设置小于0,请修改验证后重试!", DialogType.Error);
- return false;
- }
- }
- else
- {
- MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间不是纯数字,请修改验证后重试!", DialogType.Error);
- return false;
- }
-
- return true;
- }
- }
-
- #endregion 数据验证
-
- List<FuncModel> TempFunces = new List<FuncModel>();
- //查询是否是编辑现有配方。
- int index = Array.FindIndex(Json<RecipesInfo>.Data.LocalRecipes.ToArray(), p => p.Id == ID);
- //修改配方
- if (index >= 0)
- {
- foreach (var item in Functions)
- {
- //TempFunces.Add((EFunc)Enum.Parse(typeof(EFunc), item.FuncName), item.FuncPars?.ToArray());
- TempFunces.Add(new FuncModel()
- {
- eFunc = item.eFunc,
- funcPars = new List<FuncPar>(item.funcPars)
- });
- }
- Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).Id = ID;
- Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).Name = Name;
- Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).DishType = DishType;
- Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).ControlFuncs = new ConcurrentQueue<FuncModel>(TempFunces);
- Json<RecipesInfo>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"修改配方:{Name} 并保存成功!");
- }
- //没找到则为新增配方
- else
- {
- foreach (var item in Functions)
- {
- //TempFunces.Add((EFunc)Enum.Parse(typeof(EFunc), item.FuncName), item.FuncPars?.ToArray());
- TempFunces.Add(new FuncModel()
- {
- eFunc = item.eFunc,
- funcPars = new List<FuncPar>(item.funcPars)
- });
- }
- Json<RecipesInfo>.Data.LocalRecipes.Add(new ControlData()
- {
- Id = Guid.NewGuid().ToString(),
- Name = Name,
- Remark = " ",
- DishType = DishType,
- ControlFuncs = new ConcurrentQueue<FuncModel>(TempFunces)
- });
- Json<RecipesInfo>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"新增配方:{Name} 并保存成功!");
- }
- //关闭窗体。
- ActionManage.GetInstance.Send("CloseEditRecipeView");
- });
- AddFuncCommand = new BPARelayCommand(() =>
- {
- Functions.Add(new FuncModel()
- {
- eFunc = EFunc.搅拌启动,
- funcPars = InitData.FunParInit[EFunc.搅拌启动]?.ToList()
- });
- });
- FuncChangeCommand = new BPARelayCommand<object>((o) =>
- {
- if (o != null && o is FuncModel model)
- {
- if (model.eFunc != lastFuncName)
- {
- model.funcPars = InitData.FunParInit[model.eFunc].ToList();
- }
- }
- });
- DropDownOenCommand = new((o) =>
- {
- if (o != null && o is EFunc eFunc)
- {
- lastFuncName = eFunc;
- }
- });
- RemoveCommand = new BPARelayCommand<object>((o) =>
- {
- if (o != null && o is FuncModel func)
- {
- Functions.Remove(func);
- }
- });
-
- EditCommand = new BPARelayCommand<object>((o) =>
- {
- if (o is FuncModel funcSet)
- {
- EditFunctionParamView editFunction = new EditFunctionParamView();
- ActionManage.GetInstance.Send(o, "OpenFuncEditView");
- if (editFunction.ShowDialog() == true)
- {
- funcSet.funcPars.Clear();
- //funcSet.funcPars = EditFunctionParamViewModel.FuncPars?.ToList();
- foreach (var item in EditFunctionParamViewModel.FuncPars)
- {
- funcSet.funcPars.Add(new FuncPar() { Id = item.Id, ParName = item.ParName, ParValue = item.ParValue, ParUnit = item.ParUnit, ParDescribe = item.ParDescribe });
- //funcSet.funcPars.Add((FuncPar)item);
- }
- }
- }
- });
- MoveUpCommand = new((o) =>
- {
- if (o != null && o is FuncModel funcmodel)
- {
- var index = Functions.IndexOf(funcmodel);
- if (index > 0)
- {
- int newIndex = index - 1;
- //Functions.RemoveAt(index);
- //Functions.Insert(newIndex, funcmodel);
-
- Functions.Move(index, newIndex);
-
-
- }
- }
- });
-
- MoveDownCommand = new((o) =>
- {
- if (o != null && o is FuncModel funcmodel)
- {
- var index = Functions.IndexOf(funcmodel);
- if (index < Functions.Count - 1)
- {
- int newIndex = index + 1;
- //Functions.RemoveAt(index);
- //Functions.Insert(newIndex, funcmodel);
-
- Functions.Move(index, newIndex);
- }
- }
- });
- }
-
- private void AddTestData()
- {
- ID = "32587";
- Name = "红烧肉";
-
- //Functions = InitData.FunParInit;
- //Functions = new ObservableCollection<EFunc>();
- //foreach (var item in InitData.FunParInit.Keys)
- //{
- // Functions.Add(item);
- //}
- }
-
- /// <summary>保存参数。</summary>
- public BPARelayCommand SaveParamCommand { get; set; }
-
- /// <summary>添加功能</summary>
- public BPARelayCommand AddFuncCommand { get; set; }
-
- /// <summary>删除功能。</summary>
- public BPARelayCommand<object> RemoveCommand { get; set; }
-
- /// <summary>功能选择改变。</summary>
- public BPARelayCommand<object> FuncChangeCommand { get; set; }
-
- /// <summary>
- /// 打开下拉框时
- /// </summary>
- public BPARelayCommand<object> DropDownOenCommand { get; set; }
-
- /// <summary>编辑功能。</summary>
- public BPARelayCommand<object> EditCommand { get; set; }
-
- /// <summary>功能上移。</summary>
- public BPARelayCommand<object> MoveUpCommand { get; set; }
-
- /// <summary>功能下移。</summary>
- public BPARelayCommand<object> MoveDownCommand { get; set; }
-
- private ObservableCollection<FuncModel> _Functions;
-
- public ObservableCollection<FuncModel> Functions
- {
- get { return _Functions; }
- set { _Functions = value; OnPropertyChanged(); }
- }
-
- private string _Name;
-
- /// <summary>配方名称</summary>
- public string Name
- {
- get { return _Name; }
- set { _Name = value; OnPropertyChanged(); }
- }
-
- private string _ID;
-
- /// <summary>配方ID。</summary>
- public string ID
- {
- get { return _ID; }
- set { _ID = value; OnPropertyChanged(); }
- }
-
- private ObservableCollection<string> _AllFunc;
-
- /// <summary>所有功能集合。</summary>
- public ObservableCollection<string> AllFunc
- {
- get { return _AllFunc; }
- set { _AllFunc = value; OnPropertyChanged(); }
- }
-
- private ObservableCollection<string> _AllDishType;
-
- /// <summary>所有菜品集合。</summary>
- public ObservableCollection<string> AllDishType
- {
- get { return _AllDishType; }
- set { _AllDishType = value; OnPropertyChanged(); }
- }
-
- private EDishType _DishType;
-
- public EDishType DishType
- {
- get { return _DishType; }
- set { _DishType = value; OnPropertyChanged(); }
- }
- }
- }
|