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

180 lines
8.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BPA.Helper;
  7. using System.Collections.ObjectModel;
  8. using BPA.Helper;
  9. using BPASmartClient.JXJFoodBigStation.Model;
  10. using BPASmartClient.CustomResource.Pages.Model;
  11. using BPASmartClient.JXJFoodBigStation.Model.Siemens;
  12. using System.Windows.Forms;
  13. using BPASmartClient.CustomResource.UserControls;
  14. using BPASmartClient.CustomResource.UserControls.MessageShow;
  15. namespace BPASmartClient.JXJFoodBigStation.ViewModel
  16. {
  17. public class RecipeInfosViewModel : NotifyBase
  18. {
  19. public RecipeInfosViewModel()
  20. {
  21. foreach (var item in ProcessControl.GetInstance.RawMaterialsInfo)
  22. {
  23. RawMaterialLocation.Add(item.RawMaterialLocation);
  24. }
  25. ActionManage.GetInstance.Register(new Action<object>((o) =>
  26. {
  27. if (o != null && o is RecipeData rm)
  28. {
  29. RecipeName = rm.RecipeName;
  30. RecipeCode = rm.RecipeCode;
  31. TrayCode = rm.TrayCode;
  32. IsWashingBarrel = rm.IsWashingBarrel;
  33. RecipeCategory = rm.RecipeCategory;
  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 BPARelayCommand(() => {
  54. RawMaterialsInfo.Add(new RawMaterial());
  55. });
  56. Comfirm = new BPARelayCommand(() =>
  57. {
  58. if (!MessageNotify.GetInstance.ShowDialog($"是否保存该配方?"))
  59. {
  60. return;
  61. }
  62. var bom= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
  63. if (bom == null)//新配方
  64. {
  65. var name= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  66. if (name == null)
  67. {
  68. go:
  69. string recipeCode = new Random().Next(10000, 32767).ToString();
  70. var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
  71. if (res == null)
  72. {
  73. if (IsWashingBarrel)
  74. {
  75. var newRecipeData = new RecipeData { RecipeCode = recipeCode, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "洗桶", RecipeCategory = RecipeCategory };
  76. Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
  77. Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData);
  78. if (RawMaterialsInfo.Count > 0)
  79. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息");
  80. }
  81. else
  82. {
  83. var newRecipeData = new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料", RecipeCategory = RecipeCategory };
  84. Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
  85. Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData) ;
  86. }
  87. Json<LocalRecipe>.Save();
  88. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  89. MessageNotify.GetInstance.ShowUserLog($"新增【{RecipeName}】配方,订单号为【{recipeCode}】。");
  90. }
  91. else
  92. {
  93. goto go;
  94. }
  95. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  96. }
  97. else
  98. {
  99. MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  100. }
  101. }
  102. else//编辑已有配方
  103. {
  104. bom.RawMaterial.Clear();
  105. foreach (var item in RawMaterialsInfo)
  106. {
  107. bom.RawMaterial.Add(item);
  108. }
  109. bom.RecipeName = RecipeName;
  110. bom.TrayCode = TrayCode;
  111. bom.IsWashingBarrel = IsWashingBarrel;
  112. bom.RecipeCategory = RecipeCategory;
  113. if (IsWashingBarrel)
  114. {
  115. bom.RawMaterial.Clear();
  116. }
  117. Json<LocalRecipe>.Save();
  118. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  119. MessageNotify.GetInstance.ShowUserLog($"修改【{RecipeName}】配方。");
  120. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  121. }
  122. });
  123. RemoveRecipe = new BPARelayCommand<int>((index) => {
  124. var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialLocation == index);
  125. if (res != null)
  126. RawMaterialsInfo.Remove(res);
  127. });
  128. foreach (var item in Json<LocalRecipe>.Data.Recipes)
  129. {
  130. if (!Categorys.Contains(item.RecipeCategory))
  131. {
  132. Categorys.Add(item.RecipeCategory);
  133. }
  134. }
  135. }
  136. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  137. private string _mRecipeName;
  138. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  139. private string _mRecipeCode;
  140. public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
  141. private int _mTrayCode;
  142. public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
  143. private int _mSelectIndex;
  144. public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } }
  145. private bool _mIsWashingBarrel;
  146. public string RecipeCategory { get { return _RecipeCategory; } set { _RecipeCategory = value; OnPropertyChanged(); } }
  147. private string _RecipeCategory;
  148. private ObservableCollection<string> _Categorys=new();
  149. public ObservableCollection<string> Categorys
  150. {
  151. get { return _Categorys; }
  152. set { _Categorys = value; OnPropertyChanged(); }
  153. }
  154. public BPARelayCommand ReturnPage { get; set; }
  155. public BPARelayCommand AddRecipe { get; set; }
  156. public BPARelayCommand Comfirm { get; set; }
  157. public BPARelayCommand<int> RemoveRecipe { get; set; }
  158. public ObservableCollection<RawMaterial> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterial>() ;
  159. public ObservableCollection<int> RawMaterialLocation { get; set; } = new ObservableCollection<int>();
  160. }
  161. }