|
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.MorkCL.Model.DB;
- using BPASmartClient.MorkCL.Server;
-
- namespace BPASmartClient.MorkCL.ViewModel
- {
- public class EditRawMaterialViewModel : NotifyBase
- {
- public EditRawMaterialViewModel()
- {
- ActionManage.GetInstance.Register((object o) =>
- {
- //为Null代表是新增。
- if (o != null && o is MaterialBase material)
- {
- if (String.IsNullOrEmpty(material.Id) || material.Id.Length <= 0)
- {
- IsNewAdd = true;
- }
- else
- {
- IsNewAdd = false;
- if (material is AccessoriesTB)
- {
- CurrentMaterialType = "辅料";
- }
- else if (material is IngredientsTB)
- {
- CurrentMaterialType = "主料";
- }
- else if (material is SeasoningTB)
- {
- CurrentMaterialType = "调料";
- }
- else
- {
- throw new Exception($"参数不是物料数据类型");
- }
- MaterialName = material.Name;
- MaterialLoc = material.Loc;
- _materialID = material.Id;
- MaterialDescription = material.Description;
- }
- }
- }, "OpenEditRawMaterialView", true);
- ActionManage.GetInstance.Register((object o) =>
- {
- string materialType = o is String ? (String)o : null;
- CurrentMaterialType = materialType!;
- }, "ChangeMaterialType", true);
-
- SaveParamCommand = new BPARelayCommand(() =>
- {
- #region 数据验证
-
- if (!MessageNotify.GetInstance.ShowDialog("请确认,是否保存该物料信息?"))
- {
- return;
- }
-
- if (string.IsNullOrEmpty(MaterialName) || MaterialName.Length <= 0)
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"失败,物料名称不正确!");
- return;
- }
- if (MaterialLoc <= 0 && CurrentMaterialType != "主料")
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"失败,物料位置应为正整数!");
- return;
- }
-
- #endregion 数据验证
-
- //新增
- if (IsNewAdd)
- {
- try
- {
- switch (CurrentMaterialType)
- {
- case "主料":
- //if (SqliteHelper.GetInstance.GetIngredientsInfoByLoc(MaterialLoc))
- //{
- // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
- // return;
- //}
- IngredientsTB ingredients = new IngredientsTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.AddIngredients(ingredients);
- ActionManage.GetInstance.Send("RefreshIngredients");
- break;
-
- case "辅料":
- if (SqliteHelper.GetInstance.GetAccessoriesInfoByLoc(MaterialLoc))
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
- return;
- }
- AccessoriesTB accessories = new AccessoriesTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.AddAccessories(accessories);
- break;
-
- case "调料":
- if (SqliteHelper.GetInstance.GetSeasoningInfoByLoc(MaterialLoc))
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
- return;
- }
- SeasoningTB seasoning = new SeasoningTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.AddSeasoning(seasoning);
- break;
- }
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"新增物料:{MaterialName} 成功!");
- }
- catch (Exception ex)
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"新增一条物料:{MaterialName} 失败!");
- }
- }
- else
- {
- try
- {
- switch (CurrentMaterialType)
- {
- case "主料":
- //if (SqliteHelper.GetInstance.GetIngredientsInfoByLoc(MaterialLoc))
- //{
- // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
- // return;
- //}
- IngredientsTB ingredients = new IngredientsTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.EditIngredients(ingredients);
- break;
-
- case "辅料":
- //if (SqliteHelper.GetInstance.GetAccessoriesInfoByLoc(MaterialLoc))
- //{
- // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
- // return;
- //}
- AccessoriesTB accessories = new AccessoriesTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.EditAccessories(accessories);
- break;
-
- case "调料":
- //if (SqliteHelper.GetInstance.GetSeasoningInfoByLoc(MaterialLoc))
- //{
- // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
- // return;
- //}
- SeasoningTB seasoning = new SeasoningTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
- SqliteHelper.GetInstance.EditSeasoning(seasoning);
- break;
- }
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"修改物料:{MaterialName} 成功!");
- }
- catch
- {
- NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改物料:{MaterialName} 失败!");
- }
- }
- //保存处理。
- ActionManage.GetInstance.Send("CloseEditRawMaterialView", true);
- });
-
- CancelCommand = new BPARelayCommand(() =>
- {
- ActionManage.GetInstance.Send("CloseEditRawMaterialView", false);
- });
- }
-
- public BPARelayCommand SaveParamCommand { get; set; }
- public BPARelayCommand CancelCommand { get; set; }
-
- private string _materialID;
-
- public ObservableCollection<string> MaterialType { get; set; } = new ObservableCollection<string>() { "主料", "辅料", "调料" };
-
- private bool _IsNewAdd;
-
- public bool IsNewAdd
- {
- get { return _IsNewAdd; }
- set
- {
- _IsNewAdd = value;
- if (value == true)
- {
- MaterialTypeVis = Visibility.Visible;
- }
- else
- MaterialTypeVis = Visibility.Hidden;
- OnPropertyChanged();
- }
- }
-
- private string _CurrentMaterialType;
-
- /// <summary>物料类型</summary>
- public string CurrentMaterialType
- {
- get { return _CurrentMaterialType; }
- set
- {
- _CurrentMaterialType = value;
- if (value == "主料")
- {
- LocationVis = Visibility.Hidden;
- }
- else
- LocationVis = Visibility.Visible;
- OnPropertyChanged();
- }
- }
-
- private string _MaterialName;
-
- /// <summary>物料名称</summary>
- public string MaterialName
- {
- get { return _MaterialName; }
- set { _MaterialName = value; OnPropertyChanged(); }
- }
-
- private int _MaterialLoc;
-
- /// <summary>物料位置</summary>
- public int MaterialLoc
- {
- get { return _MaterialLoc; }
- set { _MaterialLoc = value; OnPropertyChanged(); }
- }
-
- private string _MaterialDescription;
-
- public string MaterialDescription
- {
- get { return _MaterialDescription; }
- set { _MaterialDescription = value; }
- }
-
- private Visibility _MaterialTypeVis;
-
- /// <summary>是否新增,如果是新增,则显示主料类型框。反之,则不显示。</summary>
- public Visibility MaterialTypeVis
- {
- get { return _MaterialTypeVis; }
- set { _MaterialTypeVis = value; OnPropertyChanged(); }
- }
-
- private Visibility _LocationVis;
-
- /// <summary>是否显示位置框,如果为主料,则不显示。反之,则显示。</summary>
- public Visibility LocationVis
- {
- get { return _LocationVis; }
- set { _LocationVis = value; OnPropertyChanged(); }
- }
- }
- }
|