using BPASmartClient.MorkTM;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
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 : ObservableObject
{
//路径
string recipePath = string.Empty;
string posionPath = string.Empty;
#region 奶茶配方录入
///
/// 奶茶配方
///
public ObservableCollection materialRecipes { get; set;} = new ObservableCollection();
///
/// 出料集合
///
public ObservableCollection MaterailList { get; set; } = new ObservableCollection();
///
/// 奶茶名称
///
public string LocalGoodName { get { return _localGoodName; } set { _localGoodName = value; OnPropertyChanged(); } }
private string _localGoodName;
///
/// 添加一条配方
///
public RelayCommand AddRecipeCommand { get; set; }
///
/// 删除一条配方
///
public RelayCommand RemoveRecipeCommand { get; set; }
///
/// 取消配方
///
public RelayCommand RecipeCancelCommand { get; set; }
///
/// 保存配方
///
public RelayCommand SaveRecipeCommand { get; set; }
#endregion
#region 本地奶茶配方
///
/// 本地奶茶配方列表
///
public ObservableCollection localMaterialRecipes { get; set; } = new ObservableCollection();
///
/// 删除配方奶茶
///
public RelayCommand DeleteRecipeCommand { get; set; }
#endregion
#region 物料位置名称
///
/// 物料位置名称集合
///
public ObservableCollection materailNameAndPosions { get; set; } = new ObservableCollection();
///
/// 更新物料位置
///
public RelayCommand 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 RelayCommand(new Action(() =>
{
materialRecipes.Add(new MaterialRecipe()
{
MaterialID = materialRecipes.Count() + 1
}) ;
}));
RemoveRecipeCommand = new RelayCommand((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 RelayCommand(new Action(() =>
{
materialRecipes.Clear();
}));
SaveRecipeCommand = new RelayCommand(new Action(() =>
{
if(LocalGoodName == "" || LocalGoodName == null) return;
if(materialRecipes.Count == 0) return;
localMaterialRecipes.Insert(0, new LocalTeaWithMilkConfig()
{
GoodNames = LocalGoodName,
materialRecipes = materialRecipes
});
UpdateLocalJosnData(recipePath, localMaterialRecipes);//更新奶茶配方json文件
MessageBox.Show("保存成功");
}));
DeleteRecipeCommand = new RelayCommand((o =>
{
if (o != null && o is int index)
{
localMaterialRecipes.RemoveAt(index);
UpdateLocalJosnData(recipePath, localMaterialRecipes);//更新奶茶配方json文件
}
}));
UpdateMaterialPosionCommand = new RelayCommand(new Action(() =>
{
UpdateLocalJosnData(posionPath, materailNameAndPosions);//更新物料位置名称
}));
Init();
}
///
/// 界面初始化加载
///
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(recipePath);
materailNameAndPosions = GetJsonToT(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);
}
}
///
/// 获取Json文件内容,转换成ObservableCollection
///
///
///
///
private ObservableCollection GetJsonToT(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>(datacache); //将string转换为class类,从而达到json文件转换的目的
if(res != null)
return res;
else return new ObservableCollection { };
}
}
}
///
/// 更新Json文件数据
///
///
///
///
private void UpdateLocalJosnData(string path,ObservableCollection ts)
{
if(ts != null) File.WriteAllText(path, JsonConvert.SerializeObject(ts));
}
}
}