feat: 添加更多文件类型支持,添加全部通过功能

This commit is contained in:
皓月归尘 2025-03-04 03:35:52 +08:00
parent 21c87d49d7
commit a54693a7bf
5 changed files with 125 additions and 16 deletions

View File

@ -36,31 +36,47 @@ codeAPI = APIRouter(
)
@codeAPI.get("/template", summary="获取上传编码模板")
@codeAPI.get("/template/{type}", summary="获取上传编码模板")
@Log(title="获取上传编码模板", business_type=BusinessType.SELECT)
@Auth(permission_list=["code:btn:uploadTemplate"])
async def get_upload_template(request: Request, current_user=Depends(LoginController.get_current_user)):
template_path = os.path.join(os.path.abspath(os.getcwd()), 'assets', 'templates', '上传模版.xlsx')
async def get_upload_template(request: Request, type: str = Path(description="文件类型"),
current_user=Depends(LoginController.get_current_user)):
if type not in ["xlsx", "xls", "csv"]:
raise ServiceException(message="文件类型错误!")
template_path = os.path.join(os.path.abspath(os.getcwd()), 'assets', 'templates', f'上传模版.{type}')
media_type = {
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xls": "application/vnd.ms-excel",
"csv": "text/csv"
}.get(type)
if not os.path.exists(template_path):
raise ServiceException(message="文件不存在!")
return FileResponse(
path=template_path,
filename="上传模版.xlsx",
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
filename=f"上传模版.{type}",
media_type=media_type
)
@codeAPI.get("/queryTemplate", summary="获取查询编码模板")
@codeAPI.get("/queryTemplate/{type}", summary="获取查询编码模板")
@Log(title="获取查询编码模板", business_type=BusinessType.SELECT)
@Auth(permission_list=["code:btn:queryTemplate"])
async def get_query_template(request: Request, current_user=Depends(LoginController.get_current_user)):
template_path = os.path.join(os.path.abspath(os.getcwd()), 'assets', 'templates', '查询模版.xlsx')
async def get_query_template(request: Request, type: str = Path(description="文件类型"),
current_user=Depends(LoginController.get_current_user)):
if type not in ["xlsx", "xls", "csv"]:
raise ServiceException(message="文件类型错误!")
template_path = os.path.join(os.path.abspath(os.getcwd()), 'assets', 'templates', f'查询模版.{type}')
if not os.path.exists(template_path):
raise ServiceException(message="文件不存在!")
media_type = {
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xls": "application/vnd.ms-excel",
"csv": "text/csv"
}.get(type)
return FileResponse(
path=template_path,
filename="查询模版.xlsx",
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
filename=f"查询模版.{type}",
media_type=media_type
)
@ -97,7 +113,17 @@ async def add_code_by_file(request: Request, id: str = Path(description="文件I
uploader_id = await file.first().values(id="uploader__id")
if str(uploader_id["id"]) == user_id:
try:
df = pd.read_excel(file.absolute_path, dtype={"code": str})
media_type = {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":"excel",
"application/vnd.ms-excel":"excel",
"text/csv": "csv"
}
if not media_type.get(file.file_type):
raise ServiceException(message="文件类型错误!")
if media_type.get(file.file_type) == "excel":
df = pd.read_excel(file.absolute_path, dtype={"code": str})
else:
df = pd.read_csv(file.absolute_path, dtype={"code": str})
df["code"] = df["code"].astype(str).str.zfill(8)
for index, row in df.iterrows():
row["code"] = row["code"].replace(".", "").replace("/", "").replace("_", "").replace("-",
@ -370,7 +396,17 @@ async def get_code_list(request: Request,
query_text = ""
query_count = 0
dataList = []
df = pd.read_excel(file.absolute_path)
media_type = {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "excel",
"application/vnd.ms-excel": "excel",
"text/csv": "csv"
}
if not media_type.get(file.file_type):
raise ServiceException(message="文件类型错误!")
if media_type.get(file.file_type) == "excel":
df = pd.read_excel(file.absolute_path, dtype={"code": str})
else:
df = pd.read_csv(file.absolute_path, dtype={"code": str})
for index, row in df.iterrows():
query_count += 1
query_text += row["text"] + "\n"
@ -821,13 +857,14 @@ async def feedback_audit(request: Request,
)
if code:
await request.app.state.es.create(index=ElasticSearchConfig.ES_INDEX,
id=code.id,
body={"id": code.id,
"code": code.code,
"description": code.description})
id=code.id,
body={"id": code.id,
"code": code.code,
"description": code.description})
await feedback.save()
return Response.success(msg="审核成功!")
@codeAPI.delete("/deleteCodeImport/{id}", response_class=JSONResponse, response_model=BaseResponse,
summary="删除编码导入")
@codeAPI.post("/deleteCodeImport/{id}", response_class=JSONResponse, response_model=BaseResponse,
@ -991,3 +1028,44 @@ async def code_import_audit(request: Request, params: UpdateCodeImportStatusPara
success, failed = await async_bulk(request.app.state.es, actions)
logger.info(f"成功导入 {success} 条数据,失败 {failed}")
return Response.success()
@codeAPI.put("/codeImportAudit/all", response_class=JSONResponse, response_model=BaseResponse, summary="全部审核通过")
@codeAPI.post("/codeImportAudit/all", response_class=JSONResponse, response_model=BaseResponse, summary="全部审核通过")
@Log(title="全部审核通过", business_type=BusinessType.UPDATE)
@Auth(permission_list=["code:btn:codeImportAuditAll"])
async def code_import_audit_all(request: Request, current_user: dict = Depends(LoginController.get_current_user)):
sub_departments = current_user.get("sub_departments")
actions = []
if codeImports := await CodeImport.filter(user__department__id__in=sub_departments, del_flag=1):
for codeImport in codeImports:
codeImport.status = 1
code = codeImport.code.replace(".", "").replace("/", "").replace("_", "").replace("-",
"").replace(
":", "").replace("", "").replace("", "").strip()
user_id = current_user.get("id")
codeInfo = await Code.create(
code=code,
description=codeImport.description,
user_id=user_id,
)
if codeInfo:
# 构造 Bulk 导入数据
actions.append(
{
"_index": ElasticSearchConfig.ES_INDEX,
"_id": codeInfo.id, # 以 code 作为 ID
"_source": {
"id": codeInfo.id,
"code": codeInfo.code,
"description": codeInfo.description
}
}
)
await codeImport.save()
if await request.app.state.es.indices.exists(index=ElasticSearchConfig.ES_INDEX):
await request.app.state.es.indices.create(index=ElasticSearchConfig.ES_INDEX, ignore=400)
success, failed = await async_bulk(request.app.state.es, actions)
logger.info(f"成功导入 {success} 条数据,失败 {failed}")
return Response.success()
return Response.error()

View File

@ -0,0 +1,13 @@
code,description
01012100,Live purebred breeding horses
01012900,Live horses other than purebred breeding horses
01013000,Live asses
01019030,Mules and hinnies imported for immediate slaughter
01019040,Mules and hinnies not imported for immediate slaughter
01022100,Live purebred breeding cattle
01022920,Cows imported specially for dairy purposes
01022940,Live cattle other than purebred or those imported for dairy purposes
01023100,Live purebred breeding buffalo
01023900,"Live buffalo, other than purebred breeding animals"
01029000,"Live bovine animals, other than cattle and buffalo"
01031000,Live purebred breeding swine
1 code description
2 01012100 Live purebred breeding horses
3 01012900 Live horses other than purebred breeding horses
4 01013000 Live asses
5 01019030 Mules and hinnies imported for immediate slaughter
6 01019040 Mules and hinnies not imported for immediate slaughter
7 01022100 Live purebred breeding cattle
8 01022920 Cows imported specially for dairy purposes
9 01022940 Live cattle other than purebred or those imported for dairy purposes
10 01023100 Live purebred breeding buffalo
11 01023900 Live buffalo, other than purebred breeding animals
12 01029000 Live bovine animals, other than cattle and buffalo
13 01031000 Live purebred breeding swine

Binary file not shown.

View File

@ -0,0 +1,18 @@
text
horses
Live purebred breeding cattle
1 text
2 horses
3 Live purebred breeding cattle

Binary file not shown.