|
- 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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
-
- namespace BPASmartClient.MilkWithTea.ViewModel
- {
- public class LocalConfigureViewModel : ObservableObject
-
- {
- #region 奶茶配方录入
- /// <summary>
- /// 奶茶配方
- /// </summary>
- public ObservableCollection<MaterialRecipe> materialRecipes { get => materialRecipes1; set => materialRecipes1 = value; }
- /// <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;
-
- private ObservableCollection<MaterialRecipe> materialRecipes1 = new ObservableCollection<MaterialRecipe>();
-
- /// <summary>
- /// 添加一条配方
- /// </summary>
- public RelayCommand AddRecipeCommand { get; set; }
- /// <summary>
- /// 删除一条配方
- /// </summary>
- public RelayCommand<object> RemoveRecipeCommand { get; set; }
- /// <summary>
- /// 取消配方
- /// </summary>
- public RelayCommand RecipeCancelCommand { get; set; }
- /// <summary>
- /// 保存配方
- /// </summary>
- public RelayCommand SaveRecipeCommand { get; set; }
-
- #endregion
-
- #region 本地奶茶配方
- /// <summary>
- /// 本地奶茶配方列表
- /// </summary>
- public ObservableCollection<LocalTeaWithMilkConfig> localMaterialRecipes { get; set; } = GLobal.MaterialRecipes;
-
-
- /// <summary>
- /// 删除配方奶茶
- /// </summary>
- public RelayCommand<object> DeleteRecipeCommand { get; set; }
-
- #endregion
-
- #region 物料位置名称
- /// <summary>
- /// 物料位置名称集合
- /// </summary>
- public ObservableCollection<MaterailNameAndPosion> materailNameAndPosions { get; set; } = new ObservableCollection<MaterailNameAndPosion>();
- public List<MaterailNameAndPosion> materail1 { get; set; } = new List<MaterailNameAndPosion>();
- public List<MaterailNameAndPosion> materail2 { get; set; } = new List<MaterailNameAndPosion>();
- /// <summary>
- /// 更新物料位置
- /// </summary>
- public RelayCommand UpdateMaterialPosionCommand { get; set; }
- #endregion
-
- public LocalConfigureViewModel()
- {
- materialRecipes.Add(new MaterialRecipe()
- {
- MaterialWeight = 10
- });
-
-
-
-
-
-
- AddRecipeCommand = new RelayCommand(new Action(() =>
- {
- materialRecipes.Add(new MaterialRecipe()
- {
- MaterialID = materialRecipes.Count() + 1
- });
-
- }));
-
- RemoveRecipeCommand = new RelayCommand<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 RelayCommand(new Action(() =>
- {
- materialRecipes.Clear();
- LocalGoodName = String.Empty;
- }));
-
- 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<LocalTeaWithMilkConfig>(GLobal.recipePath, localMaterialRecipes);//更新奶茶配方json文件
- MessageBox.Show("保存成功");
- }));
-
-
- DeleteRecipeCommand = new RelayCommand<object>((o =>
- {
- if (o != null && o is int index)
- {
- localMaterialRecipes.RemoveAt(index);
- UpdateLocalJosnData<LocalTeaWithMilkConfig>(GLobal.recipePath, localMaterialRecipes);//更新奶茶配方json文件
- }
- }));
-
- UpdateMaterialPosionCommand = new RelayCommand(new Action(() =>
- {
- materailNameAndPosions.Clear();
- foreach(var item in materail1)
- {
- materailNameAndPosions.Add(item);
- }
- foreach (var item in materail2)
- {
- materailNameAndPosions.Add(item);
- }
- UpdateLocalJosnData<MaterailNameAndPosion>(GLobal.posionPath, materailNameAndPosions);//更新物料位置名称
- MaterailList.Clear();
- foreach (MaterailNameAndPosion m in materailNameAndPosions)
- {
- if (m.MaterialName != null) MaterailList.Add(m.MaterialName);
-
- }
- }));
-
- Init();
- }
-
- /// <summary>
- /// 界面初始化加载
- /// </summary>
- private void Init()
- {
-
- materailNameAndPosions = GLobal.GetJsonToT<MaterailNameAndPosion>(GLobal.posionPath);
- if (materailNameAndPosions.Count == 0)
- {
- foreach (MaterialPosion item in Enum.GetValues(typeof(MaterialPosion)))
- {
- materailNameAndPosions.Add(new MaterailNameAndPosion()
- {
- MaterialPosion = item.ToString()
- });
- }
- }
- materail1 = materailNameAndPosions.Take<MaterailNameAndPosion>(14).ToList();
- materail2 = materailNameAndPosions.TakeLast<MaterailNameAndPosion>(14).ToList();
- foreach (MaterailNameAndPosion m in materailNameAndPosions)
- {
- if (m.MaterialName != null) MaterailList.Add(m.MaterialName);
-
- }
- }
-
-
- /// <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));
-
- }
- }
- }
|