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.
73 lines
3.2 KiB
73 lines
3.2 KiB
from datetime import date
|
|
from typing import Optional, List
|
|
from dal.student_dal_json import StudentDAL
|
|
from model import Student
|
|
|
|
|
|
class StudentBLL:
|
|
def __init__(self, dal: StudentDAL):
|
|
self.dal = dal
|
|
|
|
def add_student(self, student: Student) -> bool:
|
|
if self.dal.check_student_exists(student.sid):
|
|
raise ValueError(f"学生 ID {student.sid} 已存在。")
|
|
if not student.is_valid:
|
|
raise ValueError(f"学生数据校验不通过。错误信息:{student.get_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 delete_student_by_id_number(self, id_number: str) -> bool:
|
|
student = self.dal.find_student_by_id_number(id_number)
|
|
if not student:
|
|
raise ValueError(f"身份证号为 {id_number} 的学生不存在。")
|
|
return self.dal.delete_student_by_id_number(id_number)
|
|
|
|
def update_student(self, sid: str, student: Student) -> bool:
|
|
if not self.dal.check_student_exists(sid):
|
|
raise ValueError(f"学生 ID {sid} 不存在。")
|
|
if not student.is_valid:
|
|
raise ValueError(f"学生数据校验不通过。错误信息:{student.get_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, major: Optional[str] = None,
|
|
phone: Optional[str] = None, email: 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, major, phone, email)
|
|
|
|
def get_student_by_id(self, sid: str) -> Optional[Student]:
|
|
return self.dal.find_student_by_sid(sid)
|
|
|
|
def find_student_by_id_number(self, id_number: str) -> Optional[Student]:
|
|
return self.dal.find_student_by_id_number(id_number)
|
|
|
|
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_students_by_major(self, major: str) -> List[Student]:
|
|
return self.dal.find_students_by_major(major)
|
|
|
|
def get_all_students(self) -> List[Student]:
|
|
return self.dal.get_all_students()
|
|
|
|
def count_students_by_major(self):
|
|
"""统计各专业学生人数"""
|
|
return self.dal.count_students_by_major()
|
|
|
|
def calculate_average_height_weight(self, group_by='major'):
|
|
"""计算平均身高/体重(按班级或专业)"""
|
|
return self.dal.calculate_average_height_weight(group_by)
|
|
|
|
def calculate_gender_ratio(self):
|
|
"""统计性别比例"""
|
|
return self.dal.calculate_gender_ratio() |