终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

241 lines
11 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. using System.Windows.Controls;
  12. namespace BPASmartClient.DosingSystem.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<LocalRecipe>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  40. All_RawMt_weight = 0;
  41. RawMaterials.ToList()?.ForEach(item =>
  42. {
  43. All_RawMt_weight += (int)item.RawMaterialWeight;
  44. });
  45. }
  46. }), "Details", true);
  47. AddCommand = new BPARelayCommand(() =>
  48. {
  49. //if (RawMaterials.Count >= DeviceInquire.GetInstance.devices.Count)
  50. //{
  51. // MessageNotify.GetInstance.ShowDialog("原材料不足!");
  52. // return;
  53. //}
  54. p:
  55. string guid = Guid.NewGuid().ToString();
  56. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  57. {
  58. int index = RawMaterials.Count;//<= 0 ? 0 : RawMaterials.Count - 1;
  59. RawMaterials.Add(new RawMaterialModel()
  60. {
  61. RawMaterialId = guid,
  62. SelectIndex = index
  63. });
  64. }
  65. else goto p;
  66. });
  67. RemoveCommand = new BPARelayCommand<object>((obj) =>
  68. {
  69. if (obj is string rm)
  70. {
  71. All_RawMt_weight = 0;
  72. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  73. if (res != null) RawMaterials.Remove(res);
  74. RawMaterials.ToList()?.ForEach(item =>
  75. {
  76. All_RawMt_weight += (int)item.RawMaterialWeight;
  77. });
  78. }
  79. });
  80. TextChanged = new BPARelayCommand(() =>
  81. {
  82. All_RawMt_weight = 0;
  83. RawMaterials.ToList()?.ForEach(item =>
  84. {
  85. All_RawMt_weight += (int)item.RawMaterialWeight;
  86. });
  87. });
  88. SaveCommand = new BPARelayCommand(() =>
  89. {
  90. if (string.IsNullOrEmpty(RecipeName)) { MessageNotify.GetInstance.ShowDialog("请输入配方名称!", DialogType.Warning); ErrorInfo = "请输入配方名称"; return; }
  91. var tempRes = RawMaterials.GroupBy(p => p.RawMaterialName);
  92. //if (tempRes.Count() != RawMaterials.Count)
  93. //{
  94. // MessageNotify.GetInstance.ShowDialog("单前原料和其它原料重复,请重新选择!", DialogType.Warning);
  95. // return;
  96. //}
  97. //编辑配方
  98. if (Index >= 0 && Index < Json<LocalRecipe>.Data.Recipes.Count)
  99. {
  100. var res = Array.FindIndex(Json<LocalRecipe>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  101. if (res >= 0 && res != Index)
  102. {
  103. ErrorInfo = "配方名称已经存在!";
  104. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  105. return;
  106. }
  107. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
  108. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RawMaterials.Clear();
  109. RawMaterials.ToList()?.ForEach(item =>
  110. {
  111. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName);
  112. if (res != null)
  113. {
  114. item.DeviceIp = res.IpAddress;
  115. item.RawMaterialSource = 1;
  116. }
  117. else
  118. {
  119. item.RawMaterialSource = 0;
  120. }
  121. Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
  122. });
  123. Json<LocalRecipe>.Save();
  124. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
  125. }
  126. else //新建配方
  127. {
  128. if (Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null)
  129. {
  130. ErrorInfo = "配方名称已存在!";
  131. MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
  132. return;
  133. }
  134. ObservableCollection<RawMaterialModel> TempRawMaterials = new ObservableCollection<RawMaterialModel>();
  135. RawMaterials.ToList()?.ForEach(item =>
  136. {
  137. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName);
  138. if (res != null)
  139. {
  140. item.DeviceIp = res.IpAddress;
  141. item.RawMaterialSource = 1;
  142. }
  143. else
  144. {
  145. item.RawMaterialSource = 0;
  146. }
  147. TempRawMaterials.Add(item);
  148. });
  149. Json<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
  150. {
  151. RecipeName = RecipeName,
  152. RawMaterials = TempRawMaterials,
  153. });
  154. Json<LocalRecipe>.Save();
  155. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!");
  156. //配方名称集合上报
  157. if (ExcuteControl.GetInstance.mqttCollection.ContainsKey("Conveyer")&& ExcuteControl.GetInstance.mqttCollection["Conveyer"]._mqttClient.IsConnected)
  158. {
  159. AliyunSystemPropertyReportModel m1 = new AliyunSystemPropertyReportModel();
  160. m1.RecipeCollection = new string[Json<LocalRecipe>.Data.Recipes.Count];
  161. for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
  162. {
  163. m1.RecipeCollection[i] = Json<LocalRecipe>.Data.Recipes[i].RecipeName;
  164. }
  165. /*ExcuteControl.GetInstance.SystemPropertyReport(m1); */
  166. }
  167. }
  168. ActionManage.GetInstance.Send("CloseNewRecipeView");
  169. });
  170. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  171. {
  172. foreach (var item in Global.DeviceRawMaterials)
  173. {
  174. RawMaterialNames.Add(item.RawMaterialName);
  175. }
  176. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  177. {
  178. RawMaterialNames.Add(item.RawMaterialName);
  179. }
  180. }
  181. else
  182. {
  183. foreach (var item in Global.DeviceRawMaterials)
  184. {
  185. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  186. }
  187. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  188. {
  189. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  190. }
  191. }
  192. }
  193. private void AddRecipes()
  194. {
  195. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  196. var dates = date.Split("-").ToList();
  197. StringBuilder sb = new StringBuilder();
  198. dates?.ForEach((item) => { sb.Append(item); });
  199. Json<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
  200. {
  201. SerialNum = Json<LocalRecipe>.Data.Recipes.Count + 1,
  202. RawMaterials = RawMaterials,
  203. RecipCode = sb.ToString(),
  204. RecipeName = RecipeName,
  205. });
  206. }
  207. private string RecipCode = string.Empty;
  208. public int all_RawMt_weight = 0;
  209. public int All_RawMt_weight { get { return all_RawMt_weight; } set { all_RawMt_weight = value; OnPropertyChanged(); } }
  210. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  211. private string _mRecipeName = string.Empty;
  212. //public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  213. //private string _mErrorInfo;
  214. //public BPARelayCommand AddCommand { get; set; }
  215. //public BPARelayCommand<object> RemoveCommand { get; set; }
  216. //public BPARelayCommand SaveCommand { get; set; }
  217. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  218. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  219. }
  220. }