|
- 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 BPASmartClient.JXJFoodSmallStation.Model;
- using BPASmartClient.Helper;
- using Microsoft.Toolkit.Mvvm.Input;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.CustomResource.UserControls;
- using System.Diagnostics;
- using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
-
- namespace BPASmartClient.JXJFoodSmallStation.ViewModel
- {
- public class NewLocalRecipeViewModel : ObservableObject
- {
- public NewLocalRecipeViewModel()
- {
- ActionManage.GetInstance.CancelRegister("LocalRecipeEdit");
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is RemoteRecipeData rm)
- {
- RecipeName = rm.RecipeName;
- RecipeCode = rm.RecipeCode;
- TrayNum = rm.TrayCode;
- foreach (var item in rm.RawMaterial)
- {
- RawMaterial.Add(item);
- }
- }
- }), "LocalRecipeEdit");
- RemoveCommand = new RelayCommand<object>((o) =>
- {
- var res = RawMaterial.FirstOrDefault(p => p.RawMaterialLocation == (int)o);
- if (res != null) RawMaterial.Remove(res);
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{o.ToString()}:原料删除成功!");
- });
-
- AddCommand = new RelayCommand(() =>
- {
- int MaxRawMaterial = 15;
- if (GVL_SmallStation.GetInstance.IsUseWindSend && TrayNum == 1)
- {
- MaxRawMaterial = 20;
- }
- else
- {
- MaxRawMaterial = 15;
- }
- if (RawMaterial.Count < MaxRawMaterial)
- {
- if (RawMaterial.Count < 15)
- {
- RawMaterial.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialName = "原料" + (RawMaterial.Count + 1),
- RawMaterialLocation = RawMaterial.Count + 1,
- RawMaterialBarrelNum = 1,
- RawMaterialWeight = 100
- });
- }
- else
- {
- RawMaterial.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialName = "粉料" + (RawMaterial.Count -14),
- RawMaterialLocation = (RawMaterial.Count - 14),
- RawMaterialBarrelNum = 7,
- RawMaterialWeight = 100
- });
- }
- }
- else
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"原料最多添加{MaxRawMaterial}种");
- }
-
- });
- SaveCommand = new RelayCommand(() =>
- {
- if (RecipeName.Length < 0 || RecipeName == null|| RecipeName==String.Empty || RecipeName == "")
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方名称不能为空");
- return;
- }
- if (RecipeCode.Length < 0 || RecipeCode == null || RecipeCode == String.Empty || RecipeCode == "")
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"配方编号不能为空");
- return;
- }
- if (TrayNum != 1 && TrayNum!=2)
- {
- TrayNum = 1;
- }
- ObservableCollection<RemoteRecipeRawMaterial> RawMaterials = new ObservableCollection<RemoteRecipeRawMaterial>();
- if (RawMaterial == null || RawMaterial.Count <= 0)
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", $"没有可保存的参数!");
- return;
- }
-
- for (int i = 0; i < RawMaterial.Count; i++)
- {
- if (RawMaterial.Where(p => p.RawMaterialLocation == RawMaterial.ElementAt(i).RawMaterialLocation)?.ToList()?.Count >= 2)
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "错误", $"原料位置冲突,请检查后重试!");
- return;
- }
- }
- int index = Array.FindIndex(Json<LocalRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == RecipeCode);
- if (index >= 0)
- {
- foreach (var item in RawMaterial)
- {
- RawMaterials.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialLocation = item.RawMaterialLocation,
- RawMaterialBarrelNum = item.RawMaterialBarrelNum,
- RawMaterialWeight = item.RawMaterialWeight,
- });
- }
- Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeName= RecipeName;
- Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayNum;
- Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RecipeCode = RecipeCode;
- Json<LocalRecipeDataColl>.Data.Recipes.ElementAt(index).RawMaterial = RawMaterial;
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"修改配方完成!");
- }
- else
- {
- foreach (var item in RawMaterial)
- {
- RawMaterials.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialLocation = item.RawMaterialLocation,
- RawMaterialBarrelNum = item.RawMaterialBarrelNum,
- RawMaterialWeight = item.RawMaterialWeight,
- });
- }
- Json<LocalRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
- {
- RecipeName = RecipeName,
- RecipeCode = RecipeCode,
- TrayCode = TrayNum,
- RawMaterial = RawMaterials,
- });
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"自定义配方添加完成!");
- }
- ActionManage.GetInstance.Send("CloseNewRemoteRecipeView");
- });
- }
- public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
- private string _mRecipeCode = string.Empty;
-
- public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
- private string _mRecipeName = string.Empty;
-
- public int TrayNum { get { return _mTrayNum; } set { _mTrayNum = value; OnPropertyChanged(); } }
- private int _mTrayNum = 1;
-
- public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
- private string _mErrorInfo;
-
- public ObservableCollection<RemoteRecipeRawMaterial> RawMaterial { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
-
- public RelayCommand<object> RemoveCommand { get; set; }
-
- public RelayCommand AddCommand { get; set; }
- public RelayCommand SaveCommand { get; set; }
-
- }
- }
|