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

242 line
7.9 KiB

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