|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 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.DosingSystemSingle.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<LocaPar>.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<LocaPar>.Data.Recipes.Count)
- {
- var res = Array.FindIndex(Json<LocaPar>.Data.Recipes.ToArray(), p => p.RecipeName == RecipeName);
- if (res >= 0 && res != Index)
- {
- ErrorInfo = "配方名称已经存在!";
- MessageNotify.GetInstance.ShowDialog("配方名称已经存在!", DialogType.Warning);
- return;
- }
- Json<LocaPar>.Data.Recipes.ElementAt(Index).RecipeName = RecipeName;
- Json<LocaPar>.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<LocaPar>.Data.Recipes.ElementAt(Index).RawMaterials.Add(item);
- });
- Json<LocaPar>.Save();
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "提示", $"{RecipeName} 配方编辑完成");
- }
- else //新建配方
- {
- if (Json<LocaPar>.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<LocaPar>.Data.Recipes.Add(new RecipeModel()
- {
-
- RecipeName = RecipeName,
- RawMaterials = TempRawMaterials,
- });
- Json<LocaPar>.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<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<LocaPar>.Data.Recipes.Add(new RecipeModel()
- {
- SerialNum = Json<LocaPar>.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>();
- }
- }
|