Merge remote-tracking branch 'origin/conding' into conding
# Conflicts: # PythonProject/api/statistics_api.py
This commit is contained in:
@@ -3,17 +3,17 @@ from fastapi import HTTPException
|
||||
from schema.cla_schema import claRequest
|
||||
|
||||
|
||||
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:
|
||||
@@ -22,7 +22,7 @@ def get_class_dao(n,m,session):
|
||||
except:
|
||||
raise HTTPException(status_code=406,detail="输入查询信息有误,请重新输入!")
|
||||
|
||||
def update_class_dao(id:int,req:ClassManagement,session):
|
||||
def update_class_dao(id:int,req:claRequest,session):
|
||||
try:
|
||||
session.query(ClassManagement).filter(ClassManagement.class_id==id)\
|
||||
.filter(ClassManagement.delete_status==0).update(req.model_dump(exclude_unset=True))
|
||||
|
||||
@@ -1,69 +1,67 @@
|
||||
from fastapi import HTTPException, APIRouter
|
||||
from fastapi import HTTPException
|
||||
from model.all_model import Employment_info, ClassManagement, Student_Model
|
||||
from datetime import datetime
|
||||
|
||||
emp_api = APIRouter()
|
||||
def get_emp_dao(stu_id=None,class_id=None,company_name=None,min_salary=None,max_salary=None,db=None):
|
||||
try:
|
||||
q = db.query(Employment_info).\
|
||||
join(Student_Model,Employment_info.stu_id == Student_Model.stu_id).\
|
||||
join(ClassManagement,Student_Model.class_id == ClassManagement.class_id).\
|
||||
filter(Employment_info.delete_status == 0).\
|
||||
filter(Student_Model.delete_status == 0).\
|
||||
filter(ClassManagement.delete_status == 0)
|
||||
if stu_id:
|
||||
q = q.filter(Employment_info.stu_id == stu_id)
|
||||
if class_id:
|
||||
q = q.filter(ClassManagement.class_id == class_id)
|
||||
if company_name:
|
||||
q = q.filter(Employment_info.company_name == company_name)
|
||||
if min_salary:
|
||||
q = q.filter(Employment_info.salary >= min_salary)
|
||||
if max_salary:
|
||||
q = q.filter(Employment_info.salary <= max_salary)
|
||||
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
|
||||
,Student_Model.class_id
|
||||
,Employment_info.email
|
||||
,Employment_info.employment_opening_date
|
||||
,Employment_info.offer_issuance_date
|
||||
,Employment_info.company_name
|
||||
,Employment_info.company_address
|
||||
,Employment_info.salary)\
|
||||
.join(Student_Model,Employment_info.stu_id == Student_Model.stu_id)\
|
||||
.join(ClassManagement,Student_Model.class_id == ClassManagement.class_id)\
|
||||
.filter(Employment_info.delete_status == 0)\
|
||||
.filter(Student_Model.delete_status == 0)\
|
||||
.filter(ClassManagement.delete_status == 0)
|
||||
|
||||
if stu_id:
|
||||
q = q.filter(Employment_info.stu_id == stu_id)
|
||||
if class_id:
|
||||
q = q.filter(ClassManagement.class_id == class_id)
|
||||
if company_name:
|
||||
q = q.filter(Employment_info.company_name == company_name)
|
||||
if min_salary:
|
||||
q = q.filter(Employment_info.salary >= min_salary)
|
||||
if max_salary:
|
||||
q = q.filter(Employment_info.salary <= max_salary)
|
||||
|
||||
r = q.all()
|
||||
if not r:
|
||||
raise HTTPException(status_code=500,detail='查询异常,没有结果!')
|
||||
return r
|
||||
|
||||
r = q.all()
|
||||
|
||||
if r:
|
||||
return [{'emp_id':i.emp_id
|
||||
,'stu_id': i.stu_id
|
||||
,'stu_name':i.Student_Model.stu_name
|
||||
,'class_id':i.Student_Model.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
|
||||
]
|
||||
except:
|
||||
raise HTTPException(status_code=404,detail='信息不存在!')
|
||||
|
||||
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()
|
||||
stu = db.query(Student_Model).filter(Student_Model.stu_id == o['stu_id']
|
||||
, Student_Model.delete_status == 0).first()
|
||||
if not stu:
|
||||
raise HTTPException(status_code=404, detail='该学生不存在,无法添加就业信息')
|
||||
|
||||
exist = db.query(Employment_info).filter(Employment_info.stu_id == o['stu_id'],
|
||||
Employment_info.delete_status == 0).first()
|
||||
exist = (db.query(Employment_info).filter(Employment_info.stu_id == o['stu_id']
|
||||
,Employment_info.delete_status == 0).first())
|
||||
if exist:
|
||||
raise HTTPException(status_code=409, detail='该学生已有就业信息,请勿重复增加!')
|
||||
|
||||
e1 = Employment_info(**o)
|
||||
db.add(e1)
|
||||
db.commit()
|
||||
return o
|
||||
except HTTPException:
|
||||
raise
|
||||
return e1
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f'新增就业信息失败: {e}')
|
||||
|
||||
def update_emp_dao(stu_id,o,db):
|
||||
def update_emp_dao(emp_id,emp,db):
|
||||
try:
|
||||
rows = db.query(Employment_info).filter(Employment_info.stu_id == stu_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:
|
||||
@@ -71,10 +69,11 @@ def update_emp_dao(stu_id,o,db):
|
||||
raise HTTPException(status_code=404,detail='该就业记录已存在')
|
||||
|
||||
|
||||
def delete_emp_dao(stu_id,db):
|
||||
def delete_emp_dao(emp_id:int,db):
|
||||
try:
|
||||
rows = db.query(Employment_info)\
|
||||
.filter(Employment_info.stu_id == stu_id,Employment_info.delete_status == 0)\
|
||||
.filter(Employment_info.emp_id == emp_id
|
||||
,Employment_info.delete_status == 0)\
|
||||
.update({'delete_status':1,'delete_time': datetime.now()})
|
||||
db.commit()
|
||||
except:
|
||||
|
||||
@@ -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="学生不存在")
|
||||
|
||||
|
||||
@@ -23,7 +23,17 @@ 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()
|
||||
return l1
|
||||
all_cnt = sum(i.cnt for i in l1)
|
||||
|
||||
l2 = []
|
||||
for i in l1:
|
||||
l2.append({
|
||||
"all_cnt": all_cnt,
|
||||
"class_id": i.class_id,
|
||||
"gender": i.gender,
|
||||
"cnt": i.cnt
|
||||
})
|
||||
return l2
|
||||
except Exception:
|
||||
raise HTTPException(status_code=500,detail="查询异常")
|
||||
|
||||
@@ -32,12 +42,23 @@ def select_score(min_score,max_score,db):
|
||||
try:
|
||||
if min_score is None and max_score is None:
|
||||
raise HTTPException(status_code=400, detail="请至少传入一个参数")
|
||||
db = db.query(Student_Model,WlScore.score,WlScore.score)\
|
||||
db = db.query(WlScore.score
|
||||
,Student_Model.stu_name
|
||||
,Student_Model.age
|
||||
,Student_Model.gender
|
||||
,Student_Model.native_place
|
||||
,Student_Model.school
|
||||
,Student_Model.major
|
||||
,Student_Model.degree
|
||||
,Student_Model.admission_date
|
||||
,Student_Model.graduation_date
|
||||
,Student_Model.progress
|
||||
)\
|
||||
.join(WlScore,WlScore.stu_id == Student_Model.stu_id)\
|
||||
.filter(WlScore.delete_status==0,Student_Model.delete_status == 0)
|
||||
if min_score is not None:
|
||||
db = db.filter(WlScore.score >= min_score)
|
||||
if min_score is not None:
|
||||
if max_score is not None:
|
||||
db = db.filter(WlScore.score <= max_score)
|
||||
return db.all()
|
||||
except HTTPException:
|
||||
@@ -60,7 +81,17 @@ def select_avg_score(db):
|
||||
# 实现统计薪资最高的前五名的学员信息
|
||||
def select_salary(db):
|
||||
try:
|
||||
l1 = db.query(Student_Model,Employment_info.salary)\
|
||||
l1 = db.query(Employment_info.salary
|
||||
,Student_Model.stu_name
|
||||
,Student_Model.age
|
||||
,Student_Model.gender
|
||||
,Student_Model.native_place
|
||||
,Student_Model.school
|
||||
,Student_Model.major
|
||||
,Student_Model.degree
|
||||
,Student_Model.admission_date
|
||||
,Student_Model.graduation_date
|
||||
,Student_Model.progress)\
|
||||
.join(Employment_info,Employment_info.stu_id==Student_Model.stu_id)\
|
||||
.filter(Employment_info.delete_status==0,Student_Model.delete_status == 0)\
|
||||
.order_by(Employment_info.salary.desc())\
|
||||
@@ -70,7 +101,7 @@ def select_salary(db):
|
||||
raise HTTPException(status_code=500,detail="查询异常")
|
||||
|
||||
# 实现统计每个班级的平均就业时长
|
||||
def select_days(db):
|
||||
def select_avg_days(db):
|
||||
try:
|
||||
l1 = db.query(Student_Model.class_id,
|
||||
func.coalesce(
|
||||
@@ -84,27 +115,20 @@ def select_days(db):
|
||||
Employment_info.delete_status == 0,
|
||||
Employment_info.employment_opening_date.isnot(None),
|
||||
Employment_info.offer_issuance_date.isnot(None),)\
|
||||
.group_by( Student_Model.class_id)\
|
||||
.group_by(Student_Model.class_id)\
|
||||
.order_by(func.avg(func.datediff(Employment_info.offer_issuance_date,Student_Model.admission_date)).desc()).all()
|
||||
return l1
|
||||
except Exception:
|
||||
raise HTTPException(status_code=500, detail="查询异常")
|
||||
|
||||
#统计每个学⽣的就业时⻓
|
||||
def select_days2(db):
|
||||
def select_days(db):
|
||||
try:
|
||||
l2 =db.query(
|
||||
Employment_info.stu_id
|
||||
,Employment_info.employment_opening_date
|
||||
,Employment_info.offer_issuance_date
|
||||
,func.datediff(
|
||||
Employment_info.offer_issuance_date
|
||||
,Employment_info.employment_opening_date
|
||||
)
|
||||
.label("employment_duration_day")
|
||||
).filter(
|
||||
Employment_info.delete_status == 0
|
||||
).all()
|
||||
l2 =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
|
||||
except Exception:
|
||||
raise HTTPException(status_code=500, detail="查询异常")
|
||||
|
||||
@@ -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:
|
||||
@@ -28,14 +37,14 @@ def delete_student_dao(stu_id,db):
|
||||
|
||||
def update_student_dao(stu_id,update_data,db):
|
||||
try:
|
||||
rows = (db.query( Student_Model )
|
||||
.filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
|
||||
.update( update_data ))
|
||||
db.commit()
|
||||
rows=(db.query(Student_Model)
|
||||
.fiter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
|
||||
.update(update_data))
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return rows
|
||||
|
||||
def get_student_dao(stu_id:Optional[int]
|
||||
|
||||
Reference in New Issue
Block a user