diff --git a/StudentManager/Common/ErrorSet.cs b/StudentManager/Common/ErrorSet.cs index 1f2d3b6..4d92847 100644 --- a/StudentManager/Common/ErrorSet.cs +++ b/StudentManager/Common/ErrorSet.cs @@ -149,7 +149,7 @@ namespace StudentManager.Common if (id.QuestionBase.Status == false || ErrorArray.Contains(id.QuestionBase.ID)) return; - if ((id.CorrectCount - id.CorrectCount) <= CorrectThresholds) + if ((id.CorrectCount - id.ErrorCount) <= CorrectThresholds) { id.QuestionBase.Status = false; CorrectArray.Remove(id.QuestionBase.ID); @@ -162,7 +162,7 @@ namespace StudentManager.Common if (id.QuestionBase.Status == true || CorrectArray.Contains(id.QuestionBase.ID)) return; - if ((id.CorrectCount - id.CorrectCount) > CorrectThresholds) + if ((id.CorrectCount - id.ErrorCount) > CorrectThresholds) { id.QuestionBase.Status = true; ErrorArray.Remove(id.QuestionBase.ID); @@ -175,11 +175,11 @@ namespace StudentManager.Common AddCorrectQuestion(new QuestionBase { ID = question.Id }); } - public void QueryErrorSet(int id) + public void QueryErrorSet(long id) { Errores.Clear(); - SQLHelper.Query(Tables.UQTable, id).ForEach(x => + SQLHelper.Query(id).ForEach(x => { AddQuestion(new QuestionBase { ID = x.PID, Status = x.Status }); }); diff --git a/StudentManager/Common/FileSystem.cs b/StudentManager/Common/FileSystem.cs new file mode 100644 index 0000000..3a8b8dc --- /dev/null +++ b/StudentManager/Common/FileSystem.cs @@ -0,0 +1,18 @@ +using StudentManager.Data; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StudentManager.Common +{ + public static class FileSystem + { + public static void SaveAll() + { + StudentLib.Save(); + QuestionLib.Save(); + } + } +} diff --git a/StudentManager/Common/HomeWorkSet.cs b/StudentManager/Common/HomeWorkSet.cs index 0827111..0988b4a 100644 --- a/StudentManager/Common/HomeWorkSet.cs +++ b/StudentManager/Common/HomeWorkSet.cs @@ -215,11 +215,11 @@ namespace StudentManager.Common return works; } - public void FreshAllHomeWork(int id) + public void FreshAllHomeWork(long id) { HomeWorks.Clear(); - SQLHelper.Query(Tables.CQTable, id).ForEach(x => + SQLHelper.Query(id).ForEach(x => { List Questions = new List(); x.ProblemIDS.ForEach(y => Questions.Add(new QuestionBase { ID = y, Status = x.CorrectIDS.Contains(y) })); diff --git a/StudentManager/Common/SQLHelper.cs b/StudentManager/Common/SQLHelper.cs index ff743e0..efe0951 100644 --- a/StudentManager/Common/SQLHelper.cs +++ b/StudentManager/Common/SQLHelper.cs @@ -1,14 +1,12 @@ using DryIoc.ImTools; +using Google.Protobuf.WellKnownTypes; using MySql.Data.MySqlClient; using Newtonsoft.Json; -using StudentManager.Common; using StudentManager.Data; -using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System; +using Enum = System.Enum; +using System.Security.Cryptography; namespace StudentManager.Common { @@ -19,7 +17,7 @@ namespace StudentManager.Common static public string UQTable { get; } = "user_data"; static public string CQTable { get; } = "curson_questions"; } - + public static class SQLMapping { @@ -32,7 +30,7 @@ namespace StudentManager.Common { if (typeof(T) == typeof(StudentData)) { - (data as StudentData).UID = (int)reader[0]; + (data as StudentData).UID = (long)reader[0]; (data as StudentData).Name = reader[1].ToString(); (data as StudentData).Gender = reader[2].ToString(); (data as StudentData).Contact = reader[3].ToString(); @@ -40,14 +38,20 @@ namespace StudentManager.Common (data as StudentData).Grade = (byte)reader[5]; (data as StudentData).TotalsQuestions = (UInt16)reader[6]; (data as StudentData).CorrectionCount = (UInt16)reader[7]; - (data as StudentData).HomeWork = (UInt16)reader[8]; + (data as StudentData).CurronHomeWorkIndex = (UInt16)reader[8]; + (data as StudentData).Password = reader[9].ToString(); + (data as StudentData).Birthdate = (DateTime)reader[10]; + (data as StudentData).HWQTotalCount = (uint)reader[11]; + (data as StudentData).HWQTotalErrorCount = (uint)reader[12]; } else if (typeof(T) == typeof(QuestionData)) { DifficultyLevel dLevel = DifficultyLevel.easy; QStatus qStatus = QStatus.published; + QType qType = QType.填空题; (data as QuestionData).Id = (int)reader[0]; - (data as QuestionData).Type = reader[1].ToString(); + Enum.TryParse(reader[1].ToString(), true, out qType); + (data as QuestionData).Type = qType; (data as QuestionData).Stem = reader[2].ToString(); (data as QuestionData).Answer = reader[3].ToString(); Enum.TryParse(reader[4].ToString(), true, out dLevel); @@ -80,11 +84,11 @@ namespace StudentManager.Common - //for (int i = 0; i < reader.FieldCount; ++i) - //{ - // data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); }); - //} - } + //for (int i = 0; i < reader.FieldCount; ++i) + //{ + // data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); }); + //} + } } @@ -98,6 +102,14 @@ namespace StudentManager.Common public static class SQLHelper { + private static Dictionary TypeMap = new Dictionary + { + { typeof(QuestionData), "questions" }, + { typeof(CursonQuestionsData), "curson_questions" }, + { typeof(StudentData), "users" }, + { typeof(UserQuestionData), "user_data" }, + }; + static string config = "server=8.137.125.29;port=3306;user=StudentManager;password=wangxin55;database=studentmanager"; static MySqlConnection connection = null; static MySqlCommand cmd = null; @@ -105,13 +117,16 @@ namespace StudentManager.Common // update ** set ** = '**',... where ** = **; // insert into ** values('**','**'...); // delete from ** where ** = **; - public static List Query(string tableName, int id = -1, params string[] queryParams) where T : IDataCommon, new() + public static List Query(long id = 9999999, params string[] queryParams) where T : IDataCommon, new() { + + string tableName = string.Empty; + TypeMap.TryGetValue(typeof(T), out tableName); connection = new MySqlConnection(config); List result = new List(); - string idName = typeof(T) == typeof(StudentData) ? "user_id" : typeof(T) == typeof(QuestionData) ? "problem_id" : "user_id"; + string idName = typeof(T) == typeof(StudentData) ? "user_id" : typeof(T) == typeof(QuestionData) ? "problem_id" : "user_id"; try { connection.Open(); @@ -121,14 +136,14 @@ namespace StudentManager.Common string filedList = string.Join(",", queryParams); queryStr = $"select {filedList} from {tableName};"; - if (id != -1) + if (id != 9999999) queryStr = $@"select {filedList} from {tableName} WHERE {idName} = {id};"; } else { queryStr = $"select * from {tableName};"; - if (id != -1) - queryStr = $@"select * from {tableName} WHERE {idName} = {id};"; + if (id != 9999999) + queryStr = $@"select * from {tableName} WHERE {idName} = {id};"; } cmd = new MySqlCommand(queryStr, connection); @@ -157,7 +172,7 @@ namespace StudentManager.Common } } - public static List UnionQuery(int id = -1, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new() + public static List UnionQuery(int id = 9999999, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new() { connection = new MySqlConnection(config); @@ -165,9 +180,9 @@ namespace StudentManager.Common try { - connection.Open(); + connection = new MySqlConnection(config); string queryStr = ""; - if(id == -1) + if (id == 9999999) 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}"; @@ -198,10 +213,39 @@ namespace StudentManager.Common } } - public static void Add() + + public static void Add(T value) { + connection = new MySqlConnection(config); try { + string stableName = string.Empty; + TypeMap.TryGetValue(typeof(T), out stableName); + connection.Open(); + + if (typeof(T) == typeof(QuestionData)) + { + var ques = value as QuestionData; + string sql = $"INSERT INTO {stableName} " + + $"(problem_id, problem_type, problem_stem, problem_answer, difficulty_level, category, tags, source, lesson, status) " + + $"VALUES (@Id, @Type, @Stem, @Answer, @DifficultyLevel, @Category, @Tags, @Source, @Lesson, @Status)"; + + MySqlCommand cmd = new MySqlCommand(sql, connection); + cmd.Parameters.AddWithValue("@Id", ques.Id); + cmd.Parameters.AddWithValue("@Type", ques.Type.ToString()); + cmd.Parameters.AddWithValue("@Stem", ques.Stem); + cmd.Parameters.AddWithValue("@Answer", ques.Answer); + cmd.Parameters.AddWithValue("@DifficultyLevel", ques.DifficultyLevel.ToString()); + cmd.Parameters.AddWithValue("@Category", ques.Category); + cmd.Parameters.AddWithValue("@Tags", ques.Tags); + cmd.Parameters.AddWithValue("@Source", ques.Source); + cmd.Parameters.AddWithValue("@Lesson", ques.Lesson); + cmd.Parameters.AddWithValue("@Status", ques.Status.ToString()); + + cmd.ExecuteNonQuery(); + + } + //string sql = $"insert into {stableName}" } catch (Exception ex) { @@ -209,7 +253,100 @@ namespace StudentManager.Common } finally { + cmd.Dispose(); + connection.Close(); + } + } + internal static void UpdateErrorSet(long UID, int PID, bool status) + { + using (MySqlConnection connection = new MySqlConnection(config)) + { + try + { + connection.Open(); + + // 检查记录是否存在 + string checkQuery = "SELECT COUNT(*) FROM user_data WHERE user_id = @user_id AND problem_id = @problem_id"; + using (MySqlCommand checkCmd = new MySqlCommand(checkQuery, connection)) + { + checkCmd.Parameters.AddWithValue("@user_id", UID); + checkCmd.Parameters.AddWithValue("@problem_id", PID); + + int recordCount = Convert.ToInt32(checkCmd.ExecuteScalar()); + + if (recordCount > 0) + { + // 记录存在,执行更新操作 + string updateQuery = "UPDATE user_data SET status = @status WHERE user_id = @user_id AND problem_id = @problem_id"; + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", UID); + updateCmd.Parameters.AddWithValue("@problem_id", PID); + updateCmd.Parameters.AddWithValue("@status", status); + + updateCmd.ExecuteNonQuery(); + } + } + else + { + if (status == false) + { + string updateQuery = $"INSERT INTO user_data (user_id, problem_id) VALUES (@user_id, @problem_id)"; + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", UID); + updateCmd.Parameters.AddWithValue("@problem_id", PID); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + finally + { + connection.Close(); + } + } + } + + internal static void UpdateHomework(long UID, int lesson, string totalArray, string correctArray, DateTime dateTime, bool status) + { + using (MySqlConnection connection = new MySqlConnection(config)) + { + try + { + connection.Open(); + + string updateQuery = "UPDATE curson_questions SET status = @status, problem_ids = @problem_ids, " + + "correct_ids = @correct_ids, update_time = @update_time WHERE user_id = @user_id AND lesson = @lesson"; + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", UID); + updateCmd.Parameters.AddWithValue("@lesson", lesson); + updateCmd.Parameters.AddWithValue("@status", status); + updateCmd.Parameters.AddWithValue("@problem_ids", totalArray); + updateCmd.Parameters.AddWithValue("@correct_ids", correctArray); + updateCmd.Parameters.AddWithValue("@update_time", dateTime); + + updateCmd.ExecuteNonQuery(); + } + + + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + finally + { + connection.Close(); + } } } } diff --git a/StudentManager/Data/QuestionData.cs b/StudentManager/Data/QuestionData.cs index 7e6fa9f..77e17b3 100644 --- a/StudentManager/Data/QuestionData.cs +++ b/StudentManager/Data/QuestionData.cs @@ -31,12 +31,12 @@ namespace StudentManager.Data public class QuestionData : IDataCommon { public int Id { get; set; } = 0; - public string Type { get; set; } = string.Empty; + public QType Type { get; set; } = QType.填空题; 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 Tags { get; set; } = "默认"; public string Source { get; set; } = string.Empty; public int Lesson { get; set; } = 0; public QStatus Status { get; set; } = QStatus.published; diff --git a/StudentManager/Data/QuestionLib.cs b/StudentManager/Data/QuestionLib.cs index 424d199..8b63bde 100644 --- a/StudentManager/Data/QuestionLib.cs +++ b/StudentManager/Data/QuestionLib.cs @@ -39,7 +39,7 @@ namespace StudentManager.Data public static void FreshAllQuestion() { - SQLHelper.Query(Tables.QuesTable).ForEach(x => QuestionDic.Add(x.Id, x)); + SQLHelper.Query().ForEach(x => QuestionDic.Add(x.Id, x)); } public static ObservableCollection GetAllQuestion() @@ -62,6 +62,7 @@ namespace StudentManager.Data public static void Add(QuestionData question) { QuestionDic.Add(question.Id, question); + SQLHelper.Add(question); } public static void Remove(QuestionData question) diff --git a/StudentManager/Data/StudentData.cs b/StudentManager/Data/StudentData.cs index 39e2a94..8b480d4 100644 --- a/StudentManager/Data/StudentData.cs +++ b/StudentManager/Data/StudentData.cs @@ -6,24 +6,41 @@ using System.Threading.Tasks; namespace StudentManager.Data { + public enum Gender + { + 男, + 女, + } public class StudentData : IDataCommon { - public int UID { get; set; } = 1; + // Count info + public long UID { get; set; } = 1; + public string Password { get; set; } = "123456"; + + // Base info 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 DateTime Birthdate { get; set; } = DateTime.Now; + + // Error Set public int TotalsQuestions { get; set; } = 0; public int CorrectionCount { get; set; } = 0; - public int HomeWork { get; set; } = 0; + // HomeWork + public int CurronHomeWorkIndex { get; set; } = 0; + + // HomeWorkSet + public uint HWQTotalCount { get; set; } = 0; + public uint HWQTotalErrorCount { get; set; } = 0; + + + // Extension public float ErrorRate { get; set; } = 0; public float CorrectRate { get; set; } = 0; public int UnCorrectCount { get; set; } = 0; - - - //public string TableName { get => "users"; set => throw new NotImplementedException(); } } } diff --git a/StudentManager/Data/StudentInfo.cs b/StudentManager/Data/StudentInfo.cs index 3fcf758..607013c 100644 --- a/StudentManager/Data/StudentInfo.cs +++ b/StudentManager/Data/StudentInfo.cs @@ -16,7 +16,7 @@ namespace StudentManager.Data { public static string LibPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\FileLib\\"; public static string FileName = "studentLib.dbsi"; - public static Dictionary StudentDic { get; set; } = new Dictionary(); + public static Dictionary StudentDic { get; set; } = new Dictionary(); public static void Add(StudentInfo studentInfo) { @@ -30,7 +30,7 @@ namespace StudentManager.Data Debug.Assert(Path.Exists(LibPath)); string file = File.ReadAllText(LibPath + FileName); - StudentDic = JsonSerializer.Deserialize>(file); + StudentDic = JsonSerializer.Deserialize>(file); } public static void Save() @@ -43,7 +43,7 @@ namespace StudentManager.Data { StudentDic.Clear(); - SQLHelper.Query(Tables.UserTable).ForEach(x => + SQLHelper.Query().ForEach(x => StudentDic.Add(x.UID, new StudentInfo { UID = x.UID, @@ -51,7 +51,7 @@ namespace StudentManager.Data Address = x.Address, Contact = x.Contact, Grade = x.Grade, - CurrentHomeWorkIndex = x.HomeWork, + CurrentHomeWorkIndex = x.CurronHomeWorkIndex, })); foreach (var item in StudentDic) @@ -70,12 +70,12 @@ namespace StudentManager.Data return list; } - public static StudentInfo QueryStudentDetailInfo(int uid) + public static StudentInfo QueryStudentDetailInfo(long uid) { return GetStudentDetailInfo(Get(uid)); } - public static StudentInfo Get(int uid) + public static StudentInfo Get(long uid) { if (!StudentDic.ContainsKey(uid)) return null; return StudentDic[uid]; @@ -86,7 +86,7 @@ namespace StudentManager.Data StudentDic.Remove(studentInfo.UID); } - public static void Remove(int id) + public static void Remove(long id) { StudentDic.Remove(id); } @@ -108,7 +108,7 @@ namespace StudentManager.Data public class StudentInfo { - public int UID { get; set; } = 1; + public long UID { get; set; } = 1; public string Name { get; set; } = "Name"; public string Gender { get; set; } = "Gender"; public string Contact { get; set; } = "Contact"; diff --git a/StudentManager/Editor/AddQuestionView.xaml b/StudentManager/Editor/AddQuestionView.xaml index ab92584..d52f445 100644 --- a/StudentManager/Editor/AddQuestionView.xaml +++ b/StudentManager/Editor/AddQuestionView.xaml @@ -55,7 +55,7 @@ - + @@ -76,7 +76,7 @@ - + @@ -97,7 +97,7 @@ - + @@ -124,14 +124,16 @@