优化“知识库模块“--基本完善

This commit is contained in:
Cheng Zhou
2025-12-26 14:07:28 +08:00
parent a1a4964cd2
commit 7c9befc3c9
23 changed files with 1345 additions and 110 deletions
+78
View File
@@ -0,0 +1,78 @@
import uuid
from typing import Sequence
from sqlalchemy import delete as sa_delete, func, select, update as sa_update
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.faq_reply import FaqReply
from app.schemas.faq import FaqReplyCreate
async def create_reply(
db: AsyncSession,
*,
faq_id: uuid.UUID,
study_id: uuid.UUID | None,
created_by: uuid.UUID,
reply_in: FaqReplyCreate,
) -> FaqReply:
reply = FaqReply(
faq_id=faq_id,
study_id=study_id,
content=reply_in.content,
created_by=created_by,
quote_reply_id=reply_in.quote_reply_id,
)
db.add(reply)
await db.commit()
await db.refresh(reply)
return reply
async def list_replies(db: AsyncSession, faq_id: uuid.UUID) -> Sequence[FaqReply]:
result = await db.execute(
select(FaqReply).where(FaqReply.faq_id == faq_id).order_by(FaqReply.created_at)
)
return result.scalars().all()
async def get_reply(db: AsyncSession, reply_id: uuid.UUID) -> FaqReply | None:
result = await db.execute(select(FaqReply).where(FaqReply.id == reply_id))
return result.scalar_one_or_none()
async def delete_reply(db: AsyncSession, reply: FaqReply) -> None:
await db.delete(reply)
await db.commit()
async def soft_delete_reply(db: AsyncSession, reply: FaqReply) -> None:
reply.is_deleted = True
reply.content = ""
await db.commit()
async def count_quote_references(db: AsyncSession, reply_id: uuid.UUID) -> int:
result = await db.execute(
select(func.count()).select_from(FaqReply).where(FaqReply.quote_reply_id == reply_id)
)
return int(result.scalar_one())
async def count_active_replies(db: AsyncSession, faq_id: uuid.UUID) -> int:
result = await db.execute(
select(func.count())
.select_from(FaqReply)
.where(FaqReply.faq_id == faq_id, FaqReply.is_deleted.is_(False))
)
return int(result.scalar_one())
async def delete_replies_by_faq_id(db: AsyncSession, faq_id: uuid.UUID) -> None:
await db.execute(
sa_update(FaqReply)
.where(FaqReply.faq_id == faq_id)
.values(quote_reply_id=None)
)
await db.execute(sa_delete(FaqReply).where(FaqReply.faq_id == faq_id))
await db.commit()