BLL课件版

main
王海州 10 months ago
parent f06ac37cab
commit ec11989131

@ -0,0 +1,119 @@
import json
from datetime import date
from typing import List, Optional
from BELL import Student
class StudentService:
def __init__(self, dal):
self.dal = dal
def add_student(self, student: 'Student') -> bool:
if self.dal.check_student_exists(student.sid):
raise ValueError(f"学生 ID {student.sid} 已存在")
validation_errors = student.get_errors()
if validation_errors:
raise ValueError(f"学生数据校验不通过: {validation_errors}")
return self.dal.add_student(student)
def delete_student(self, sid: str) -> bool:
if not self.dal.check_student_exists(sid):
raise ValueError(f"学生 ID {sid} 不存在")
return self.dal.delete_student(sid)
def update_student(self, sid: str, student: 'Student') -> bool:
if not self.dal.check_student_exists(sid):
raise ValueError(f"学生 ID {sid} 不存在")
validation_errors = student.get_errors()
if validation_errors:
raise ValueError(f"学生数据校验不通过: {validation_errors}")
return self.dal.update_student(sid, student)
def update_student_partial(self, sid: str, name: Optional[str] = None, height: Optional[int] = None,
birth_date: Optional[date] = None, enrollment_date: Optional[date] = None,
class_name: Optional[str] = None) -> bool:
if not self.dal.check_student_exists(sid):
raise ValueError(f"学生 ID {sid} 不存在")
return self.dal.update_student_partial(sid, name, height, birth_date,
enrollment_date, class_name)
def get_student_by_id(self, sid: str) -> Optional['Student']:
return self.dal.find_student_by_sid(sid)
def get_students_by_name(self, name: str) -> List['Student']:
return self.dal.find_students_by_name(name)
def get_students_by_class(self, class_name: str) -> List['Student']:
return self.dal.find_students_by_class_name(class_name)
def get_all_students(self) -> List['Student']:
return self.dal.get_all_students()
def get_student_count(self) -> int:
return self.dal.get_student_count()
def get_students_by_height(self, min_height: int, max_height: int) -> List['Student']:
if min_height > max_height:
raise ValueError("最小身高不能大于最大身高")
return self.dal.find_students_by_height_range(min_height, max_height)
def get_avg_height(self) -> float:
total_height = self.dal.get_total_height()
student_count = self.dal.get_student_count()
return total_height / student_count if student_count > 0 else 0.0
def get_students_by_enrollment_year(self, year: int) -> List['Student']:
return self.dal.find_students_by_enrollment_year(year)
def get_students_by_age(self, min_age: int, max_age: int) -> List['Student']:
if min_age > max_age:
raise ValueError("最小年龄不能大于最大年龄")
today = date.today()
min_birth_date = date(today.year - max_age - 1, today.month, today.day)
max_birth_date = date(today.year - min_age, today.month, today.day)
return self.dal.find_students_by_birth_date_range(min_birth_date, max_birth_date)
def export_students(self, file_path: str) -> bool:
try:
students = self.get_all_students()
with open(file_path, 'w', encoding='utf-8') as f:
json.dump([student.to_dict() for student in students], f, ensure_ascii=False, indent=4)
return True
except Exception as e:
raise RuntimeError(f"导出学生数据失败: {str(e)}") from e
def import_students(self, file_path: str) -> int:
try:
with open(file_path, 'r', encoding='utf-8') as f:
student_dicts = json.load(f)
added_count = 0
for student_dict in student_dicts:
student = Student.from_dict(student_dict)
if not student.is_valid:
raise ValueError(f"学生数据校验不通过: {student.get_errors()}")
if self.dal.check_student_exists(student.sid):
continue
self.dal.add_student(student)
added_count += 1
return added_count
except Exception as e:
raise RuntimeError(f"导入学生数据失败: {str(e)}") from e
def clear_all_students(self) -> None:
self.dal.clear_all_students()
Loading…
Cancel
Save