|
@@ -261,3 +261,102 @@ async def delete_pattern(
|
|
|
except Exception as e:
|
|
|
traceback.print_exc()
|
|
|
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/import_data")
|
|
|
+async def import_data(
|
|
|
+ body = Depends(remove_xss_json),
|
|
|
+ user_id=Depends(valid_access_token), db: Session = Depends(get_db)):
|
|
|
+ # 获取表结构
|
|
|
+ import os,openpyxl
|
|
|
+ schema_name = "mmyjhd"
|
|
|
+ table_name = "resource_provision_warehouse_info"
|
|
|
+ columns = [{"column_name":"warehouse_name","column_comment":"仓库名称"},
|
|
|
+ {"column_name":"status","column_comment":"状态1启用0关闭"},
|
|
|
+ {"column_name":"contact_person","column_comment":"联系人"},
|
|
|
+ {"column_name":"contact_phone","column_comment":"联系电话"},
|
|
|
+ {"column_name":"address","column_comment":"地址"},
|
|
|
+ {"column_name":"type","column_comment":"类型"},
|
|
|
+ {"column_name":"level","column_comment":"等级"},
|
|
|
+ {"column_name":"storage_dept_name","column_comment":"物资保管部门名称"},
|
|
|
+ {"column_name":"area_name","column_comment":"地区(区县)"},
|
|
|
+ {"column_name":"longitude","column_comment":"经度"},
|
|
|
+ {"column_name":"latitude","column_comment":"纬度"},
|
|
|
+ {"column_name":"area","column_comment":"占地面积(平方米)"},
|
|
|
+ {"column_name":"remark","column_comment":"备注"}]
|
|
|
+ filename = body['filename']
|
|
|
+ if '../' in filename or '/' in filename:
|
|
|
+ return JSONResponse(status_code=400, content={'code': 400, "msg": '警告:禁止篡改文件路径'})
|
|
|
+ file_path = f'/data/upload/mergefile/uploads/{filename}'
|
|
|
+ if not os.path.exists(file_path):
|
|
|
+ return JSONResponse(status_code=404, content={'code': 404, 'msg': f"文件不存在"})
|
|
|
+ # print("文件不存在,请检查路径!")
|
|
|
+ # 读取 Excel 文件
|
|
|
+ try:
|
|
|
+ workbook = openpyxl.load_workbook(file_path)
|
|
|
+ sheet = workbook.active
|
|
|
+ data = pd.read_excel(file_path, header=1)
|
|
|
+ data = data.to_dict(orient='records')
|
|
|
+ except Exception as e:
|
|
|
+ traceback.print_exc()
|
|
|
+ return JSONResponse(status_code=400, content={'code': 400, 'msg': f"接口发生错误:{e}"})
|
|
|
+ # raise HTTPException(status_code=400, detail="Invalid Excel file")
|
|
|
+
|
|
|
+ # 获取字段名和字段备注名
|
|
|
+ column_names = [col["column_name"] for col in columns if col["column_name"]!='id']
|
|
|
+ column_comments = [col["column_comment"] for col in columns if col["column_name"]!='id']
|
|
|
+
|
|
|
+ # 检查第一行是否为字段备注名
|
|
|
+ first_row = [cell.value for cell in sheet[1]]
|
|
|
+ if first_row != column_comments:
|
|
|
+ print("接口发生错误:Excel columns do not match the expected columns")
|
|
|
+
|
|
|
+ return JSONResponse(status_code=400, content={'code': 400, 'msg': f"接口发生错误:Excel columns do not match the expected columns"})
|
|
|
+ # raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
|
|
|
+
|
|
|
+ # 检查第二行是否为字段名
|
|
|
+ second_row = [cell.value for cell in sheet[2]]
|
|
|
+ if second_row != column_names:
|
|
|
+ print("接口发生错误:Excel columns do not match the expected columns")
|
|
|
+ return JSONResponse(status_code=400,
|
|
|
+ content={'code': 400, 'msg': f"接口发生错误:Excel columns do not match the expected columns"})
|
|
|
+
|
|
|
+ # raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
|
|
|
+
|
|
|
+ # 将数据插入到数据库
|
|
|
+ try:
|
|
|
+ # insert_query = text(
|
|
|
+ # f"INSERT INTO `{schema_name}`.`{table_name}` ({', '.join(column_names)}) VALUES ({', '.join([':' + col for col in column_names])})")
|
|
|
+ # for row in sheet.iter_rows(min_row=3, values_only=True):
|
|
|
+ for row in data:
|
|
|
+ infotype = row['type']
|
|
|
+ 'warehouse_type'
|
|
|
+ 'warehouse_level'
|
|
|
+ new_type = ResourceProvisionWarehouseInfo(
|
|
|
+ warehouse_id=new_guid(),
|
|
|
+ warehouse_name=row['warehouse_name'],
|
|
|
+ status=row['status'],
|
|
|
+ contact_person=row['contact_person'],
|
|
|
+ contact_phone=row['contact_phone'],
|
|
|
+ address=row['address'],
|
|
|
+ type=infotype,
|
|
|
+ level=row['level'],
|
|
|
+ storage_dept_id=row['storage_dept_id'],
|
|
|
+ storage_dept_name=row['storage_dept_name'], # body['storage_dept_name']
|
|
|
+ area_name=row['area_name'],
|
|
|
+ longitude=row['longitude'],
|
|
|
+ latitude=row['latitude'],
|
|
|
+ area=row['area'],
|
|
|
+ remark=row['remark'],
|
|
|
+ create_by=user_id
|
|
|
+ )
|
|
|
+ db.add(new_type)
|
|
|
+ db.commit()
|
|
|
+ # db.execute(insert_query, dict(zip(column_names, row)))
|
|
|
+ # db.commit()
|
|
|
+ return {"code":200,"msg": "Data imported successfully"}
|
|
|
+ except Exception as e:
|
|
|
+ db.rollback()
|
|
|
+ traceback.print_exc()
|
|
|
+ return JSONResponse(status_code=500,
|
|
|
+ content={'code': 500, 'msg': f"接口发生错误:{e}"})
|