feat: add_complex_search_route
This commit is contained in:
@@ -0,0 +1,86 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from sqlmodel import Field, select
|
||||||
|
|
||||||
|
from app.model.BasicModel import BasicModel
|
||||||
|
from app.model.LlmDemoMdel import LlmDemoModel
|
||||||
|
from app.utils.model_utils import FormattedDatetime
|
||||||
|
from app.utils.mysql_utils import AsyncSessionDep
|
||||||
|
|
||||||
|
|
||||||
|
def add_complex_search_route(app: FastAPI):
|
||||||
|
@app.post('/llm_demo/search')
|
||||||
|
async def _search(
|
||||||
|
session: AsyncSessionDep,
|
||||||
|
body: SearchQuerySchema,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
多条件组合查询用户
|
||||||
|
"""
|
||||||
|
query = select(LlmDemoModel)
|
||||||
|
|
||||||
|
# 按名称模糊查询
|
||||||
|
if body.full_name:
|
||||||
|
query = query.where(LlmDemoModel.full_name.like(f"%{body.full_name}%"))
|
||||||
|
|
||||||
|
# 按开通会员时间范围查询
|
||||||
|
if body.datetime_start_min:
|
||||||
|
query = query.where(LlmDemoModel.datetime_start >= body.datetime_start_min)
|
||||||
|
if body.datetime_start_max:
|
||||||
|
query = query.where(LlmDemoModel.datetime_start <= body.datetime_start_max)
|
||||||
|
|
||||||
|
# 按会员到期时间范围查询
|
||||||
|
if body.datetime_end_min:
|
||||||
|
query = query.where(LlmDemoModel.datetime_end >= body.datetime_end_min)
|
||||||
|
if body.datetime_end_max:
|
||||||
|
query = query.where(LlmDemoModel.datetime_end <= body.datetime_end_max)
|
||||||
|
|
||||||
|
# 按金额范围查询
|
||||||
|
if body.amount_min is not None:
|
||||||
|
query = query.where(LlmDemoModel.amount >= body.amount_min)
|
||||||
|
if body.amount_max is not None:
|
||||||
|
query = query.where(LlmDemoModel.amount <= body.amount_max)
|
||||||
|
|
||||||
|
# 按创建时间范围查询
|
||||||
|
if body.created_at_min:
|
||||||
|
query = query.where(LlmDemoModel.created_at >= body.created_at_min)
|
||||||
|
if body.created_at_max:
|
||||||
|
query = query.where(LlmDemoModel.created_at <= body.created_at_max)
|
||||||
|
|
||||||
|
# 排序
|
||||||
|
if body.order_by:
|
||||||
|
if body.order_by == "amount_desc":
|
||||||
|
query = query.order_by(LlmDemoModel.amount.desc())
|
||||||
|
elif body.order_by == "amount_asc":
|
||||||
|
query = query.order_by(LlmDemoModel.amount.asc())
|
||||||
|
elif body.order_by == "created_at_desc":
|
||||||
|
query = query.order_by(LlmDemoModel.created_at.desc())
|
||||||
|
elif body.order_by == "created_at_asc":
|
||||||
|
query = query.order_by(LlmDemoModel.created_at.asc())
|
||||||
|
else:
|
||||||
|
query = query.order_by(LlmDemoModel.created_at.desc())
|
||||||
|
|
||||||
|
# 分页
|
||||||
|
query = query.offset(body.page * body.page_size).limit(body.page_size)
|
||||||
|
|
||||||
|
result = await session.execute(query)
|
||||||
|
query_cls_list: List[LlmDemoModel] = result.scalars().all()
|
||||||
|
|
||||||
|
return {"list": query_cls_list}
|
||||||
|
|
||||||
|
|
||||||
|
class SearchQuerySchema(BasicModel):
|
||||||
|
full_name: str | None = Field(default=None, description="用户名称(模糊查询)")
|
||||||
|
datetime_start_min: FormattedDatetime | None = Field(default=None, description="开通会员开始时间")
|
||||||
|
datetime_start_max: FormattedDatetime | None = Field(default=None, description="开通会员结束时间")
|
||||||
|
datetime_end_min: FormattedDatetime | None = Field(default=None, description="会员到期开始时间")
|
||||||
|
datetime_end_max: FormattedDatetime | None = Field(default=None, description="会员到期结束时间")
|
||||||
|
amount_min: Decimal | None = Field(default=None, description="最小金额")
|
||||||
|
amount_max: Decimal | None = Field(default=None, description="最大金额")
|
||||||
|
created_at_min: FormattedDatetime | None = Field(default=None, description="创建开始时间")
|
||||||
|
created_at_max: FormattedDatetime | None = Field(default=None, description="创建结束时间")
|
||||||
|
order_by: str | None = Field(default=None, description="排序字段:amount_desc, amount_asc, created_at_desc, created_at_asc")
|
||||||
|
page: int = Field(default=0, description="页码")
|
||||||
|
page_size: int = Field(default=10, description="每页数量")
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from app.controller.add_complex_search_route import add_complex_search_route
|
||||||
from app.controller.add_custom_stream_route import add_custom_stream_route
|
from app.controller.add_custom_stream_route import add_custom_stream_route
|
||||||
from app.controller.add_docs_route import add_docs_route
|
from app.controller.add_docs_route import add_docs_route
|
||||||
from app.controller.add_graph_proxy_route import add_graph_proxy_route
|
from app.controller.add_graph_proxy_route import add_graph_proxy_route
|
||||||
@@ -11,5 +12,6 @@ routes = [
|
|||||||
add_graph_proxy_route, # 代理自定义工作流
|
add_graph_proxy_route, # 代理自定义工作流
|
||||||
add_custom_stream_route, # 自定义流式接口
|
add_custom_stream_route, # 自定义流式接口
|
||||||
add_llm_demo_route, # LlmDemo 测试用户模块
|
add_llm_demo_route, # LlmDemo 测试用户模块
|
||||||
|
add_complex_search_route, # 多条件组合查询案例
|
||||||
]
|
]
|
||||||
# /*@formatter:on*/
|
# /*@formatter:on*/
|
||||||
|
|||||||
Reference in New Issue
Block a user