终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

147 lines
6.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Toolkit.Mvvm.ComponentModel;
  7. using System.Collections.ObjectModel;
  8. using Microsoft.Toolkit.Mvvm.Input;
  9. using BPASmartClient.Helper;
  10. using BPASmartClient.JXJFoodBigStation.Model;
  11. using BPASmartClient.CustomResource.Pages.Model;
  12. using BPASmartClient.JXJFoodBigStation.Model.Siemens;
  13. using System.Windows.Forms;
  14. using BPASmartClient.CustomResource.UserControls;
  15. using BPASmartClient.CustomResource.UserControls.MessageShow;
  16. namespace BPASmartClient.JXJFoodBigStation.ViewModel
  17. {
  18. public class RecipeInfosViewModel : ObservableObject
  19. {
  20. public RecipeInfosViewModel()
  21. {
  22. foreach (var item in ProcessControl.GetInstance.RawMaterialsInfo)
  23. {
  24. RawMaterialNames.Add(item.RawMaterialName);
  25. }
  26. ActionManage.GetInstance.Register(new Action<object>((o) =>
  27. {
  28. if (o != null && o is RecipeData rm)
  29. {
  30. RecipeName = rm.RecipeName;
  31. RecipeCode = rm.RecipeCode;
  32. TrayCode = rm.TrayCode;
  33. IsWashingBarrel = rm.IsWashingBarrel;
  34. foreach (var item in rm.RawMaterial)
  35. {
  36. item.RawMaterialCount = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialName == item.RawMaterialName);
  37. RawMaterialsInfo.Add(item);
  38. }
  39. }
  40. }), "RecipeInfo");
  41. ActionManage.GetInstance.Register(new Action(() =>
  42. {
  43. foreach (var item in RawMaterialsInfo)
  44. {
  45. if (RawMaterialNames.Contains(item.RawMaterialName))
  46. {
  47. int index = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialName == item.RawMaterialName);
  48. item.RawMaterialLocation = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialLocation;
  49. }
  50. }
  51. }), "RawMaterialNames",true);
  52. AddRecipe = new RelayCommand(() => {
  53. RawMaterialsInfo.Add(new RawMaterial());
  54. });
  55. Comfirm = new RelayCommand(() =>
  56. {
  57. var bom= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
  58. if (bom == null)//新配方
  59. {
  60. var name= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  61. if (name == null)
  62. {
  63. go:
  64. string recipeCode = new Random().Next(10000, 32767).ToString();
  65. var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
  66. if (res == null)
  67. {
  68. if (IsWashingBarrel)
  69. {
  70. Json<LocalRecipe>.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode,RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType="洗桶" });
  71. if (RawMaterialsInfo.Count > 0)
  72. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息");
  73. }
  74. else
  75. {
  76. Json<LocalRecipe>.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料" });
  77. }
  78. Json<LocalRecipe>.Save();
  79. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  80. }
  81. else
  82. {
  83. goto go;
  84. }
  85. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  86. }
  87. else
  88. {
  89. MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  90. }
  91. }
  92. else//编辑已有配方
  93. {
  94. bom.RawMaterial.Clear();
  95. foreach (var item in RawMaterialsInfo)
  96. {
  97. bom.RawMaterial.Add(item);
  98. }
  99. bom.RecipeName = RecipeName;
  100. bom.TrayCode = TrayCode;
  101. bom.IsWashingBarrel = IsWashingBarrel;
  102. if (IsWashingBarrel)
  103. {
  104. bom.RawMaterial.Clear();
  105. }
  106. Json<LocalRecipe>.Save();
  107. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  108. }
  109. });
  110. RemoveRecipe = new RelayCommand<int>((index) => {
  111. var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialCount == index);
  112. if (res != null)
  113. RawMaterialsInfo.Remove(res);
  114. });
  115. }
  116. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  117. private string _mRecipeName;
  118. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  119. private string _mRecipeCode;
  120. public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
  121. private int _mTrayCode;
  122. public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
  123. private int _mSelectIndex;
  124. public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } }
  125. private bool _mIsWashingBarrel;
  126. public RelayCommand ReturnPage { get; set; }
  127. public RelayCommand AddRecipe { get; set; }
  128. public RelayCommand Comfirm { get; set; }
  129. public RelayCommand<int> RemoveRecipe { get; set; }
  130. public ObservableCollection<RawMaterial> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterial>() ;
  131. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  132. }
  133. }