using BPASmartClient.Helper; using BPASmartClient.Message; using FryPot_DosingSystem.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace FryPot_DosingSystem.ViewModel { internal class RecipeSetDownViewModel : ObservableObject { /// /// 配方下发 /// public RelayCommand RecipeSetDownCommand { get; set; } /// /// 配方一键下发 /// public RelayCommand AllRecipeSetDownCommand { get; set; } public ObservableCollection Recipes { get; set; } = new ObservableCollection(); public RecipeSetDownViewModel() { Json.Read(); Recipes =Json.Data.Recipes; RecipeSetDownCommand = new RelayCommand((Id) => { if (Id != null && Id is string strId) { var res = Recipes.FirstOrDefault(p => p.RecipeId == strId); if (res != null) { RecipeSetDown(new NewRecipeModel[] { res }); res.RecipeSetInfo = "配方下发成功"; Task.Run(() => { Thread.Sleep(1500); res.RecipeSetInfo = string.Empty; }); } else { res.RecipeSetInfo = "配方下发失败"; Task.Run(() => { Thread.Sleep(1500); res.RecipeSetInfo = string.Empty; }); } } }); AllRecipeSetDownCommand = new RelayCommand(() => { if (Recipes != null) { //foreach (var recipeModel in Recipes) //{ // RecipeSetDown(recipeModel); //} RecipeSetDown(Recipes.ToArray()); Task.Run(() => { foreach (var item in Recipes) { item.RecipeSetInfo = "下发成功"; } Thread.Sleep(1500); foreach (var item in Recipes) { item.RecipeSetInfo = string.Empty; } }); } }); } /// /// 数据下发方法 /// public void RecipeSetDown(NewRecipeModel[] recipes) { ActionManage.GetInstance.Send("RecipeSetDown", recipes); } } }