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

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