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

492 line
20 KiB

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