终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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