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.
92 lines
3.0 KiB
92 lines
3.0 KiB
from datetime import date
|
|
from typing import List, Optional
|
|
from dal.StudentDAL import StudentDAL
|
|
from student.Student import Student
|
|
|
|
|
|
class StudentBLL:
|
|
def __init__(self, dal: StudentDAL):
|
|
self.dal = dal
|
|
|
|
def add_student(self, student: Student):
|
|
if not student.is_valid:
|
|
errors = ", ".join(student.get_errors())
|
|
raise ValueError(f"学生数据校验失败: {errors}")
|
|
|
|
if not self.dal.add_student(student):
|
|
raise ValueError("添加失败,身份证号或学号可能已存在")
|
|
|
|
def delete_student(self, stu_id: str):
|
|
if not self.dal.delete_student(stu_id):
|
|
raise ValueError(f"找不到学号为 {stu_id} 的学生")
|
|
|
|
def update_student(self, stu_id: str, student: Student):
|
|
if not student.is_valid:
|
|
errors = ", ".join(student.get_errors())
|
|
raise ValueError(f"学生数据校验失败: {errors}")
|
|
|
|
if not self.dal.update_student(stu_id, student):
|
|
raise ValueError(f"找不到学号为 {stu_id} 的学生")
|
|
|
|
def get_student_by_id_card(self, id_card: str) -> Optional[Student]:
|
|
return self.dal.get_by_id_card(id_card)
|
|
|
|
def get_student_by_stu_id(self, stu_id: str) -> Optional[Student]:
|
|
return self.dal.get_by_stu_id(stu_id)
|
|
|
|
def get_all_students(self) -> List[Student]:
|
|
return self.dal.get_all()
|
|
|
|
def search_students(self, search_type: str, keyword: str) -> List[Student]:
|
|
search_type = search_type.lower()
|
|
if search_type == 'name':
|
|
return self.dal.search_by_name(keyword)
|
|
elif search_type == 'class':
|
|
return self.dal.search_by_class(keyword)
|
|
elif search_type == 'major':
|
|
return self.dal.search_by_major(keyword)
|
|
else:
|
|
return []
|
|
|
|
def get_student_count(self) -> int:
|
|
return self.dal.get_student_count()
|
|
|
|
def get_major_counts(self) -> dict:
|
|
return self.dal.get_major_counts()
|
|
|
|
def calculate_avg_height(self) -> float:
|
|
students = self.dal.get_all()
|
|
heights = [s.height for s in students if s.height is not None]
|
|
if not heights:
|
|
return 0.0
|
|
return sum(heights) / len(heights)
|
|
|
|
def calculate_avg_weight(self) -> float:
|
|
students = self.dal.get_all()
|
|
weights = [s.weight for s in students if s.weight is not None]
|
|
if not weights:
|
|
return 0.0
|
|
return sum(weights) / len(weights)
|
|
|
|
def get_students_by_age(self, min_age: int, max_age: int) -> List[Student]:
|
|
today = date.today()
|
|
students = self.dal.get_all()
|
|
result = []
|
|
|
|
for student in students:
|
|
if student.age is None:
|
|
continue
|
|
|
|
if min_age <= student.age <= max_age:
|
|
result.append(student)
|
|
|
|
return result
|
|
|
|
def export_data(self, file_path: str) -> bool:
|
|
return self.dal.export_data(file_path)
|
|
|
|
def import_data(self, file_path: str) -> bool:
|
|
return self.dal.import_data(file_path)
|
|
|
|
def clear_all(self) -> bool:
|
|
return self.dal.clear_all() |