|
@@ -60,48 +60,77 @@ class UnitListSchema(BaseModel):
|
|
|
class Config:
|
|
|
orm_mode = True
|
|
|
#创建
|
|
|
-@router.post("/")
|
|
|
-def create_units(unit_list_data: UnitListSchema, db: Session = Depends(get_db)):
|
|
|
- units = unit_list_data.units
|
|
|
- if not units: # 确保列表不为空
|
|
|
- raise HTTPException(status_code=400, detail="单位列表不能为空")
|
|
|
- try:
|
|
|
- new_units = [] # 创建一个空列表来存储新对象
|
|
|
- for unit_data in units:
|
|
|
-
|
|
|
- unit_data = unit_data.dict(exclude_none=True)
|
|
|
- unit_data["add_time"] = datetime.now()
|
|
|
- print(unit_data)
|
|
|
- new_unit = RescueUnit(**unit_data)
|
|
|
- db.add(new_unit)
|
|
|
- db.commit()
|
|
|
- db.refresh(new_unit)
|
|
|
- new_units.append(new_unit)
|
|
|
-
|
|
|
- unit_ids = [unit.id for unit in new_units] # 获取所有新对象的ID
|
|
|
- return {"code": 200, "msg": "创建成功", "unit_ids": unit_ids}
|
|
|
- except Exception as e:
|
|
|
- traceback.print_exc()
|
|
|
- db.rollback()
|
|
|
- raise HTTPException(status_code=400, detail=str(e))
|
|
|
+# @router.post("/")
|
|
|
+# def create_units(unit_list_data: UnitListSchema, db: Session = Depends(get_db)):
|
|
|
+# units = unit_list_data.units
|
|
|
+# if not units: # 确保列表不为空
|
|
|
+# raise HTTPException(status_code=400, detail="单位列表不能为空")
|
|
|
+# try:
|
|
|
+# new_units = [] # 创建一个空列表来存储新对象
|
|
|
+# for unit_data in units:
|
|
|
+#
|
|
|
+# unit_data = unit_data.dict(exclude_none=True)
|
|
|
+# unit_data["add_time"] = datetime.now()
|
|
|
+# print(unit_data)
|
|
|
+# new_unit = RescueUnit(**unit_data)
|
|
|
+# db.add(new_unit)
|
|
|
+# db.commit()
|
|
|
+# db.refresh(new_unit)
|
|
|
+# new_units.append(new_unit)
|
|
|
+#
|
|
|
+# unit_ids = [unit.id for unit in new_units] # 获取所有新对象的ID
|
|
|
+# return {"code": 200, "msg": "创建成功", "unit_ids": unit_ids}
|
|
|
+# except Exception as e:
|
|
|
+# traceback.print_exc()
|
|
|
+# db.rollback()
|
|
|
+# raise HTTPException(status_code=400, detail=str(e))
|
|
|
#删除
|
|
|
-@router.delete("/{unit_id}")
|
|
|
-def delete_unit(unit_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
- unit = db.query(RescueUnit).get(unit_id)
|
|
|
- if not unit:
|
|
|
- raise HTTPException(status_code=404, detail="单位不存在")
|
|
|
+# @router.delete("/{unit_id}")
|
|
|
+# def delete_unit(unit_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+# unit = db.query(RescueUnit).get(unit_id)
|
|
|
+# if not unit:
|
|
|
+# raise HTTPException(status_code=404, detail="单位不存在")
|
|
|
+# try:
|
|
|
+# # 更新 is_delete 字段为 1,而不是删除记录
|
|
|
+# unit.is_delete = 1
|
|
|
+# db.commit()
|
|
|
+# return {"code": 200, "msg": "删除成功"}
|
|
|
+# except Exception as e:
|
|
|
+# traceback.print_exc()
|
|
|
+# db.rollback()
|
|
|
+# raise HTTPException(status_code=400, detail=str(e))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@router.delete('/{unit_id}')
|
|
|
+async def delete(
|
|
|
+ request: Request,
|
|
|
+ unit_id: int,
|
|
|
+ db: Session = Depends(get_db_share),
|
|
|
+ body=Depends(remove_xss_json),
|
|
|
+ user_id=Depends(valid_access_token)
|
|
|
+):
|
|
|
try:
|
|
|
- # 更新 is_delete 字段为 1,而不是删除记录
|
|
|
- unit.is_delete = 1
|
|
|
+ query = db.query(RescueTeams)
|
|
|
+ query = query.filter(RescueTeams.id == unit_id)
|
|
|
+ query = query.filter(RescueTeams.del_flag != '2')
|
|
|
+ unit_to_delete = query.first()
|
|
|
+ if not unit_to_delete:
|
|
|
+ return JSONResponse(status_code=404, content={"code": 404, "msg": "该id不存在"})
|
|
|
+ unit_to_delete.update_by = user_id
|
|
|
+ unit_to_delete.update_time = datetime.now()
|
|
|
+ unit_to_delete.del_flag = '2'
|
|
|
db.commit()
|
|
|
- return {"code": 200, "msg": "删除成功"}
|
|
|
+ return {
|
|
|
+ "code": 200,
|
|
|
+ "msg": "菜单删除成功"
|
|
|
+ }
|
|
|
except Exception as e:
|
|
|
traceback.print_exc()
|
|
|
- db.rollback()
|
|
|
- raise HTTPException(status_code=400, detail=str(e))
|
|
|
-
|
|
|
-
|
|
|
|
|
|
+ db.rollback()
|
|
|
+ return JSONResponse(status_code=404, content={"code": 404, "msg": str(e)})
|
|
|
# class UnitListQueryParams(BaseModel):
|
|
|
# page: int = Field(default=1, gt=0)
|
|
|
# page_size: int = Field(default=10, gt=0)
|
|
@@ -110,11 +139,11 @@ def delete_unit(unit_id: int, db: Session = Depends(get_db), user_id=Depends(val
|
|
|
@router.get("/list")
|
|
|
def get_units(page: int = Query(default=1, gt=0),
|
|
|
pageSize: int = Query(default=10, gt=0),
|
|
|
- db: Session = Depends(get_db),
|
|
|
+ db: Session = Depends(get_db_share),
|
|
|
user_id=Depends(valid_access_token)):
|
|
|
# 应用过滤条件,仅查询未被删除的单位
|
|
|
- data_query = db.query(RescueUnit).filter(RescueUnit.is_delete == 0)
|
|
|
- data_query = data_query.order_by(RescueUnit.add_time.desc())
|
|
|
+ data_query = db.query(RescueTeams).filter(RescueTeams.del_flag == '0')
|
|
|
+ data_query = data_query.order_by(RescueTeams.create_time.desc())
|
|
|
# 计算总数
|
|
|
total_count = data_query.count()
|
|
|
|
|
@@ -123,7 +152,25 @@ def get_units(page: int = Query(default=1, gt=0),
|
|
|
units = data_query.offset(offset).limit(pageSize).all()
|
|
|
|
|
|
# 构造结果
|
|
|
- result_items = [unit.to_dict() for unit in units]
|
|
|
+ result_items = [{'id' :unit.id,
|
|
|
+ 'name' :unit.name,
|
|
|
+ 'team_type' :unit.team_type,
|
|
|
+ 'professional_category' :unit.professional_category,
|
|
|
+ 'team_category' :unit.team_category,
|
|
|
+ 'team_attribute' :unit.team_attribute,
|
|
|
+ 'team_level' :unit.team_level,
|
|
|
+ 'affiliated_unit' :unit.affiliated_unit,
|
|
|
+ 'leader_name' :unit.leader_name,
|
|
|
+ 'leader_phone' :unit.leader_phone,
|
|
|
+ 'city' :unit.city,
|
|
|
+ 'district' :unit.district,
|
|
|
+ 'main_equipment' :unit.main_equipment,
|
|
|
+ 'number_of_people' :unit.number_of_people,
|
|
|
+ 'address' :unit.address,
|
|
|
+ 'description' :unit.description,
|
|
|
+ 'longitude' :unit.longitude,
|
|
|
+ 'latitude' :unit.latitude,
|
|
|
+ 's_last_updatetime' :unit.s_last_updatetime} for unit in units]
|
|
|
|
|
|
result = {
|
|
|
"code": 200,
|
|
@@ -140,36 +187,55 @@ def get_units(page: int = Query(default=1, gt=0),
|
|
|
|
|
|
#查询详情
|
|
|
@router.get("/detail/{unit_id}")
|
|
|
-def get_unit_by_id(unit_id: int, db: Session = Depends(get_db), user_id=Depends(valid_access_token)):
|
|
|
+def get_unit_by_id(unit_id: int, db: Session = Depends(get_db_share), user_id=Depends(valid_access_token)):
|
|
|
# unit = db.query(Unit).filter(Unit.is_delete == 0).get(unit_id)
|
|
|
- unit = db.query(RescueUnit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+ unit = db.query(RescueTeams).filter_by(id=unit_id, del_flag='0').first()
|
|
|
|
|
|
if not unit:
|
|
|
raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
- return {"code": 200, "msg": "查询成功", "unit": unit.to_dict()}
|
|
|
+ return {"code": 200, "msg": "查询成功", "unit": {'id' :unit.id,
|
|
|
+ 'name' :unit.name,
|
|
|
+ 'team_type' :unit.team_type,
|
|
|
+ 'professional_category' :unit.professional_category,
|
|
|
+ 'team_category' :unit.team_category,
|
|
|
+ 'team_attribute' :unit.team_attribute,
|
|
|
+ 'team_level' :unit.team_level,
|
|
|
+ 'affiliated_unit' :unit.affiliated_unit,
|
|
|
+ 'leader_name' :unit.leader_name,
|
|
|
+ 'leader_phone' :unit.leader_phone,
|
|
|
+ 'city' :unit.city,
|
|
|
+ 'district' :unit.district,
|
|
|
+ 'main_equipment' :unit.main_equipment,
|
|
|
+ 'number_of_people' :unit.number_of_people,
|
|
|
+ 'address' :unit.address,
|
|
|
+ 'description' :unit.description,
|
|
|
+ 'longitude' :unit.longitude,
|
|
|
+ 'latitude' :unit.latitude,
|
|
|
+ 's_last_updatetime' :unit.s_last_updatetime}}
|
|
|
|
|
|
#修改
|
|
|
@router.put("/edit/{unit_id}")
|
|
|
def update_unit(unit_id: int, update_data: UnitSchema, db: Session = Depends(get_db),
|
|
|
user_id=Depends(valid_access_token)):
|
|
|
+ return {"code": 404, "msg": "数据由数据整理团队提供,不能修改", "unit": None}
|
|
|
# 根据id和is_delete字段获取单位
|
|
|
- unit = db.query(RescueUnit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
- if not unit:
|
|
|
- raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
-
|
|
|
- try:
|
|
|
- # 更新非空字段,排除id字段
|
|
|
- for key, value in update_data.dict(exclude_none=True).items():
|
|
|
- # 确保不更新id字段
|
|
|
- if key != 'id':
|
|
|
- setattr(unit, key, value)
|
|
|
- db.commit()
|
|
|
- db.refresh(unit)
|
|
|
- return {"code": 200, "msg": "更新成功", "unit": unit.to_dict()}
|
|
|
- except Exception as e:
|
|
|
- traceback.print_exc()
|
|
|
- db.rollback()
|
|
|
- raise HTTPException(status_code=400, detail=str(e))
|
|
|
+ # unit = db.query(RescueUnit).filter_by(id=unit_id, is_delete=0).first()
|
|
|
+ # if not unit:
|
|
|
+ # raise HTTPException(status_code=404, detail="单位不存在或已被标记为删除")
|
|
|
+ #
|
|
|
+ # try:
|
|
|
+ # # 更新非空字段,排除id字段
|
|
|
+ # for key, value in update_data.dict(exclude_none=True).items():
|
|
|
+ # # 确保不更新id字段
|
|
|
+ # if key != 'id':
|
|
|
+ # setattr(unit, key, value)
|
|
|
+ # db.commit()
|
|
|
+ # db.refresh(unit)
|
|
|
+ # return {"code": 200, "msg": "更新成功", "unit": unit.to_dict()}
|
|
|
+ # except Exception as e:
|
|
|
+ # traceback.print_exc()
|
|
|
+ # db.rollback()
|
|
|
+ # raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
# 导入
|
|
|
@router.post('/import')
|
|
@@ -263,70 +329,3 @@ async def import_doc(
|
|
|
|
|
|
|
|
|
|
|
|
-@router.get("/export")
|
|
|
-async def get_list_info(
|
|
|
- request: Request,
|
|
|
- keyword: str = Query(None),
|
|
|
- auth_user: AuthUser = Depends(find_auth_user),
|
|
|
- db: Session = Depends(get_db_share),
|
|
|
- mmdb:Session = Depends(get_db)
|
|
|
-):
|
|
|
- try:
|
|
|
- query = db.query(EmergencyExpertInfo)
|
|
|
- if keyword:
|
|
|
- query = query.filter(or_(EmergencyExpertInfo.name.like(f'%{keyword}%'),
|
|
|
- EmergencyExpertInfo.unit.like(f'%{keyword}%'),
|
|
|
- EmergencyExpertInfo.professional_title.like(f'%{keyword}%'),
|
|
|
- EmergencyExpertInfo.professional_group.like(f'%{keyword}%'),
|
|
|
- EmergencyExpertInfo.professional_field.like(f'%{keyword}%')))
|
|
|
- data = query.all()
|
|
|
- data = [{'序号':info.id,
|
|
|
- '姓名':info.name,
|
|
|
- '所属区县':info.county,
|
|
|
- '专家类型':info.expert_type,
|
|
|
- '荣誉称号':info.honorary_title,
|
|
|
- '单位' :info.unit,
|
|
|
- '职位' :info.position,
|
|
|
- '职称' :info.professional_title,
|
|
|
- '擅长事故类型' :info.specialty,
|
|
|
- '救援经历' :info.rescue_experience,
|
|
|
- '出生日期' :info.birth_date,
|
|
|
- '工作时间' :info.work_start_date,
|
|
|
- '发证日期' :info.certificate_issue_date,
|
|
|
- '专业分组' :info.professional_group,
|
|
|
- '专业领域' :info.professional_field,
|
|
|
- '工作电话' :info.work_phone,
|
|
|
- '住宅电话' :info.home_phone,
|
|
|
- '移动电话' :info.mobile_phone,
|
|
|
- '电子邮箱' :info.email,
|
|
|
- '联系地址' :info.contact_address,
|
|
|
- '经度' :info.longitude,
|
|
|
- '纬度' :info.latitude} for info in data]
|
|
|
- # 返回结果
|
|
|
- import pandas as pd
|
|
|
- from io import BytesIO
|
|
|
- # 将查询结果转换为 DataFrame
|
|
|
- df = pd.DataFrame(data)
|
|
|
-
|
|
|
- # 将 DataFrame 导出为 Excel 文件
|
|
|
- output = BytesIO()
|
|
|
- with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
|
|
- df.to_excel(writer, index=False)
|
|
|
-
|
|
|
- # 设置响应头
|
|
|
- output.seek(0)
|
|
|
- from urllib.parse import quote
|
|
|
- encoded_filename = f'应急专家管理导出{datetime.now().strftime("%Y%m%d%H%mi%s")}.xlsx'
|
|
|
- encoded_filename = quote(encoded_filename, encoding='utf-8')
|
|
|
- headers = {
|
|
|
- 'Content-Disposition': f'attachment; filename*=UTF-8\'\'{encoded_filename}',
|
|
|
- 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
- }
|
|
|
-
|
|
|
- db_czrz.log(mmdb, auth_user, "应急专家管理", f"应急专家管理导出数据成功", request.client.host)
|
|
|
-
|
|
|
- # 返回文件流
|
|
|
- return StreamingResponse(output, headers=headers)
|
|
|
- except Exception as e:
|
|
|
- traceback.print_exc()
|
|
|
- return JSONResponse(status_code=500, content={'msg': f"Internal server error: {str(e)}", 'code': 500})
|