终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

261 lines
11 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.CustomResource.UserControls.MessageShow;
  3. using BPASmartClient.MorkCL.Model.DB;
  4. using BPASmartClient.MorkCL.Server;
  5. namespace BPASmartClient.MorkCL.ViewModel
  6. {
  7. public class EditRawMaterialViewModel : NotifyBase
  8. {
  9. public EditRawMaterialViewModel()
  10. {
  11. ActionManage.GetInstance.Register((object o) =>
  12. {
  13. //为Null代表是新增。
  14. if (o != null && o is MaterialBase material)
  15. {
  16. if (String.IsNullOrEmpty(material.Id) || material.Id.Length <= 0)
  17. {
  18. IsNewAdd = true;
  19. }
  20. else
  21. {
  22. IsNewAdd = false;
  23. if (material is AccessoriesTB)
  24. {
  25. CurrentMaterialType = "辅料";
  26. }
  27. else if (material is IngredientsTB)
  28. {
  29. CurrentMaterialType = "主料";
  30. }
  31. else if (material is SeasoningTB)
  32. {
  33. CurrentMaterialType = "调料";
  34. }
  35. else
  36. {
  37. throw new Exception($"参数不是物料数据类型");
  38. }
  39. MaterialName = material.Name;
  40. MaterialLoc = material.Loc;
  41. _materialID = material.Id;
  42. MaterialDescription = material.Description;
  43. }
  44. }
  45. }, "OpenEditRawMaterialView", true);
  46. ActionManage.GetInstance.Register((object o) =>
  47. {
  48. string materialType = o is String ? (String)o : null;
  49. CurrentMaterialType = materialType!;
  50. }, "ChangeMaterialType", true);
  51. SaveParamCommand = new BPARelayCommand(() =>
  52. {
  53. #region 数据验证
  54. if (!MessageNotify.GetInstance.ShowDialog("请确认,是否保存该物料信息?"))
  55. {
  56. return;
  57. }
  58. if (string.IsNullOrEmpty(MaterialName) || MaterialName.Length <= 0)
  59. {
  60. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"失败,物料名称不正确!");
  61. return;
  62. }
  63. if (MaterialLoc <= 0 && CurrentMaterialType != "主料")
  64. {
  65. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"失败,物料位置应为正整数!");
  66. return;
  67. }
  68. #endregion 数据验证
  69. //新增
  70. if (IsNewAdd)
  71. {
  72. try
  73. {
  74. switch (CurrentMaterialType)
  75. {
  76. case "主料":
  77. //if (SqliteHelper.GetInstance.GetIngredientsInfoByLoc(MaterialLoc))
  78. //{
  79. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
  80. // return;
  81. //}
  82. IngredientsTB ingredients = new IngredientsTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  83. SqliteHelper.GetInstance.AddIngredients(ingredients);
  84. ActionManage.GetInstance.Send("RefreshIngredients");
  85. break;
  86. case "辅料":
  87. if (SqliteHelper.GetInstance.GetAccessoriesInfoByLoc(MaterialLoc))
  88. {
  89. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
  90. return;
  91. }
  92. AccessoriesTB accessories = new AccessoriesTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  93. SqliteHelper.GetInstance.AddAccessories(accessories);
  94. break;
  95. case "调料":
  96. if (SqliteHelper.GetInstance.GetSeasoningInfoByLoc(MaterialLoc))
  97. {
  98. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"添加失败,该位置已有物料!");
  99. return;
  100. }
  101. SeasoningTB seasoning = new SeasoningTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  102. SqliteHelper.GetInstance.AddSeasoning(seasoning);
  103. break;
  104. }
  105. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"新增物料:{MaterialName} 成功!");
  106. }
  107. catch (Exception ex)
  108. {
  109. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"新增一条物料:{MaterialName} 失败!");
  110. }
  111. }
  112. else
  113. {
  114. try
  115. {
  116. switch (CurrentMaterialType)
  117. {
  118. case "主料":
  119. //if (SqliteHelper.GetInstance.GetIngredientsInfoByLoc(MaterialLoc))
  120. //{
  121. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
  122. // return;
  123. //}
  124. IngredientsTB ingredients = new IngredientsTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  125. SqliteHelper.GetInstance.EditIngredients(ingredients);
  126. break;
  127. case "辅料":
  128. //if (SqliteHelper.GetInstance.GetAccessoriesInfoByLoc(MaterialLoc))
  129. //{
  130. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
  131. // return;
  132. //}
  133. AccessoriesTB accessories = new AccessoriesTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  134. SqliteHelper.GetInstance.EditAccessories(accessories);
  135. break;
  136. case "调料":
  137. //if (SqliteHelper.GetInstance.GetSeasoningInfoByLoc(MaterialLoc))
  138. //{
  139. // NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改失败,该位置已有物料!");
  140. // return;
  141. //}
  142. SeasoningTB seasoning = new SeasoningTB() { Name = MaterialName, Loc = MaterialLoc, Id = _materialID, Description = MaterialDescription };
  143. SqliteHelper.GetInstance.EditSeasoning(seasoning);
  144. break;
  145. }
  146. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"修改物料:{MaterialName} 成功!");
  147. }
  148. catch
  149. {
  150. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, Application.Current.MainWindow, "提示", $"修改物料:{MaterialName} 失败!");
  151. }
  152. }
  153. //保存处理。
  154. ActionManage.GetInstance.Send("CloseEditRawMaterialView", true);
  155. });
  156. CancelCommand = new BPARelayCommand(() =>
  157. {
  158. ActionManage.GetInstance.Send("CloseEditRawMaterialView", false);
  159. });
  160. }
  161. public BPARelayCommand SaveParamCommand { get; set; }
  162. public BPARelayCommand CancelCommand { get; set; }
  163. private string _materialID;
  164. public ObservableCollection<string> MaterialType { get; set; } = new ObservableCollection<string>() { "主料", "辅料", "调料" };
  165. private bool _IsNewAdd;
  166. public bool IsNewAdd
  167. {
  168. get { return _IsNewAdd; }
  169. set
  170. {
  171. _IsNewAdd = value;
  172. if (value == true)
  173. {
  174. MaterialTypeVis = Visibility.Visible;
  175. }
  176. else
  177. MaterialTypeVis = Visibility.Hidden;
  178. OnPropertyChanged();
  179. }
  180. }
  181. private string _CurrentMaterialType;
  182. /// <summary>物料类型</summary>
  183. public string CurrentMaterialType
  184. {
  185. get { return _CurrentMaterialType; }
  186. set
  187. {
  188. _CurrentMaterialType = value;
  189. if (value == "主料")
  190. {
  191. LocationVis = Visibility.Hidden;
  192. }
  193. else
  194. LocationVis = Visibility.Visible;
  195. OnPropertyChanged();
  196. }
  197. }
  198. private string _MaterialName;
  199. /// <summary>物料名称</summary>
  200. public string MaterialName
  201. {
  202. get { return _MaterialName; }
  203. set { _MaterialName = value; OnPropertyChanged(); }
  204. }
  205. private int _MaterialLoc;
  206. /// <summary>物料位置</summary>
  207. public int MaterialLoc
  208. {
  209. get { return _MaterialLoc; }
  210. set { _MaterialLoc = value; OnPropertyChanged(); }
  211. }
  212. private string _MaterialDescription;
  213. public string MaterialDescription
  214. {
  215. get { return _MaterialDescription; }
  216. set { _MaterialDescription = value; }
  217. }
  218. private Visibility _MaterialTypeVis;
  219. /// <summary>是否新增,如果是新增,则显示主料类型框。反之,则不显示。</summary>
  220. public Visibility MaterialTypeVis
  221. {
  222. get { return _MaterialTypeVis; }
  223. set { _MaterialTypeVis = value; OnPropertyChanged(); }
  224. }
  225. private Visibility _LocationVis;
  226. /// <summary>是否显示位置框,如果为主料,则不显示。反之,则显示。</summary>
  227. public Visibility LocationVis
  228. {
  229. get { return _LocationVis; }
  230. set { _LocationVis = value; OnPropertyChanged(); }
  231. }
  232. }
  233. }