|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using BPA.Helper;
- using System.Collections.ObjectModel;
-
- using BPA.Helper;
- using BPASmartClient.JXJFoodSmallStation.Model;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Model;
- using BPASmartClient.JXJFoodSmallStation.Model.RawMaterial;
- using System.Windows.Controls;
- using Renci.SshNet.Messages;
-
- namespace BPASmartClient.JXJFoodSmallStation.ViewModel
- {
- public class NewRecipeViewModel : NotifyBase
- {
- public NewRecipeViewModel()
- {
- 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);
- }
- RecipeCode = rm.RecipCode;
- }
- }), "Details");
-
- AddCommand = new BPARelayCommand(() =>
- {
- p:
- string guid = Guid.NewGuid().ToString();
- if (RawMaterials.FirstOrDefault(p => p.RawMaterialId == guid) == null)
- {
- RawMaterials.Add(new RawMaterialModel()
- {
- RawMaterialId = guid
- });
- MessageNotify.GetInstance.ShowUserLog($"添加一条原料:{guid}");
- }
- else goto p;
- });
-
- RemoveCommand = new BPARelayCommand<object>((obj) =>
- {
- if (obj is string rm)
- {
- var res = RawMaterials.FirstOrDefault(p => p.RawMaterialId == rm);
- if (res != null)
- {
- Global.DeviceRawMaterials.Remove(res);
- MessageNotify.GetInstance.ShowUserLog($"删除一条原料,原料名称:【{res.RawMaterialName}】");
- }
- }
- });
-
- SaveCommand = new BPARelayCommand(() =>
- {
- ErrorInfo = String.Empty;
- if (RecipeName == String.Empty) { ErrorInfo = "配方名称未填写"; return; }
- for (int i = 0; i < RawMaterials.Count; i++)
- {
- var res = DeviceInquire.GetInstance.devices.FirstOrDefault(p => p.DeviceName == RawMaterials.ElementAt(i).RawMaterialName);
- if (res != null)
- {
- RawMaterials.ElementAt(i).DeviceIp = res.IpAddress;//根据设备名称和原料名称的唯一匹配关系,给原料配置IP
- RawMaterials.ElementAt(i).RawMaterialSource = 1;
- }
- var su = Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == RawMaterials.ElementAt(i).RawMaterialName);
- if (su != null)
- {
- RawMaterials.ElementAt(i).RawMaterialSource = su.RawMaterialSource;
- }
- }
-
- if (RecipeCode.Length <= 0)//新建配方
- {
- var res = Array.FindIndex(Json<LocaPar>.Data.RemoteRecipes.ToArray(), p => p.RecipeName == RecipeName);
-
- if (res >= 0 && res < Json<LocaPar>.Data.RemoteRecipes.Count)
- {
- ErrorInfo = "该配方已存在,请重新输入";
- }
- else
- {
- AddRecipes();
- }
- }
- else//修改配方
- {
- var res = Array.FindIndex(Json<LocaPar>.Data.RemoteRecipes.ToArray(), p => p.RecipCode == RecipeCode);
- if (res >= 0 && res < Json<LocaPar>.Data.RemoteRecipes.Count)
- {
- Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RecipeName = RecipeName;
- Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RawMaterials.Clear();
- foreach (var item in RawMaterials)
- {
- Json<LocaPar>.Data.RemoteRecipes.ElementAt(res).RawMaterials.Add(item);
- }
- MessageNotify.GetInstance.ShowUserLog($"修改了一条配方,配方编号:【{RecipeCode}】");
- }
- }
- ActionManage.GetInstance.Send("CloseNewRecipeView");
- });
-
- //RawMaterialNames.Add("乳化剂");
- //RawMaterialNames.Add("酶制剂");
- //RawMaterialNames.Add("增味剂");
-
-
- //RawMaterialNames.Add("营养强化剂");
- //RawMaterialNames.Add("抗结剂");
- //RawMaterialNames.Add("消泡剂");
- //RawMaterialNames.Add("膨松剂");
- //RawMaterialNames.Add("防腐剂");
- //RawMaterialNames.Add("着色剂");
- //RawMaterialNames.Add("甜味剂");
- //RawMaterialNames.Add("酸味剂");
- //RawMaterialNames.Add("增白剂");
- //RawMaterialNames.Add("香料");
- //RawMaterialNames.Add("抗氧化剂");
- //RawMaterialNames.Add("食用香料");
- //RawMaterialNames.Add("增稠剂");
-
- 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.RemoteRecipes.Add(new RecipeModel()
- {
- SerialNum = Json<LocaPar>.Data.RemoteRecipes.Count + 1,
- RawMaterials = RawMaterials,
- RecipCode = sb.ToString(),
- RecipeName = RecipeName,
- });
- MessageNotify.GetInstance.ShowUserLog($"新增了一条配方,配方名称:【{RecipeName}】,配方编号:【{RecipeCode}】");
- }
-
- public string RecipeCode { get { return _mRecipeCode; } set { _mRecipeCode = value; OnPropertyChanged(); } }
- private string _mRecipeCode = 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>();
- }
- }
|