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

165 lines
6.6 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.DosingHKProject.Model;
  11. using BPASmartClient.CustomResource.Pages.Model;
  12. using BPASmartClient.Model;
  13. using BPASmartClient.DosingHKProject.Model.RawMaterial;
  14. namespace BPASmartClient.DosingHKProject.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. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  99. {
  100. foreach (var item in Global.DeviceRawMaterials)
  101. {
  102. RawMaterialNames.Add(item.RawMaterialName);
  103. }
  104. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  105. {
  106. RawMaterialNames.Add(item.RawMaterialName);
  107. }
  108. }
  109. else
  110. {
  111. foreach (var item in Global.DeviceRawMaterials)
  112. {
  113. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  114. }
  115. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  116. {
  117. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  118. }
  119. }
  120. }
  121. private void AddRecipes()
  122. {
  123. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  124. var dates = date.Split("-").ToList();
  125. StringBuilder sb = new StringBuilder();
  126. dates?.ForEach((item) => { sb.Append(item); });
  127. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  128. {
  129. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  130. RawMaterials = RawMaterials,
  131. RecipCode = sb.ToString(),
  132. RecipeName = RecipeName,
  133. });
  134. }
  135. public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
  136. private string _mRecipeCode = string.Empty;
  137. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  138. private string _mRecipeName = string.Empty;
  139. public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  140. private string _mErrorInfo;
  141. public RelayCommand AddCommand { get; set; }
  142. public RelayCommand<object> RemoveCommand { get; set; }
  143. public RelayCommand SaveCommand { get; set; }
  144. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  145. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  146. }
  147. }