diff --git a/StudentManager/Common/CursonManager.cs b/StudentManager/Common/CursonManager.cs index 10f4b11..6346c7b 100644 --- a/StudentManager/Common/CursonManager.cs +++ b/StudentManager/Common/CursonManager.cs @@ -11,6 +11,5 @@ namespace StudentManager.Common public int CursonIndex { get; set; } = 0; public int CursonCount { get; set; } = 0; public string CursonName { get; set; } = string.Empty; - } } diff --git a/StudentManager/Common/ErrorSet.cs b/StudentManager/Common/ErrorSet.cs index 0207f8a..7608cd5 100644 --- a/StudentManager/Common/ErrorSet.cs +++ b/StudentManager/Common/ErrorSet.cs @@ -52,6 +52,11 @@ namespace StudentManager.Common public List CorrectArray { get; set; } = new List(); public int CorrectThresholds { get; set; } = 2; + public ErrorBase Get(int id) + { + return Errores.ContainsKey(id) ? Errores[id] : null; + } + public void UpdateErrorSetData() { foreach (var item in Errores) @@ -90,6 +95,7 @@ namespace StudentManager.Common QuestionData = QuestionLib.Get(item.Key), CorrectCount = item.Value.CorrectCount, ErrorCount = item.Value.ErrorCount, + TotalUseCount = item.Value.TotalUseCount, Status = item.Value.QuestionBase.Status }); } @@ -174,9 +180,9 @@ namespace StudentManager.Common { Errores.Clear(); - SQLHelper.Query(id).ForEach(x => + SQLHelper.Instance.Query(id).ForEach(x => { - AddQuestion(new QuestionBase { ID = x.PID, Status = x.Status }); + Errores.Add(x.PID, new ErrorBase { CorrectCount = x.CorrectCount, ErrorCount = x.ErrorCount, TotalUseCount = x.CorrectCount + x.ErrorCount, QuestionBase = new QuestionBase { ID= x.PID, Status = x.Status } }); }); } } diff --git a/StudentManager/Common/FileSystem.cs b/StudentManager/Common/FileSystem.cs index 77138ab..6dec314 100644 --- a/StudentManager/Common/FileSystem.cs +++ b/StudentManager/Common/FileSystem.cs @@ -1,4 +1,5 @@ using StudentManager.Data; +using StudentManager.Interface; using System; using System.Collections.Generic; using System.Linq; @@ -7,15 +8,15 @@ using System.Threading.Tasks; namespace StudentManager.Common { - public static class FileSystem + public class FileSystem : Singleton { - public static void SaveAll() + public void SaveAll() { StudentLib.Save(); QuestionLib.Save(); } - public static void FreashAllInfo() + public void FreashAllInfo() { StudentLib.FreshAllStudentInfo(); QuestionLib.FreshAllQuestion(); diff --git a/StudentManager/Common/HomeWorkManager.cs b/StudentManager/Common/HomeWorkManager.cs index 39db1ba..0bc2eaa 100644 --- a/StudentManager/Common/HomeWorkManager.cs +++ b/StudentManager/Common/HomeWorkManager.cs @@ -1,7 +1,9 @@ using DryIoc.ImTools; using StudentManager.Data; +using StudentManager.Interface; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -9,16 +11,19 @@ using System.Threading.Tasks; namespace StudentManager.Common { - public static class HomeWorkManager + public class HomeWorkManager : Singleton { - public static HomeWork CommonHomeWork { get; set; } = new HomeWork(); - - public static void PublicHomework(uint lesson, bool appedCommonWork = false, bool appendErrorSet = false, int workNum = -1) + public void PublicHomework(uint lesson, bool appedCommonWork = false, bool appendErrorSet = false, int workNum = -1) { foreach (var item in StudentLib.StudentDic) { item.Value.PublicHomeWork(lesson, appedCommonWork, appendErrorSet, workNum); } } + + internal void DeleteHomework(DetailHomeWorkSetInfo homeWork) + { + throw new NotImplementedException(); + } } } diff --git a/StudentManager/Common/HomeWorkSet.cs b/StudentManager/Common/HomeWorkSet.cs index 41233b3..9bd5667 100644 --- a/StudentManager/Common/HomeWorkSet.cs +++ b/StudentManager/Common/HomeWorkSet.cs @@ -1,4 +1,5 @@ using DryIoc.ImTools; +using LiveCharts; using MySqlX.XDevAPI; using StudentManager.Data; using System; @@ -40,7 +41,7 @@ namespace StudentManager.Common public List Questions { get; set; } = new List(); - public int TotalCount { get; set; } = 0; + public int TotalCount { get{return Questions.Count;} } public int ErrorCount { get; set; } = 0; public int CorrectCount { get; set; } = 0; @@ -69,11 +70,11 @@ namespace StudentManager.Common return list; } - public void UpdateData() + public void UpdateData(bool FirstAdd = false) { CorrectCount = 0; ErrorCount = 0; - TotalCount = Questions.Count; + if(FirstAdd) return; foreach (var item in Questions) { if (item.Status == true) CorrectCount++; @@ -92,7 +93,6 @@ namespace StudentManager.Common public void AddHomeWork(int id) { Questions.Add(new QuestionBase { ID = id }); - UpdateData(); } public void RemoveHomeWork(QuestionBase workInfo) @@ -136,25 +136,61 @@ namespace StudentManager.Common public int TotalCorrectQuestionCount { get; set; } = 0; public int TotalErrorQuestionRate { get; set; } = 0; + [JsonIgnore] + public List Lessons { get; set; } = new List(); + [JsonIgnore] + public ChartValues ErrorCounts { get; set; } = new ChartValues(); + [JsonIgnore] + public ChartValues CorrectCounts { get; set; } = new ChartValues(); + + + public void UpdateDataView() + { + if(ErrorCounts.Count == 0) + { + foreach (var item in HomeWorks) + { + Lessons.Add(item.Key.ToString()); + ErrorCounts.Add(item.Value.ErrorCount); + CorrectCounts.Add(item.Value.CorrectCount); + } + } + } + public void UpdateDate() { TotalSetCount = HomeWorks.Count; TotalQuestionCount = 0; - foreach (var item in HomeWorks) - { - TotalQuestionCount = item.Value.TotalCount; - TotalErrorQuestionCount = item.Value.ErrorCount; - TotalCorrectQuestionCount = item.Value.CorrectCount; - } + TotalErrorQuestionCount = 0; + TotalCorrectQuestionCount = 0; - TotalErrorQuestionRate = TotalErrorQuestionCount / TotalQuestionCount; + Lessons.Clear(); + ErrorCounts.Clear(); + CorrectCounts.Clear(); + + foreach (var item in HomeWorks) + { + TotalQuestionCount += item.Value.TotalCount; + TotalErrorQuestionCount += item.Value.ErrorCount; + TotalCorrectQuestionCount += item.Value.CorrectCount; + + Lessons.Add(item.Key.ToString()); + ErrorCounts.Add(item.Value.ErrorCount); + CorrectCounts.Add(item.Value.CorrectCount); + } + TotalErrorQuestionRate = TotalQuestionCount == 0 ? 0 : TotalErrorQuestionCount / TotalQuestionCount; } + public bool Contain(uint id) + { + return HomeWorks.ContainsKey(id); + } + public void AddHomeWork(HomeWork homeWork) { HomeWorks.Add(homeWork.Lesson, homeWork); - UpdateDate(); + } public ObservableCollection GetDetailHomeWorkList(uint lesson) @@ -164,16 +200,8 @@ namespace StudentManager.Common { list.Add(new DetailHomeWorkInfo { - //QuestionData = { - // QuestionData = QuestionLib.Get(item.ID), - // Status = item.Status }, QuestionData = QuestionLib.Get(item.ID), Status = item.Status, - //Lesson = HomeWorks[lesson].Lesson, - //DateTime = HomeWorks[lesson].DateTime, - //TotalCount = HomeWorks[lesson].TotalCount, - //ErrorCount = HomeWorks[lesson].ErrorCount, - //CorrectCount = HomeWorks[lesson].CorrectCount, PID = item.ID }); @@ -239,16 +267,15 @@ namespace StudentManager.Common { HomeWorks.Clear(); - SQLHelper.Query(id).ForEach(x => + SQLHelper.Instance.Query(id).ForEach(x => { List Questions = new List(); x.ProblemIDS.ForEach(y => Questions.Add(new QuestionBase { ID = y, Status = x.CorrectIDS.Contains(y) })); HomeWorks.Add(x.Lesson, new HomeWork { Lesson = x.Lesson, - TotalCount = x.ProblemIDS.Length, CorrectCount = x.CorrectCount, - ErrorCount = x.TotalCount - x.CorrectCount, + ErrorCount = x.Status == 1 ? x.TotalCount - x.CorrectCount : 0, DateTime = x.DateTime, Questions = Questions, Status = x.Status == 1 @@ -257,6 +284,12 @@ namespace StudentManager.Common UpdateDate(); } + + internal void DeleteHomework(uint lesson) + { + HomeWorks.Remove(lesson); + UpdateDate(); + } } } diff --git a/StudentManager/Common/SQLHelper.cs b/StudentManager/Common/SQLHelper.cs index 5c28ba2..4100c46 100644 --- a/StudentManager/Common/SQLHelper.cs +++ b/StudentManager/Common/SQLHelper.cs @@ -7,18 +7,10 @@ using System.Diagnostics; using System; using Enum = System.Enum; using System.Security.Cryptography; +using StudentManager.Interface; 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"; - static public string CQTable { get; } = "curson_questions"; - } - - public static class SQLMapping { public static void Mapping(MySqlDataReader reader) @@ -66,7 +58,7 @@ namespace StudentManager.Common else if (typeof(T) == typeof(CursonQuestionsData)) { - (data as CursonQuestionsData).UID = (int)reader[0]; + (data as CursonQuestionsData).UID = (long)reader[0]; (data as CursonQuestionsData).ProblemIDS = string.IsNullOrEmpty(reader[1].ToString()) ? Array.Empty() : JsonConvert.DeserializeObject(reader[1].ToString()); (data as CursonQuestionsData).CorrectIDS = string.IsNullOrEmpty(reader[2].ToString()) ? Array.Empty() : JsonConvert.DeserializeObject(reader[2].ToString()); (data as CursonQuestionsData).Lesson = (uint)reader[3]; @@ -82,28 +74,12 @@ namespace StudentManager.Common (data as UserQuestionData).CorrectCount = (byte)reader[3]; (data as UserQuestionData).Status = (byte)reader[4] == 1; } - - - - //for (int i = 0; i < reader.FieldCount; ++i) - //{ - // data?.GetType().GetProperties().ForEach(x => { x.SetValue(reader[i].GetType(), reader[i]); }); - //} } - - } - - - struct userTable + public class SQLHelper : Singleton { - - } - - public static class SQLHelper - { - private static Dictionary TypeMap = new Dictionary + private Dictionary TypeMap = new Dictionary { { typeof(QuestionData), "questions" }, { typeof(CursonQuestionsData), "curson_questions" }, @@ -111,157 +87,186 @@ namespace StudentManager.Common { 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; + private string config = "server=8.137.125.29;port=3306;user=StudentManager;password=wangxin55;database=studentmanager"; + private MySqlConnection connection = null; + private MySqlCommand cmd = null; - // update ** set ** = '**',... where ** = **; - // insert into ** values('**','**'...); - // delete from ** where ** = **; - public static List Query(long id = 9999999, params string[] queryParams) where T : IDataCommon, new() + + public List Query(long? id = null, 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"; - try + using (var connection = new MySqlConnection(config)) { - connection.Open(); - string queryStr = ""; - if (queryParams.Length > 0) + List result = new List(); + string idName = typeof(T) == typeof(StudentData) ? "user_id" : typeof(T) == typeof(QuestionData) ? "problem_id" : "user_id"; + + try { - string filedList = string.Join(",", queryParams); - queryStr = $"select {filedList} from {tableName};"; + connection.Open(); + string queryStr = ""; + if (queryParams.Length > 0) + { + string fieldList = string.Join(",", queryParams); + queryStr = $"SELECT {fieldList} FROM {tableName}"; - if (id != 9999999) - queryStr = $@"select {filedList} from {tableName} WHERE {idName} = {id};"; + if (id.HasValue) + queryStr += $" WHERE {idName} = @id"; + } + else + { + queryStr = $"SELECT * FROM {tableName}"; + if (id.HasValue) + queryStr += $" WHERE {idName} = @id"; + } + + using (var cmd = new MySqlCommand(queryStr, connection)) + { + if (id.HasValue) + cmd.Parameters.AddWithValue("@id", id.Value); + + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + T row = new T(); + SQLMapping.Mapping(row, reader); + result.Add(row); + } + } + } + + return result; } - else + catch (MySqlException ex) { - queryStr = $"select * from {tableName};"; - if (id != 9999999) - queryStr = $@"select * from {tableName} WHERE {idName} = {id};"; + Console.WriteLine($"MySQL error: {ex.Message}"); + return new List { }; } - - cmd = new MySqlCommand(queryStr, connection); - MySqlDataReader reader = cmd.ExecuteReader(); - - - - while (reader.Read()) + catch (Exception ex) { - T row = new T(); - SQLMapping.Mapping(row, reader); - result.Add(row); + Console.WriteLine($"General error: {ex.Message}"); + return new List { }; } - - return result; - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - return null; - } - finally - { - cmd.Dispose(); - connection.Close(); } } - public static List UnionQuery(int id = 9999999, string tableName = "questions", string otherTable = "user_data") where T : IDataCommon, new() + + public bool Add(T value) where T : IDataCommon { - connection = new MySqlConnection(config); + string tableName = string.Empty; + TypeMap.TryGetValue(typeof(T), out tableName); - List result = new List(); - - try + using (var connection = new MySqlConnection(config)) { - connection = new MySqlConnection(config); - string queryStr = ""; - 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}"; - - cmd = new MySqlCommand(queryStr, connection); - MySqlDataReader reader = cmd.ExecuteReader(); - - - - while (reader.Read()) + try { - T row = new T(); - SQLMapping.Mapping(row, reader); - result.Add(row); - } + connection.Open(); - return result; - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - return null; - } - finally - { - cmd.Dispose(); - connection.Close(); + if (typeof(T) == typeof(QuestionData)) + { + var ques = value as QuestionData; + string sql = $"INSERT INTO {tableName} " + + "(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)"; + + using (var 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(); + } + } + } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } + catch (Exception ex) + { + Console.WriteLine($"General error: {ex.Message}"); + return false; + } + return true; } } - - public static void Add(T value) + public bool Delete(T value) where T : IDataCommon { - connection = new MySqlConnection(config); - try - { - string stableName = string.Empty; - TypeMap.TryGetValue(typeof(T), out stableName); - connection.Open(); + string tableName = string.Empty; + TypeMap.TryGetValue(typeof(T), out tableName); - if (typeof(T) == typeof(QuestionData)) + using (var connection = new MySqlConnection(config)) + { + try { - 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)"; + connection.Open(); - 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()); + if (typeof(T) == typeof(CursonQuestionsData)) + { + var ques = value as CursonQuestionsData; + string sql = "DELETE FROM " + + $"{tableName} " + + "WHERE user_id = @user_id AND lesson = @lesson"; - cmd.ExecuteNonQuery(); + using (var cmd = new MySqlCommand(sql, connection)) + { + cmd.Parameters.AddWithValue("@user_id", ques.UID); + cmd.Parameters.AddWithValue("@lesson", ques.Lesson); - cmd.Dispose(); + cmd.ExecuteNonQuery(); + } + } + if (typeof(T) == typeof(QuestionData)) + { + var ques = value as QuestionData; + if (ques == null) + { + Console.WriteLine($"ERROR: Check Input"); + return false; + } + + string sql = $"DELETE " + + $"FROM {tableName}" + + "WHERE problem_id = @problem_id"; + + using (var cmd = new MySqlCommand(sql, connection)) + { + cmd.Parameters.AddWithValue("@problem_id", ques.Id); + + cmd.ExecuteNonQuery(); + } + + } } - //string sql = $"insert into {stableName}" - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - finally - { - //cmd.Dispose(); - connection.Close(); + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } + catch (Exception ex) + { + Console.WriteLine($"General error: {ex.Message}"); + return false; + } + + return true; } } - internal static void UpdateErrorSet(long UID, int PID, bool status) + + internal bool UpdateErrorSet(long UID, ErrorBase error) { using (MySqlConnection connection = new MySqlConnection(config)) { @@ -274,32 +279,35 @@ namespace StudentManager.Common using (MySqlCommand checkCmd = new MySqlCommand(checkQuery, connection)) { checkCmd.Parameters.AddWithValue("@user_id", UID); - checkCmd.Parameters.AddWithValue("@problem_id", PID); + checkCmd.Parameters.AddWithValue("@problem_id", error.QuestionBase.ID); 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"; + string updateQuery = "UPDATE user_data SET status = @status, error_count = @error_count, correct_count = @correct_count" + + " 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.Parameters.AddWithValue("@problem_id", error.QuestionBase.ID); + updateCmd.Parameters.AddWithValue("@error_count", error.ErrorCount); + updateCmd.Parameters.AddWithValue("@correct_count", error.CorrectCount); + updateCmd.Parameters.AddWithValue("@status", error.QuestionBase.Status); updateCmd.ExecuteNonQuery(); } } else { - if (status == false) + if (error.QuestionBase.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.Parameters.AddWithValue("@problem_id", error.QuestionBase.ID); updateCmd.ExecuteNonQuery(); } @@ -307,18 +315,26 @@ namespace StudentManager.Common } } } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } catch (Exception ex) { Console.WriteLine(ex.Message); + return false; } finally { connection.Close(); } + + return true; } } - internal static void UpdateHomework(long UID, uint lesson, string totalArray, string correctArray, DateTime dateTime, bool status) + internal bool UpdateHomework(long UID, uint lesson, string totalArray, string correctArray, DateTime dateTime, bool status) { using (MySqlConnection connection = new MySqlConnection(config)) { @@ -341,19 +357,104 @@ namespace StudentManager.Common } + } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; } catch (Exception ex) { Console.WriteLine(ex.Message); + return false; } finally { connection.Close(); } + return true; } } - internal static void AddHomework(CursonQuestionsData homeWork) + internal bool UpdateErrorSetUserInfo(long UID, uint totalCont, uint totalCorrectCount) + { + using (MySqlConnection connection = new MySqlConnection(config)) + { + try + { + connection.Open(); + + string updateQuery = "UPDATE users SET errset_totals_questions = @totalCont, errset_correction_count = @totalErrorCount " + + " WHERE user_id = @user_id"; + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", UID); + updateCmd.Parameters.AddWithValue("@totalCont", totalCont); + updateCmd.Parameters.AddWithValue("@totalErrorCount", totalCorrectCount); + + updateCmd.ExecuteNonQuery(); + } + + + } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + finally + { + connection.Close(); + } + return true; + } + } + + internal bool UpdateHomeworkTotalInfo(long UID, uint totalCont, uint totalErrorCount) + { + using (MySqlConnection connection = new MySqlConnection(config)) + { + try + { + connection.Open(); + + string updateQuery = "UPDATE users SET total_hwq_count = @totalCont, total_hwq_error_count = @totalErrorCount, " + + "WHERE user_id = @user_id"; + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", UID); + updateCmd.Parameters.AddWithValue("@totalCont", totalCont); + updateCmd.Parameters.AddWithValue("@totalErrorCount", totalErrorCount); + + updateCmd.ExecuteNonQuery(); + } + + + } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + finally + { + connection.Close(); + } + return true; + } + } + + internal bool AddHomework(CursonQuestionsData homeWork) { using (MySqlConnection connection = new MySqlConnection(config)) { @@ -375,19 +476,37 @@ namespace StudentManager.Common updateCmd.ExecuteNonQuery(); } + updateQuery = "UPDATE users SET home_work = @lesson " + + "WHERE user_id = @user_id"; + + using (MySqlCommand updateCmd = new MySqlCommand(updateQuery, connection)) + { + updateCmd.Parameters.AddWithValue("@user_id", homeWork.UID); + updateCmd.Parameters.AddWithValue("@lesson", homeWork.Lesson); + + updateCmd.ExecuteNonQuery(); + } + + } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; } catch (Exception ex) { - + return false; } finally { connection.Close(); } + + return true; } } - internal static void UpdateQuestion(QuestionData ques) + internal bool UpdateQuestion(QuestionData ques) { using (MySqlConnection connection = new MySqlConnection(config)) { @@ -425,14 +544,21 @@ namespace StudentManager.Common Console.WriteLine($"{rowsAffected} rows updated."); } } + catch (MySqlException ex) + { + Console.WriteLine($"MySQL error: {ex.Message}"); + return false; + } catch (Exception ex) { Console.WriteLine(ex.Message); + return false; } finally { connection.Close(); } + return true; } } } diff --git a/StudentManager/Data/QuestionLib.cs b/StudentManager/Data/QuestionLib.cs index d1b311a..b922ca1 100644 --- a/StudentManager/Data/QuestionLib.cs +++ b/StudentManager/Data/QuestionLib.cs @@ -70,8 +70,8 @@ namespace StudentManager.Data QuestionDic.Clear (); QuestionTableDic.Clear (); QuestionTableDesc.Clear (); - SQLHelper.Query().ForEach(x => QuestionDic.Add(x.Id, x)); - SQLHelper.Query().ForEach(x => { + SQLHelper.Instance.Query().ForEach(x => QuestionDic.Add(x.Id, x)); + SQLHelper.Instance.Query().ForEach(x => { if (!QuestionTableDic.ContainsKey(x.Lesson)) { QuestionTableDic[x.Lesson] = new Dictionary(); @@ -136,7 +136,7 @@ namespace StudentManager.Data QuestionTableDesc[question.Lesson].UpdateTime = DateTime.Now; QuestionTableDesc[question.Lesson].TotalQuestions = (uint)QuestionTableDic[question.Lesson].Count; - SQLHelper.Add(question); + SQLHelper.Instance.Add(question); } public static void Remove(QuestionData question) diff --git a/StudentManager/Data/StudentInfo.cs b/StudentManager/Data/StudentInfo.cs index d907d2a..cc41bae 100644 --- a/StudentManager/Data/StudentInfo.cs +++ b/StudentManager/Data/StudentInfo.cs @@ -28,7 +28,7 @@ namespace StudentManager.Data { StudentDic.Clear(); - Debug.Assert(Path.Exists(LibPath)); + if (!Path.Exists(LibPath + FileName)) return; string file = File.ReadAllText(LibPath + FileName); StudentDic = JsonSerializer.Deserialize>(file); } @@ -43,7 +43,7 @@ namespace StudentManager.Data { StudentDic.Clear(); - SQLHelper.Query().ForEach(x => + SQLHelper.Instance.Query().ForEach(x => StudentDic.Add(x.UID, new StudentInfo { UID = x.UID, @@ -51,14 +51,15 @@ namespace StudentManager.Data Address = x.Address, Contact = x.Contact, Grade = x.Grade, + Gender = x.Gender, CurrentHomeWorkIndex = x.CurronHomeWorkIndex, })); - foreach (var item in StudentDic) - { + foreach (var item in StudentDic) + { GetStudentDetailInfo(item.Value); } - } + } public static ObservableCollection GetAllStudents() { @@ -96,6 +97,8 @@ namespace StudentManager.Data studentData.ErrorSet.QueryErrorSet(studentData.UID); studentData.HomeWorkSet.FreshAllHomeWork(studentData.UID); studentData.CurentHomeWork = studentData.HomeWorkSet.Get(studentData.CurrentHomeWorkIndex); + + return studentData; } @@ -126,25 +129,29 @@ namespace StudentManager.Data public void PublicHomeWork(uint lesson, bool appendCommonWork = false, bool appendErrorSet = false, int workNum = -1) { HomeWork homeWork = new HomeWork(); - homeWork.Lesson = (uint)(HomeWorkSet.TotalSetCount + 1) ; + homeWork.Lesson = (uint)(HomeWorkSet.TotalSetCount + 1); + while (HomeWorkSet.Contain(homeWork.Lesson)) + { + homeWork.Lesson++; + } homeWork.DateTime = DateTime.Now; - if(appendErrorSet) + if (appendErrorSet) { ErrorSet.ErrorArray.ForEach(error => homeWork.AddHomeWork(error)); - } - if(appendCommonWork) + } + if (appendCommonWork) { - foreach (var item in QuestionLib.GetAllQuestionByLesson(lesson)) - { + foreach (var item in QuestionLib.GetAllQuestionByLesson(lesson)) + { homeWork.AddHomeWork(item.Id); - } - } + } + } if (workNum != -1) { } - HomeWorkSet.AddHomeWork(homeWork); + homeWork.UpdateData(true); CursonQuestionsData cqd = new CursonQuestionsData(); @@ -153,7 +160,7 @@ namespace StudentManager.Data int index = 0; homeWork.Questions.ForEach(question => { - + cqd.ProblemIDS[index] = question.ID; index++; }); @@ -161,9 +168,17 @@ namespace StudentManager.Data cqd.Lesson = homeWork.Lesson; cqd.DateTime = homeWork.DateTime; cqd.Status = 0; - - SQLHelper.AddHomework(cqd); + + if (SQLHelper.Instance.AddHomework(cqd)) + { + HomeWorkSet.AddHomeWork(homeWork); + HomeWorkSet.UpdateDate(); + } + else + { + //TODO: NOTIFY + } } } } diff --git a/StudentManager/Editor/CheckView.xaml b/StudentManager/Editor/CheckView.xaml index 7a3ae6f..e32ab5b 100644 --- a/StudentManager/Editor/CheckView.xaml +++ b/StudentManager/Editor/CheckView.xaml @@ -58,7 +58,7 @@ - + @@ -69,8 +69,8 @@ - +