|
- using BPA.Helper;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.DosingSystem.Model;
- using BPASmartClient.Model;
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
-
- namespace BPASmartClient.DosingSystem.ViewModel
- {
- public class NewRecipeViewModel : ViewModelBase
- {
- public NewRecipeViewModel()
- {
- ActionManage.GetInstance.RegisterAsync(new Action<object>((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<object>((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<LocalRecipe>.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<object>((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<LocalRecipe>.Data.Recipes.Count)
- {
- var res = Array.FindIndex(Json<LocalRecipe>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
- if (res >= 0 && res != Index)
- {
- ErrorInfo = "配方名称已经存在!";
- MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
- return;
- }
- Json<LocalRecipe>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
- Json<LocalRecipe>.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<LocalRecipe>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
- });
- Json<LocalRecipe>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
- }
- else //新建配方
- {
- if (Json<LocalRecipe>.Data.Recipes.FirstOrDefault(p => p.RecipeName == RecipeName) != null)
- {
- ErrorInfo = "配方名称已存在!";
- MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
- return;
- }
- ObservableCollection<RawMaterialModel> TempRawMaterials = new ObservableCollection<RawMaterialModel>();
- 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<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
- {
- RecipeName = RecipeName,
- RawMaterials = TempRawMaterials,
- });
- Json<LocalRecipe>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"新建配方成功!");
- //配方名称集合上报
- if (ExcuteControl.GetInstance.mqttCollection.ContainsKey("Conveyer")&& ExcuteControl.GetInstance.mqttCollection["Conveyer"]._mqttClient.IsConnected)
- {
- AliyunSystemPropertyReportModel m1 = new AliyunSystemPropertyReportModel();
- m1.RecipeCollection = new string[Json<LocalRecipe>.Data.Recipes.Count];
- for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
- {
- m1.RecipeCollection[i] = Json<LocalRecipe>.Data.Recipes[i].RecipeName;
- }
- ExcuteControl.GetInstance.SystemPropertyReport(m1);
- }
- }
- 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<LocaMaterial>.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<LocaMaterial>.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<LocalRecipe>.Data.Recipes.Add(new RecipeModel()
- {
- SerialNum = Json<LocalRecipe>.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<object> RemoveCommand { get; set; }
-
- //public BPARelayCommand SaveCommand { get; set; }
-
- public ObservableCollection<RawMaterialModel> RawMaterials { get; set; } = new ObservableCollection<RawMaterialModel>();
-
-
- public ObservableCollection<string> RawMaterialNames { get; set; } = new ObservableCollection<string>();
- }
- }
|