diff --git a/api/code.py b/api/code.py index 098bd84..67dd768 100644 --- a/api/code.py +++ b/api/code.py @@ -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() diff --git a/assets/templates/上传模版.csv b/assets/templates/上传模版.csv new file mode 100644 index 0000000..54e8abc --- /dev/null +++ b/assets/templates/上传模版.csv @@ -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 diff --git a/assets/templates/上传模版.xls b/assets/templates/上传模版.xls new file mode 100644 index 0000000..0b554fa Binary files /dev/null and b/assets/templates/上传模版.xls differ diff --git a/assets/templates/查询模版.csv b/assets/templates/查询模版.csv new file mode 100644 index 0000000..c7153d8 --- /dev/null +++ b/assets/templates/查询模版.csv @@ -0,0 +1,18 @@ +text +horses +Live purebred breeding cattle + + + + + + + + + + + + + + + diff --git a/assets/templates/查询模版.xls b/assets/templates/查询模版.xls new file mode 100644 index 0000000..7d44296 Binary files /dev/null and b/assets/templates/查询模版.xls differ