using Prism.Commands; using Prism.Events; using ProjectManagementSystem.Common.Config; using ProjectManagementSystem.Common.Function; using ProjectManagementSystem.Common.Models; using ProjectManagementSystem.Language; using ProjectManagementSystemView.Infrastructure; using ProjectManagementSystemView.Infrastructure.Extensions; using PropertyChanged; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectManagementSystemView.ViewModels { [AddINotifyPropertyChangedInterface] public class Wms2DViewModel : WmsQueryViewModel { private int sWidth; private int sHeight; private double multipleX; private double multipleY; private double offsetX; private double offsetY; public double GridHeight { get; set; } public double GridWidth { get; set; } public double Scale { get; set; } public double TranslateX { get; set; } public double TranslateY { get; set; } public DelegateCommand ResetLayoutCommand { get; private set; } public DelegateCommand SaveLayoutCommand { get; private set; } public Wms2DViewModel(IDialogHostService dialogService, IEventAggregator eventAggregator) : base(dialogService, eventAggregator) { sWidth = AppSetting.TryGetValue("StationUIWidth"); sHeight = AppSetting.TryGetValue("StationUIHeight"); Scale = AppSetting.TryGetValue(nameof(Scale)); TranslateX = AppSetting.TryGetValue(nameof(TranslateX)); TranslateY = AppSetting.TryGetValue(nameof(TranslateY)); Scale = Scale <= 0 ? 1 : Scale; ResetLayoutCommand = new DelegateCommand(ResetLayout); SaveLayoutCommand = new DelegateCommand(SaveLayout); } protected override void UpdateVmDatas(List dataList) { var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict; var routeConfifDict = ExcelConfig.Instance.RouteConfigDictionary; VmDatas.Update(dataList, (s, v) => s.LocationCode == v.LocationCode, (s, v) => { if (buttonPropertyDict.TryGetValue(v.LocationCode, out CButton cButton)) { if (v.X == 0 && v.Y == 0) { v.X = cButton.LocationX; v.Y = cButton.LocationY; } } else { if (routeConfifDict.TryGetValue(v.LocationCode, out RouteConfig config)) { v.X = (Math.Abs(config.X + offsetX) * multipleX) + 5; v.Y = (Math.Abs(config.Y + offsetY) * multipleY) + 5; var cb = new CButton() { ButtonName = v.LocationCode, LocationX = v.X, LocationY = v.Y }; buttonPropertyDict.Add(v.LocationCode, cb); } } v.XLength = sWidth; v.YLength = sHeight; }); } private void GetLocationXY() { if (GridWidth == 0 && GridHeight == 0) return; var configList = string.IsNullOrEmpty(WareHouseCode) ? ExcelConfig.Instance.RouteConfigList.Where(d => !string.IsNullOrEmpty(d.Area)).ToList() : ExcelConfig.Instance.RouteConfigList.Where(d => d.Area == WareHouseCode).ToList(); if (configList.Count == 0) return; double MinX = configList.Min(d => d.X); double MaxX = configList.Max(d => d.X); double MinY = configList.Min(d => d.Y); double MaxY = configList.Max(d => d.Y); if (MinX == 0 && MaxX == 0 && MinY == 0 && MaxY == 0) { return; } double DistanceX = Math.Abs(MaxX - MinX); double DistanceY = Math.Abs(MaxY - MinY); if (DistanceX < GridWidth) DistanceX = GridWidth * 10; if (DistanceY < GridHeight) DistanceY = GridHeight * 10; multipleX = GridWidth * 0.85 / DistanceX; multipleY = GridHeight * 0.85 / DistanceY; multipleX = multipleX < 0.03 ? 0.03 : multipleX; multipleY = multipleY < 0.03 ? 0.03 : multipleY; offsetX = 0 - MinX; offsetY = 0 - MaxY; } protected override void Loaded() { GetLocationXY(); } protected override void ReloadLayout() { GetLocationXY(); VmDatas.Clear(); } private void ResetLayout() { GetLocationXY(); var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict; for (int i = 0; i < VmDatas.Count; i++) { string locationCode = VmDatas[i].LocationCode; if (!string.IsNullOrEmpty(locationCode)) { buttonPropertyDict.Remove(locationCode); } } var config = string.IsNullOrEmpty(WareHouseCode) ? ExcelConfig.Instance.RouteConfigList.FirstOrDefault(d => !string.IsNullOrEmpty(d.Area)) : ExcelConfig.Instance.RouteConfigList.FirstOrDefault(d => d.Area == WareHouseCode); Scale = 1; TranslateX = config == null ? 0 : 0 - (Math.Abs(config.X + offsetX) * multipleX); TranslateY = config == null ? 0 : 0 - (Math.Abs(config.Y + offsetY) * multipleY); VmDatas.Clear(); } private void SaveLayout() { var buttonPropertyDict = ExcelConfig.Instance.ButtonPropertyDict; foreach (var v in VmDatas) { if (buttonPropertyDict.TryGetValue(v.LocationCode, out CButton cButton)) { cButton.LocationX = v.X; cButton.LocationY = v.Y; } } if (!ExcelConfig.Instance.SaveButtonPropertyList()) { HandyControl.Controls.Growl.Error(Langs.SaveLayoutFailed); } var dict = new Dictionary(); dict.Add(nameof(Scale), Scale.ToString()); dict.Add(nameof(TranslateX), TranslateX.ToString()); dict.Add(nameof(TranslateY), TranslateY.ToString()); if (!AppSetting.TrySaveValues(dict)) { HandyControl.Controls.Growl.Error($"AppSetting:{Langs.SaveLayoutFailed}"); } VmDatas.Clear(); } } }