From 5ced78a009b34d3cdca4dbdd11d31a1de8eb4962 Mon Sep 17 00:00:00 2001 From: yb <739325183@qq.com> Date: Tue, 22 Sep 2026 11:42:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0debug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stu_jiuye/CURD_api.py | 26 +++++++++++++++++--------- stu_jiuye/dao.py | 28 +++++++++++++++++----------- stu_jiuye/model.py | 8 ++++---- stu_jiuye/request.py | 2 +- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/stu_jiuye/CURD_api.py b/stu_jiuye/CURD_api.py index f01936c..3aac5c5 100644 --- a/stu_jiuye/CURD_api.py +++ b/stu_jiuye/CURD_api.py @@ -73,9 +73,13 @@ def updata_stuinfo( #更新数据模块 ,db=Depends(get_db) ): #根据id选择对象 d = up.model_dump(exclude_unset=True) #解析 + d.pop('create_time', None) + d.pop('is_deleted', None) r = update_emp_dao(id=id, update_data=d, db=db) #调用更新函数 + if not d: + raise HTTPException(status_code=400, detail='没有需要更新的字段') if not r: - raise HTTPException(status_code=500, detail='没有更新!') #失败返回错误信息 + raise HTTPException(status_code=404, detail='未找到对应记录') #失败返回错误信息 return {'code': 200, 'totals': r, 'detail': '更新成功'} #成功返回信息 @CURD.delete('/emp/{id}',summary='删除就业信息') @@ -95,12 +99,14 @@ def salarytop5(db=Depends(get_db)): .join(Company, Employments.company_id == Company.id) \ .filter(Employments.is_deleted == 0)\ .order_by(Employments.employment_salary.desc())\ - .offset(0).limit(5) .all() + .limit(5) .all() return [{'学生姓名':i.sname,'学生班级编号':i.class_num,'就业时间':i.offer_recived_time,'公司':employment_company} for i,employment_company in q] @CURD.get('/worktime',summary='学生就业时常') def worktime(db=Depends(get_db)): - q = db.query(Employments).all() + q = db.query(Employments)\ + .filter(Employments.is_deleted==0)\ + .all() return [{'学生姓名':i.sname,'就业时常':f'{i.offer_recived_time-i.employment_open_time}' if i.offer_recived_time and i.employment_open_time else '暂无数据'} for i in q] @@ -109,12 +115,14 @@ def worktime(db=Depends(get_db)): def avgworktime(db=Depends(get_db)): q = db.query(Employments)\ .filter(Employments.employment_open_time.isnot(None) - ,Employments.offer_recived_time.isnot(None))\ + ,Employments.offer_recived_time.isnot(None) + ,Employments.is_deleted==0)\ .all() if not q: return {'平均就业时常': '暂无数据'} - s=0 - for i in q: - s+=(i.offer_recived_time - i.employment_open_time) - avg=s/len(q) - return f'平均就业时常:{avg}' \ No newline at end of file + total = sum((i.offer_recived_time - i.employment_open_time).total_seconds() for i in q) + avg_days=total /len(q)/86400 #一天86400秒 + return { + '人数': len(q), + '平均就业时常(天)': round(avg_days, 2) + } \ No newline at end of file diff --git a/stu_jiuye/dao.py b/stu_jiuye/dao.py index 2efedc6..de1d9b4 100644 --- a/stu_jiuye/dao.py +++ b/stu_jiuye/dao.py @@ -23,6 +23,9 @@ def add_employment_dao(o,db): #新增就业信息函数 def add_company_dao(o,db): #添加公司信息函数 try: o.pop('id', None) #摘除id字段,自增字段无需手动添加 + o.pop('create_time', None) + o.pop('update_time', None) + o.pop('is_deleted', None) o1 = Company( **o) #实例化参数 db.add(o1) #添加数据进入表 except Exception as e: #失败返回 @@ -35,6 +38,9 @@ def add_company_dao(o,db): #添加公司信息函数 def add_address_dao(o,db): #添加地址区域信息函数 try: o.pop('id', None) #摘除id字段,自增字段无需手动添加 + o.pop('create_time', None) + o.pop('update_time', None) + o.pop('is_deleted', None) o1 = Address( **o) #实例化参数 db.add(o1) #添加数据进入表 except Exception as e: #失败返回 @@ -54,7 +60,7 @@ def update_emp_dao(id,update_data,db): #更新数据函数 except Exception as e: print(f'更新失败,{e}') #返回失败信息 db.rollback() #数据回滚 - return False + return 0 else: db.commit() #提交事务,永久保存 return rows @@ -75,8 +81,8 @@ def get_emp_dao( #获取学生 # ,page_size ): #根据选择条件获取信息 q = db.query(Employments,Company.employment_company,Address.somewhere)\ - .join(Company,Employments.company_id==Company.id)\ - .join(Address,Company.address_id==Address.id)\ + .outerjoin(Company,Employments.company_id==Company.id)\ + .outerjoin(Address,Company.address_id==Address.id)\ .filter(Employments.is_deleted == 0) #连接公司表和地址表,筛选未被删除信息 # if stuid: # q = q.filter( Employments.id == stuid ) @@ -103,22 +109,22 @@ def get_emp_dao( #获取学生 return [ {"id": i.id, '学生姓名':i.sname, '学生班级编号':i.class_num, - "公司": employment_company, - "公司区域":somewhere, + "公司": employment_company or '未就业', + "公司区域":somewhere or '-', # "company_id": i.company_id, - "薪资":i.employment_salary, + "薪资":i.employment_salary or '-', # "address_id": i.address_id, "就业开放时间":i.employment_open_time, - "offer下发时间":i.offer_recived_time, - "created_date": i.create_time, - "updated_date": i.update_time, - "is_deleted":i.is_deleted } for i,employment_company,somewhere in r ] #返回信息列表 + "offer下发时间":i.offer_recived_time or '-', + "创建时间": i.create_time, + "更新时间": i.update_time, + "是否删除":i.is_deleted } for i,employment_company,somewhere in r ] #返回信息列表 def delete_emp_dao(id,db): #删除函数 try: rows = db.query(Employments)\ - .filter(Employments.id == id)\ + .filter(Employments.id == id,Employments.is_deleted == 0)\ .update( {'is_deleted':1} ) #软删除,is_deleted返回1 except Exception as e: #失败返回信息 diff --git a/stu_jiuye/model.py b/stu_jiuye/model.py index 7479618..26f9db3 100644 --- a/stu_jiuye/model.py +++ b/stu_jiuye/model.py @@ -46,18 +46,18 @@ class Employments(Base): #学生就 # ) company_id = Column(Integer #公司id,外键匹配公司表 , ForeignKey('Company.id') - , nullable=False + , nullable=True ) # address_id = Column(Integer # , ForeignKey('Address.id') # , nullable=False # ) - employment_salary = Column(Integer) #薪资 + employment_salary = Column(Integer, nullable=True) #薪资 - employment_open_time = Column(DATE) #就业开放时间 + employment_open_time = Column(DATE, nullable=True) #就业开放时间 - offer_recived_time = Column(DATE) #offer下发时间 + offer_recived_time = Column(DATE, nullable=True) #offer下发时间 create_time = Column(DATETIME #创建数据时间 , default=datetime.now diff --git a/stu_jiuye/request.py b/stu_jiuye/request.py index 5b9c041..4161872 100644 --- a/stu_jiuye/request.py +++ b/stu_jiuye/request.py @@ -21,7 +21,7 @@ class EmpResponse(BaseModel): #学生就 class CompRequest(BaseModel): #公司信息请求体模型 employment_company:str - address_id: int | None = None + address_id: int class CompResponse(BaseModel): #公司信息响应体 code: int = 200