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

220 line
9.8 KiB

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