终端一体化运控平台
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.

EditRecipeViewModel.cs 17 KiB

1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.CustomResource.UserControls.MessageShow;
  3. using BPASmartClient.MorkCL.Model.Json;
  4. using BPASmartClient.MorkCL.Model.Recipe;
  5. namespace BPASmartClient.MorkCL.ViewModel
  6. {
  7. public class EditRecipeViewModel : NotifyBase
  8. {
  9. public EditRecipeViewModel()
  10. {
  11. //AddTestData();
  12. Functions = new();
  13. ActionManage.GetInstance.Register((object o) =>
  14. {
  15. if (o != null && o is ControlData recipe)
  16. {
  17. this.Name = recipe.Name;
  18. this.ID = recipe.Id;
  19. this.DishType = recipe.DishType;
  20. int step = 0;
  21. Functions.Clear();
  22. foreach (var func in recipe.ControlFuncs)
  23. {
  24. step++;
  25. Functions.Add(new FuncModel() { eFunc = func.eFunc, funcPars = func.funcPars });
  26. }
  27. }
  28. }, "OpenRecipeEditView", true);
  29. AllFunc = new ObservableCollection<string>();
  30. foreach (var item in Enum.GetNames(typeof(EFunc)))
  31. {
  32. AllFunc.Add(item);
  33. }
  34. AllDishType = new ObservableCollection<string>();
  35. foreach (var item in Enum.GetNames(typeof(EDishType)))
  36. {
  37. AllDishType.Add(item);
  38. }
  39. SaveParamCommand = new BPARelayCommand(() =>
  40. {
  41. if (!MessageNotify.GetInstance.ShowDialog("请确认是否保存该配方?"))
  42. {
  43. return;
  44. }
  45. #region 数据验证
  46. if (String.IsNullOrEmpty(Name) || Name.Length <= 0)
  47. {
  48. MessageNotify.GetInstance.ShowDialog("配方名称不可为空,请重新输入后重试!", DialogType.Error);
  49. return;
  50. }
  51. //if (!int.TryParse(ID,out int Id))
  52. //{
  53. // MessageNotify.GetInstance.ShowDialog("配方ID不是数字,请重新输入后重试!");
  54. // return;
  55. //}
  56. if (Functions == null || Functions.Count <= 0)
  57. {
  58. MessageNotify.GetInstance.ShowDialog("没有可保存的配方功能配置,请验证后重试!", DialogType.Error);
  59. return;
  60. }
  61. foreach (FuncModel item in Functions)
  62. {
  63. for (int i = 0; i < item.funcPars.Count; i++)
  64. {
  65. var argument = item.funcPars[i].ParValue;
  66. if (argument == null || String.IsNullOrEmpty(argument.ToString()) || argument.ToString().Length <= 0)
  67. {
  68. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的参数项未设置,请修改验证后重试!", DialogType.Error);
  69. return;
  70. }
  71. }
  72. switch (item.eFunc)
  73. {
  74. case EFunc.搅拌启动:
  75. if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int agitateSpeed))
  76. {
  77. if (agitateSpeed > 60 || agitateSpeed <= 0)
  78. {
  79. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的搅拌速度限值0--60HZ,请修改验证后重试!", DialogType.Error);
  80. return;
  81. }
  82. }
  83. else
  84. {
  85. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的搅拌速度不是整数,请修改验证后重试!", DialogType.Error);
  86. return;
  87. }
  88. break;
  89. case EFunc.搅拌停止:
  90. break;
  91. case EFunc.加热启动:
  92. if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int heatGear))
  93. {
  94. if (heatGear > 8 || heatGear <= 0)
  95. {
  96. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的加热档位限值1--8,请修改验证后重试!", DialogType.Error);
  97. return;
  98. }
  99. }
  100. else
  101. {
  102. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的加热档位不是整数,请修改验证后重试!", DialogType.Error);
  103. return;
  104. }
  105. break;
  106. case EFunc.加热停止:
  107. break;
  108. case EFunc.添加调料:
  109. if (double.TryParse(item.funcPars[1].ParValue?.ToString(), out double seasoningWeight))
  110. {
  111. if (seasoningWeight <= 0)
  112. {
  113. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的调料需求值小于0,请修改验证后重试!", DialogType.Error);
  114. return;
  115. }
  116. }
  117. else
  118. {
  119. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的调料需求值不是纯数字,请修改验证后重试!", DialogType.Error);
  120. return;
  121. }
  122. break;
  123. case EFunc.添加主料:
  124. if (double.TryParse(item.funcPars[1].ParValue?.ToString(), out double ingredientsWeight))
  125. {
  126. if (ingredientsWeight <= 0)
  127. {
  128. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的主料份量小于0,请修改验证后重试!", DialogType.Error);
  129. return;
  130. }
  131. }
  132. else
  133. {
  134. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的主料份量不是纯数字,请修改验证后重试!", DialogType.Error);
  135. return;
  136. }
  137. break;
  138. case EFunc.添加辅料:
  139. break;
  140. case EFunc.炒锅回原点位:
  141. break;
  142. case EFunc.出餐启动:
  143. break;
  144. case EFunc.炒锅清洗:
  145. break;
  146. case EFunc.炒锅回调料投料位置:
  147. break;
  148. case EFunc.去指定炒制位:
  149. if (int.TryParse(item.funcPars[0].ParValue?.ToString(), out int fryingPanLoc))
  150. {
  151. if (fryingPanLoc > 4 || fryingPanLoc <= 0)
  152. {
  153. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制位限值1--4,请修改验证后重试!", DialogType.Error);
  154. return;
  155. }
  156. }
  157. else
  158. {
  159. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的设定炒制不是整数,请修改验证后重试!", DialogType.Error);
  160. return;
  161. }
  162. break;
  163. case EFunc.炒制:
  164. if (int.TryParse(item.funcPars[0].ParValue.ToString(), out int stirFryTime))
  165. {
  166. if (stirFryTime <= 0)
  167. {
  168. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间设置小于0,请修改验证后重试!", DialogType.Error);
  169. return;
  170. }
  171. }
  172. else
  173. {
  174. MessageNotify.GetInstance.ShowDialog($"{item.eFunc}功能的炒制时间不是纯数字,请修改验证后重试!", DialogType.Error);
  175. return;
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. #endregion 数据验证
  183. List<FuncModel> TempFunces = new List<FuncModel>();
  184. //查询是否是编辑现有配方。
  185. int index = Array.FindIndex(Json<RecipesInfo>.Data.LocalRecipes.ToArray(), p => p.Id == ID);
  186. //修改配方
  187. if (index >= 0)
  188. {
  189. foreach (var item in Functions)
  190. {
  191. //TempFunces.Add((EFunc)Enum.Parse(typeof(EFunc), item.FuncName), item.FuncPars?.ToArray());
  192. TempFunces.Add(new FuncModel()
  193. {
  194. eFunc = item.eFunc,
  195. funcPars = new List<FuncPar>(item.funcPars)
  196. });
  197. }
  198. Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).Id = ID;
  199. Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).Name = Name;
  200. Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).DishType = DishType;
  201. Json<RecipesInfo>.Data.LocalRecipes.ElementAt(index).ControlFuncs = new ConcurrentQueue<FuncModel>(TempFunces);
  202. Json<RecipesInfo>.Save();
  203. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"修改配方:{Name} 并保存成功!");
  204. }
  205. //没找到则为新增配方
  206. else
  207. {
  208. foreach (var item in Functions)
  209. {
  210. //TempFunces.Add((EFunc)Enum.Parse(typeof(EFunc), item.FuncName), item.FuncPars?.ToArray());
  211. TempFunces.Add(new FuncModel()
  212. {
  213. eFunc = item.eFunc,
  214. funcPars = new List<FuncPar>(item.funcPars)
  215. });
  216. }
  217. Json<RecipesInfo>.Data.LocalRecipes.Add(new ControlData()
  218. {
  219. Id = Guid.NewGuid().ToString(),
  220. Name = Name,
  221. Remark = " ",
  222. DishType = DishType,
  223. ControlFuncs = new ConcurrentQueue<FuncModel>(TempFunces)
  224. });
  225. Json<RecipesInfo>.Save();
  226. NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, Application.Current.MainWindow, "提示", $"新增配方:{Name} 并保存成功!");
  227. }
  228. //关闭窗体。
  229. ActionManage.GetInstance.Send("CloseEditRecipeView");
  230. });
  231. AddFuncCommand = new BPARelayCommand(() =>
  232. {
  233. Functions.Add(new FuncModel()
  234. {
  235. eFunc = EFunc.搅拌启动,
  236. funcPars = InitData.FunParInit[EFunc.搅拌启动]?.ToList()
  237. });
  238. });
  239. FuncChangeCommand = new BPARelayCommand<object>((o) =>
  240. {
  241. if (o != null && o is FuncModel model)
  242. {
  243. model.funcPars = InitData.FunParInit[model.eFunc].ToList();
  244. }
  245. });
  246. RemoveCommand = new BPARelayCommand<object>((o) =>
  247. {
  248. if (o != null && o is FuncModel func)
  249. {
  250. Functions.Remove(func);
  251. }
  252. });
  253. EditCommand = new BPARelayCommand<object>((o) =>
  254. {
  255. if (o is FuncModel funcSet)
  256. {
  257. EditFunctionParamView editFunction = new EditFunctionParamView();
  258. ActionManage.GetInstance.Send("OpenFuncEditView", o);
  259. if (editFunction.ShowDialog() == true)
  260. {
  261. funcSet.funcPars.Clear();
  262. //funcSet.funcPars = EditFunctionParamViewModel.FuncPars?.ToList();
  263. foreach (var item in EditFunctionParamViewModel.FuncPars)
  264. {
  265. funcSet.funcPars.Add(new FuncPar() { Id = item.Id, ParName = item.ParName, ParValue = item.ParValue, ParUnit = item.ParUnit, ParDescribe = item.ParDescribe });
  266. //funcSet.funcPars.Add((FuncPar)item);
  267. }
  268. }
  269. }
  270. });
  271. MoveUpCommand = new((o) =>
  272. {
  273. if (o != null && o is FuncModel funcmodel)
  274. {
  275. var index = Functions.IndexOf(funcmodel);
  276. if (index > 0)
  277. {
  278. int newIndex = index - 1;
  279. Functions.RemoveAt(index);
  280. Functions.Insert(newIndex, funcmodel);
  281. }
  282. }
  283. });
  284. MoveDownCommand = new((o) =>
  285. {
  286. if (o != null && o is FuncModel funcmodel)
  287. {
  288. var index = Functions.IndexOf(funcmodel);
  289. if (index < Functions.Count - 1)
  290. {
  291. int newIndex = index + 1;
  292. Functions.RemoveAt(index);
  293. Functions.Insert(newIndex, funcmodel);
  294. }
  295. }
  296. });
  297. }
  298. private void AddTestData()
  299. {
  300. ID = "32587";
  301. Name = "红烧肉";
  302. //Functions = InitData.FunParInit;
  303. //Functions = new ObservableCollection<EFunc>();
  304. //foreach (var item in InitData.FunParInit.Keys)
  305. //{
  306. // Functions.Add(item);
  307. //}
  308. }
  309. /// <summary>保存参数。</summary>
  310. public BPARelayCommand SaveParamCommand { get; set; }
  311. /// <summary>添加功能</summary>
  312. public BPARelayCommand AddFuncCommand { get; set; }
  313. /// <summary>删除功能。</summary>
  314. public BPARelayCommand<object> RemoveCommand { get; set; }
  315. /// <summary>功能选择改变。</summary>
  316. public BPARelayCommand<object> FuncChangeCommand { get; set; }
  317. /// <summary>编辑功能。</summary>
  318. public BPARelayCommand<object> EditCommand { get; set; }
  319. /// <summary>功能上移。</summary>
  320. public BPARelayCommand<object> MoveUpCommand { get; set; }
  321. /// <summary>功能下移。</summary>
  322. public BPARelayCommand<object> MoveDownCommand { get; set; }
  323. private ObservableCollection<FuncModel> _Functions;
  324. public ObservableCollection<FuncModel> Functions
  325. {
  326. get { return _Functions; }
  327. set { _Functions = value; OnPropertyChanged(); }
  328. }
  329. private string _Name;
  330. /// <summary>配方名称</summary>
  331. public string Name
  332. {
  333. get { return _Name; }
  334. set { _Name = value; OnPropertyChanged(); }
  335. }
  336. private string _ID;
  337. /// <summary>配方ID。</summary>
  338. public string ID
  339. {
  340. get { return _ID; }
  341. set { _ID = value; OnPropertyChanged(); }
  342. }
  343. private ObservableCollection<string> _AllFunc;
  344. /// <summary>所有功能集合。</summary>
  345. public ObservableCollection<string> AllFunc
  346. {
  347. get { return _AllFunc; }
  348. set { _AllFunc = value; OnPropertyChanged(); }
  349. }
  350. private ObservableCollection<string> _AllDishType;
  351. /// <summary>所有菜品集合。</summary>
  352. public ObservableCollection<string> AllDishType
  353. {
  354. get { return _AllDishType; }
  355. set { _AllDishType = value; OnPropertyChanged(); }
  356. }
  357. private EDishType _DishType;
  358. public EDishType DishType
  359. {
  360. get { return _DishType; }
  361. set { _DishType = value; OnPropertyChanged(); }
  362. }
  363. }
  364. }