feat: 保存时验证数据

This commit is contained in:
denglihong2007
2025-03-14 01:48:02 +08:00
parent a5ff87da03
commit 025878f4f0
7 changed files with 95 additions and 13 deletions

View File

@@ -9,7 +9,7 @@
<LangVersion>13.0</LangVersion>
<StartupObject>CRSim.App</StartupObject>
<ApplicationIcon>Assets\CRSimIcon.ico</ApplicationIcon>
<Version>1.2.6.0</Version>
<Version>1.2.7.0</Version>
</PropertyGroup>
<ItemGroup>

View File

@@ -1,8 +1,10 @@
global using System.Diagnostics;
global using System.IO;
global using System.Diagnostics;
global using System.Globalization;
global using System.ComponentModel;
global using System.Collections.ObjectModel;
global using System.Reflection;
global using System.Text;
global using System.Text.Json;
global using System.Windows;
global using System.Windows.Data;

View File

@@ -37,10 +37,8 @@ namespace CRSim.Services
}
public void ShowMessage(string title, string message)
{
var dialog = new MessageDialog(title, message)
{
Owner = _owner
};
dynamic dialog = message.Length > 25 ? new LongMessageDialog(title, message) : new MessageDialog(title, message);
dialog.Owner = _owner;
dialog.ShowDialog();
}

View File

@@ -1,9 +1,3 @@
using CRSim.Core.Models;
using System.IO;
using System.Linq;
using System.Text;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CRSim.ViewModels;
public partial class StationManagementPageViewModel : ObservableObject
@@ -223,9 +217,40 @@ public partial class StationManagementPageViewModel : ObservableObject
[RelayCommand]
public async Task SaveChanges()
{
if (!(await Validate())) return;
_databaseService.UpdateStation(SelectedStation.Name, GenerateStation(SelectedStation.Name,WaitingAreaNames,TicketChecks,StationStops,Platforms));
await _databaseService.SaveData();
}
private Task<bool> Validate()
{
string message = "";
foreach(var t in TicketChecks)
{
if (!WaitingAreaNames.Contains(t.WaitingAreaName))
{
message += $"\n检票口 {t.Name} 所在的候车室 {t.WaitingAreaName} 不存在;";
}
}
foreach(var s in StationStops)
{
if(!Platforms.Any(x=> x.Name == s.Platform))
{
message += $"\n车次 {s.Number} 所分配的站台 {s.Platform} 不存在;";
}
foreach(var t in s.TicketChecks)
{
if (!TicketChecks.Any(x => x.Name==t))
{
message += $"\n车次 {s.Number} 所分配的检票口 {t} 不存在;";
}
}
}
if (message == "") return Task.FromResult(true);
_dialogService.ShowMessage("保存失败", $"发现 {message.Split("\n").Length-1} 个错误:{message}\n请修复所有错误后再次尝试保存。");
return Task.FromResult(false);
}
public static Station GenerateStation(string stationName, ObservableCollection<string> waitingAreaNames, ObservableCollection<TicketCheck> ticketChecks,ObservableCollection<StationStop> stationStops,ObservableCollection<Platform> platforms)
{
var station = new Station

View File

@@ -628,7 +628,7 @@
<StackPanel
Grid.Column="1" Margin="12" Orientation="Vertical">
<TextBlock Text="保存车站配置" FontSize="14" Style="{StaticResource BodyTextBlockStyle}"/>
<TextBlock Opacity="0.7" FontSize="12" Style="{StaticResource CaptionTextBlockStyle}">请再次检查各设施配置是否正确</TextBlock>
<TextBlock Opacity="0.7" FontSize="12" Style="{StaticResource CaptionTextBlockStyle}">验证并保存数据配置</TextBlock>
</StackPanel>
<Button HorizontalAlignment="Right" Margin="10" Grid.Column="2" Command="{Binding ViewModel.SaveChangesCommand}"
FontSize="14" Style="{DynamicResource AccentButtonStyle}" Content="保存配置" />

View File

@@ -0,0 +1,24 @@
<Window x:Class="CRSim.Views.LongMessageDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CRSim.Views"
mc:Ignorable="d"
Title="UserInputDialog"
MinHeight="554" MaxHeight="554" Width="600"
WindowStyle="None"
WindowStartupLocation="CenterOwner">
<StackPanel Background="{DynamicResource SolidBackgroundFillColorQuarternaryBrush}">
<TextBlock x:Name="Title" FontFamily="Microsoft YaHei" FontSize="28" Margin="24,32,0,20"/>
<ScrollViewer Margin="24,0,24,24" Height="350">
<TextBox x:Name="Description" MinHeight="350" FontFamily="微软雅黑"
IsReadOnly="True"/>
</ScrollViewer>
<Rectangle Height="1" Fill="{DynamicResource SeparatorBorderBrush}"/>
<StackPanel Background="{DynamicResource SolidBackgroundFillColorBaseBrush}" Orientation="Horizontal" HorizontalAlignment="Stretch" FlowDirection="RightToLeft">
<Button Style="{DynamicResource AccentButtonStyle}" Content="关闭" FontFamily="Microsoft YaHei" Click="OkButton_Click" Margin="24,24,0,24" x:Name="AccentButton"
Width="170" Height="40" HorizontalAlignment="Right" FontSize="14"/>
</StackPanel>
</StackPanel>
</Window>

View File

@@ -0,0 +1,33 @@
namespace CRSim.Views
{
/// <summary>
/// UserInputDialog.xaml 的交互逻辑
/// </summary>
public partial class LongMessageDialog : Window
{
public string UserInput { get; private set; }
public LongMessageDialog(string text,string description)
{
WindowChrome.SetWindowChrome(
this,
new WindowChrome
{
CaptionHeight = 50,
CornerRadius = default,
GlassFrameThickness = new Thickness(-1),
ResizeBorderThickness = ResizeMode == ResizeMode.NoResize ? default : new Thickness(4),
UseAeroCaptionButtons = true
}
);
InitializeComponent();
Title.Text = text;
Description.Text = description;
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
}