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

185 rivejä
7.5 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. namespace BPASmartClient.JXJFoodSmallStation.ViewModel
  15. {
  16. public class NewRecipeViewModel : ObservableObject
  17. {
  18. public NewRecipeViewModel()
  19. {
  20. ActionManage.GetInstance.Register(new Action<object>((o) =>
  21. {
  22. if (o != null && o is RecipeModel rm)
  23. {
  24. RecipeName = rm.RecipeName;
  25. foreach (var item in rm.RawMaterials)
  26. {
  27. item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName);
  28. RawMaterials.Add(item);
  29. }
  30. RecipeCode = rm.RecipCode;
  31. }
  32. }), "Details");
  33. AddCommand = new RelayCommand(() =>
  34. {
  35. p:
  36. string guid = Guid.NewGuid().ToString();
  37. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  38. {
  39. RawMaterials.Add(new RawMaterialModel()
  40. {
  41. RawMaterialId = guid
  42. });
  43. }
  44. else goto p;
  45. });
  46. RemoveCommand = new RelayCommand<object>((obj) =>
  47. {
  48. if (obj is string rm)
  49. {
  50. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  51. if (res != null) Global.DeviceRawMaterials.Remove(res);
  52. }
  53. });
  54. SaveCommand = new RelayCommand(() =>
  55. {
  56. ErrorInfo = String.Empty;
  57. if (RecipeName == String.Empty) { 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<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  75. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  76. {
  77. ErrorInfo = "该配方已存在,请重新输入";
  78. }
  79. else
  80. {
  81. AddRecipes();
  82. }
  83. }
  84. else//修改配方
  85. {
  86. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipCode == RecipeCode);
  87. if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  88. {
  89. Json<LocaPar>.Data.Recipes.ElementAt(res).RecipeName = RecipeName;
  90. Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Clear();
  91. foreach (var item in RawMaterials)
  92. {
  93. Json<LocaPar>.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) 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) RawMaterialNames.Add(item.RawMaterialName);
  135. }
  136. }
  137. }
  138. private void AddRecipes()
  139. {
  140. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  141. var dates = date.Split("-").ToList();
  142. StringBuilder sb = new StringBuilder();
  143. dates?.ForEach((item) => { sb.Append(item); });
  144. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  145. {
  146. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  147. RawMaterials = RawMaterials,
  148. RecipCode = sb.ToString(),
  149. RecipeName = RecipeName,
  150. });
  151. }
  152. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  153. private string _mRecipeCode = string.Empty;
  154. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  155. private string _mRecipeName = string.Empty;
  156. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  157. private string _mErrorInfo;
  158. public RelayCommand AddCommand { get; set; }
  159. public RelayCommand<object> RemoveCommand { get; set; }
  160. public RelayCommand SaveCommand { get; set; }
  161. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  162. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  163. }
  164. }