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

208 lines
9.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 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. if (string.IsNullOrWhiteSpace(RecipeName) )
  63. {
  64. MessageNotify.GetInstance.ShowDialog("配方名称不可为空,请修改后再试。",DialogType.Error);
  65. return;
  66. }
  67. if (TrayCode is <=0 or >5)
  68. {
  69. MessageNotify.GetInstance.ShowDialog("请输入正确的托盘编号,请修改后再试。", DialogType.Error);
  70. return;
  71. }
  72. foreach (RawMaterial item in RawMaterialsInfo)
  73. {
  74. if (item.RawMaterialLocation ==0)
  75. {
  76. MessageNotify.GetInstance.ShowDialog("请输入正确的原料位置,请修改后再试。", DialogType.Error);
  77. return;
  78. }
  79. if (item.RawMaterialBarrelNum is <=0 or >4)
  80. {
  81. MessageNotify.GetInstance.ShowDialog("请输入正确的托盘桶号,请修改后再试。", DialogType.Error);
  82. return;
  83. }
  84. if (item.RawMaterialWeight <=0)
  85. {
  86. MessageNotify.GetInstance.ShowDialog("请输入正确的原料重量,请修改后再试。", DialogType.Error);
  87. return;
  88. }
  89. }
  90. var bom= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == RecipeCode);
  91. if (bom == null)//新配方
  92. {
  93. var name= Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName);
  94. if (name == null)
  95. {
  96. go:
  97. string recipeCode = new Random().Next(10000, 32767).ToString();
  98. var res = Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == recipeCode);
  99. if (res == null)
  100. {
  101. if (IsWashingBarrel)
  102. {
  103. var newRecipeData = new RecipeData { RecipeCode = recipeCode, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "洗桶", RecipeCategory = RecipeCategory };
  104. Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
  105. Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData);
  106. if (RawMaterialsInfo.Count > 0)
  107. NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, App.MainWindow, "提示", $"订单类型为洗桶,不保存原料信息");
  108. }
  109. else
  110. {
  111. var newRecipeData = new RecipeData { RecipeCode = recipeCode, RawMaterial = RawMaterialsInfo, RecipeName = RecipeName, TrayCode = TrayCode, IsWashingBarrel = IsWashingBarrel, OrderType = "配料", RecipeCategory = RecipeCategory };
  112. Json<LocalRecipe>.Data.Recipes.Add(newRecipeData);
  113. Json<LocalRecipe>.Data.SelectedRecipes.Add(newRecipeData) ;
  114. }
  115. Json<LocalRecipe>.Save();
  116. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  117. MessageNotify.GetInstance.ShowUserLog($"新增【{RecipeName}】配方,订单号为【{recipeCode}】。");
  118. }
  119. else
  120. {
  121. goto go;
  122. }
  123. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  124. }
  125. else
  126. {
  127. MessageBox.Show("配方名称重复,请重命名!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  128. }
  129. }
  130. else//编辑已有配方
  131. {
  132. bom.RawMaterial.Clear();
  133. foreach (var item in RawMaterialsInfo)
  134. {
  135. bom.RawMaterial.Add(item);
  136. }
  137. bom.RecipeName = RecipeName;
  138. bom.TrayCode = TrayCode;
  139. bom.IsWashingBarrel = IsWashingBarrel;
  140. bom.RecipeCategory = RecipeCategory;
  141. if (IsWashingBarrel)
  142. {
  143. bom.RawMaterial.Clear();
  144. }
  145. Json<LocalRecipe>.Save();
  146. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"配方保存完成");
  147. MessageNotify.GetInstance.ShowUserLog($"修改【{RecipeName}】配方。");
  148. ActionManage.GetInstance.Send("CloseRecipeInfosView");
  149. }
  150. });
  151. RemoveRecipe = new BPARelayCommand<int>((index) => {
  152. var res = RawMaterialsInfo.FirstOrDefault(p => p.RawMaterialLocation == index);
  153. if (res != null)
  154. RawMaterialsInfo.Remove(res);
  155. });
  156. foreach (var item in Json<LocalRecipe>.Data.Recipes)
  157. {
  158. if (!Categorys.Contains(item.RecipeCategory))
  159. {
  160. Categorys.Add(item.RecipeCategory);
  161. }
  162. }
  163. }
  164. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  165. private string _mRecipeName;
  166. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  167. private string _mRecipeCode;
  168. public int TrayCode { get { return _mTrayCode; } set { _mTrayCode = value; OnPropertyChanged(); } }
  169. private int _mTrayCode;
  170. public int SelectIndex { get { return _mSelectIndex; } set { _mSelectIndex = value; OnPropertyChanged(); } }
  171. private int _mSelectIndex;
  172. public bool IsWashingBarrel { get { return _mIsWashingBarrel; } set { _mIsWashingBarrel = value; OnPropertyChanged(); } }
  173. private bool _mIsWashingBarrel;
  174. public string RecipeCategory { get { return _RecipeCategory; } set { _RecipeCategory = value; OnPropertyChanged(); } }
  175. private string _RecipeCategory;
  176. private ObservableCollection<string> _Categorys=new();
  177. public ObservableCollection<string> Categorys
  178. {
  179. get { return _Categorys; }
  180. set { _Categorys = value; OnPropertyChanged(); }
  181. }
  182. public BPARelayCommand ReturnPage { get; set; }
  183. public BPARelayCommand AddRecipe { get; set; }
  184. public BPARelayCommand Comfirm { get; set; }
  185. public BPARelayCommand<int> RemoveRecipe { get; set; }
  186. public ObservableCollection<RawMaterial> RawMaterialsInfo { get; set; } = new ObservableCollection<RawMaterial>() ;
  187. public ObservableCollection<int> RawMaterialLocation { get; set; } = new ObservableCollection<int>();
  188. }
  189. }