Merge remote-tracking branch 'origin/conding' into conding

This commit is contained in:
2026-09-22 15:01:17 +08:00
10 changed files with 123 additions and 66 deletions
+9 -18
View File
@@ -1,19 +1,11 @@
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='学生编号')
,db=Depends(get_db)):
e1 = employment_dao.get_emp_dao(stu_id=stu_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)):
@@ -22,8 +14,8 @@ def get_emp_info2(class_id: Optional[int]=Path(description='班级编号')
return e2
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/class/{stu_id}',response_model=list[EmploymentResponse],summary='按照学⽣编号,公司名字,⼯资范围查询学⽣就业信息')
def get_emp_info3(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,11 @@ 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
l1 = employment_dao.get_emp_dao(db, stu_id=stu_id
, company_name=company_name
, min_salary=min_salary
,max_salary=max_salary
,db=db)]
, max_salary=max_salary)
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 +37,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':'更新成功!'}
+1 -1
View File
@@ -19,7 +19,7 @@ def get_grade(stu_id: int,db=Depends(get_db)):
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)
+5
View File
@@ -40,3 +40,8 @@ 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
+16 -7
View File
@@ -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
@@ -32,11 +32,16 @@ def update_students(s:StudentRequest
,stu_id:int
,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='更新内容不能为空!')
raise HTTPException(status_code=400, detail='更新内容不能为空')
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
if r == 'conflict':
raise HTTPException(status_code=409, detail = '身份证号已被其他学生占用')
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='删除学生信息')
@@ -51,7 +56,11 @@ def del_students(stu_id:int
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
if not d:
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
+7 -20
View File
@@ -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
@@ -33,24 +33,11 @@ def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_
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 +57,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:
+22
View File
@@ -132,3 +132,25 @@ def select_days(db):
return l2
except Exception:
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='查询异常!')
+34 -12
View File
@@ -1,19 +1,28 @@
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:
db.refresh(o2) # 回填自增的 stu_id
return o2
except IntegrityError:
db.rollback()
return None
return 'conflict'
except Exception:
db.rollback()
return 'error'
def delete_student_dao(stu_id,db):
try:
@@ -27,15 +36,28 @@ def delete_student_dao(stu_id,db):
return rows
def update_student_dao(stu_id,update_data,db):
if update_data.get('id_card'):
conflict = (db.query(Student_Model)
.filter(Student_Model.id_card == update_data['id_card'],
Student_Model.stu_id != stu_id,
Student_Model.delete_status == 0)
.first())
if conflict:
return 'conflict'
try:
rows = (db.query(Student_Model)
.filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
.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 'conflict'
except Exception:
db.rollback()
return 'error'
return rows
def get_student_dao(stu_id:Optional[int]
+2
View File
@@ -25,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='毕业学校')
+15
View File
@@ -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'
+4
View File
@@ -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
@@ -57,6 +58,7 @@ class StugetResponse(BaseModel):
stu_name: str | None = None
age: int | None = None
gender: str | None = None
id_card: str
native_place: str | None = None
birthday: date | None = None
school: str | None = None
@@ -82,3 +84,5 @@ class StudentPageResponse(BaseModel):
page_size: int
totals: int
data: List[StugetResponse]