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

187 lines
7.4 KiB

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