Wms2DViewModel.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using ProjectManagementSystem.Common.Config;
  4. using ProjectManagementSystem.Common.Function;
  5. using ProjectManagementSystem.Common.Models;
  6. using ProjectManagementSystem.Language;
  7. using ProjectManagementSystemView.Infrastructure;
  8. using ProjectManagementSystemView.Infrastructure.Extensions;
  9. using PropertyChanged;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace ProjectManagementSystemView.ViewModels
  16. {
  17. [AddINotifyPropertyChangedInterface]
  18. public class Wms2DViewModel : WmsQueryViewModel
  19. {
  20. private int sWidth;
  21. private int sHeight;
  22. private double multipleX;
  23. private double multipleY;
  24. private double offsetX;
  25. private double offsetY;
  26. public double GridHeight { get; set; }
  27. public double GridWidth { get; set; }
  28. public double Scale { get; set; }
  29. public double TranslateX { get; set; }
  30. public double TranslateY { get; set; }
  31. public DelegateCommand ResetLayoutCommand { get; private set; }
  32. public DelegateCommand SaveLayoutCommand { get; private set; }
  33. public Wms2DViewModel(IDialogHostService dialogService, IEventAggregator eventAggregator) : base(dialogService, eventAggregator)
  34. {
  35. sWidth = AppSetting.TryGetValue<int>("StationUIWidth");
  36. sHeight = AppSetting.TryGetValue<int>("StationUIHeight");
  37. Scale = AppSetting.TryGetValue<double>(nameof(Scale));
  38. TranslateX = AppSetting.TryGetValue<double>(nameof(TranslateX));
  39. TranslateY = AppSetting.TryGetValue<double>(nameof(TranslateY));
  40. Scale = Scale <= 0 ? 1 : Scale;
  41. ResetLayoutCommand = new DelegateCommand(ResetLayout);
  42. SaveLayoutCommand = new DelegateCommand(SaveLayout);
  43. }
  44. protected override void UpdateVmDatas(List<LocationPropertyDto> dataList)
  45. {
  46. var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict;
  47. var routeConfifDict = ExcelConfig.Instance.RouteConfigDictionary;
  48. VmDatas.Update(dataList, (s, v) => s.LocationCode == v.LocationCode,
  49. (s, v) =>
  50. {
  51. if (buttonPropertyDict.TryGetValue(v.LocationCode, out CButton cButton))
  52. {
  53. if (v.X == 0 && v.Y == 0)
  54. {
  55. v.X = cButton.LocationX;
  56. v.Y = cButton.LocationY;
  57. }
  58. }
  59. else
  60. {
  61. if (routeConfifDict.TryGetValue(v.LocationCode, out RouteConfig config))
  62. {
  63. v.X = (Math.Abs(config.X + offsetX) * multipleX) + 5;
  64. v.Y = (Math.Abs(config.Y + offsetY) * multipleY) + 5;
  65. var cb = new CButton()
  66. {
  67. ButtonName = v.LocationCode,
  68. LocationX = v.X,
  69. LocationY = v.Y
  70. };
  71. buttonPropertyDict.Add(v.LocationCode, cb);
  72. }
  73. }
  74. v.XLength = sWidth;
  75. v.YLength = sHeight;
  76. });
  77. }
  78. private void GetLocationXY()
  79. {
  80. if (GridWidth == 0 && GridHeight == 0) return;
  81. var configList = string.IsNullOrEmpty(WareHouseCode) ?
  82. ExcelConfig.Instance.RouteConfigList.Where(d => !string.IsNullOrEmpty(d.Area)).ToList() :
  83. ExcelConfig.Instance.RouteConfigList.Where(d => d.Area == WareHouseCode).ToList();
  84. if (configList.Count == 0) return;
  85. double MinX = configList.Min(d => d.X);
  86. double MaxX = configList.Max(d => d.X);
  87. double MinY = configList.Min(d => d.Y);
  88. double MaxY = configList.Max(d => d.Y);
  89. if (MinX == 0 && MaxX == 0 && MinY == 0 && MaxY == 0)
  90. {
  91. return;
  92. }
  93. double DistanceX = Math.Abs(MaxX - MinX);
  94. double DistanceY = Math.Abs(MaxY - MinY);
  95. if (DistanceX < GridWidth) DistanceX = GridWidth * 10;
  96. if (DistanceY < GridHeight) DistanceY = GridHeight * 10;
  97. multipleX = GridWidth * 0.85 / DistanceX;
  98. multipleY = GridHeight * 0.85 / DistanceY;
  99. multipleX = multipleX < 0.03 ? 0.03 : multipleX;
  100. multipleY = multipleY < 0.03 ? 0.03 : multipleY;
  101. offsetX = 0 - MinX;
  102. offsetY = 0 - MaxY;
  103. }
  104. protected override void Loaded()
  105. {
  106. GetLocationXY();
  107. }
  108. protected override void ReloadLayout()
  109. {
  110. GetLocationXY();
  111. VmDatas.Clear();
  112. }
  113. private void ResetLayout()
  114. {
  115. GetLocationXY();
  116. var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict;
  117. for (int i = 0; i < VmDatas.Count; i++)
  118. {
  119. string locationCode = VmDatas[i].LocationCode;
  120. if (!string.IsNullOrEmpty(locationCode))
  121. {
  122. buttonPropertyDict.Remove(locationCode);
  123. }
  124. }
  125. var config = string.IsNullOrEmpty(WareHouseCode) ?
  126. ExcelConfig.Instance.RouteConfigList.FirstOrDefault(d => !string.IsNullOrEmpty(d.Area)) :
  127. ExcelConfig.Instance.RouteConfigList.FirstOrDefault(d => d.Area == WareHouseCode);
  128. Scale = 1;
  129. TranslateX = config == null ? 0 : 0 - (Math.Abs(config.X + offsetX) * multipleX);
  130. TranslateY = config == null ? 0 : 0 - (Math.Abs(config.Y + offsetY) * multipleY);
  131. VmDatas.Clear();
  132. }
  133. private void SaveLayout()
  134. {
  135. var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict;
  136. foreach (var v in VmDatas)
  137. {
  138. if (buttonPropertyDict.TryGetValue(v.LocationCode, out CButton cButton))
  139. {
  140. cButton.LocationX = v.X;
  141. cButton.LocationY = v.Y;
  142. }
  143. }
  144. if (!ExcelConfig.Instance.SaveButtonPropertyList())
  145. {
  146. HandyControl.Controls.Growl.Error(Langs.SaveLayoutFailed);
  147. }
  148. var dict = new Dictionary<string, string>();
  149. dict.Add(nameof(Scale), Scale.ToString());
  150. dict.Add(nameof(TranslateX), TranslateX.ToString());
  151. dict.Add(nameof(TranslateY), TranslateY.ToString());
  152. if (!AppSetting.TrySaveValues(dict))
  153. {
  154. HandyControl.Controls.Growl.Error($"AppSetting:{Langs.SaveLayoutFailed}");
  155. }
  156. VmDatas.Clear();
  157. }
  158. }
  159. }