Files
2026-08-21 16:00:13 +08:00

66 lines
1.9 KiB
Python

"""资料导入数据访问(api-spec §8)。"""
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.imports import CharacterImportJob, ImportItem
async def get_job(session: AsyncSession, job_id: str) -> CharacterImportJob | None:
return await session.get(CharacterImportJob, job_id)
async def list_jobs(
session: AsyncSession,
character_id: str,
*,
page: int = 1,
page_size: int = 20,
) -> tuple[list[CharacterImportJob], int]:
conditions = [CharacterImportJob.character_id == character_id]
total = (
await session.execute(
select(func.count()).select_from(CharacterImportJob).where(*conditions)
)
).scalar_one()
items_result = await session.execute(
select(CharacterImportJob)
.where(*conditions)
.order_by(CharacterImportJob.created_at.desc())
.offset(max(page - 1, 0) * page_size)
.limit(page_size)
)
return list(items_result.scalars().all()), int(total or 0)
async def add_job(session: AsyncSession, job: CharacterImportJob) -> CharacterImportJob:
session.add(job)
await session.flush()
return job
async def list_items(
session: AsyncSession,
job_id: str,
*,
page: int = 1,
page_size: int = 20,
) -> tuple[list[ImportItem], int]:
conditions = [ImportItem.import_job_id == job_id]
total = (
await session.execute(select(func.count()).select_from(ImportItem).where(*conditions))
).scalar_one()
items_result = await session.execute(
select(ImportItem)
.where(*conditions)
.order_by(ImportItem.sequence.asc())
.offset(max(page - 1, 0) * page_size)
.limit(page_size)
)
return list(items_result.scalars().all()), int(total or 0)
async def add_items(session: AsyncSession, items: list[ImportItem]) -> None:
session.add_all(items)
await session.flush()