|
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.Helper;
- using FryPot_DosingSystem.Model;
- using FryPot_DosingSystem.View;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Media3D;
-
- namespace FryPot_DosingSystem.ViewModel
- {
- internal class RecipeSetViewModel : ObservableObject
- {
- ///// <summary>
- ///// 配方编号
- ///// </summary>
- //private int _serialNumber;
- //public int SerialNumber { get { return _serialNumber; } set { _serialNumber = value;OnPropertyChanged(); } }
- ///// <summary>
- ///// 配方名称
- ///// </summary>
- //private string _recipeName;
- //public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
- /// <summary>
- /// 复制配方的Id
- /// </summary>
- public static string CopyRecipeId { get; set; }
-
-
- public ObservableCollection<NewRecipeModel> recipeModels { get; set; } = new ObservableCollection<NewRecipeModel>();
-
- public RelayCommand NewRecipe { get; set; }
-
- public RelayCommand SaveRecipe { get; set; }
- /// <summary>
- /// 工艺操作
- /// </summary>
- public RelayCommand<string> OperateFlowProcess { get; set; }
- /// <summary>
- /// 编辑配方
- /// </summary>
- public RelayCommand<object> EditRecipeCommand { get; set; }
- /// <summary>
- /// 复制配方
- /// </summary>
- public RelayCommand<object> CopyRecipeCommand { get; set; }
- /// <summary>
- /// 删除配方
- /// </summary>
- public RelayCommand<object> DeleteRecipeCommand { get; set; }
-
-
- public RecipeSetViewModel()
- {
- // Json<RecipeManage>.Read();
- recipeModels =Json<RecipeManage>.Data.Recipes;
- int count = recipeModels.Count;
- bool sign = false;
- ActionManage.GetInstance.CancelRegister("RecipeIsChange");
- ActionManage.GetInstance.RegisterAsync(new Action(() =>
- {
- if (!sign)
- {
- bool b = Json<RecipeManage>.Data.Recipes.Count == count ? true : false;
- if (!b)
- {
- MessageBoxResult result = MessageBox.Show("配方数据未保存,是否保存", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- if (result == MessageBoxResult.OK)
- {
- Json<RecipeManage>.Save();//保存配方
- count = recipeModels.Count;
- }
- else
- {
- sign = true;
- Json<RecipeManage>.Data.Recipes.RemoveAt(Json<RecipeManage>.Data.Recipes.Count - 1);
- }
- }
- }
-
- }), "RecipeIsChange");
- //复制指定炒锅配方
- ActionManage.GetInstance.Register<object>(new Action<object>((Num) => {
- if (Num != null&& CopyRecipeId!=null)
- {
- var res= Json<RecipeManage>.Data.Recipes.FirstOrDefault(p=>p.RecipeId==CopyRecipeId);
- if (res != null)
- {
- prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
- var res1 = Json<RecipeManage>.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
- if (res1 == null)
- {
- string recipeNamec = string.Empty;
- if (res.RecipeName.Contains('('))
- {
- recipeNamec = res.RecipeName.Split('(')[0] + $"({Num}号锅)";
- }
- else
- {
- recipeNamec= res.RecipeName+$"({Num}号锅)";
- }
- //复制料筒配方
- ObservableCollection<MaterialType> collect = new ObservableCollection<MaterialType>();
- //浅拷贝
- foreach (var item in res.materialCollection)
- {
- var clone=(MaterialType)item.Clone();
- collect.Add(clone);
- }
- foreach (var item in collect)
- {
- if (ushort.TryParse((Num.ToString() + item.MaterialLoc.ToString().Substring(1, 2)), out ushort loc))
- {
- item.MaterialLoc = loc;
- }
- else
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"复制配方失败");
- return;
- }
- }
-
- //复制炒锅工艺配方
- FlowProcessManage FryFlow = new FlowProcessManage();
- FryFlow.StirTime=res.FlowProcess.StirTime;
- FryFlow.RecipeName = recipeNamec;
- FryFlow.targetWeightOffset = res.FlowProcess.targetWeightOffset;
- ObservableCollection<FlowProcessModel> flowCollect = new ObservableCollection<FlowProcessModel>();
- //浅拷贝
- foreach (var item in res.FlowProcess.fpModels)
- {
- var clone = (FlowProcessModel)item.Clone();
- flowCollect.Add(clone);
- }
- foreach (var item in flowCollect)
- {
- if (ushort.TryParse(item.FryMaterialNum, out ushort rollerNum))
- {
- item.FryMaterialNum = Num.ToString() + item.FryMaterialNum.ToString().Substring(1, 2);
- }
- }
- FryFlow.fpModels = flowCollect;
- Json<RecipeManage>.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, DataTime = DateTime.Now.ToShortDateString(), RecipeName = recipeNamec, materialCollection = collect, FlowProcess = FryFlow });//配方添加
- //Json<RecipeManage>.Save();
- }
- else
- {
- goto prop;
- }
- MessageNotify.GetInstance.ShowUserLog("复制配方成功");
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"复制配方成功");
- }
- }
-
- }), "CopyPotNum");
-
- NewRecipe =new RelayCommand( new Action(() =>
- {
- NewRecipeView nrv = new NewRecipeView();
- nrv.ShowDialog();
- //MessageLog.GetInstance.ShowUserLog("新建配方");
- }));
- SaveRecipe =new RelayCommand( new Action(() =>
- {
- Json<RecipeManage>.Save();
- count = recipeModels.Count;
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方保存成功!");
- }));
- EditRecipeCommand = new RelayCommand<object>((Id) =>
- {
- if (Id != null)
- {
- ActionManage.GetInstance.CancelRegister("EditRecipe");
- NewRecipeView nrv = new NewRecipeView();
- ActionManage.GetInstance.Send("EditRecipe", Id);
- nrv.ShowDialog();
- }
- });
- DeleteRecipeCommand = new RelayCommand<object>((Id) =>
- {
- if (Id != null && Id is String strId)
- {
- var res = recipeModels.FirstOrDefault(p => p.RecipeId == strId);
- if (res != null && res is NewRecipeModel nes)
- {
- recipeModels.Remove(nes);//删除配方
- Json<RecipeManage>.Save();//保存配方
- count = recipeModels.Count;
- MessageNotify.GetInstance.ShowUserLog($"成功删除配方【{nes.RecipeName}】");
- }
- }
- });
- //工艺流程操作
- OperateFlowProcess = new RelayCommand<string>((recipeName) =>
- {
- if (recipeName != null && recipeName != string.Empty && recipeName != "")
- {
- ActionManage.GetInstance.CancelRegister("EditFlowProcess");
- FlowProcessView fps = new FlowProcessView();
- ActionManage.GetInstance.Send("EditFlowProcess", recipeName);
- fps.ShowDialog();
- }
-
- });
- //复制配方
- CopyRecipeCommand = new RelayCommand<object>((Id) => {
- CopyInfoView copyInfoView = new CopyInfoView();
- CopyRecipeId = Id.ToString() ;
- copyInfoView.Show();
- });
-
- }
- }
- }
|