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

207 lines
8.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.CustomResource.Pages.Model;
  11. using BPASmartClient.Model;
  12. using BPASmartClient.CustomResource.UserControls.MessageShow;
  13. using BPASmartClient.CustomResource.UserControls;
  14. namespace BPASmartClient.DosingSystem.ViewModel
  15. {
  16. public class NewRecipeViewModel : ViewModelBase
  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. RecipCode = rm.RecipCode;
  31. Index = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  32. }
  33. }), "Details", true);
  34. AddCommand = new RelayCommand(() =>
  35. {
  36. p:
  37. string guid = Guid.NewGuid().ToString();
  38. if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
  39. {
  40. RawMaterials.Add(new RawMaterialModel()
  41. {
  42. RawMaterialId = guid
  43. });
  44. }
  45. else goto p;
  46. });
  47. RemoveCommand = new RelayCommand<object>((obj) =>
  48. {
  49. if (obj is string rm)
  50. {
  51. var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
  52. if (res != null) Global.DeviceRawMaterials.Remove(res);
  53. }
  54. });
  55. SaveCommand = new RelayCommand(() =>
  56. {
  57. if (string.IsNullOrEmpty(RecipeName)) { ErrorInfo = "请输入配方名称"; return; }
  58. //编辑配方
  59. if (Index >= 0 && Index < Json<LocaPar>.Data.Recipes.Count)
  60. {
  61. var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  62. if (res >= 0 && res != Index)
  63. {
  64. ErrorInfo = "配方名称已经存在!";
  65. return;
  66. }
  67. Json<LocaPar>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
  68. Json<LocaPar>.Data.Recipes.ElementAt(Index).RawMaterials.Clear();
  69. RawMaterials.ToList()?.ForEach(item =>
  70. {
  71. Json<LocaPar>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
  72. });
  73. Json<LocaPar>.Save();
  74. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
  75. }
  76. else //新建配方
  77. {
  78. if (Json<LocaPar>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null)
  79. {
  80. ErrorInfo = "配方名称已存在!";
  81. return;
  82. }
  83. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  84. {
  85. RecipeName = RecipeName,
  86. RawMaterials = RawMaterials,
  87. });
  88. Json<LocaPar>.Save();
  89. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!");
  90. }
  91. //for (int i = 0; i < RawMaterials.Count; i++)
  92. //{
  93. // var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
  94. // if (res != null)
  95. // {
  96. // RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;//根据设备名称和原料名称的唯一匹配关系,给原料配置IP
  97. // RawMaterials.ElementAt(i).RawMaterialSource = 1;
  98. // }
  99. // var su = Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == RawMaterials.ElementAt(i).RawMaterialName);
  100. // if (su != null)
  101. // {
  102. // RawMaterials.ElementAt(i).RawMaterialSource = su.RawMaterialSource;
  103. // }
  104. //}
  105. //if (RecipCode.Length <= 0)//新建配方
  106. //{
  107. // var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
  108. // if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  109. // {
  110. // ErrorInfo = "该配方已存在,请重新输入";
  111. // }
  112. // else
  113. // {
  114. // AddRecipes();
  115. // }
  116. //}
  117. //else//修改配方
  118. //{
  119. // var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipCode == RecipCode);
  120. // if (res >= 0 && res < Json<LocaPar>.Data.Recipes.Count)
  121. // {
  122. // Json<LocaPar>.Data.Recipes.ElementAt(res).RecipeName = RecipeName;
  123. // Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Clear();
  124. // foreach (var item in RawMaterials)
  125. // {
  126. // Json<LocaPar>.Data.Recipes.ElementAt(res).RawMaterials.Add(item);
  127. // }
  128. // }
  129. //}
  130. ActionManage.GetInstance.Send("CloseNewRecipeView");
  131. });
  132. if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员)
  133. {
  134. foreach (var item in Global.DeviceRawMaterials)
  135. {
  136. RawMaterialNames.Add(item.RawMaterialName);
  137. }
  138. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  139. {
  140. RawMaterialNames.Add(item.RawMaterialName);
  141. }
  142. }
  143. else
  144. {
  145. foreach (var item in Global.DeviceRawMaterials)
  146. {
  147. if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  148. }
  149. foreach (var item in Json<LocaMaterial>.Data.LocalMaterails)
  150. {
  151. if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName);
  152. }
  153. }
  154. }
  155. private void AddRecipes()
  156. {
  157. string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  158. var dates = date.Split("-").ToList();
  159. StringBuilder sb = new StringBuilder();
  160. dates?.ForEach((item) => { sb.Append(item); });
  161. Json<LocaPar>.Data.Recipes.Add(new RecipeModel()
  162. {
  163. SerialNum = Json<LocaPar>.Data.Recipes.Count + 1,
  164. RawMaterials = RawMaterials,
  165. RecipCode = sb.ToString(),
  166. RecipeName = RecipeName,
  167. });
  168. }
  169. private string RecipCode = string.Empty;
  170. public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } }
  171. private string _mRecipeName = string.Empty;
  172. //public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } }
  173. //private string _mErrorInfo;
  174. //public RelayCommand AddCommand { get; set; }
  175. //public RelayCommand<object> RemoveCommand { get; set; }
  176. //public RelayCommand SaveCommand { get; set; }
  177. public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
  178. public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
  179. }
  180. }