71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
"""候选数据访问(api-spec §8;档案 / 记忆候选)。"""
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.candidates import MemoryCandidate, ProfileCandidate
|
|
|
|
|
|
async def add_profile_candidates(session: AsyncSession, candidates: list[ProfileCandidate]) -> None:
|
|
session.add_all(candidates)
|
|
await session.flush()
|
|
|
|
|
|
async def add_memory_candidates(session: AsyncSession, candidates: list[MemoryCandidate]) -> None:
|
|
session.add_all(candidates)
|
|
await session.flush()
|
|
|
|
|
|
async def list_profile_candidates(
|
|
session: AsyncSession,
|
|
character_id: str,
|
|
*,
|
|
import_job_id: str | None = None,
|
|
status: str | None = None,
|
|
) -> list[ProfileCandidate]:
|
|
conditions = [ProfileCandidate.character_id == character_id]
|
|
if import_job_id is not None:
|
|
conditions.append(ProfileCandidate.import_job_id == import_job_id)
|
|
if status is not None:
|
|
conditions.append(ProfileCandidate.status == status)
|
|
result = await session.execute(
|
|
select(ProfileCandidate).where(*conditions).order_by(ProfileCandidate.created_at.desc())
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
|
|
async def list_memory_candidates(
|
|
session: AsyncSession,
|
|
character_id: str,
|
|
*,
|
|
import_job_id: str | None = None,
|
|
cycle_id: str | None = None,
|
|
status: str | None = None,
|
|
source: str | None = None,
|
|
) -> list[MemoryCandidate]:
|
|
conditions = [MemoryCandidate.character_id == character_id]
|
|
if import_job_id is not None:
|
|
conditions.append(MemoryCandidate.import_job_id == import_job_id)
|
|
if cycle_id is not None:
|
|
conditions.append(MemoryCandidate.cycle_id == cycle_id)
|
|
if status is not None:
|
|
conditions.append(MemoryCandidate.status == status)
|
|
if source == "cycle":
|
|
conditions.append(MemoryCandidate.import_job_id.is_(None))
|
|
elif source == "import":
|
|
conditions.append(MemoryCandidate.import_job_id.is_not(None))
|
|
result = await session.execute(
|
|
select(MemoryCandidate).where(*conditions).order_by(MemoryCandidate.created_at.desc())
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
|
|
async def get_profile_candidate(
|
|
session: AsyncSession, candidate_id: str
|
|
) -> ProfileCandidate | None:
|
|
return await session.get(ProfileCandidate, candidate_id)
|
|
|
|
|
|
async def get_memory_candidate(session: AsyncSession, candidate_id: str) -> MemoryCandidate | None:
|
|
return await session.get(MemoryCandidate, candidate_id)
|