From 325c6879a4cd38ac636371e12ff50a78e6d166d6 Mon Sep 17 00:00:00 2001 From: denglihong2007 <2639367181@qq.com> Date: Fri, 4 Apr 2025 00:44:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=A6=E6=AC=A1=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StationManagementPageViewModel.cs | 4 +- .../TrainNumberManagementPageViewModel.cs | 275 +++++++++++++----- .../TrainNumberManagementPage.xaml | 63 +++- 3 files changed, 265 insertions(+), 77 deletions(-) diff --git a/CRSim/ViewModels/Pages/DataManagement/StationManagementPageViewModel.cs b/CRSim/ViewModels/Pages/DataManagement/StationManagementPageViewModel.cs index c4edd76..66ecde4 100644 --- a/CRSim/ViewModels/Pages/DataManagement/StationManagementPageViewModel.cs +++ b/CRSim/ViewModels/Pages/DataManagement/StationManagementPageViewModel.cs @@ -269,7 +269,7 @@ public partial class StationManagementPageViewModel : ObservableObject } foreach (TrainNumber trainNumber in trainNumbers) { - var t = trainNumber.TimeTable.First(x => x.Station == SelectedStation.Name); + var t = trainNumber.TimeTable.FirstOrDefault(x => x.Station == SelectedStation.Name); if (t == null) continue; t.Number = trainNumber.Number; t = RandomTrainStopProperties(t); @@ -334,7 +334,7 @@ public partial class StationManagementPageViewModel : ObservableObject } } [RelayCommand] - public async Task ImportFromExcel() + public void ImportFromExcel() { if (TrainStops.Count != 0) { diff --git a/CRSim/ViewModels/Pages/DataManagement/TrainNumberManagementPageViewModel.cs b/CRSim/ViewModels/Pages/DataManagement/TrainNumberManagementPageViewModel.cs index ecf9a3a..cea4498 100644 --- a/CRSim/ViewModels/Pages/DataManagement/TrainNumberManagementPageViewModel.cs +++ b/CRSim/ViewModels/Pages/DataManagement/TrainNumberManagementPageViewModel.cs @@ -163,10 +163,123 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject await _databaseService.SaveData(); RefreshTrainNumbers(); } + [RelayCommand] + public async Task ImportFromLulutong() + { + var path = _dialogService.GetFile("CSV 文件 (*.csv)|*.csv|所有文件 (*.*)|*.*"); + if (path == null) return; + try + { + var numbers = ReadCsvFirstColumn(path); + var intersection = numbers.Intersect(TrainNumbers.Select(x => x.Number)).ToList(); + if (intersection.Count != 0) + { + if (!_dialogService.GetConfirm("当前操作可能覆盖现有车次配置,是否继续?")) return; + } + foreach (string s in intersection) + { + _databaseService.DeleteTrainNumber(_databaseService.GetTrainNumberByNumber(s)); + } + RefreshTrainNumbers(); + int total = numbers.Count; + int current = 0; + foreach (string number in numbers) + { + current++; + ProgressValue = (int)((double)current / total * 100); + List? timeTable = await _networkService.GetTimeTableAsync(number); + await Task.Delay(100); + timeTable ??= []; + _databaseService.AddTrainNumber(number); + var sections = new List
(); + var isEmu = number.StartsWith('G') || number.StartsWith('D') || number.StartsWith('C'); + for (int i = 1; i < timeTable.Count; i++) + { + sections.Add(new Section + { + From = timeTable[i - 1].Station, + To = timeTable[i].Station, + Tickets = new Tickets + { + SW = isEmu ? new() : null, + ZYY = isEmu ? new() : null, + ZY = isEmu ? new() : null, + ZE = isEmu ? new() : null, + RZ = !isEmu ? new() : null, + GRW = !isEmu ? new() : null, + RW = !isEmu ? new() : null, + WZ = new(), + YW = !isEmu ? new() : null, + YZ = !isEmu ? new() : null + } + }); + } + _databaseService.UpdateTrainNumber(_databaseService.GetTrainNumberByNumber(number), timeTable, sections); + } + ProgressValue = 0; + await _databaseService.SaveData(); + RefreshTrainNumbers(); + } + catch + { + _dialogService.ShowMessage("导入失败", "文件格式错误或被占用。"); + ProgressValue = 0; + } + } #endregion #region TimeTable [RelayCommand] + public async Task TimeTableImportFromInternetBatch() + { + if (!_dialogService.GetConfirm("当前操作会覆盖现有车次配置,是否继续?")) return; + TrainNumberSelected(null); + var numbers = TrainNumbers.Select(x => x.Number).ToList(); + foreach (string s in numbers) + { + _databaseService.DeleteTrainNumber(_databaseService.GetTrainNumberByNumber(s)); + } + RefreshTrainNumbers(); + int total = numbers.Count; + int current = 0; + foreach (string number in numbers) + { + current++; + ProgressValue = (int)((double)current / total * 100); + List? timeTable = await _networkService.GetTimeTableAsync(number); + await Task.Delay(100); + timeTable ??= []; + _databaseService.AddTrainNumber(number); + var sections = new List
(); + var isEmu = number.StartsWith('G') || number.StartsWith('D') || number.StartsWith('C'); + for (int i = 1; i < timeTable.Count; i++) + { + sections.Add(new Section + { + From = timeTable[i - 1].Station, + To = timeTable[i].Station, + Tickets = new Tickets + { + SW = isEmu ? new() : null, + ZYY = isEmu ? new() : null, + ZY = isEmu ? new() : null, + ZE = isEmu ? new() : null, + RZ = !isEmu ? new() : null, + GRW = !isEmu ? new() : null, + RW = !isEmu ? new() : null, + WZ = new(), + YW = !isEmu ? new() : null, + YZ = !isEmu ? new() : null + } + }); + } + _databaseService.UpdateTrainNumber(_databaseService.GetTrainNumberByNumber(number), timeTable, sections); + } + ProgressValue = 0; + await _databaseService.SaveData(); + RefreshTrainNumbers(); + } + [RelayCommand] public async Task TimeTableImportFromInternet() { if (TimeTable.Count != 0) @@ -320,7 +433,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject property.SetValue(section.Tickets, @checked ? new Ticket() : null); } } - RefreshUI(); + RefreshSectionsUI(); } } @@ -361,9 +474,50 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject #region Price [RelayCommand] - public void PriceImportFromInternet() + public void SetPriceBatch(object s) { - throw new NotImplementedException(); + if (s is string type) + { + if (!_dialogService.GetConfirm("当前操作会覆盖现有车次配置,是否继续?")) return; + TrainNumberSelected(null); + + Dictionary<(string, string, string), double> sectionPrices = []; + string[] seatTypes = ["SW", "GRW", "YW", "ZE", "ZYY", "ZY", "WZ", "RW", "YZ", "RZ"]; + + foreach (var trainNumber in TrainNumbers) + { + bool isHighSpeed = trainNumber.Number.StartsWith('G') || trainNumber.Number.StartsWith('D') || trainNumber.Number.StartsWith('C'); + if ((type == "1" && isHighSpeed) || (type == "2" && !isHighSpeed)) continue; + + foreach (var section in trainNumber.Sections) + { + foreach (var seatType in seatTypes) + { + if (section.Tickets.GetType().GetProperty(seatType)?.GetValue(section.Tickets) is Ticket ticket && (!sectionPrices.ContainsKey((section.From, section.To, seatType)) || sectionPrices[(section.From, section.To, seatType)] == 0.0)) + { + sectionPrices[(section.From, section.To, seatType)] = ticket.Price; + } + } + } + } + + foreach (var trainNumber in TrainNumbers) + { + bool isHighSpeed = trainNumber.Number.StartsWith('G') || trainNumber.Number.StartsWith('D') || trainNumber.Number.StartsWith('C'); + if ((type == "1" && isHighSpeed) || (type == "2" && !isHighSpeed)) continue; + + foreach (var section in trainNumber.Sections) + { + foreach (var seatType in seatTypes) + { + if (section.Tickets.GetType().GetProperty(seatType)?.GetValue(section.Tickets) is Ticket ticket) + { + ticket.Price = sectionPrices[(section.From, section.To, seatType)]; + } + } + } + } + } } [RelayCommand] public void PriceImportFromExcel() @@ -423,7 +577,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject } } } - RefreshUI(); + RefreshSectionsUI(); } catch { @@ -471,9 +625,44 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject #region Quantity [RelayCommand] - public void QuantityImportFromInternet() + public void SetQuantityBatch(object s) { - throw new NotImplementedException(); + if(s is string extent) + { + if (!_dialogService.GetConfirm("当前操作会覆盖现有车次配置,是否继续?")) return; + TrainNumberSelected(null); + foreach(var trainNumber in TrainNumbers) + { + foreach(var Section in trainNumber.Sections) + { + if (Section.Tickets.SW != null) Section.Tickets.SW.Quantity = RandomQuantity(extent); + if (Section.Tickets.GRW != null) Section.Tickets.GRW.Quantity = RandomQuantity(extent); + if (Section.Tickets.YW != null) Section.Tickets.YW.Quantity = RandomQuantity(extent); + if (Section.Tickets.ZE != null) Section.Tickets.ZE.Quantity = RandomQuantity(extent); + if (Section.Tickets.ZYY != null) Section.Tickets.ZYY.Quantity = RandomQuantity(extent); + if (Section.Tickets.ZY != null) Section.Tickets.ZY.Quantity = RandomQuantity(extent); + if (Section.Tickets.WZ != null) Section.Tickets.WZ.Quantity = RandomQuantity(extent); + if (Section.Tickets.RW != null) Section.Tickets.RW.Quantity = RandomQuantity(extent); + if (Section.Tickets.YZ != null) Section.Tickets.YZ.Quantity = RandomQuantity(extent); + if (Section.Tickets.RZ != null) Section.Tickets.RZ.Quantity = RandomQuantity(extent); + } + } + } + } + private static int RandomQuantity(string extent) + { + switch (extent) + { + case "0": + return (new Random()).Next(10, 100); + case "1": + return (new Random()).Next(0, 25); + case "2": + return (new Random()).Next(0, 2) == 0 ? (new Random()).Next(1, 5) : 0; + default: + break; + } + return 0; } [RelayCommand] public void QuantityImportFromExcel() @@ -533,7 +722,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject } } } - RefreshUI(); + RefreshSectionsUI(); } catch { @@ -578,6 +767,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject package.SaveAs(new FileInfo(path)); } #endregion + [RelayCommand] public async Task SaveChanges() { @@ -585,6 +775,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject _databaseService.UpdateTrainNumber(SelectedTrainNumber, [.. TimeTable], [.. Sections]); await _databaseService.SaveData(); } + private Task Validate() { string message = ""; @@ -616,69 +807,7 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject _dialogService.ShowMessage("保存失败", $"发现 {message.Split("\n").Length - 1} 个错误:{message}\n请修复所有错误后再次尝试保存。"); return Task.FromResult(false); } - [RelayCommand] - public async Task ImportFromLulutong() - { - var path = _dialogService.GetFile("CSV 文件 (*.csv)|*.csv|所有文件 (*.*)|*.*"); - if (path == null) return; - try - { - var numbers = ReadCsvFirstColumn(path); - var intersection = numbers.Intersect(TrainNumbers.Select(x => x.Number)).ToList(); - if (intersection.Count != 0) - { - if (!_dialogService.GetConfirm("当前操作可能覆盖现有车次配置,是否继续?")) return; - } - foreach (string s in intersection) - { - _databaseService.DeleteTrainNumber(_databaseService.GetTrainNumberByNumber(s)); - } - RefreshTrainNumbers(); - int total = numbers.Count; - int current = 0; - foreach (string number in numbers) - { - current++; - ProgressValue = (int)((double)current / total * 100); - List? timeTable = await _networkService.GetTimeTableAsync(number); - await Task.Delay(100); - timeTable ??= []; - _databaseService.AddTrainNumber(number); - var sections = new List
(); - var isEmu = number.StartsWith('G') || number.StartsWith('D') || number.StartsWith('C'); - for (int i = 1; i < timeTable.Count; i++) - { - sections.Add(new Section - { - From = timeTable[i - 1].Station, - To = timeTable[i].Station, - Tickets = new Tickets - { - SW = isEmu ? new() : null, - ZYY = isEmu ? new() : null, - ZY = isEmu ? new() : null, - ZE = isEmu ? new() : null, - RZ = !isEmu ? new() : null, - GRW = !isEmu ? new() : null, - RW = !isEmu ? new() : null, - WZ = new(), - YW = !isEmu ? new() : null, - YZ = !isEmu ? new() : null - } - }); - } - _databaseService.UpdateTrainNumber(_databaseService.GetTrainNumberByNumber(number), timeTable, sections); - } - ProgressValue = 0; - await _databaseService.SaveData(); - RefreshTrainNumbers(); - } - catch - { - _dialogService.ShowMessage("导入失败", "文件格式错误或被占用。"); - ProgressValue = 0; - } - } + private static List ReadCsvFirstColumn(string filePath) { var firstColumnData = new List(); @@ -711,7 +840,8 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject } return firstColumnData; } - public void RefreshUI() + + public void RefreshSectionsUI() { var temp = Sections.ToList(); Sections.Clear(); @@ -720,5 +850,4 @@ public partial class TrainNumberManagementPageViewModel : ObservableObject Sections.Add(item); } } - -} +} \ No newline at end of file diff --git a/CRSim/Views/Pages/DataManagement/TrainNumberManagementPage.xaml b/CRSim/Views/Pages/DataManagement/TrainNumberManagementPage.xaml index b046c16..4898075 100644 --- a/CRSim/Views/Pages/DataManagement/TrainNumberManagementPage.xaml +++ b/CRSim/Views/Pages/DataManagement/TrainNumberManagementPage.xaml @@ -1,4 +1,4 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - +