using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using BPA.Helper; using System.Collections.ObjectModel; using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.Model; using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.CustomResource.UserControls; namespace BPASmartClient.DosingSystem.ViewModel { public class NewRecipeViewModel : ViewModelBase { public NewRecipeViewModel() { ActionManage.GetInstance.RegisterAsync(new Action((o) => { if (!string.IsNullOrEmpty(o?.ToString())) { if (RawMaterials.FirstOrDefault(p => p.RawMaterialName == o.ToString()) != null) { App.Current.Dispatcher.Invoke(() => { MessageNotify.GetInstance.ShowDialog("当前原料和其它原料重复,请重新选择!", DialogType.Warning); }); } } }), "原料选择修改"); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is RecipeModel rm) { RecipeName = rm.RecipeName; foreach (var item in rm.RawMaterials) { item.SelectIndex = Array.FindIndex(RawMaterialNames.ToArray(), p => p == item.RawMaterialName); RawMaterials.Add(item); } RecipCode = rm.RecipCode; Index = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName); } }), "Details", true); AddCommand = new BPARelayCommand(() => { if (RawMaterials.Count >= DeviceInquire.GetInstance.devices.Count) { MessageNotify.GetInstance.ShowDialog("原材料不足!"); return; } p: string guid = Guid.NewGuid().ToString(); if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null) { int index = RawMaterials.Count;//<= 0 ? 0 : RawMaterials.Count - 1; RawMaterials.Add(new RawMaterialModel() { RawMaterialId = guid, SelectIndex = index }); } else goto p; }); RemoveCommand = new BPARelayCommand((obj) => { if (obj is string rm) { var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm); if (res != null) RawMaterials.Remove(res); } }); SaveCommand = new BPARelayCommand(() => { if (string.IsNullOrEmpty(RecipeName)) { MessageNotify.GetInstance.ShowDialog("请输入配方名称!", DialogType.Warning); ErrorInfo = "请输入配方名称"; return; } var tempRes = RawMaterials.GroupBy(p => p.RawMaterialName); if (tempRes.Count() != RawMaterials.Count) { MessageNotify.GetInstance.ShowDialog("单前原料和其它原料重复,请重新选择!", DialogType.Warning); return; } //编辑配方 if (Index >= 0 && Index < Json.Data.Recipes.Count) { var res = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName); if (res >= 0 && res != Index) { ErrorInfo = "配方名称已经存在!"; MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning); return; } Json.Data.Recipes.ElementAt(Index).RecipeName = RecipeName; Json.Data.Recipes.ElementAt(Index).RawMaterials.Clear(); RawMaterials.ToList()?.ForEach(item => { var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName); if (res != null) { item.DeviceIp = res.IpAddress; item.RawMaterialSource = 1; } else { item.RawMaterialSource = 0; } Json.Data.Recipes.ElementAt(Index).RawMaterials.Add(item); }); Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成"); } else //新建配方 { if (Json.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null) { ErrorInfo = "配方名称已存在!"; MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning); return; } ObservableCollection TempRawMaterials = new ObservableCollection(); RawMaterials.ToList()?.ForEach(item => { var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == item.RawMaterialName); if (res != null) { item.DeviceIp = res.IpAddress; item.RawMaterialSource = 1; } else { item.RawMaterialSource = 0; } TempRawMaterials.Add(item); }); Json.Data.Recipes.Add(new RecipeModel() { RecipeName = RecipeName, RawMaterials = TempRawMaterials, }); Json.Save(); NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!"); } ActionManage.GetInstance.Send("CloseNewRecipeView"); }); if (Global.userInfo.permission == CustomResource.Pages.Enums.Permission.管理员) { foreach (var item in Global.DeviceRawMaterials) { RawMaterialNames.Add(item.RawMaterialName); } foreach (var item in Json.Data.LocalMaterails) { RawMaterialNames.Add(item.RawMaterialName); } } else { foreach (var item in Global.DeviceRawMaterials) { if (Global.userInfo.devRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName); } foreach (var item in Json.Data.LocalMaterails) { if (Global.userInfo.locaRawMaterials.FirstOrDefault(p => p.RawMaterialName == item.RawMaterialName) != null) RawMaterialNames.Add(item.RawMaterialName); } } } private void AddRecipes() { string date = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); var dates = date.Split("-").ToList(); StringBuilder sb = new StringBuilder(); dates?.ForEach((item) => { sb.Append(item); }); Json.Data.Recipes.Add(new RecipeModel() { SerialNum = Json.Data.Recipes.Count + 1, RawMaterials = RawMaterials, RecipCode = sb.ToString(), RecipeName = RecipeName, }); } private string RecipCode = string.Empty; public string RecipeName { get { return _mRecipeName; } set { _mRecipeName = value; OnPropertyChanged(); } } private string _mRecipeName = string.Empty; //public string ErrorInfo { get { return _mErrorInfo; } set { _mErrorInfo = value; OnPropertyChanged(); } } //private string _mErrorInfo; //public BPARelayCommand AddCommand { get; set; } //public BPARelayCommand RemoveCommand { get; set; } //public BPARelayCommand SaveCommand { get; set; } public ObservableCollection RawMaterials { get; set; } = new ObservableCollection(); public ObservableCollection RawMaterialNames { get; set; } = new ObservableCollection(); } }