终端一体化运控平台
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.
 
 
 

148 lines
6.8 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. RawMaterialLocation.Add(item.RawMaterialLocation);
  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 (RawMaterialLocation.Contains(item.RawMaterialLocation))
  46. {
  47. int index = Array.FindIndex(ProcessControl.GetInstance.RawMaterialsInfo.ToArray(), p => p.RawMaterialLocation == item.RawMaterialLocation);
  48. item.RawMaterialName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialName;
  49. item.RawMaterialChineseName = ProcessControl.GetInstance.RawMaterialsInfo.ElementAt(index).RawMaterialChineseName;
  50. }
  51. }
  52. }), "RawMaterialNames",true);
  53. AddRecipe = new RelayCommand(() => {
  54. RawMaterialsInfo.Add(new RawMaterial());
  55. });
  56. Comfirm = new RelayCommand(() =>
  57. {
  58. var bom= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
  59. if (bom == null)//新配方
  60. {
  61. var name= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  62. if (name == null)
  63. {
  64. go:
  65. string recipeCode = new Random().Next(10000, 32767).ToString();
  66. var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
  67. if (res == null)
  68. {
  69. if (IsWashingBarrel)
  70. {
  71. Json<LocalRecipe>.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode,RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType="洗桶" });
  72. if (RawMaterialsInfo.Count > 0)
  73. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息");
  74. }
  75. else
  76. {
  77. Json<LocalRecipe>.Data.Recipes.Add(new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料" });
  78. }
  79. Json<LocalRecipe>.Save();
  80. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  81. }
  82. else
  83. {
  84. goto go;
  85. }
  86. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  87. }
  88. else
  89. {
  90. MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  91. }
  92. }
  93. else//编辑已有配方
  94. {
  95. bom.RawMaterial.Clear();
  96. foreach (var item in RawMaterialsInfo)
  97. {
  98. bom.RawMaterial.Add(item);
  99. }
  100. bom.RecipeName = RecipeName;
  101. bom.TrayCode = TrayCode;
  102. bom.IsWashingBarrel = IsWashingBarrel;
  103. if (IsWashingBarrel)
  104. {
  105. bom.RawMaterial.Clear();
  106. }
  107. Json<LocalRecipe>.Save();
  108. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  109. }
  110. });
  111. RemoveRecipe = new RelayCommand<int>((index) => {
  112. var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialLocation == index);
  113. if (res != null)
  114. RawMaterialsInfo.Remove(res);
  115. });
  116. }
  117. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  118. private string _mRecipeName;
  119. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  120. private string _mRecipeCode;
  121. public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
  122. private int _mTrayCode;
  123. public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
  124. private int _mSelectIndex;
  125. public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } }
  126. private bool _mIsWashingBarrel;
  127. public RelayCommand ReturnPage { get; set; }
  128. public RelayCommand AddRecipe { get; set; }
  129. public RelayCommand Comfirm { get; set; }
  130. public RelayCommand<int> RemoveRecipe { get; set; }
  131. public ObservableCollection<RawMaterial> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterial>() ;
  132. public ObservableCollection<int> RawMaterialLocation { get; set; } = new ObservableCollection<int>();
  133. }
  134. }