132 lines
3.4 KiB
C#
132 lines
3.4 KiB
C#
using StudentManager.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StudentManager.Data
|
|
{
|
|
public static class StudentLib
|
|
{
|
|
public static string LibPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\FileLib\\";
|
|
public static string FileName = "studentLib.dbsi";
|
|
public static Dictionary<int, StudentInfo> StudentDic { get; set; } = new Dictionary<int, StudentInfo>();
|
|
|
|
public static void Add(StudentInfo studentInfo)
|
|
{
|
|
StudentDic.Add(studentInfo.UID, studentInfo);
|
|
}
|
|
|
|
|
|
public static void Load()
|
|
{
|
|
StudentDic.Clear();
|
|
|
|
Debug.Assert(Path.Exists(LibPath));
|
|
string file = File.ReadAllText(LibPath + FileName);
|
|
StudentDic = JsonSerializer.Deserialize<Dictionary<int, StudentInfo>>(file);
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
Directory.CreateDirectory(Path.GetFullPath(LibPath));
|
|
File.WriteAllText((LibPath + FileName), JsonSerializer.Serialize(StudentDic));
|
|
}
|
|
|
|
public static void FreshAllStudentInfo()
|
|
{
|
|
StudentDic.Clear();
|
|
|
|
SQLHelper.Query<StudentData>(Tables.UserTable).ForEach(x =>
|
|
StudentDic.Add(x.UID, new StudentInfo
|
|
{
|
|
UID = x.UID,
|
|
Name = x.Name,
|
|
Address = x.Address,
|
|
Contact = x.Contact,
|
|
Grade = x.Grade,
|
|
CurrentHomeWorkIndex = x.HomeWork,
|
|
}));
|
|
|
|
foreach (var item in StudentDic)
|
|
{
|
|
GetStudentDetailInfo(item.Value);
|
|
}
|
|
}
|
|
|
|
public static ObservableCollection<StudentInfo> GetAllStudents()
|
|
{
|
|
var list = new ObservableCollection<StudentInfo>();
|
|
foreach (var item in StudentDic)
|
|
{
|
|
list.Add(item.Value);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public static StudentInfo QueryStudentDetailInfo(int uid)
|
|
{
|
|
return GetStudentDetailInfo(Get(uid));
|
|
}
|
|
|
|
public static StudentInfo Get(int uid)
|
|
{
|
|
if (!StudentDic.ContainsKey(uid)) return null;
|
|
return StudentDic[uid];
|
|
}
|
|
|
|
public static void Remove(StudentInfo studentInfo)
|
|
{
|
|
StudentDic.Remove(studentInfo.UID);
|
|
}
|
|
|
|
public static void Remove(int id)
|
|
{
|
|
StudentDic.Remove(id);
|
|
}
|
|
|
|
public static StudentInfo GetStudentDetailInfo(StudentInfo studentData)
|
|
{
|
|
studentData.ErrorSet.QueryErrorSet(studentData.UID);
|
|
studentData.HomeWorkSet.FreshAllHomeWork(studentData.UID);
|
|
studentData.CurentHomeWork = studentData.HomeWorkSet.Get(studentData.CurrentHomeWorkIndex);
|
|
return studentData;
|
|
}
|
|
|
|
public static void GetStudentDetailInfo(ref StudentInfo studentData)
|
|
{
|
|
studentData.ErrorSet.QueryErrorSet(studentData.UID);
|
|
studentData.HomeWorkSet.FreshAllHomeWork(studentData.UID);
|
|
}
|
|
}
|
|
|
|
public class StudentInfo
|
|
{
|
|
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 HomeWorkCount { get; set; } = 0;
|
|
|
|
|
|
public ErrorSet ErrorSet { get; set; } = new ErrorSet();
|
|
public HomeWorkSet HomeWorkSet { get; set; } = new HomeWorkSet();
|
|
|
|
public int CurrentHomeWorkIndex = 0;
|
|
public HomeWork CurentHomeWork { get; set; } = new HomeWork();
|
|
|
|
public void PublicHomeWork(HomeWork homeWork)
|
|
{
|
|
HomeWorkSet.AddHomeWork(homeWork);
|
|
}
|
|
}
|
|
}
|