This commit is contained in:
wangxiner55 2024-09-04 21:43:10 +08:00 committed by wangxiner55
parent 83a260adfe
commit 6a417aaf45
15 changed files with 469 additions and 37 deletions

View File

@ -1,4 +1,5 @@
using StudentManager.Editor; using StudentManager.Editor;
using StudentManager.Model;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
using System.Windows; using System.Windows;
@ -17,7 +18,9 @@ namespace StudentManager
protected override void RegisterTypes(IContainerRegistry containerRegistry) protected override void RegisterTypes(IContainerRegistry containerRegistry)
{ {
containerRegistry.RegisterForNavigation<MainEditor, Students>();
containerRegistry.RegisterForNavigation<StudentsView>();
containerRegistry.RegisterForNavigation<QuestionsView>();
} }
} }

View File

@ -1,5 +1,6 @@
using DryIoc.ImTools; using DryIoc.ImTools;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using StudentManager.Common;
using StudentManager.Data; using StudentManager.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,12 +11,65 @@ using System.Threading.Tasks;
namespace StudentManager.Common namespace StudentManager.Common
{ {
static class Tables
{
static public string UserTable { get; } = "users";
static public string QuesTable { get; } = "questions";
static public string UQTable { get; } = "user_data";
}
public static class SQLMapping public static class SQLMapping
{ {
public static T Mapping<T>() public static void Mapping<T>(MySqlDataReader reader)
{ {
typeof(T).Attributes.ToString(); typeof(T).GetProperties().ForEach(x => { Debug.WriteLine(x.Name.ToLower()); });
} }
public static void Mapping<T>(T data, MySqlDataReader reader)
{
if (typeof(T) == typeof(StudentData))
{
(data as StudentData).UID = (int)reader[0];
(data as StudentData).Name = reader[1].ToString();
(data as StudentData).Gender = reader[2].ToString();
(data as StudentData).Contact = reader[3].ToString();
(data as StudentData).Address = reader[4].ToString();
(data as StudentData).Grade = (int)reader[5];
}
else if(typeof(T) == typeof(QuestionData))
{
DifficultyLevel dLevel = DifficultyLevel.easy;
QStatus qStatus = QStatus.published;
(data as QuestionData).Id = (int)reader[0];
(data as QuestionData).Type = reader[1].ToString();
(data as QuestionData).Stem = reader[2].ToString();
(data as QuestionData).Answer = reader[3].ToString();
Enum.TryParse(reader[4].ToString(), true, out dLevel);
(data as QuestionData).DifficultyLevel = dLevel;
(data as QuestionData).Category = reader[5].ToString();
(data as QuestionData).Tags = reader[6].ToString();
(data as QuestionData).Source = reader[7].ToString();
Enum.TryParse(reader[8].ToString(), true, out qStatus);
(data as QuestionData).Status = qStatus;
}
//for (int i = 0; i < reader.FieldCount; ++i)
//{
// data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); });
//}
}
}
struct userTable
{
} }
public static class SQLHelper public static class SQLHelper
@ -27,35 +81,91 @@ namespace StudentManager.Common
// update ** set ** = '**',... where ** = **; // update ** set ** = '**',... where ** = **;
// insert into ** values('**','**'...); // insert into ** values('**','**'...);
// delete from ** where ** = **; // delete from ** where ** = **;
public static T Query<T>(ref T table, params string[] queryParams) where T : IDataCommon public static List<T> Query<T>(string tableName, int id = -1, params string[] queryParams) where T : IDataCommon, new()
{ {
connection = new MySqlConnection(config); connection = new MySqlConnection(config);
List<T> result = new List<T>();
string idName = typeof(T) == typeof(StudentData) ? Tables.UserTable : Tables.QuesTable;
try try
{ {
connection.Open(); connection.Open();
string queryStr = "";
if (queryParams.Length > 0) if (queryParams.Length > 0)
{ {
string filedList = string.Join(",", queryParams);
queryStr = $"select {filedList} from {tableName};";
if (id != -1)
queryStr = $@"select {filedList} from {tableName} WHERE {idName} = {id};";
}
else
{
queryStr = $"select * from {tableName};";
if (id != -1)
queryStr = $@"select * from {tableName} WHERE {idName} = {id};";
} }
string queryStr = $"select * from {table.TableName};";
cmd = new MySqlCommand(queryStr, connection); cmd = new MySqlCommand(queryStr, connection);
MySqlDataReader reader = cmd.ExecuteReader(); MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) while (reader.Read())
{ {
for (int i = 0; i < reader.FieldCount; ++i) T row = new T();
{ SQLMapping.Mapping(row, reader);
Debug.WriteLine(reader[i].ToString()); result.Add(row);
}
} }
return result;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
return null;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
public static List<T> UnionQuery<T>(int id = -1, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new()
{
connection = new MySqlConnection(config);
List<T> result = new List<T>();
try
{
connection.Open();
string queryStr = "";
if(id == -1)
queryStr = $@"SELECT u.user_id, q.* FROM {otherTable} u JOIN {tableName} q ON u.problem_id = q.problem_id";
else
queryStr = $@"SELECT q.* FROM {tableName} q JOIN {otherTable} u ON q.problem_id = u.problem_id WHERE u.user_id = {id}";
cmd = new MySqlCommand(queryStr, connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
T row = new T();
SQLMapping.Mapping(row, reader);
result.Add(row);
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
} }
finally finally
{ {

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager.Data
{
public enum DifficultyLevel
{
easy,
medium,
hard,
}
public enum QStatus
{
published,
pending_review,
deprecated,
}
public class QuestionData : IDataCommon
{
public int Id { get; set; } = 0;
public string Type { get; set; } = string.Empty;
public string Stem { get; set; } = string.Empty;
public string Answer { get; set; } = String.Empty;
public DifficultyLevel DifficultyLevel { get; set; } = DifficultyLevel.easy;
public string Category { get; set; } = string.Empty;
public string Tags { get; set; } = string.Empty;
public string Source { get; set; } = string.Empty;
public QStatus Status { get; set; } = QStatus.published;
public string TableName { get => "questions"; set => throw new NotImplementedException(); }
}
}

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager.Data
{
public class Student
{
public int UId { get; set; }
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager.Data
{
public class StudentData : IDataCommon
{
public int UID { get; set; } = 1;
public string Name { get; set; } = "Name";
public string Gender { get; set; } = "Gender";
public string Contact { get; set; } = "Contact";
public string Address { get; set; } = "Address";
public int Grade { get; set; } = 1;
public int TotalsQuestions { get; set; } = 0;
public int CorrectionCount { get; set; } = 0;
public string TableName { get => "users"; set => throw new NotImplementedException(); }
}
}

View File

@ -4,9 +4,25 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StudentManager.Editor" xmlns:local="clr-namespace:StudentManager.Editor"
xmlns:prism="http://prismlibrary.com/"
xmlns:extent="clr-namespace:StudentManager.Extensions"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainEditor" Height="450" Width="800"> Title="MainEditor" Height="450" Width="800">
<Grid> <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ItemsControl ItemsSource="{Binding MenuBars}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Title}" Command="{Binding DataContext.RegionTo, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding NameSpace}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ContentControl Grid.Column="1" prism:RegionManager.RegionName="{x:Static extent:PrismManager.MainRegionName}"/>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,4 +1,5 @@
using StudentManager.Common; using StudentManager.Common;
using StudentManager.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -15,21 +16,11 @@ using System.Windows.Shapes;
namespace StudentManager.Editor namespace StudentManager.Editor
{ {
/// <summary>
/// MainEditor.xaml 的交互逻辑
/// </summary>
public partial class MainEditor : Window public partial class MainEditor : Window
{ {
public MainEditor() public MainEditor()
{ {
InitializeComponent(); InitializeComponent();
Loaded += MainEditor_Loaded;
}
private void MainEditor_Loaded(object sender, RoutedEventArgs e)
{
SQLHelper.Query();
} }
} }
} }

View File

@ -0,0 +1,38 @@
<UserControl x:Class="StudentManager.Editor.QuestionsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StudentManager.Editor"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,5"/>
</Style>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding QuestionDatas}">
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction x:Name="menuTrigger" Command="{Binding navigateCommand}" CommandParameter="{Binding ElementName=menu, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>-->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Id}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Type}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Stem}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Answer}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding DifficultyLevel}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Category}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Tags}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Source}"/>
<TextBlock Width="50" Style="{StaticResource TextStyle}" Text="{Binding Status}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace StudentManager.Editor
{
/// <summary>
/// QuestionsView.xaml 的交互逻辑
/// </summary>
public partial class QuestionsView : UserControl
{
public QuestionsView()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,37 @@
<UserControl x:Class="StudentManager.Editor.StudentsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StudentManager.Editor"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,5"/>
</Style>
</UserControl.Resources>
<Grid>
<ListBox ItemsSource="{Binding StudentDatas}">
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction x:Name="menuTrigger" Command="{Binding navigateCommand}" CommandParameter="{Binding ElementName=menu, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>-->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding UID}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Name}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Gender}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Contact}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Address}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding Grade}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding TotalsQuestions}"/>
<TextBlock Style="{StaticResource TextStyle}" Text="{Binding CorrectionCount}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace StudentManager.Editor
{
/// <summary>
/// StudentsView.xaml 的交互逻辑
/// </summary>
public partial class StudentsView : UserControl
{
public StudentsView()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager.Extensions
{
public static class PrismManager
{
public static readonly string MainRegionName = "MainRegion";
}
}

View File

@ -0,0 +1,107 @@
using Mysqlx.Crud;
using StudentManager.Common;
using StudentManager.Data;
using StudentManager.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager.Model
{
class MenuBar : BindableBase
{
private string icon;
/// <summary>
/// Icon
/// </summary>
public string Icon
{
get { return icon; }
set { icon = value; }
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string nameSpace;
public string NameSpace
{
get { return nameSpace; }
set { nameSpace = value; }
}
}
class Students : BindableBase
{
private ObservableCollection<StudentData> studentDatas = new ObservableCollection<StudentData>();
public ReadOnlyObservableCollection<StudentData> StudentDatas { get; private set; }
private ObservableCollection<QuestionData> questionDatas = new ObservableCollection<QuestionData>();
public ReadOnlyObservableCollection<QuestionData> QuestionDatas { get; private set; }
private ObservableCollection<MenuBar> menuBars = new ObservableCollection<MenuBar>();
public ReadOnlyObservableCollection<MenuBar> MenuBars { get; private set; }
private readonly IRegionManager regionManager;
public DelegateCommand<string> RegionTo { get; set; }
Students(IRegionManager regionManager)
{
CreateMenuBar();
RegionTo = new DelegateCommand<string>((x) => {
regionManager.Regions[PrismManager.MainRegionName].RequestNavigate(x.ToString());
RegionToStudents();
});
StudentDatas = new ReadOnlyObservableCollection<StudentData>(studentDatas);
QuestionDatas = new ReadOnlyObservableCollection<QuestionData>(questionDatas);
MenuBars = new ReadOnlyObservableCollection<MenuBar>(menuBars);
this.regionManager = regionManager;
QueryAll();
}
private void RegionToStudents()
{
}
void CreateMenuBar()
{
menuBars.Add(new MenuBar() { Icon = "ForumPlusOutline", Title = "学生", NameSpace = "StudentsView" });
menuBars.Add(new MenuBar() { Icon = "CodeGreaterThanOrEqual", Title = "题库", NameSpace = "QuestionsView" });
}
private void QueryAll()
{
studentDatas.Clear();
questionDatas.Clear();
SQLHelper.Query<StudentData>(Tables.UserTable).ForEach(x => studentDatas.Add(x));
SQLHelper.Query<QuestionData>(Tables.QuesTable).ForEach(x => questionDatas.Add(x));
//SQLHelper.UnionQuery<QuestionData>(1).ForEach(x => questionDatas.Add(x));
}
}
}

View File

@ -14,7 +14,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Model\" />
<Folder Include="Interface\" /> <Folder Include="Interface\" />
</ItemGroup> </ItemGroup>

View File

@ -10,10 +10,22 @@
<Compile Update="Editor\MainEditor.xaml.cs"> <Compile Update="Editor\MainEditor.xaml.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Update="Editor\QuestionsView.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="Editor\StudentsView.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Page Update="Editor\MainEditor.xaml"> <Page Update="Editor\MainEditor.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Update="Editor\QuestionsView.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="Editor\StudentsView.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup> </ItemGroup>
</Project> </Project>