终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

194 řádky
8.1 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.JXJFoodSmallStation.Model;
  10. using BPASmartClient.CustomResource.Pages.Model;
  11. using BPASmartClient.Model;
  12. using BPASmartClient.JXJFoodSmallStation.Model.RawMaterial;
  13. using System.Windows.Controls;
  14. using Renci.SshNet.Messages;
  15. namespace BPASmartClient.JXJFoodSmallStation.ViewModel
  16. {
  17. public class NewRecipeViewModel : NotifyBase
  18. {
  19. public NewRecipeViewModel()
  20. {
  21. ActionManage.GetInstance.Register(new Action<object>((o) =>
  22. {
  23. if (o != null && o is RecipeModel rm)
  24. {
  25. RecipeName = rm.RecipeName;
  26. foreach (var item in rm.RawMaterials)
  27. {
  28. item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName);
  29. RawMaterials.Add(item);
  30. }
  31. RecipeCode = rm.RecipCode;
  32. }
  33. }), "Details");
  34. AddCommand = new BPARelayCommand(() =>
  35. {
  36. p:
  37. string guid = Guid.NewGuid().ToString();
  38. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  39. {
  40. RawMaterials.Add(new RawMaterialModel()
  41. {
  42. RawMaterialId = guid
  43. });
  44. MessageNotify.GetInstance.ShowUserLog($"添加一条原料:{guid}");
  45. }
  46. else goto p;
  47. });
  48. RemoveCommand = new BPARelayCommand<object>((obj) =>
  49. {
  50. if (obj is string rm)
  51. {
  52. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  53. if (res != null)
  54. {
  55. Global.DeviceRawMaterials.Remove(res);
  56. MessageNotify.GetInstance.ShowUserLog($"删除一条原料,原料名称:【{res.RawMaterialName}】");
  57. }
  58. }
  59. });
  60. SaveCommand = new BPARelayCommand(() =>
  61. {
  62. ErrorInfo = String.Empty;
  63. if (RecipeName == String.Empty) { ErrorInfo = "配方名称未填写"; return; }
  64. for (int i = 0; i < RawMaterials.Count; i++)
  65. {
  66. var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
  67. if (res != null)
  68. {
  69. RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;//根据设备名称和原料名称的唯一匹配关系,给原料配置IP
  70. RawMaterials.ElementAt(i).RawMaterialSource = 1;
  71. }
  72. var su = Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == RawMaterials.ElementAt(i).RawMaterialName);
  73. if (su != null)
  74. {
  75. RawMaterials.ElementAt(i).RawMaterialSource = su.RawMaterialSource;
  76. }
  77. }
  78. if (RecipeCode.Length <= 0)//新建配方
  79. {
  80. var res = Array.FindIndex(Json<LocaPar>.Data.RemoteRecipes.ToArray(), p => p.RecipeName == RecipeName);
  81. if (res >= 0 && res < Json<LocaPar>.Data.RemoteRecipes.Count)
  82. {
  83. ErrorInfo = "该配方已存在,请重新输入";
  84. }
  85. else
  86. {
  87. AddRecipes();
  88. }
  89. }
  90. else//修改配方
  91. {
  92. var res = Array.FindIndex(Json<LocaPar>.Data.RemoteRecipes.ToArray(), p => p.RecipCode == RecipeCode);
  93. if (res >= 0 && res < Json<LocaPar>.Data.RemoteRecipes.Count)
  94. {
  95. Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RecipeName = RecipeName;
  96. Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RawMaterials.Clear();
  97. foreach (var item in RawMaterials)
  98. {
  99. Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RawMaterials.Add(item);
  100. }
  101. MessageNotify.GetInstance.ShowUserLog($"修改了一条配方,配方编号:【{RecipeCode}】");
  102. }
  103. }
  104. ActionManage.GetInstance.Send("CloseNewRecipeView");
  105. });
  106. //RawMaterialNames.Add("乳化剂");
  107. //RawMaterialNames.Add("酶制剂");
  108. //RawMaterialNames.Add("增味剂");
  109. //RawMaterialNames.Add("营养强化剂");
  110. //RawMaterialNames.Add("抗结剂");
  111. //RawMaterialNames.Add("消泡剂");
  112. //RawMaterialNames.Add("膨松剂");
  113. //RawMaterialNames.Add("防腐剂");
  114. //RawMaterialNames.Add("着色剂");
  115. //RawMaterialNames.Add("甜味剂");
  116. //RawMaterialNames.Add("酸味剂");
  117. //RawMaterialNames.Add("增白剂");
  118. //RawMaterialNames.Add("香料");
  119. //RawMaterialNames.Add("抗氧化剂");
  120. //RawMaterialNames.Add("食用香料");
  121. //RawMaterialNames.Add("增稠剂");
  122. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  123. {
  124. foreach (var item in Global.DeviceRawMaterials)
  125. {
  126. RawMaterialNames.Add(item.RawMaterialName);
  127. }
  128. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  129. {
  130. RawMaterialNames.Add(item.RawMaterialName);
  131. }
  132. }
  133. else
  134. {
  135. foreach (var item in Global.DeviceRawMaterials)
  136. {
  137. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  138. }
  139. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  140. {
  141. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  142. }
  143. }
  144. }
  145. private void AddRecipes()
  146. {
  147. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  148. var dates = date.Split("-").ToList();
  149. StringBuilder sb = new StringBuilder();
  150. dates?.ForEach((item) => { sb.Append(item); });
  151. Json<LocaPar>.Data.RemoteRecipes.Add(new RecipeModel()
  152. {
  153. SerialNum = Json<LocaPar>.Data.RemoteRecipes.Count + 1,
  154. RawMaterials = RawMaterials,
  155. RecipCode = sb.ToString(),
  156. RecipeName = RecipeName,
  157. });
  158. MessageNotify.GetInstance.ShowUserLog($"新增了一条配方,配方名称:【{RecipeName}】,配方编号:【{RecipeCode}】");
  159. }
  160. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  161. private string _mRecipeCode = string.Empty;
  162. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  163. private string _mRecipeName = string.Empty;
  164. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  165. private string _mErrorInfo;
  166. public BPARelayCommand AddCommand { get; set; }
  167. public BPARelayCommand<object> RemoveCommand { get; set; }
  168. public BPARelayCommand SaveCommand { get; set; }
  169. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  170. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  171. }
  172. }