|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using System.Collections.ObjectModel;
- using Microsoft.Toolkit.Mvvm.Input;
- using BPASmartClient.Helper;
- using BPASmartClient.JXJFoodBigStation.Model;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.JXJFoodBigStation.Model.Siemens;
- using System.Windows.Forms;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
-
- namespace BPASmartClient.JXJFoodBigStation.ViewModel
- {
- public class RecipeInfosViewModel : ObservableObject
- {
- public RecipeInfosViewModel()
- {
- foreach (var item in ProcessControl.GetInstance.RawMaterialsInfo)
- {
- RawMaterialLocation.Add(item.RawMaterialLocation);
- }
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is RecipeData rm)
- {
- RecipeName = rm.RecipeName;
- RecipeCode = rm.RecipeCode;
- TrayCode = rm.TrayCode;
- IsWashingBarrel = rm.IsWashingBarrel;
- RecipeCategory = rm.RecipeCategory;
- foreach (var item in rm.RawMaterial)
- {
- //item.RawMaterialCount = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialName == item.RawMaterialName);
- RawMaterialsInfo.Add(item);
- }
- }
- }), "RecipeInfo");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- foreach (var item in RawMaterialsInfo)
- {
- if (RawMaterialLocation.Contains(item.RawMaterialLocation))
- {
- int index = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialLocation == item.RawMaterialLocation);
- item.RawMaterialName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialName;
- item.RawMaterialChineseName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialChineseName;
- }
- }
- }), "RawMaterialNames",true);
-
- AddRecipe = new RelayCommand(() => {
- RawMaterialsInfo.Add(new RawMaterial());
- });
- Comfirm = new RelayCommand(() =>
- {
- if (!MessageNotify.GetInstance.ShowDialog($"是否保存该配方?"))
- {
- return;
- }
- var bom= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
- if (bom == null)//新配方
- {
- var name= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
- if (name == null)
- {
- go:
- string recipeCode = new Random().Next(10000, 32767).ToString();
- var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
- if (res == null)
- {
- if (IsWashingBarrel)
- {
- var newRecipeData = new RecipeData { RecipeCode = recipeCode, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "洗桶", RecipeCategory = RecipeCategory };
- Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
- Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData);
- if (RawMaterialsInfo.Count > 0)
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息");
- }
- else
- {
- var newRecipeData = new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料", RecipeCategory = RecipeCategory };
- Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
- Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData) ;
- }
- Json<LocalRecipe>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
- MessageNotify.GetInstance.ShowUserLog($"新增【{RecipeName}】配方,订单号为【{recipeCode}】。");
- }
- else
- {
- goto go;
- }
- ActionManage.GetInstance.Send("CloseRecipeInfosView");
- }
- else
- {
- MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- else//编辑已有配方
- {
- bom.RawMaterial.Clear();
- foreach (var item in RawMaterialsInfo)
- {
- bom.RawMaterial.Add(item);
- }
- bom.RecipeName = RecipeName;
- bom.TrayCode = TrayCode;
- bom.IsWashingBarrel = IsWashingBarrel;
- bom.RecipeCategory = RecipeCategory;
- if (IsWashingBarrel)
- {
- bom.RawMaterial.Clear();
- }
- Json<LocalRecipe>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
- MessageNotify.GetInstance.ShowUserLog($"修改【{RecipeName}】配方。");
- ActionManage.GetInstance.Send("CloseRecipeInfosView");
- }
- });
-
- RemoveRecipe = new RelayCommand<int>((index) => {
- var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialLocation == index);
- if (res != null)
- RawMaterialsInfo.Remove(res);
- });
-
- foreach (var item in Json<LocalRecipe>.Data.Recipes)
- {
- if (!Categorys.Contains(item.RecipeCategory))
- {
- Categorys.Add(item.RecipeCategory);
- }
- }
-
- }
- public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
- private string _mRecipeName;
-
- public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
- private string _mRecipeCode;
-
- public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
- private int _mTrayCode;
-
- public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
- private int _mSelectIndex;
- public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } }
- private bool _mIsWashingBarrel;
-
- public string RecipeCategory { get { return _RecipeCategory; } set { _RecipeCategory = value; OnPropertyChanged(); } }
- private string _RecipeCategory;
-
- private ObservableCollection<string> _Categorys=new();
-
- public ObservableCollection<string> Categorys
- {
- get { return _Categorys; }
- set { _Categorys = value; OnPropertyChanged(); }
- }
-
- public RelayCommand ReturnPage { get; set; }
-
- public RelayCommand AddRecipe { get; set; }
-
- public RelayCommand Comfirm { get; set; }
-
- public RelayCommand<int> RemoveRecipe { get; set; }
-
- public ObservableCollection<RawMaterial> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterial>() ;
-
- public ObservableCollection<int> RawMaterialLocation { get; set; } = new ObservableCollection<int>();
- }
- }
|