using BPASmartClient.CustomResource.Pages.Model;
using BPASmartClient.CustomResource.UserControls;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.Helper;
using FryPot_DosingSystem.Model;
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.Forms;
namespace FryPot_DosingSystem.ViewModel
{
internal class NewRecipeViewModel : ObservableObject
{
///
/// 配方唯一编码,用于编辑配方
///
public string recipeId { get; set; }
///
/// 配方名称
///
private string _recipeName;
public string RecipeName { get { return _recipeName; } set { _recipeName = value; OnPropertyChanged(); } }
private int _recipeRollerNum;
///
/// 配方中桶数
///
public int RecipeRollerNum { get { return _recipeRollerNum; } set { _recipeRollerNum = value; OnPropertyChanged(); } }
public ObservableCollection materials { get; set; } = new ObservableCollection();
public ObservableCollection materialNames { get; set; } = new ObservableCollection();
public RelayCommand AddRecipe { get; set; }
public RelayCommand RemoveRecipe { get; set; }
public RelayCommand Comfirm { get; set; }
public RelayCommand SaveAs { get; set; }
public NewRecipeViewModel()
{
Json.Read();
MaterialNames.GetInstance.Names = Json.Data.Names;
materialNames = Json.Data.Names;
ActionManage.GetInstance.Register(new Action(Id =>
{
if (Id != null && Id is string strId)
{
var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeId == strId);
if (res != null && res is NewRecipeModel rom)
{
RecipeName = rom.RecipeName;
RecipeRollerNum = rom.materialCollection.Count;
foreach (var item in rom.materialCollection)
{
materials.Add(item);
}
recipeId = strId;
}
}
}), "EditRecipe");
AddRecipe = new RelayCommand(() =>
{
for (int i = 0; i < RecipeRollerNum; i++)
{
pr1:
string materialCode = Guid.NewGuid().ToString();//原料唯一ID ,后期需要根据实际要求更改
var res = materials.FirstOrDefault(p => p.MaterialCode == materialCode);
if (res == null)
{
materials.Add(new MaterialType() { MaterialCode = materialCode });
}
else
{
goto pr1;
}
}
});
RemoveRecipe = new RelayCommand(code =>
{
var res = materials.FirstOrDefault(m => m.MaterialCode == code);
if (res != null)
materials.Remove(res);
});
Comfirm = new RelayCommand(() =>
{
var bom = Json.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
if (bom == null)//新配方
{
var name = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
if (name == null)
{
if (materials.Count<=8&& materials.Count> 0)
{
int lineNum = materials.ElementAt(0).MaterialLoc / 100;
//桶号正确性验证
for (int i = 0; i < materials.Count; i++)
{
if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
{
MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶号异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
ActionManage.GetInstance.Send("CloseNewRecipeView");
return;
}
}
prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
if (res == null)
{
Json.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, RecipeName = RecipeName, FlowProcess=new FlowProcessManage(), materialCollection = materials,DataTime=DateTime.Now.ToShortDateString()});//配方添加
}
else
{
goto prop;
}
MessageNotify.GetInstance.ShowUserLog($"配方【{RecipeName}】新建成功");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方【{RecipeName}】成功");
}
else
{
MessageNotify.GetInstance.ShowUserLog($"新建配方【{RecipeName}】无效:【配方中原料桶数异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"新建配方【{RecipeName}】无效");
}
ActionManage.GetInstance.Send("CloseNewRecipeView");
}
else
{
MessageBox.Show("配方名称重复或为空,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else //已有配方,用于编辑
{
if (materials.Count > 0 && materials.Count <= 8)
{
int lineNum = materials.ElementAt(0).MaterialLoc / 100;
//桶号正确性验证
for (int i = 0; i < materials.Count; i++)
{
if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
{
MessageNotify.GetInstance.ShowUserLog($"另存配方【{RecipeName}】无效:【配方中原料桶号异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{RecipeName}】无效");
ActionManage.GetInstance.Send("CloseNewRecipeView");
return;
}
}
bom.materialCollection = materials;
bom.RecipeName = RecipeName;
bom.UpdateTime = DateTime.Now.ToShortDateString();
if (bom.FlowProcess != null)
bom.FlowProcess.RecipeName = RecipeName;
Json.Save();
MessageNotify.GetInstance.ShowUserLog($"配方【{RecipeName}】修改成功");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方【{RecipeName}】成功");
}
else
{
MessageNotify.GetInstance.ShowUserLog($"修改配方【{RecipeName}】无效:【配方中原料桶数异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"修改配方【{RecipeName}】无效");
}
ActionManage.GetInstance.Send("CloseNewRecipeView");
}
});
SaveAs = new RelayCommand(() =>
{
var bom = Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
var rec = Json.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeId);
if (bom == null && rec != null)//配方名称更改
{
if (materials.Count>0&& materials.Count<=8)//验证配方中原料桶数
{
int lineNum = materials.ElementAt(0).MaterialLoc / 100;
//桶号正确性验证
for (int i = 0; i < materials.Count; i++)
{
if (materials.ElementAt(i).MaterialLoc / 100 != lineNum || materials.ElementAt(i).MaterialLoc % 100 != i + 1 || lineNum <= 0 || lineNum > 5)
{
MessageNotify.GetInstance.ShowUserLog($"另存配方【{RecipeName}】无效:【配方中原料桶号异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{RecipeName}】无效");
ActionManage.GetInstance.Send("CloseNewRecipeView");
return;
}
}
prop: string recipeID = Guid.NewGuid().ToString();//配方唯一ID,后期根据实际要求更改
var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeId == recipeID);
if (res == null)
{
Json.Data.Recipes.Add(new NewRecipeModel { RecipeId = recipeID, DataTime = DateTime.Now.ToShortDateString(), RecipeName = RecipeName, materialCollection = materials,FlowProcess=new FlowProcessManage()});//配方添加
//Json.Save();
}
else
{
goto prop;
}
MessageNotify.GetInstance.ShowUserLog("另存配方成功");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"另存配方【{RecipeName}】成功");
}
else
{
MessageNotify.GetInstance.ShowUserLog($"另存配方【{rec.RecipeName}】无效:【配方中原料桶数异常】");
NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "提示", $"另存配方【{rec.RecipeName}】无效");
}
ActionManage.GetInstance.Send("CloseNewRecipeView");
}
else
{
MessageBox.Show("另存配方失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
ActionManage.GetInstance.Send("CloseNewRecipeView");
});
}
}
}