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

212 line
7.2 KiB

  1. using BPASmartClient.MorkTM;
  2. using Microsoft.Toolkit.Mvvm.ComponentModel;
  3. using Microsoft.Toolkit.Mvvm.Input;
  4. using Model;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace BPASmartClient.MilkWithTea.ViewModel
  15. {
  16. public class LocalConfigureViewModel : ObservableObject
  17. {
  18. #region 奶茶配方录入
  19. /// <summary>
  20. /// 奶茶配方
  21. /// </summary>
  22. public ObservableCollection<MaterialRecipe> materialRecipes { get => materialRecipes1; set => materialRecipes1 = value; }
  23. /// <summary>
  24. /// 出料集合
  25. /// </summary>
  26. public ObservableCollection<string> MaterailList { get; set; } = new ObservableCollection<string>();
  27. /// <summary>
  28. /// 奶茶名称
  29. /// </summary>
  30. public string LocalGoodName { get { return _localGoodName; } set { _localGoodName = value; OnPropertyChanged(); } }
  31. private string _localGoodName;
  32. private ObservableCollection<MaterialRecipe> materialRecipes1 = new ObservableCollection<MaterialRecipe>();
  33. /// <summary>
  34. /// 添加一条配方
  35. /// </summary>
  36. public RelayCommand AddRecipeCommand { get; set; }
  37. /// <summary>
  38. /// 删除一条配方
  39. /// </summary>
  40. public RelayCommand<object> RemoveRecipeCommand { get; set; }
  41. /// <summary>
  42. /// 取消配方
  43. /// </summary>
  44. public RelayCommand RecipeCancelCommand { get; set; }
  45. /// <summary>
  46. /// 保存配方
  47. /// </summary>
  48. public RelayCommand SaveRecipeCommand { get; set; }
  49. #endregion
  50. #region 本地奶茶配方
  51. /// <summary>
  52. /// 本地奶茶配方列表
  53. /// </summary>
  54. public ObservableCollection<LocalTeaWithMilkConfig> localMaterialRecipes { get; set; } = GLobal.MaterialRecipes;
  55. /// <summary>
  56. /// 删除配方奶茶
  57. /// </summary>
  58. public RelayCommand<object> DeleteRecipeCommand { get; set; }
  59. #endregion
  60. #region 物料位置名称
  61. /// <summary>
  62. /// 物料位置名称集合
  63. /// </summary>
  64. public ObservableCollection<MaterailNameAndPosion> materailNameAndPosions { get; set; } = new ObservableCollection<MaterailNameAndPosion>();
  65. public List<MaterailNameAndPosion> materail1 { get; set; } = new List<MaterailNameAndPosion>();
  66. public List<MaterailNameAndPosion> materail2 { get; set; } = new List<MaterailNameAndPosion>();
  67. /// <summary>
  68. /// 更新物料位置
  69. /// </summary>
  70. public RelayCommand UpdateMaterialPosionCommand { get; set; }
  71. #endregion
  72. public LocalConfigureViewModel()
  73. {
  74. materialRecipes.Add(new MaterialRecipe()
  75. {
  76. MaterialWeight = 10
  77. });
  78. AddRecipeCommand = new RelayCommand(new Action(() =>
  79. {
  80. materialRecipes.Add(new MaterialRecipe()
  81. {
  82. MaterialID = materialRecipes.Count() + 1
  83. });
  84. }));
  85. RemoveRecipeCommand = new RelayCommand<object>((o =>
  86. {
  87. if (o != null && o is int index)
  88. {
  89. materialRecipes.RemoveAt(index);
  90. for (int i = 0; i < materialRecipes.Count; i++)//ID排序
  91. {
  92. materialRecipes[i].MaterialID = i + 1;
  93. }
  94. }
  95. }));
  96. RecipeCancelCommand = new RelayCommand(new Action(() =>
  97. {
  98. materialRecipes.Clear();
  99. LocalGoodName = String.Empty;
  100. }));
  101. SaveRecipeCommand = new RelayCommand(new Action(() =>
  102. {
  103. if (LocalGoodName == "" || LocalGoodName == null) return;
  104. if (materialRecipes.Count == 0) return;
  105. localMaterialRecipes.Insert(0, new LocalTeaWithMilkConfig()
  106. {
  107. GoodNames = LocalGoodName,
  108. materialRecipes = materialRecipes
  109. });
  110. UpdateLocalJosnData<LocalTeaWithMilkConfig>(GLobal.recipePath, localMaterialRecipes);//更新奶茶配方json文件
  111. MessageBox.Show("保存成功");
  112. }));
  113. DeleteRecipeCommand = new RelayCommand<object>((o =>
  114. {
  115. if (o != null && o is int index)
  116. {
  117. localMaterialRecipes.RemoveAt(index);
  118. UpdateLocalJosnData<LocalTeaWithMilkConfig>(GLobal.recipePath, localMaterialRecipes);//更新奶茶配方json文件
  119. }
  120. }));
  121. UpdateMaterialPosionCommand = new RelayCommand(new Action(() =>
  122. {
  123. materailNameAndPosions.Clear();
  124. foreach(var item in materail1)
  125. {
  126. materailNameAndPosions.Add(item);
  127. }
  128. foreach (var item in materail2)
  129. {
  130. materailNameAndPosions.Add(item);
  131. }
  132. UpdateLocalJosnData<MaterailNameAndPosion>(GLobal.posionPath, materailNameAndPosions);//更新物料位置名称
  133. MaterailList.Clear();
  134. foreach (MaterailNameAndPosion m in materailNameAndPosions)
  135. {
  136. if (m.MaterialName != null) MaterailList.Add(m.MaterialName);
  137. }
  138. }));
  139. Init();
  140. }
  141. /// <summary>
  142. /// 界面初始化加载
  143. /// </summary>
  144. private void Init()
  145. {
  146. materailNameAndPosions = GLobal.GetJsonToT<MaterailNameAndPosion>(GLobal.posionPath);
  147. if (materailNameAndPosions.Count == 0)
  148. {
  149. foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
  150. {
  151. materailNameAndPosions.Add(new MaterailNameAndPosion()
  152. {
  153. MaterialPosion = item.ToString()
  154. });
  155. }
  156. }
  157. materail1 = materailNameAndPosions.Take<MaterailNameAndPosion>(14).ToList();
  158. materail2 = materailNameAndPosions.TakeLast<MaterailNameAndPosion>(14).ToList();
  159. foreach (MaterailNameAndPosion m in materailNameAndPosions)
  160. {
  161. if (m.MaterialName != null) MaterailList.Add(m.MaterialName);
  162. }
  163. }
  164. /// <summary>
  165. /// 更新Json文件数据
  166. /// </summary>
  167. /// <typeparam name="T"></typeparam>
  168. /// <param name="path"></param>
  169. /// <param name="ts"></param>
  170. private void UpdateLocalJosnData<T>(string path, ObservableCollection<T> ts)
  171. {
  172. if (ts != null) File.WriteAllText(path, JsonConvert.SerializeObject(ts));
  173. }
  174. }
  175. }