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

NewRecipeViewModel.cs 7.2 KiB

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