|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using BPASmartClient.MorkTM;
- using BPA.Helper;
-
- using Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Text;
- using System.Windows;
-
- namespace ViewModel
- {
- public class RecipeViewModel : NotifyBase
- {
- //路径
- string recipePath = string.Empty;
- string posionPath = string.Empty;
-
-
- #region 奶茶配方录入
- /// <summary>
- /// 奶茶配方
- /// </summary>
- public ObservableCollection<MaterialRecipe> materialRecipes { get; set;} = new ObservableCollection<MaterialRecipe>();
- /// <summary>
- /// 出料集合
- /// </summary>
- public ObservableCollection<string> MaterailList { get; set; } = new ObservableCollection<string>();
- /// <summary>
- /// 奶茶名称
- /// </summary>
- public string LocalGoodName { get { return _localGoodName; } set { _localGoodName = value; OnPropertyChanged(); } }
- private string _localGoodName;
- /// <summary>
- /// 添加一条配方
- /// </summary>
- public BPARelayCommand AddRecipeCommand { get; set; }
- /// <summary>
- /// 删除一条配方
- /// </summary>
- public BPARelayCommand<object> RemoveRecipeCommand { get; set; }
- /// <summary>
- /// 取消配方
- /// </summary>
- public BPARelayCommand RecipeCancelCommand { get; set; }
- /// <summary>
- /// 保存配方
- /// </summary>
- public BPARelayCommand SaveRecipeCommand { get; set; }
-
- #endregion
-
- #region 本地奶茶配方
- /// <summary>
- /// 本地奶茶配方列表
- /// </summary>
- public ObservableCollection<LocalTeaWithMilkConfig> localMaterialRecipes { get; set; } = new ObservableCollection<LocalTeaWithMilkConfig>();
- /// <summary>
- /// 删除配方奶茶
- /// </summary>
- public BPARelayCommand<object> DeleteRecipeCommand { get; set; }
-
- #endregion
-
- #region 物料位置名称
- /// <summary>
- /// 物料位置名称集合
- /// </summary>
- public ObservableCollection<MaterailNameAndPosion> materailNameAndPosions { get; set; } = new ObservableCollection<MaterailNameAndPosion>();
- /// <summary>
- /// 更新物料位置
- /// </summary>
- public BPARelayCommand UpdateMaterialPosionCommand{ get; set; }
- #endregion
-
- public RecipeViewModel()
- {
- materialRecipes.Add(new MaterialRecipe()
- {
- MaterialWeight = 10
- });
-
-
- foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
- {
- materailNameAndPosions.Add(new MaterailNameAndPosion()
- {
- MaterialPosion = item.ToString()
- });
- }
-
-
- AddRecipeCommand = new BPARelayCommand(new Action(() =>
- {
- materialRecipes.Add(new MaterialRecipe()
- {
- MaterialID = materialRecipes.Count() + 1
- }) ;
-
- }));
-
- RemoveRecipeCommand = new BPARelayCommand<object>((o=>
- {
- if(o!=null&&o is int index)
- {
- materialRecipes.RemoveAt(index);
- for (int i = 0; i < materialRecipes.Count; i++)//ID排序
- {
- materialRecipes[i].MaterialID = i + 1;
- }
- }
-
- }));
-
- RecipeCancelCommand = new BPARelayCommand(new Action(() =>
- {
- materialRecipes.Clear();
- }));
-
- SaveRecipeCommand = new BPARelayCommand(new Action(() =>
- {
- if(LocalGoodName == "" || LocalGoodName == null) return;
- if(materialRecipes.Count == 0) return;
-
- localMaterialRecipes.Insert(0, new LocalTeaWithMilkConfig()
- {
- GoodNames = LocalGoodName,
- materialRecipes = materialRecipes
-
- });
-
- UpdateLocalJosnData<LocalTeaWithMilkConfig>(recipePath, localMaterialRecipes);//更新奶茶配方json文件
- MessageBox.Show("保存成功");
- }));
-
-
- DeleteRecipeCommand = new BPARelayCommand<object>((o =>
- {
- if (o != null && o is int index)
- {
- localMaterialRecipes.RemoveAt(index);
- UpdateLocalJosnData<LocalTeaWithMilkConfig>(recipePath, localMaterialRecipes);//更新奶茶配方json文件
- }
- }));
-
- UpdateMaterialPosionCommand = new BPARelayCommand(new Action(() =>
- {
- UpdateLocalJosnData<MaterailNameAndPosion>(posionPath, materailNameAndPosions);//更新物料位置名称
- }));
-
- Init();
- }
-
- /// <summary>
- /// 界面初始化加载
- /// </summary>
- private void Init()
- {
- string path = Path.Combine(Environment.CurrentDirectory, "AccessFile", "Recipes");
- //判断文件夹是否存在,如果不存在就创建file文件夹
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- recipePath = Path.Combine(path, "LocalRecipes.json");
- posionPath = Path.Combine(path, "MaterialPosion.json");
-
- localMaterialRecipes = GetJsonToT<LocalTeaWithMilkConfig>(recipePath);
- materailNameAndPosions = GetJsonToT<MaterailNameAndPosion>(posionPath);
- if(materailNameAndPosions.Count == 0)
- {
- foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
- {
- materailNameAndPosions.Add(new MaterailNameAndPosion()
- {
- MaterialPosion = item.ToString()
- });
- }
- }
-
- foreach(MaterailNameAndPosion m in materailNameAndPosions)
- {
- if(m.MaterialName!=null) MaterailList.Add(m.MaterialName);
-
- }
-
-
-
- }
-
- /// <summary>
- /// 获取Json文件内容,转换成ObservableCollection
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="path"></param>
- /// <returns></returns>
- private ObservableCollection<T> GetJsonToT<T>(string path)
- {
- if (!File.Exists(path))
- {
- //创建该文件
- File.Create(path);
- return default;
- }
- else
- {
- using (StreamReader recipeReader = new StreamReader(path))//读取json文件
- {
- string datacache = "";
- string line;
- while ((line = recipeReader.ReadLine()) != null) //循环将每一行数据拼接为一个完整的字符串
- {
- datacache = datacache + line;
- }
-
- var res = JsonConvert.DeserializeObject<ObservableCollection<T>>(datacache); //将string转换为class类,从而达到json文件转换的目的
- if(res != null)
- return res;
- else return new ObservableCollection<T> { };
- }
- }
- }
- /// <summary>
- /// 更新Json文件数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="path"></param>
- /// <param name="ts"></param>
- private void UpdateLocalJosnData<T>(string path,ObservableCollection<T> ts)
- {
- if(ts != null) File.WriteAllText(path, JsonConvert.SerializeObject(ts));
-
- }
-
- }
-
-
-
- }
|