终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NewRecipeViewModel.cs 7.4 KiB

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