You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.9 KiB
157 lines
4.9 KiB
import os
|
|
import json
|
|
from datetime import date
|
|
from typing import List, Optional
|
|
from student.Student import Student
|
|
|
|
|
|
class StudentDAL:
|
|
def __init__(self, file_path: str):
|
|
self.file_path = file_path
|
|
self._ensure_file_exists()
|
|
|
|
def _ensure_file_exists(self):
|
|
"""确保文件存在"""
|
|
if not os.path.exists(self.file_path):
|
|
os.makedirs(os.path.dirname(self.file_path), exist_ok=True)
|
|
with open(self.file_path, 'w', encoding='utf-8') as f:
|
|
json.dump([], f)
|
|
|
|
def _load_data(self) -> List[dict]:
|
|
"""加载数据"""
|
|
try:
|
|
with open(self.file_path, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return []
|
|
|
|
def _save_data(self, data: List[dict]):
|
|
"""保存数据"""
|
|
with open(self.file_path, 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
|
|
def add_student(self, student: Student) -> bool:
|
|
data = self._load_data()
|
|
|
|
# 检查身份证号是否唯一
|
|
if any(s['id_card'] == student.id_card for s in data):
|
|
return False
|
|
|
|
# 检查学号是否唯一
|
|
if any(s['stu_id'] == student.stu_id for s in data):
|
|
return False
|
|
|
|
data.append(student.to_dict())
|
|
self._save_data(data)
|
|
return True
|
|
|
|
def get_by_id_card(self, id_card: str) -> Optional[Student]:
|
|
data = self._load_data()
|
|
for student_dict in data:
|
|
if student_dict['id_card'] == id_card:
|
|
return Student.from_dict(student_dict)
|
|
return None
|
|
|
|
def get_by_stu_id(self, stu_id: str) -> Optional[Student]:
|
|
data = self._load_data()
|
|
for student_dict in data:
|
|
if student_dict['stu_id'] == stu_id:
|
|
return Student.from_dict(student_dict)
|
|
return None
|
|
|
|
def get_all(self) -> List[Student]:
|
|
return [Student.from_dict(d) for d in self._load_data()]
|
|
|
|
def update_student(self, stu_id: str, student: Student) -> bool:
|
|
data = self._load_data()
|
|
updated = False
|
|
|
|
for i, student_dict in enumerate(data):
|
|
if student_dict['stu_id'] == stu_id:
|
|
# 保留原始身份证号
|
|
new_data = student.to_dict()
|
|
new_data['id_card'] = student_dict['id_card']
|
|
data[i] = new_data
|
|
updated = True
|
|
break
|
|
|
|
if updated:
|
|
self._save_data(data)
|
|
return updated
|
|
|
|
def delete_student(self, stu_id: str) -> bool:
|
|
data = self._load_data()
|
|
original_count = len(data)
|
|
data = [s for s in data if s['stu_id'] != stu_id]
|
|
|
|
if len(data) < original_count:
|
|
self._save_data(data)
|
|
return True
|
|
return False
|
|
|
|
def search_by_name(self, name: str) -> List[Student]:
|
|
data = self._load_data()
|
|
name_lower = name.lower()
|
|
return [
|
|
Student.from_dict(d)
|
|
for d in data
|
|
if name_lower in d['name'].lower()
|
|
]
|
|
|
|
def search_by_class(self, class_name: str) -> List[Student]:
|
|
data = self._load_data()
|
|
class_lower = class_name.lower()
|
|
return [
|
|
Student.from_dict(d)
|
|
for d in data
|
|
if d.get('class_name') and class_lower in d['class_name'].lower()
|
|
]
|
|
|
|
def search_by_major(self, major: str) -> List[Student]:
|
|
data = self._load_data()
|
|
major_lower = major.lower()
|
|
return [
|
|
Student.from_dict(d)
|
|
for d in data
|
|
if d.get('major') and major_lower in d['major'].lower()
|
|
]
|
|
|
|
def get_student_count(self) -> int:
|
|
return len(self._load_data())
|
|
|
|
def get_major_counts(self) -> dict:
|
|
data = self._load_data()
|
|
counts = {}
|
|
for student_dict in data:
|
|
major = student_dict.get('major', '未指定')
|
|
counts[major] = counts.get(major, 0) + 1
|
|
return counts
|
|
|
|
def clear_all(self) -> bool:
|
|
self._save_data([])
|
|
return True
|
|
|
|
def export_data(self, file_path: str) -> bool:
|
|
data = self._load_data()
|
|
try:
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
def import_data(self, file_path: str) -> bool:
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
new_data = json.load(f)
|
|
|
|
current_data = self._load_data()
|
|
current_ids = {s['id_card'] for s in current_data}
|
|
|
|
# 只导入不重复的学生
|
|
to_import = [s for s in new_data if s['id_card'] not in current_ids]
|
|
|
|
self._save_data(current_data + to_import)
|
|
return True
|
|
except Exception:
|
|
return False |