素材列表展示
This commit is contained in:
@@ -24,6 +24,7 @@ from app.api.v1.upload_material import router as upload_material_router
|
||||
from app.api.v1.pre_test_template import router as pre_test_template_router
|
||||
from app.api.v1.material_consumption import router as material_consumption_router
|
||||
from app.api.v1.open_type import router as open_type_router
|
||||
from app.api.v1.resources_material import router as resources_material_router
|
||||
from app.api.admin import router as admin_module_router
|
||||
|
||||
api_router = APIRouter()
|
||||
@@ -51,4 +52,5 @@ api_router.include_router(upload_material_router)
|
||||
api_router.include_router(pre_test_template_router)
|
||||
api_router.include_router(material_consumption_router)
|
||||
api_router.include_router(open_type_router)
|
||||
api_router.include_router(resources_material_router)
|
||||
api_router.include_router(admin_module_router)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from typing import Any, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.schemas.resources_material import ResourcesMaterialListResponse
|
||||
from app.services.resources_material_service import get_resources_material_list
|
||||
|
||||
router = APIRouter(prefix="/resources-material", tags=["resources-material"])
|
||||
|
||||
|
||||
@router.get(
|
||||
"/list",
|
||||
summary="查询素材列表",
|
||||
description="通过advertiser_id,material_id,upload_id,resource_type查询素材列表,同时返回关联的资源信息",
|
||||
response_model=ResourcesMaterialListResponse,
|
||||
)
|
||||
async def get_resources_material_list_api(
|
||||
advertiser_id: Optional[str] = Query(None, description="广告主id"),
|
||||
material_id: Optional[str] = Query(None, description="素材id"),
|
||||
upload_id: Optional[str] = Query(None, description="上传资源平台id"),
|
||||
file_name: Optional[str] = Query(None, description="文件名"),
|
||||
resource_type: Optional[str] = Query(None, description="资源类型,image或者video"),
|
||||
page: int = Query(1, description="页码"),
|
||||
page_size: int = Query(20, description="每页数量"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
items, total = await get_resources_material_list(
|
||||
db=db,
|
||||
advertiser_id=advertiser_id,
|
||||
material_id=material_id,
|
||||
upload_id=upload_id,
|
||||
file_name=file_name,
|
||||
resource_type=resource_type,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "查询成功",
|
||||
"data": items,
|
||||
"total": total,
|
||||
}
|
||||
@@ -351,8 +351,8 @@ async def get_upload_history(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
try:
|
||||
from app.services.upload_material_service import get_upload_history as get_upload_history_service
|
||||
|
||||
result = await get_upload_history_service(
|
||||
user_id=current_user.id,
|
||||
db=db,
|
||||
@@ -366,3 +366,10 @@ async def get_upload_history(
|
||||
"data": result["data"],
|
||||
"pagination": result["pagination"],
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"code": 0,
|
||||
"message": f"查询上传任务历史失败:{str(e)}",
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
from datetime import date, datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class ResourcesMaterialQuery(BaseModel):
|
||||
advertiser_id: Optional[str] = Field(None, description="广告主id")
|
||||
material_id: Optional[str] = Field(None, description="素材id")
|
||||
upload_id: Optional[str] = Field(None, description="上传资源平台id")
|
||||
resource_type: Optional[str] = Field(None, description="资源类型,image或者video")
|
||||
page: int = Field(1, description="页码")
|
||||
page_size: int = Field(20, description="每页数量")
|
||||
|
||||
|
||||
class GeneratedResourceOut(BaseModel):
|
||||
file_name: Optional[str] = Field(None, description="文件名")
|
||||
resource_url: str = Field(..., description="资源URL")
|
||||
remote_url: Optional[str] = Field(None, description="远程URL")
|
||||
storage_type: str = Field(..., description="存储类型")
|
||||
storage_path: Optional[str] = Field(None, description="存储路径")
|
||||
file_size_bytes: int = Field(..., description="文件大小(字节)")
|
||||
source_model: str = Field(..., description="来源模型")
|
||||
source_model_module: Optional[str] = Field(None, description="来源模型模块")
|
||||
source_id: str = Field(..., description="来源id")
|
||||
engine_id: Optional[str] = Field(None, description="引擎id")
|
||||
engine_type: Optional[str] = Field(None, description="引擎类型")
|
||||
provider: Optional[str] = Field(None, description="提供商")
|
||||
model_name: Optional[str] = Field(None, description="模型名称")
|
||||
generated_at: Optional[datetime] = Field(None, description="生成时间")
|
||||
resource_month: date = Field(..., description="资源月份")
|
||||
created_at: datetime = Field(..., description="创建时间")
|
||||
|
||||
|
||||
class ResourcesMaterialOut(BaseModel):
|
||||
id: str = Field(..., description="主键")
|
||||
oauth_id: str = Field(..., description="授权表user_oauth自增id")
|
||||
advertiser_id: Optional[str] = Field(None, description="广告主id")
|
||||
target_table: Optional[str] = Field(None, description="资源表名称")
|
||||
target_id: Optional[str] = Field(None, description="资源表id")
|
||||
material_id: Optional[str] = Field(None, description="素材id")
|
||||
upload_id: Optional[str] = Field(None, description="上传资源平台id")
|
||||
resource_type: Optional[str] = Field(None, description="资源类型")
|
||||
user_id: Optional[str] = Field(None, description="用户登录id")
|
||||
task_id: Optional[str] = Field(None, description="前测任务id")
|
||||
note: Optional[str] = Field(None, description="备注")
|
||||
status: Optional[str] = Field(None, description="前测状态")
|
||||
pre_result: Optional[str] = Field(None, description="前测结果")
|
||||
pre_test_template_id: Optional[str] = Field(None, description="前测模板id")
|
||||
created_at: datetime = Field(..., description="创建时间")
|
||||
updated_at: datetime = Field(..., description="更新时间")
|
||||
resource: Optional[GeneratedResourceOut] = Field(None, description="关联的资源信息")
|
||||
|
||||
|
||||
class ResourcesMaterialListResponse(BaseModel):
|
||||
code: int = Field(0, description="返回码,0表示成功")
|
||||
message: str = Field("查询成功", description="返回消息")
|
||||
data: list[ResourcesMaterialOut] = Field(..., description="素材列表数据")
|
||||
total: int = Field(..., description="总记录数")
|
||||
@@ -0,0 +1,109 @@
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from app.models.generated_resource import GeneratedResource
|
||||
from app.models.resources_material import ResourcesMaterial
|
||||
from app.schemas.resources_material import GeneratedResourceOut, ResourcesMaterialOut
|
||||
|
||||
|
||||
async def get_resources_material_list(
|
||||
db: AsyncSession,
|
||||
advertiser_id: Optional[str] = None,
|
||||
material_id: Optional[str] = None,
|
||||
upload_id: Optional[str] = None,
|
||||
file_name: Optional[str] = None,
|
||||
resource_type: Optional[str] = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> Tuple[list[ResourcesMaterialOut], int]:
|
||||
resource_alias = aliased(GeneratedResource)
|
||||
|
||||
query = select(ResourcesMaterial).where(ResourcesMaterial.deleted_at.is_(None))
|
||||
|
||||
if advertiser_id:
|
||||
query = query.where(ResourcesMaterial.advertiser_id == advertiser_id)
|
||||
if material_id:
|
||||
query = query.where(ResourcesMaterial.material_id == material_id)
|
||||
if upload_id:
|
||||
query = query.where(ResourcesMaterial.upload_id == upload_id)
|
||||
if resource_type:
|
||||
query = query.where(ResourcesMaterial.resource_type == resource_type)
|
||||
|
||||
if file_name:
|
||||
query = query.join(
|
||||
resource_alias,
|
||||
(ResourcesMaterial.target_id == resource_alias.id) &
|
||||
(ResourcesMaterial.target_table == "generated_resources") &
|
||||
(resource_alias.deleted_at.is_(None)),
|
||||
isouter=True
|
||||
).where(resource_alias.file_name.like(f"%{file_name}%"))
|
||||
|
||||
total = (await db.execute(select(func.count()).select_from(query.subquery()))).scalar_one()
|
||||
|
||||
query = query.order_by(ResourcesMaterial.created_at.desc())
|
||||
query = query.offset((page - 1) * page_size).limit(page_size)
|
||||
|
||||
result = await db.execute(query)
|
||||
materials = result.scalars().all()
|
||||
|
||||
target_ids = [material.target_id for material in materials if material.target_id and material.target_table == "generated_resources"]
|
||||
|
||||
resource_map = {}
|
||||
if target_ids:
|
||||
resource_query = select(GeneratedResource).where(
|
||||
GeneratedResource.id.in_(target_ids),
|
||||
GeneratedResource.deleted_at.is_(None)
|
||||
)
|
||||
resource_result = await db.execute(resource_query)
|
||||
resources = resource_result.scalars().all()
|
||||
resource_map = {resource.id: resource for resource in resources}
|
||||
|
||||
items = []
|
||||
for material in materials:
|
||||
resource = None
|
||||
if material.target_id and material.target_table == "generated_resources":
|
||||
generated_resource = resource_map.get(material.target_id)
|
||||
if generated_resource:
|
||||
resource = GeneratedResourceOut(
|
||||
file_name=generated_resource.file_name,
|
||||
resource_url=generated_resource.resource_url,
|
||||
remote_url=generated_resource.remote_url,
|
||||
storage_type=generated_resource.storage_type,
|
||||
storage_path=generated_resource.storage_path,
|
||||
file_size_bytes=generated_resource.file_size_bytes,
|
||||
source_model=generated_resource.source_model,
|
||||
source_model_module=generated_resource.source_model_module,
|
||||
source_id=generated_resource.source_id,
|
||||
engine_id=generated_resource.engine_id,
|
||||
engine_type=generated_resource.engine_type,
|
||||
provider=generated_resource.provider,
|
||||
model_name=generated_resource.model_name,
|
||||
generated_at=generated_resource.generated_at,
|
||||
resource_month=generated_resource.resource_month,
|
||||
created_at=generated_resource.created_at,
|
||||
)
|
||||
|
||||
items.append(ResourcesMaterialOut(
|
||||
id=material.id,
|
||||
oauth_id=material.oauth_id,
|
||||
advertiser_id=material.advertiser_id,
|
||||
target_table=material.target_table,
|
||||
target_id=material.target_id,
|
||||
material_id=material.material_id,
|
||||
upload_id=material.upload_id,
|
||||
resource_type=material.resource_type,
|
||||
user_id=material.user_id,
|
||||
task_id=material.task_id,
|
||||
note=material.note,
|
||||
status=material.status,
|
||||
pre_result=material.pre_result,
|
||||
pre_test_template_id=material.pre_test_template_id,
|
||||
created_at=material.created_at,
|
||||
updated_at=material.updated_at,
|
||||
resource=resource,
|
||||
))
|
||||
|
||||
return items, total
|
||||
@@ -551,6 +551,7 @@ async def get_upload_history(
|
||||
GeneratedResource.storage_path,
|
||||
GeneratedResource.file_size_bytes,
|
||||
GeneratedResource.model_name,
|
||||
GeneratedResource.file_name,
|
||||
)
|
||||
.outerjoin(
|
||||
GeneratedResource,
|
||||
@@ -591,7 +592,7 @@ async def get_upload_history(
|
||||
}
|
||||
|
||||
data = []
|
||||
for task, resource_type, resource_url, remote_url, storage_type, storage_path, file_size_bytes, model_name in tasks:
|
||||
for task, resource_type, resource_url, remote_url, storage_type, storage_path, file_size_bytes, model_name, file_name in tasks:
|
||||
data.append({
|
||||
"task_id": task.id,
|
||||
"status": task.status,
|
||||
@@ -608,6 +609,7 @@ async def get_upload_history(
|
||||
"note": task.note,
|
||||
"created_at": task.created_at,
|
||||
"updated_at": task.updated_at,
|
||||
"file_name": file_name,
|
||||
})
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user