90 lines
3.5 KiB
Python
90 lines
3.5 KiB
Python
|
|
"""GET /api/voice-clips — 长语音 (voice_clip) list.
|
||
|
|
|
||
|
|
Read-only query over the mirrored ``voice_clip`` table. Powers the
|
||
|
|
``VoiceClipModal`` (微信长语音 / WA wavoicecalllist 共用的只读展示 modal).
|
||
|
|
|
||
|
|
Filters (all optional):
|
||
|
|
|
||
|
|
* ``user_id`` — exact employee-id match (modal opens from a CallList
|
||
|
|
row keyed on the employee). Takes PRECEDENCE over ``user_real_name`` when
|
||
|
|
both are given (the id is more precise).
|
||
|
|
* ``user_real_name`` — employee-name ILIKE / contains match (the 云客 modal's
|
||
|
|
员工姓名 fuzzy filter). Ignored when ``user_id`` is present.
|
||
|
|
* ``channel`` — messaging channel (``wechat`` / ``whatsapp``).
|
||
|
|
* ``company_code`` — exact 企业串码 match.
|
||
|
|
* ``start`` / ``end`` — ms-epoch window compared against ``end_time`` (录音结束时间,
|
||
|
|
inclusive) — mirrors the 云客 modal's 录音结束时间 range filter.
|
||
|
|
|
||
|
|
Ordered by ``end_time desc`` (nulls last) — most recent clips first, matching
|
||
|
|
the 云客 modal's default ordering.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Annotated
|
||
|
|
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from fastapi import APIRouter, Depends, Query
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.api.deps import get_session
|
||
|
|
from app.api.schemas import Page, VoiceClipOut
|
||
|
|
from app.db.models import VoiceClip
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api", tags=["voice_clips"])
|
||
|
|
|
||
|
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
||
|
|
PageParam = Annotated[int, Query(ge=1)]
|
||
|
|
PageSizeParam = Annotated[int, Query(ge=1, le=100)]
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/voice-clips")
|
||
|
|
async def list_voice_clips(
|
||
|
|
session: SessionDep,
|
||
|
|
page: PageParam = 1,
|
||
|
|
page_size: PageSizeParam = 20,
|
||
|
|
user_id: Annotated[str | None, Query()] = None,
|
||
|
|
user_real_name: Annotated[str | None, Query()] = None,
|
||
|
|
channel: Annotated[str | None, Query()] = None,
|
||
|
|
company_code: Annotated[str | None, Query()] = None,
|
||
|
|
start: Annotated[int | None, Query(ge=0)] = None,
|
||
|
|
end: Annotated[int | None, Query(ge=0)] = None,
|
||
|
|
) -> Page[VoiceClipOut]:
|
||
|
|
"""List voice clips with optional filters, newest by recording end first."""
|
||
|
|
conditions = []
|
||
|
|
# user_id takes precedence over user_real_name (id is the more precise
|
||
|
|
# filter; the 云客 modal switches between them based on whether the user
|
||
|
|
# edited the staff-name box).
|
||
|
|
if user_id is not None:
|
||
|
|
conditions.append(VoiceClip.user_id == user_id)
|
||
|
|
elif user_real_name is not None:
|
||
|
|
conditions.append(VoiceClip.user_real_name.ilike(f"%{user_real_name}%"))
|
||
|
|
if channel is not None:
|
||
|
|
conditions.append(VoiceClip.channel == channel)
|
||
|
|
if company_code is not None:
|
||
|
|
conditions.append(VoiceClip.company_code == company_code)
|
||
|
|
if start is not None:
|
||
|
|
conditions.append(VoiceClip.end_time >= start)
|
||
|
|
if end is not None:
|
||
|
|
conditions.append(VoiceClip.end_time <= end)
|
||
|
|
|
||
|
|
total_stmt = sa.select(sa.func.count()).select_from(VoiceClip)
|
||
|
|
if conditions:
|
||
|
|
total_stmt = total_stmt.where(*conditions)
|
||
|
|
total = (await session.execute(total_stmt)).scalar_one()
|
||
|
|
|
||
|
|
list_stmt = sa.select(VoiceClip)
|
||
|
|
if conditions:
|
||
|
|
list_stmt = list_stmt.where(*conditions)
|
||
|
|
list_stmt = (
|
||
|
|
list_stmt.order_by(
|
||
|
|
sa.nullslast(VoiceClip.end_time.desc()),
|
||
|
|
VoiceClip.voice_clip_id.asc(),
|
||
|
|
)
|
||
|
|
.limit(page_size)
|
||
|
|
.offset((page - 1) * page_size)
|
||
|
|
)
|
||
|
|
rows = (await session.execute(list_stmt)).scalars().all()
|
||
|
|
items = [VoiceClipOut.model_validate(row) for row in rows]
|
||
|
|
return Page(items=items, total=total, page=page, page_size=page_size)
|