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

NewRecipeViewModel.cs 6.7 KiB

пре 4 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using BPA.Helper;
  2. using BPASmartClient.CustomResource.Pages.Model;
  3. using BPASmartClient.CustomResource.UserControls;
  4. using BPASmartClient.CustomResource.UserControls.MessageShow;
  5. using BPASmartClient.Academy.Model;
  6. using BPASmartClient.Model;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. namespace BPASmartClient.Academy.ViewModel
  12. {
  13. public class NewRecipeViewModel : ViewModelBase
  14. {
  15. public NewRecipeViewModel()
  16. {
  17. ActionManage.GetInstance.Register(new Action<object>((o) =>
  18. {
  19. if (o != null && o is RecipeModel rm)
  20. {
  21. RecipeName = rm.RecipeName;
  22. foreach (var item in rm.RawMaterials)
  23. {
  24. item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName);
  25. RawMaterials.Add(item);
  26. }
  27. RecipCode = rm.RecipCode;
  28. Index = Array.FindIndex(Json<LocalRecipe>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  29. }
  30. }), "Details", true);
  31. AddCommand = new BPARelayCommand(() =>
  32. {
  33. p:
  34. string guid = Guid.NewGuid().ToString();
  35. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  36. {
  37. int index = RawMaterials.Count;
  38. RawMaterials.Add(new RawMaterialModel()
  39. {
  40. RawMaterialId = guid,
  41. SelectIndex = index
  42. });
  43. }
  44. else goto p;
  45. });
  46. RemoveCommand = new BPARelayCommand<object>((obj) =>
  47. {
  48. if (obj is string rm)
  49. {
  50. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  51. if (res != null) RawMaterials.Remove(res);
  52. }
  53. });
  54. SaveCommand = new BPARelayCommand(() =>
  55. {
  56. if (string.IsNullOrEmpty(RecipeName)) { MessageNotify.GetInstance.ShowDialog("请输入配方名称!", DialogType.Warning); ErrorInfo = "请输入配方名称"; return; }
  57. var tempRes = RawMaterials.GroupBy(p => p.RawMaterialName);
  58. //编辑配方
  59. if (Index >= 0 && Index < Json<LocalRecipe>.Data.Recipes.Count)
  60. {
  61. var res = Array.FindIndex(Json<LocalRecipe>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  62. if (res >= 0 && res != Index)
  63. {
  64. ErrorInfo = "配方名称已经存在!";
  65. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  66. return;
  67. }
  68. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
  69. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RawMaterials.Clear();
  70. RawMaterials.ToList()?.ForEach(item =>
  71. {
  72. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
  73. });
  74. Json<LocalRecipe>.Save();
  75. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
  76. }
  77. else //新建配方
  78. {
  79. if (Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null)
  80. {
  81. ErrorInfo = "配方名称已存在!";
  82. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  83. return;
  84. }
  85. ObservableCollection<RawMaterialModel> TempRawMaterials = new ObservableCollection<RawMaterialModel>();
  86. RawMaterials.ToList()?.ForEach(item =>
  87. {
  88. TempRawMaterials.Add(item);
  89. });
  90. Json<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
  91. {
  92. RecipeName = RecipeName,
  93. RawMaterials = TempRawMaterials,
  94. });
  95. Json<LocalRecipe>.Save();
  96. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!");
  97. }
  98. ActionManage.GetInstance.Send("CloseNewRecipeView");
  99. });
  100. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  101. {
  102. foreach (var item in Global.DeviceRawMaterials)
  103. {
  104. RawMaterialNames.Add(item.RawMaterialName);
  105. }
  106. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  107. {
  108. RawMaterialNames.Add(item.RawMaterialName);
  109. }
  110. }
  111. else
  112. {
  113. foreach (var item in Global.DeviceRawMaterials)
  114. {
  115. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  116. }
  117. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  118. {
  119. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  120. }
  121. }
  122. }
  123. private void AddRecipes()
  124. {
  125. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  126. var dates = date.Split("-").ToList();
  127. StringBuilder sb = new StringBuilder();
  128. dates?.ForEach((item) => { sb.Append(item); });
  129. Json<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
  130. {
  131. SerialNum = Json<LocalRecipe>.Data.Recipes.Count + 1,
  132. RawMaterials = RawMaterials,
  133. RecipCode = sb.ToString(),
  134. RecipeName = RecipeName,
  135. });
  136. }
  137. private string RecipCode = string.Empty;
  138. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  139. private string _mRecipeName = string.Empty;
  140. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  141. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  142. }
  143. }