|
- 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;
-
- namespace BPASmartClient.JXJFoodBigStation.ViewModel
- {
- public class RecipeInfosViewModel : ObservableObject
- {
- public RecipeInfosViewModel()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is RecipeModel rm)
- {
- RecipeName = rm.RecipeName;
- RecipeCode = rm.RecipeCode;
- TrayCode = rm.TrayCode;
- //RawMaterialsInfo = rm.RawMaterial;
- //var rest = RawMaterialsInfo.GetHashCode();
-
- foreach (var item in rm.RawMaterial)
- {
- RawMaterialsInfo.Add(item);
- }
- }
- }), "RecipeInfo");
-
- AddRecipe = new RelayCommand(() => {
- RawMaterialsInfo.Add(new RawMaterialModel());
- });
- Comfirm = new RelayCommand(() =>
- {
- var bom= Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
- if (bom == null)//新配方
- {
- var name= Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
- if (name == null)
- {
- go:
- long recipeCode = new Random().Next(10000, 99999);
- var res = Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
- if (res == null)
- {
- Json<LocaPar>.Data.Recipes.Add(new RecipeModel { RecipeCode = recipeCode, RawMaterial= RawMaterialsInfo,RecipeName=RecipeName,TrayCode=TrayCode});
- Json<LocaPar>.Save();
- }
- 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;
- Json<LocaPar>.Save();
- ActionManage.GetInstance.Send("CloseRecipeInfosView");
- }
- });
-
- SaveAs = new RelayCommand(() => {
-
- var bom = Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
- var rec = Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
- if (bom == null && rec != null)//配方名称更改
- {
-
- prop: long recipeCode = new Random().Next(10000, 99999);//配方唯一ID,后期根据实际要求更改
- var res = Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
- if (res == null)
- {
- Json<LocaPar>.Data.Recipes.Add(new RecipeModel { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode });//配方添加
- Json<LocaPar>.Save();
- }
- else
- {
- goto prop;
- }
- ActionManage.GetInstance.Send("CloseRecipeInfosView");
- }
- else
- {
- MessageBox.Show("另存配方失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- ActionManage.GetInstance.Send("CloseNewRecipeView");
- });
-
- RemoveRecipe = new RelayCommand<int>((materilaName) => {
-
- var res= RawMaterialsInfo.FirstOrDefault(p=>p.RawMaterialLocation==materilaName);
- if (res != null)
- RawMaterialsInfo.Remove(res);
- });
- //ReturnPage = new RelayCommand(() =>
- //{
- // ActionManage.GetInstance.Send("CloseRecipeInfosView");
- //});
- }
-
- public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
- private string _mRecipeName;
-
- public long RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
- private long _mRecipeCode;
-
- public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
- private int _mTrayCode;
-
- public RelayCommand ReturnPage { get; set; }
-
- public RelayCommand AddRecipe { get; set; }
-
- public RelayCommand Comfirm { get; set; }
-
- public RelayCommand SaveAs { get; set; }
-
- public RelayCommand<int> RemoveRecipe { get; set; }
-
- public ObservableCollection<RawMaterialModel> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterialModel>() ;
- }
- }
|