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

188 lines
7.2 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.DosingSystem.Model;
  11. using BPASmartClient.CustomResource.Pages.Model;
  12. using BPASmartClient.Model;
  13. namespace BPASmartClient.DosingSystem.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 = DeviceInquire.GetInstance.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. }