diff --git a/PythonProject/api/employment_api.py b/PythonProject/api/employment_api.py index a3aa6d7..6faec0e 100644 --- a/PythonProject/api/employment_api.py +++ b/PythonProject/api/employment_api.py @@ -1,29 +1,21 @@ from fastapi import APIRouter, Query, Depends, HTTPException, Path,Form from dao import employment_dao from dao.employment_dao import add_emp_dao, update_emp_dao, delete_emp_dao -from schema.employment_schema import EmploymentRequest,EmploymentResponse +from schema.employment_schema import EmploymentRequest,UpdateEmpRequest,EmploymentResponse from util.database import get_db from typing import Optional emp_api = APIRouter(tags=['学生就业管理模块']) -@emp_api.get('/employment/class/{stu_id}',response_model=EmploymentResponse,summary='获取学生就业信息') -def get_emp_info1(stu_id: Optional[int]=Path(description='学生编号') +@emp_api.get('/employment/class/{class_id}',response_model=list[EmploymentResponse],summary='获取班级学生就业信息') +def get_emp_info1(class_id: Optional[int]=Path(description='班级编号') ,db=Depends(get_db)): - e1 = employment_dao.get_emp_dao(stu_id=stu_id, db=db) + e1 = employment_dao.get_emp_dao(class_id=class_id, db=db) if e1: - return e1[0] - raise HTTPException(status_code=404, detail='该学生就业信息不存在') - -@emp_api.get('/employment/class/{class_id}',response_model=EmploymentResponse,summary='获取班级学生就业信息') -def get_emp_info2(class_id: Optional[int]=Path(description='班级编号') - ,db=Depends(get_db)): - e2 = employment_dao.get_emp_dao(class_id=class_id, db=db) - if e2: - return e2 + return e1 raise HTTPException(status_code=404, detail='该班级就业信息不存在') -@emp_api.get('/employment',response_model=EmploymentResponse,summary='按照学⽣编号,公司名字,⼯资范围查询学⽣就业信息') -def get_emp_info3(stu_id:int|None = Query(None, description='学生编号') +@emp_api.get('/employment/students/{stu_id}',response_model=list[EmploymentResponse],summary='按照学⽣编号,公司名字,⼯资范围查询学⽣就业信息') +def get_emp_info2(stu_id:int = Path(description='学生编号') ,company_name: str|None = Query(None,description='公司名称') ,min_salary: float|None = Query(None,ge=0,description='最低工资') ,max_salary: float|None = Query(None,ge=0,description='最高工资') @@ -31,11 +23,13 @@ def get_emp_info3(stu_id:int|None = Query(None, description='学生编号') if min_salary and max_salary: if min_salary > max_salary: raise HTTPException(status_code=404, detail='最低工资不能大于最高工资') - return [employment_dao.get_emp_dao(stu_id=stu_id - ,company_name=company_name - ,min_salary=min_salary - ,max_salary=max_salary - ,db=db)] + l1 = employment_dao.get_emp_dao(db, stu_id=stu_id + , company_name=company_name + , min_salary=min_salary + , max_salary=max_salary) + if not l1: + raise HTTPException(status_code=404,detail='该学生就业信息不存在!') + return l1 @emp_api.post('/employment/students/{stu_id}',response_model=EmploymentResponse,summary='新增学生就业信息') def create_emp_info(emp:EmploymentRequest=Form(),db=Depends(get_db)): @@ -45,10 +39,9 @@ def create_emp_info(emp:EmploymentRequest=Form(),db=Depends(get_db)): raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!') return r -@emp_api.put('/employment/students/{emp_id}',response_model=EmploymentResponse,summary='更新学生就业信息') -def update_emp_info(emp_id:int,emp:EmploymentRequest,db=Depends(get_db)): - d = emp.model_dump(exclude_unset=True) - r = update_emp_dao(emp_id=emp_id,db=db) +@emp_api.put('/employment/students/{emp_id}',summary='更新学生就业信息') +def update_emp_info(emp_id:int,emp:UpdateEmpRequest,db=Depends(get_db)): + r = update_emp_dao(emp_id,emp.model_dump(exclude_unset=True),db) if not r: raise HTTPException(status_code=500,detail='没有更新!') return {'code':200,'totals':r,'detail':'更新成功!'} diff --git a/PythonProject/api/grade_api.py b/PythonProject/api/grade_api.py index f1988c8..fb3d5cf 100644 --- a/PythonProject/api/grade_api.py +++ b/PythonProject/api/grade_api.py @@ -7,15 +7,19 @@ from fastapi import APIRouter, Depends, HTTPException, Body gradeAPI = APIRouter(tags=['学生考核成绩管理模块']) -@gradeAPI.get("/grade/{stu_id}",response_model=list[GradeResponse],summary="根据学生id查询成绩") +@gradeAPI.get("/grade/{stu_id}",summary="根据学生id查询成绩") def get_grade(stu_id: int,db=Depends(get_db)): try: res=get_grade_dao(stu_id,db) - return res + return { + "code": 200, + "detail": "查询成功", + "data": res + } except Exception as e: raise HTTPException(status_code=500,detail=f'数据库查询异常:{str(e)}') -@gradeAPI.post('/grade',response_model=GradeResponse,summary='添加学生成绩') +@gradeAPI.post('/grade',summary='添加学生成绩') def post_grade(o:GradeRequest,db=Depends(get_db)): d=o.model_dump() r=add_grade_dao(g=d,db=db) diff --git a/PythonProject/api/statistics_api.py b/PythonProject/api/statistics_api.py index 401efd1..86c09e2 100644 --- a/PythonProject/api/statistics_api.py +++ b/PythonProject/api/statistics_api.py @@ -6,7 +6,7 @@ from util.database import get_db StatisticsAPI = APIRouter(tags=['统计分析模块']) -@StatisticsAPI.get("/age",response_model=list[StudentResponse],summary='根据年龄查询学生信息') +@StatisticsAPI.get("/age",response_model=list[StugetResponse],summary='根据年龄查询学生信息') def get_students(min_age:int|None=Query(None,ge=0,le=150),max_age:int|None=Query(None,ge=0,le=150),db=Depends(get_db)): l = select_age(min_age,max_age,db) return l @@ -40,3 +40,13 @@ def get_days(db=Depends(get_db)): def get_emp(db=Depends(get_db)): l= select_days(db) return l + +@StatisticsAPI.get('/salaries',summary='统计薪资分布') +def get_salaries(db=Depends(get_db)): + s=classify_salary(db) + return s + +@StatisticsAPI.get('/avg_salaries',summary='统计每个班级平均薪资') +def get_avg_salaries(db=Depends(get_db)): + s=avg_salary(db) + return s diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py index 57676e7..b39f4b4 100644 --- a/PythonProject/api/student_api.py +++ b/PythonProject/api/student_api.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter,Depends,HTTPException +from fastapi import APIRouter,HTTPException,Depends from dao.student_dao import * from schema.student_schema import * from util.database import get_db @@ -6,7 +6,7 @@ from util.database import get_db StudentAPI = APIRouter(tags=['学生基本信息管理模块']) -@StudentAPI.get('/students',response_model=StudentPageResponse,summary='学生信息查询接口',description='查询学生信息') +@StudentAPI.get('/students',response_model=StuPageResponse,summary='学生信息查询接口',description='查询学生信息') def get_students(stu_id:int|None=None ,stu_name:str|None=None ,class_id:int|None=None @@ -19,39 +19,43 @@ def get_students(stu_id:int|None=None ,page=page ,page_size=page_size ,db=db) - - if not r: - raise HTTPException(status_code=404,detail='学生不存在') - return StudentPageResponse(page=page, + return StuPageResponse(page=page, page_size=page_size, totals=total, data=r) -@StudentAPI.put('/{stu_id}',summary='学生信息更新接口',description='更新学生信息') -def update_students(s:StudentRequest - ,stu_id:int - ,db=Depends(get_db)): +@StudentAPI.post('/students',response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息') +def add_students(s:StudentRequest + ,db=Depends(get_db)): d = s.model_dump(exclude_unset=True) if not d: - raise HTTPException(status_code=400, detail='更新内容不能为空!') + raise HTTPException(status_code=400, detail='添加内容不能为空') + r = add_student_dao( o=d, db=db) + if r == 'conflict': + raise HTTPException(status_code=409, detail='身份证号已存在,请勿重复添加') + if r == 'error': + raise HTTPException(status_code=500, detail='添加失败,请稍后重试') + return r + +@StudentAPI.put('/students/{stu_id}',summary='学生信息更新接口',description='更新学生信息') +def update_students(stu_id:int + ,s:StuUpdateRequest + ,db=Depends(get_db)): + d = s.model_dump(exclude_unset=True) + d.pop('stu_id', None) + if not d: + raise HTTPException(status_code=400, detail='更新内容不能为空') r = update_student_dao( stu_id=stu_id,update_data=d,db=db ) + if r == 'error': + raise HTTPException(status_code=500, detail='更新失败,请稍后重试') if not r: - raise HTTPException(status_code=500, detail='没有更新!') + raise HTTPException(status_code=404, detail='学生不存在或已被删除,更新失败') return {'code':200,'totals':r,'detail':'更新成功'} -@StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息') +@StudentAPI.delete('/students/{stu_id}',response_model= StudelResponse,summary='学生信息删除接口',description='删除学生信息') def del_students(stu_id:int ,db=Depends(get_db)): rows=delete_student_dao( stu_id=stu_id,db=db ) if not rows: - raise HTTPException(status_code=404,detail='对象已被删除') + raise HTTPException(status_code=404,detail='学生不存在或已被删除') return {'code':200,'totals':rows,'detail':'删除成功'} - -@StudentAPI.post('/students',response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息') -def add_students(s:StudentRequest - ,db=Depends(get_db)): - d=s.model_dump(exclude_unset=True) - stu_new=add_student_dao(o=d,db=db) - if not stu_new: - raise HTTPException(status_code=404,detail='添加失败') - return stu_new diff --git a/PythonProject/dao/cla_dao.py b/PythonProject/dao/cla_dao.py index f491b9f..a75a7e4 100644 --- a/PythonProject/dao/cla_dao.py +++ b/PythonProject/dao/cla_dao.py @@ -1,19 +1,20 @@ from model.all_model import ClassManagement from fastapi import HTTPException from schema.cla_schema import claRequest +from datetime import datetime -def add_class_dao(c:claRequest,session): +def add_class_dao(c: claRequest, session): try: data = c.model_dump() d = ClassManagement(**data) session.add(d) + session.commit() + session.refresh(d) + return d except: session.rollback() - return '输入有误,请重新添加班级!' - else: - session.commit() - return '添加成功!' + raise HTTPException(400, "输入有误,请重新添加班级!") def get_class_dao(n,m,session): try: @@ -34,7 +35,7 @@ def update_class_dao(id:int,req:claRequest,session): def delete_class_dao(id:int,session): try: session.query(ClassManagement).filter(ClassManagement.class_id==id,ClassManagement.delete_status==0)\ - .update({'delete_status': 1}) + .update({'delete_status': 1,'delete_time': datetime.now()}) session.commit() except: raise HTTPException(status_code=404, detail="删除异常,请重新核对之后删除!") diff --git a/PythonProject/dao/employment_dao.py b/PythonProject/dao/employment_dao.py index 6168d87..9e0f8b7 100644 --- a/PythonProject/dao/employment_dao.py +++ b/PythonProject/dao/employment_dao.py @@ -2,7 +2,7 @@ from fastapi import HTTPException from model.all_model import Employment_info, ClassManagement, Student_Model from datetime import datetime -def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None,db=None): +def get_emp_dao(db,stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None): q = db.query(Employment_info.emp_id ,Employment_info.stu_id ,Student_Model.stu_name @@ -31,26 +31,10 @@ def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_ q = q.filter(Employment_info.salary <= max_salary) r = q.all() - if not r: - raise HTTPException(status_code=500,detail='查询异常,没有结果!') - return [ - {'emp_id':i.emp_id - ,'stu_id': i.stu_id - ,'stu_name':i.stu_name - ,'class_id':i.class_id - ,'email':i.email - ,'employment_opening_date':i.employment_opening_date - ,'offer_issuance_date':i.offer_issuance_date - ,'company_name':i.company_name - ,'company_address':i.company_address - ,'salary':i.salary - } - for i in r - ] + return r - -def add_emp_dao(o:dict,db): +def add_emp_dao(o,db): try: stu = db.query(Student_Model).filter(Student_Model.stu_id == o['stu_id'] , Student_Model.delete_status == 0).first() @@ -70,11 +54,11 @@ def add_emp_dao(o:dict,db): db.rollback() raise HTTPException(status_code=500, detail=f'新增就业信息失败: {e}') -def update_emp_dao(emp_id:int,db): +def update_emp_dao(emp_id,emp,db): try: - rows = db.query(Employment_info).filter(Employment_info.emp_id == emp_id - ,Employment_info.delete_status == 0).first() - + rows = db.query(Employment_info)\ + .filter(Employment_info.emp_id == emp_id,Employment_info.delete_status == 0)\ + .update(emp) db.commit() return rows except: diff --git a/PythonProject/dao/grade_dao.py b/PythonProject/dao/grade_dao.py index 358645e..2619fac 100644 --- a/PythonProject/dao/grade_dao.py +++ b/PythonProject/dao/grade_dao.py @@ -19,10 +19,26 @@ def get_score_by_stu_exam_order_dao(stu_id:str,exam_order:int,db): def get_grade_dao( stu_id:int , db): - r1 =db.query(WlScore).filter(WlScore.stu_id == stu_id,WlScore.delete_status == 0).all() + r1 =(db.query( WlScore.score_id + ,WlScore.stu_id + ,Student_Model.stu_name + ,WlScore.exam_order + ,WlScore.score + ) + .join(Student_Model,WlScore.stu_id == Student_Model.stu_id) + .filter(WlScore.stu_id == stu_id,WlScore.delete_status == 0,Student_Model.delete_status == 0) + .all()) if r1: - - return r1 + data_list = [] + for row in r1: + data_list.append({ + "score_id": row.score_id, + "stu_id": row.stu_id, + "stu_name": row.stu_name, + "exam_order": row.exam_order, + "score": row.score + }) + return data_list else: raise HTTPException(status_code=404,detail="学生不存在") diff --git a/PythonProject/dao/statistics_dao.py b/PythonProject/dao/statistics_dao.py index c576206..85b59f9 100644 --- a/PythonProject/dao/statistics_dao.py +++ b/PythonProject/dao/statistics_dao.py @@ -23,17 +23,26 @@ def select_all(db): try: l1 = db.query(Student_Model.class_id,Student_Model.gender,func.count(1).label('cnt'))\ .group_by(Student_Model.class_id,Student_Model.gender).all() - all_cnt = sum(i.cnt for i in l1) + + d1 = {} + for i in l1: + if i.class_id not in d1: + d1[i.class_id] = 0 + d1[i.class_id] += i.cnt l2 = [] for i in l1: l2.append({ - "all_cnt": all_cnt, + "all_cnt": d1[i.class_id], "class_id": i.class_id, "gender": i.gender, "cnt": i.cnt }) + if not l2: + raise HTTPException(status_code=404, detail="没有数据") return l2 + except HTTPException: + raise except Exception: raise HTTPException(status_code=500,detail="查询异常") @@ -60,7 +69,10 @@ def select_score(min_score,max_score,db): db = db.filter(WlScore.score >= min_score) if max_score is not None: db = db.filter(WlScore.score <= max_score) - return db.all() + l1 = db.all() + if not l1: + raise HTTPException(status_code=404, detail="没有数据") + return l1 except HTTPException: raise except Exception: @@ -69,12 +81,16 @@ def select_score(min_score,max_score,db): # 实现统计每次考试每个班级的平均分并排序的功能 def select_avg_score(db): try: - l1 = db.query(Student_Model.class_id,func.avg(WlScore.score).label('avg_score'))\ + l1 = db.query(Student_Model.class_id,func.round(func.avg(WlScore.score),1).label('avg_score'))\ .join(Student_Model,WlScore.stu_id == Student_Model.stu_id)\ .filter(WlScore.delete_status==0,Student_Model.delete_status == 0)\ .group_by(Student_Model.class_id)\ - .order_by(func.avg(WlScore.score)).all() + .order_by(func.round(func.avg(WlScore.score),1)).all() + if not l1: + raise HTTPException(status_code=404, detail="没有数据") return l1 + except HTTPException: + raise except Exception: raise HTTPException(status_code=500,detail="查询异常") @@ -96,7 +112,11 @@ def select_salary(db): .filter(Employment_info.delete_status==0,Student_Model.delete_status == 0)\ .order_by(Employment_info.salary.desc())\ .limit(5).all() + if not l1: + raise HTTPException(status_code=404, detail="没有数据") return l1 + except HTTPException: + raise except Exception: raise HTTPException(status_code=500,detail="查询异常") @@ -117,18 +137,60 @@ def select_avg_days(db): Employment_info.offer_issuance_date.isnot(None),)\ .group_by(Student_Model.class_id)\ .order_by(func.avg(func.datediff(Employment_info.offer_issuance_date,Student_Model.admission_date)).desc()).all() + if not l1: + raise HTTPException(status_code=404, detail="没有数据") return l1 + except HTTPException: + raise except Exception: raise HTTPException(status_code=500, detail="查询异常") #统计每个学⽣的就业时⻓ def select_days(db): try: - l2 =db.query(Employment_info.stu_id + l1 =db.query(Employment_info.stu_id ,func.coalesce(func.datediff(Employment_info.offer_issuance_date ,Employment_info.employment_opening_date),0)\ .label("diff_days") ).filter(Employment_info.delete_status == 0).all() - return l2 + if not l1: + raise HTTPException(status_code=404, detail="没有数据") + return l1 + except HTTPException: + raise except Exception: - raise HTTPException(status_code=500, detail="查询异常") \ No newline at end of file + raise HTTPException(status_code=500, detail="查询异常") + + +# 薪资区间分布:统计薪资在5k以下、5k-10k、10k-15k、15k以上的人数分布,反映学生的整体就业质量。 +def classify_salary(db): + try: + a=b=c=d=0 + l=db.query(Employment_info).filter(Employment_info.delete_status==0).all() + for i in l: + if i.salary is None: + continue + elif i.salary<5000: + a+=1 + elif 5000<=i.salary<10000: + b+=1 + elif 10000<=i.salary<15000: + c+=1 + else: + d+=1 + return {'5k以下': a, '5k-10k': b, '10k-15k': c, '15k以上': d} + except: + raise HTTPException(status_code=500,detail='查询异常!') + +#统计每个班的平均就业薪资 +def avg_salary(db): + try: + l=db.query(Student_Model.class_id,func.avg(Employment_info.salary).label('avg_salaries'))\ + .join(Employment_info,Employment_info.stu_id == Student_Model.stu_id)\ + .filter(Employment_info.salary>0,Employment_info.delete_status==0,Student_Model.delete_status==0)\ + .group_by(Student_Model.class_id)\ + .order_by(func.avg(Employment_info.salary).desc()).all() + return [{'class_id':i[0],'avg_salaries':i[1]} for i in l] + except: + raise HTTPException(status_code=500,detail='统计异常!') + diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index 9f755ac..9b528cc 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -1,42 +1,56 @@ -from fastapi import Depends -from util.database import get_db from model.all_model import Student_Model from typing import List, Optional, Dict, Any from datetime import datetime +from sqlalchemy.exc import IntegrityError def add_student_dao(o,db): + if o.get('id_card'): + conflict = (db.query(Student_Model) + .filter(Student_Model.id_card == o['id_card'], + Student_Model.delete_status == 0) + .first()) + if conflict: + return 'conflict' try: - o1 = Student_Model( **o) - db.add(o1) + o2 = Student_Model(**o) + db.add(o2) db.commit() - stu_id=o1.stu_id - return db.query(Student_Model).filter(Student_Model.stu_id == stu_id).first() - except: + return o2 + except IntegrityError: db.rollback() - return None + return 'conflict' + except Exception: + db.rollback() + return 'error' -def delete_student_dao(stu_id,db): +def delete_student_dao(stu_id, db): try: - rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)\ - .update({'delete_status':1,'delete_time': datetime.now()}) + rows = (db.query(Student_Model) + .filter(Student_Model.stu_id == stu_id, + Student_Model.delete_status == 0) + .update({'delete_status': 1, 'delete_time': datetime.now()})) db.commit() - except: - db.rollback() - rows = 0 - finally: return rows + except Exception: + db.rollback() + raise -def update_student_dao(stu_id,update_data,db): +def update_student_dao(stu_id, update_data, db): + if not update_data: + return 0 try: - rows = (db.query( Student_Model ) - .filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0) - .update( update_data )) + rows = (db.query(Student_Model) + .filter(Student_Model.stu_id == stu_id, + Student_Model.delete_status == 0) + .update(update_data)) db.commit() - except: + except IntegrityError: db.rollback() - return False - else: - return rows + return 'conflict' + except Exception: + db.rollback() + return 'error' + return rows def get_student_dao(stu_id:Optional[int] ,stu_name:Optional[str] @@ -45,17 +59,13 @@ def get_student_dao(stu_id:Optional[int] ,page_size: int ,db ) -> tuple[List[Dict[str, Any]], int]: - try: - q = db.query(Student_Model).filter(Student_Model.delete_status == 0) - if stu_id: - q = q.filter(Student_Model.stu_id == stu_id) - if stu_name and stu_name.strip() != "": - q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%")) - if class_id: - q= q.filter(Student_Model.class_id == class_id) - total = q.count() - r = q.offset((page - 1) * page_size).limit(page_size).all() - return r,total - except: - db.rollback() - return [],0 + q = db.query(Student_Model).filter(Student_Model.delete_status == 0) + if stu_id: + q = q.filter(Student_Model.stu_id == stu_id) + if stu_name and stu_name.strip() != "": + q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%")) + if class_id: + q = q.filter(Student_Model.class_id == class_id) + total = q.count() + r = q.offset((page - 1) * page_size).limit(page_size).all() + return r, total diff --git a/PythonProject/dao/teacher_dao.py b/PythonProject/dao/teacher_dao.py index fe97b02..f2f2da3 100644 --- a/PythonProject/dao/teacher_dao.py +++ b/PythonProject/dao/teacher_dao.py @@ -10,7 +10,6 @@ def add_teacher(teacher,session): return teacher except: session.rollback() - # raise HTTPException(status_code=500,detail='添加异常!') return None def get_teacher(id,session): diff --git a/PythonProject/main.py b/PythonProject/main.py index 3474b17..8bee816 100644 --- a/PythonProject/main.py +++ b/PythonProject/main.py @@ -14,7 +14,7 @@ tags_metadata = [ {"name": "学生就业管理模块"}, {"name": "统计分析模块"}, ] -app = FastAPI(title='沃林学⽣管理系统',openapi_tags=tags_metadata) +app = FastAPI(title='沃林学生管理系统',openapi_tags=tags_metadata) app.include_router(StudentAPI) diff --git a/PythonProject/model/all_model.py b/PythonProject/model/all_model.py index 9743859..5408a44 100644 --- a/PythonProject/model/all_model.py +++ b/PythonProject/model/all_model.py @@ -1,5 +1,7 @@ -from sqlalchemy import DATETIME,Column,Integer,String,DATE,ForeignKey,Numeric -from datetime import datetime + + +from sqlalchemy import DATETIME,Column,Integer,String,DATE,ForeignKey,Numeric,Date +from datetime import datetime,date from util.database import Base,engine class Student_Model(Base): @@ -23,6 +25,8 @@ class Student_Model(Base): native_place=Column(String(200),comment='籍贯') + id_card=Column(String(18),unique=True,comment='身份证号') + birthday=Column(DATE,comment='生日') school=Column(String(100),comment='毕业学校') @@ -54,9 +58,9 @@ class ClassManagement(Base): class_id= Column(Integer, primary_key=True, autoincrement=True,comment='班级编号') - class_name = Column(String(100), comment='班级姓名') + class_name = Column(String(100), comment='班级名字') - start_class_date = Column(DATE,comment='开课时间') + start_class_date = Column(Date,default=date.today,comment='开课时间') head_teacher_id= Column(Integer,ForeignKey('wl_teacher.teac_id'),comment='班主任') @@ -145,7 +149,7 @@ class Employment_info(Base): delete_status = Column( Integer,default=0,comment='删除状态:0未删除,1已删除') - delete_time = Column(DATETIME,default=None,comment='删除时间') + delete_time = Column(DATETIME,comment='删除时间') class WlScore(Base): __tablename__ = 'wl_score' diff --git a/PythonProject/schema/employment_schema.py b/PythonProject/schema/employment_schema.py index 4c5dd52..1505868 100644 --- a/PythonProject/schema/employment_schema.py +++ b/PythonProject/schema/employment_schema.py @@ -18,6 +18,21 @@ class EmploymentRequest(BaseModel): raise ValueError('offer下发日期不能早于就业开放日期') return self +class UpdateEmpRequest(BaseModel): + email: Optional[str] = Field(None, description='邮箱') + employment_opening_date:Optional[date] = Field(None, description='就业开放日期') + offer_issuance_date:Optional[date] = Field(None, description='offer下发日期') + company_name:Optional[str] = Field(None, description='公司名称') + company_address:Optional[str] = Field(None, description='公司地址') + salary:Optional[float] = Field(None, description='薪资') + + @model_validator(mode='after') + def check_date(self): + if self.employment_opening_date and self.offer_issuance_date: + if self.employment_opening_date > self.offer_issuance_date: + raise ValueError('offer下发日期不能早于就业开放日期') + return self + class EmploymentResponse(BaseModel): code:int = 200 detail:str = 'OK' diff --git a/PythonProject/schema/grade_schema.py b/PythonProject/schema/grade_schema.py index 4d89cd4..d6babdc 100644 --- a/PythonProject/schema/grade_schema.py +++ b/PythonProject/schema/grade_schema.py @@ -1,5 +1,5 @@ -from pydantic import BaseModel, Field +from pydantic import BaseModel, Field, field_validator class GradeRequest(BaseModel): @@ -7,6 +7,18 @@ class GradeRequest(BaseModel): exam_order:int = Field(description='考核序次') score:float = Field(description='成绩') + @field_validator("score") + def check_score(cls, v): + if not (0 <= v <= 100): + raise ValueError("成绩必须在0~100之间") + return v + + @field_validator("exam_order") + def check_exam_order(cls, v): + if v <= 0: + raise ValueError("考核序次必须大于0") + return v + class GradeResponse(BaseModel): code: int = 200 detail: str = 'OK' @@ -15,6 +27,3 @@ class GradeResponse(BaseModel): exam_order:int score:float -# class GradeUpdate(BaseModel): -# exam_order: int | None = Field( None,description="考核序次") -# score: float | None = Field( None,description="成绩") diff --git a/PythonProject/schema/student_schema.py b/PythonProject/schema/student_schema.py index 786eba5..540b2d6 100644 --- a/PythonProject/schema/student_schema.py +++ b/PythonProject/schema/student_schema.py @@ -9,6 +9,7 @@ class StudentRequest(BaseModel): stu_name:str age:int |None = None gender:str |None = None + id_card:str native_place:str |None = None birthday:date |None = None school:str |None = None @@ -30,23 +31,34 @@ class StudentRequest(BaseModel): raise ValueError ('入学日期不能晚于毕业日期') return self +class StuUpdateRequest(BaseModel): + class_id: int | None = None + stu_name: str | None = None + age: int | None = None + gender: str | None = None + id_card: str | None = None + native_place: str | None = None + birthday: date | None = None + school: str | None = None + major: str | None = None + degree: str | None = None + admission_date: date | None = None + graduation_date: date | None = None + progress: int | None = None -class StudentResponse(BaseModel): - code:int = 200 - detail:str = 'ok' - stu_name:str - age:int - gender:str - progress:int + @field_validator('age') + @classmethod + def check_age(cls, v): + if v is not None and v < 0: + raise ValueError('年龄不能为负数') + return v - @field_serializer('progress') - def int_to_string(self,progress:int): - d1 ={ - 0:'学习中', - 1:'求职中', - 2:'已就业' - } - return d1.get(progress,'暂不明确') + @model_validator(mode='after') + def check_admission_graduation(self) -> Self: + if self.admission_date and self.graduation_date: + if self.admission_date > self.graduation_date: + raise ValueError('入学日期不能晚于毕业日期') + return self class StugetResponse(BaseModel): model_config = ConfigDict(from_attributes=True) @@ -57,6 +69,7 @@ class StugetResponse(BaseModel): stu_name: str | None = None age: int | None = None gender: str | None = None + id_card: str | None = None native_place: str | None = None birthday: date | None = None school: str | None = None @@ -64,21 +77,32 @@ class StugetResponse(BaseModel): degree: str | None = None admission_date: date | None = None graduation_date: date | None = None - progress: int = 0 + progress: int @field_serializer('progress') - def int_to_string(self, progress: int): + def progress_to_label(self, progress): d1 = { 0: '学习中', 1: '求职中', 2: '已就业' } - return d1.get(progress, '暂不明确') + try: + return d1.get(int(progress), '暂不明确') + except (TypeError, ValueError): + return progress -class StudentPageResponse(BaseModel): +class StuPageResponse(BaseModel): code: int = 200 detail: str = "ok" page: int page_size: int totals: int - data: List[StugetResponse] \ No newline at end of file + data: List[StugetResponse] + +class StudelResponse(BaseModel): + code: int = 200 + detail: str = "ok" + totals: int + + +