using BPASmartClient.CustomResource.Pages.Model;
using BPASmartClient.CustomResource.UserControls;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPA.Helper;
using FryPot_DosingSystem.Model;
using FryPot_DosingSystem.View;
using BPA.Helper;
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 : NotifyBase
{
/////
///// 配方编号
/////
//private int _serialNumber;
//public int SerialNumber { get { return _serialNumber; } set { _serialNumber = value;OnPropertyChanged(); } }
/////
///// 配方名称
/////
//private string _recipeName;
//public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
///
/// 复制配方的Id
///
public static string CopyRecipeId { get; set; }
public ObservableCollection recipeModels { get; set; } = new ObservableCollection();
public BPARelayCommand NewRecipe { get; set; }
public BPARelayCommand SaveRecipe { get; set; }
///
/// 工艺操作
///
public BPARelayCommand OperateFlowProcess { get; set; }
///
/// 编辑配方
///
public BPARelayCommand EditRecipeCommand { get; set; }
///
/// 复制配方
///
public BPARelayCommand CopyRecipeCommand { get; set; }
///
/// 删除配方
///
public BPARelayCommand DeleteRecipeCommand { get; set; }
public RecipeSetViewModel()
{
// Json.Read();
recipeModels =Json.Data.Recipes;
int count = recipeModels.Count;
bool sign = false;
ActionManage.GetInstance.CancelRegister("RecipeIsChange");
ActionManage.GetInstance.RegisterAsync(new Action(() =>
{
if (!sign)
{
bool b = Json.Data.Recipes.Count == count ? true : false;
if (!b)
{
MessageBoxResult result = MessageBox.Show("配方数据未保存,是否保存", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);
if (result == MessageBoxResult.OK)
{
Json.Save();//保存配方
count = recipeModels.Count;
}
else
{
sign = true;
Json.Data.Recipes.RemoveAt(Json.Data.Recipes.Count - 1);
}
}
}
}), "RecipeIsChange");
//复制指定炒锅配方
ActionManage.GetInstance.Register(new Action((Num) => {
if (Num != null&& CopyRecipeId!=null)
{
var res= Json.Data.Recipes.FirstOrDefault(p=>p.RecipeId==CopyRecipeId);
if (res != null)
{
prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
var res1 = Json.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 collect = new ObservableCollection();
//浅拷贝
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 flowCollect = new ObservableCollection();
//浅拷贝
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.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, DataTime = DateTime.Now.ToShortDateString(), RecipeName = recipeNamec, materialCollection = collect, FlowProcess = FryFlow });//配方添加
//Json.Save();
}
else
{
goto prop;
}
MessageNotify.GetInstance.ShowUserLog("复制配方成功");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"复制配方成功");
}
}
}), "CopyPotNum");
NewRecipe =new BPARelayCommand( new Action(() =>
{
NewRecipeView nrv = new NewRecipeView();
nrv.ShowDialog();
//MessageLog.GetInstance.ShowUserLog("新建配方");
}));
SaveRecipe =new BPARelayCommand( new Action(() =>
{
Json.Save();
count = recipeModels.Count;
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"配方保存成功!");
}));
EditRecipeCommand = new BPARelayCommand((Id) =>
{
if (Id != null)
{
ActionManage.GetInstance.CancelRegister("EditRecipe");
NewRecipeView nrv = new NewRecipeView();
ActionManage.GetInstance.Send("EditRecipe", Id);
nrv.ShowDialog();
}
});
DeleteRecipeCommand = new BPARelayCommand((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.Save();//保存配方
count = recipeModels.Count;
MessageNotify.GetInstance.ShowUserLog($"成功删除配方【{nes.RecipeName}】");
}
}
});
//工艺流程操作
OperateFlowProcess = new BPARelayCommand((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 BPARelayCommand((Id) => {
CopyInfoView copyInfoView = new CopyInfoView();
CopyRecipeId = Id.ToString() ;
copyInfoView.Show();
});
}
}
}