终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

242 行
7.9 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.Text;
  11. using System.Windows;
  12. namespace ViewModel
  13. {
  14. public class RecipeViewModel : ObservableObject
  15. {
  16. //路径
  17. string recipePath = string.Empty;
  18. string posionPath = string.Empty;
  19. #region 奶茶配方录入
  20. /// <summary>
  21. /// 奶茶配方
  22. /// </summary>
  23. public ObservableCollection<MaterialRecipe> materialRecipes { get; set;} = new ObservableCollection<MaterialRecipe>();
  24. /// <summary>
  25. /// 出料集合
  26. /// </summary>
  27. public ObservableCollection<string> MaterailList { get; set; } = new ObservableCollection<string>();
  28. /// <summary>
  29. /// 奶茶名称
  30. /// </summary>
  31. public string LocalGoodName { get { return _localGoodName; } set { _localGoodName = value; OnPropertyChanged(); } }
  32. private string _localGoodName;
  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; } = new ObservableCollection<LocalTeaWithMilkConfig>();
  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. /// <summary>
  66. /// 更新物料位置
  67. /// </summary>
  68. public RelayCommand UpdateMaterialPosionCommand{ get; set; }
  69. #endregion
  70. public RecipeViewModel()
  71. {
  72. materialRecipes.Add(new MaterialRecipe()
  73. {
  74. MaterialWeight = 10
  75. });
  76. foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
  77. {
  78. materailNameAndPosions.Add(new MaterailNameAndPosion()
  79. {
  80. MaterialPosion = item.ToString()
  81. });
  82. }
  83. AddRecipeCommand = new RelayCommand(new Action(() =>
  84. {
  85. materialRecipes.Add(new MaterialRecipe()
  86. {
  87. MaterialID = materialRecipes.Count() + 1
  88. }) ;
  89. }));
  90. RemoveRecipeCommand = new RelayCommand<object>((o=>
  91. {
  92. if(o!=null&&o is int index)
  93. {
  94. materialRecipes.RemoveAt(index);
  95. for (int i = 0; i < materialRecipes.Count; i++)//ID排序
  96. {
  97. materialRecipes[i].MaterialID = i + 1;
  98. }
  99. }
  100. }));
  101. RecipeCancelCommand = new RelayCommand(new Action(() =>
  102. {
  103. materialRecipes.Clear();
  104. }));
  105. SaveRecipeCommand = new RelayCommand(new Action(() =>
  106. {
  107. if(LocalGoodName == "" || LocalGoodName == null) return;
  108. if(materialRecipes.Count == 0) return;
  109. localMaterialRecipes.Insert(0, new LocalTeaWithMilkConfig()
  110. {
  111. GoodNames = LocalGoodName,
  112. materialRecipes = materialRecipes
  113. });
  114. UpdateLocalJosnData<LocalTeaWithMilkConfig>(recipePath, localMaterialRecipes);//更新奶茶配方json文件
  115. MessageBox.Show("保存成功");
  116. }));
  117. DeleteRecipeCommand = new RelayCommand<object>((o =>
  118. {
  119. if (o != null && o is int index)
  120. {
  121. localMaterialRecipes.RemoveAt(index);
  122. UpdateLocalJosnData<LocalTeaWithMilkConfig>(recipePath, localMaterialRecipes);//更新奶茶配方json文件
  123. }
  124. }));
  125. UpdateMaterialPosionCommand = new RelayCommand(new Action(() =>
  126. {
  127. UpdateLocalJosnData<MaterailNameAndPosion>(posionPath, materailNameAndPosions);//更新物料位置名称
  128. }));
  129. Init();
  130. }
  131. /// <summary>
  132. /// 界面初始化加载
  133. /// </summary>
  134. private void Init()
  135. {
  136. string path = Path.Combine(Environment.CurrentDirectory, "AccessFile", "Recipes");
  137. //判断文件夹是否存在,如果不存在就创建file文件夹
  138. if (!Directory.Exists(path))
  139. {
  140. Directory.CreateDirectory(path);
  141. }
  142. recipePath = Path.Combine(path, "LocalRecipes.json");
  143. posionPath = Path.Combine(path, "MaterialPosion.json");
  144. localMaterialRecipes = GetJsonToT<LocalTeaWithMilkConfig>(recipePath);
  145. materailNameAndPosions = GetJsonToT<MaterailNameAndPosion>(posionPath);
  146. if(materailNameAndPosions.Count == 0)
  147. {
  148. foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
  149. {
  150. materailNameAndPosions.Add(new MaterailNameAndPosion()
  151. {
  152. MaterialPosion = item.ToString()
  153. });
  154. }
  155. }
  156. foreach(MaterailNameAndPosion m in materailNameAndPosions)
  157. {
  158. if(m.MaterialName!=null) MaterailList.Add(m.MaterialName);
  159. }
  160. }
  161. /// <summary>
  162. /// 获取Json文件内容,转换成ObservableCollection
  163. /// </summary>
  164. /// <typeparam name="T"></typeparam>
  165. /// <param name="path"></param>
  166. /// <returns></returns>
  167. private ObservableCollection<T> GetJsonToT<T>(string path)
  168. {
  169. if (!File.Exists(path))
  170. {
  171. //创建该文件
  172. File.Create(path);
  173. return default;
  174. }
  175. else
  176. {
  177. using (StreamReader recipeReader = new StreamReader(path))//读取json文件
  178. {
  179. string datacache = "";
  180. string line;
  181. while ((line = recipeReader.ReadLine()) != null) //循环将每一行数据拼接为一个完整的字符串
  182. {
  183. datacache = datacache + line;
  184. }
  185. var res = JsonConvert.DeserializeObject<ObservableCollection<T>>(datacache); //将string转换为class类,从而达到json文件转换的目的
  186. if(res != null)
  187. return res;
  188. else return new ObservableCollection<T> { };
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// 更新Json文件数据
  194. /// </summary>
  195. /// <typeparam name="T"></typeparam>
  196. /// <param name="path"></param>
  197. /// <param name="ts"></param>
  198. private void UpdateLocalJosnData<T>(string path,ObservableCollection<T> ts)
  199. {
  200. if(ts != null) File.WriteAllText(path, JsonConvert.SerializeObject(ts));
  201. }
  202. }
  203. }