终端一体化运控平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

211 рядки
9.0 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 BPASmartClient.CustomResource.Pages.Model;
  9. using BPASmartClient.Model;
  10. using BPASmartClient.CustomResource.UserControls.MessageShow;
  11. using BPASmartClient.CustomResource.UserControls;
  12. namespace BPASmartClient.DosingSystemSingle.ViewModel
  13. {
  14. public class NewRecipeViewModel : ViewModelBase
  15. {
  16. public NewRecipeViewModel()
  17. {
  18. ActionManage.GetInstance.RegisterAsync(new Action<object>((o) =>
  19. {
  20. if (!string.IsNullOrEmpty(o?.ToString()))
  21. {
  22. if (RawMaterials.FirstOrDefault(p => p.RawMaterialName == o.ToString()) != null)
  23. {
  24. App.Current.Dispatcher.Invoke(() => { MessageNotify.GetInstance.ShowDialog("当前原料和其它原料重复,请重新选择!", DialogType.Warning); });
  25. }
  26. }
  27. }), "原料选择修改");
  28. ActionManage.GetInstance.Register(new Action<object>((o) =>
  29. {
  30. if (o != null && o is RecipeModel rm)
  31. {
  32. RecipeName = rm.RecipeName;
  33. foreach (var item in rm.RawMaterials)
  34. {
  35. item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName);
  36. RawMaterials.Add(item);
  37. }
  38. RecipCode = rm.RecipCode;
  39. Index = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  40. }
  41. }), "Details", true);
  42. AddCommand = new BPARelayCommand(() =>
  43. {
  44. if (RawMaterials.Count >= DeviceInquire.GetInstance.devices.Count)
  45. {
  46. MessageNotify.GetInstance.ShowDialog("原材料不足!");
  47. return;
  48. }
  49. p:
  50. string guid = Guid.NewGuid().ToString();
  51. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  52. {
  53. int index = RawMaterials.Count;//<= 0 ? 0 : RawMaterials.Count - 1;
  54. RawMaterials.Add(new RawMaterialModel()
  55. {
  56. RawMaterialId = guid,
  57. SelectIndex = index
  58. });
  59. }
  60. else goto p;
  61. });
  62. RemoveCommand = new BPARelayCommand<object>((obj) =>
  63. {
  64. if (obj is string rm)
  65. {
  66. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  67. if (res != null) RawMaterials.Remove(res);
  68. }
  69. });
  70. SaveCommand = new BPARelayCommand(() =>
  71. {
  72. if (string.IsNullOrEmpty(RecipeName)) { MessageNotify.GetInstance.ShowDialog("请输入配方名称!", DialogType.Warning); ErrorInfo = "请输入配方名称"; return; }
  73. var tempRes = RawMaterials.GroupBy(p => p.RawMaterialName);
  74. if (tempRes.Count() != RawMaterials.Count)
  75. {
  76. MessageNotify.GetInstance.ShowDialog("单前原料和其它原料重复,请重新选择!", DialogType.Warning);
  77. return;
  78. }
  79. //编辑配方
  80. if (Index >= 0 && Index < Json<LocaPar>.Data.Recipes.Count)
  81. {
  82. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  83. if (res >= 0 && res != Index)
  84. {
  85. ErrorInfo = "配方名称已经存在!";
  86. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  87. return;
  88. }
  89. Json<LocaPar>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
  90. Json<LocaPar>.Data.Recipes.ElementAt(Index).RawMaterials.Clear();
  91. RawMaterials.ToList()?.ForEach(item =>
  92. {
  93. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName);
  94. if (res != null)
  95. {
  96. item.DeviceIp = res.IpAddress;
  97. item.RawMaterialSource = 1;
  98. }
  99. else
  100. {
  101. item.RawMaterialSource = 0;
  102. }
  103. Json<LocaPar>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
  104. });
  105. Json<LocaPar>.Save();
  106. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
  107. }
  108. else //新建配方
  109. {
  110. if (Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null)
  111. {
  112. ErrorInfo = "配方名称已存在!";
  113. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  114. return;
  115. }
  116. ObservableCollection<RawMaterialModel> TempRawMaterials = new ObservableCollection<RawMaterialModel>();
  117. RawMaterials.ToList()?.ForEach(item =>
  118. {
  119. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName);
  120. if (res != null)
  121. {
  122. item.DeviceIp = res.IpAddress;
  123. item.RawMaterialSource = 1;
  124. }
  125. else
  126. {
  127. item.RawMaterialSource = 0;
  128. }
  129. TempRawMaterials.Add(item);
  130. });
  131. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  132. {
  133. RecipeName = RecipeName,
  134. RawMaterials = TempRawMaterials,
  135. });
  136. Json<LocaPar>.Save();
  137. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!");
  138. }
  139. ActionManage.GetInstance.Send("CloseNewRecipeView");
  140. });
  141. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  142. {
  143. foreach (var item in Global.DeviceRawMaterials)
  144. {
  145. RawMaterialNames.Add(item.RawMaterialName);
  146. }
  147. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  148. {
  149. RawMaterialNames.Add(item.RawMaterialName);
  150. }
  151. }
  152. else
  153. {
  154. foreach (var item in Global.DeviceRawMaterials)
  155. {
  156. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  157. }
  158. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  159. {
  160. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  161. }
  162. }
  163. }
  164. private void AddRecipes()
  165. {
  166. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  167. var dates = date.Split("-").ToList();
  168. StringBuilder sb = new StringBuilder();
  169. dates?.ForEach((item) => { sb.Append(item); });
  170. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  171. {
  172. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  173. RawMaterials = RawMaterials,
  174. RecipCode = sb.ToString(),
  175. RecipeName = RecipeName,
  176. });
  177. }
  178. private string RecipCode = string.Empty;
  179. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  180. private string _mRecipeName = string.Empty;
  181. //public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  182. //private string _mErrorInfo;
  183. //public BPARelayCommand AddCommand { get; set; }
  184. //public BPARelayCommand<object> RemoveCommand { get; set; }
  185. //public BPARelayCommand SaveCommand { get; set; }
  186. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  187. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  188. }
  189. }