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

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
+14
View File
@@ -45,6 +45,20 @@ async def get_category(db: AsyncSession, category_id: uuid.UUID) -> FaqCategory
return result.scalar_one_or_none()
async def get_category_by_name(
db: AsyncSession,
study_id: uuid.UUID | None,
name: str,
) -> FaqCategory | None:
stmt = select(FaqCategory).where(FaqCategory.name == name)
if study_id is None:
stmt = stmt.where(FaqCategory.study_id.is_(None))
else:
stmt = stmt.where(FaqCategory.study_id == study_id)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def update_category(db: AsyncSession, category: FaqCategory, category_in: CategoryUpdate) -> FaqCategory:
update_data = category_in.model_dump(exclude_unset=True)
if update_data:
+39 -4
View File
@@ -1,7 +1,7 @@
import uuid
from typing import Sequence
from sqlalchemy import or_, select, update as sa_update
from sqlalchemy import func, or_, select, update as sa_update
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.faq_category import FaqCategory
@@ -23,8 +23,8 @@ async def create_item(db: AsyncSession, item_in: FaqCreate, *, created_by: uuid.
study_id=item_in.study_id,
category_id=item_in.category_id,
question=item_in.question,
answer=item_in.answer,
keywords=item_in.keywords,
answer=item_in.answer or "",
status="PENDING",
version=1,
is_active=True,
created_by=created_by,
@@ -56,7 +56,7 @@ async def list_items(
elif study_scope == "global":
stmt = stmt.where(FaqItem.study_id.is_(None))
else: # project (default)
stmt = stmt.where((FaqItem.study_id == study_id) | (FaqItem.study_id.is_(None)))
stmt = stmt.where(FaqItem.study_id == study_id)
if category_id:
stmt = stmt.where(FaqItem.category_id == category_id)
@@ -88,3 +88,38 @@ async def update_item(db: AsyncSession, item: FaqItem, item_in: FaqUpdate) -> Fa
await db.commit()
await db.refresh(item)
return item
async def touch_item(db: AsyncSession, item_id: uuid.UUID) -> None:
await db.execute(
sa_update(FaqItem)
.where(FaqItem.id == item_id)
.values(updated_at=func.now())
)
await db.commit()
async def set_best_reply(db: AsyncSession, item_id: uuid.UUID, reply_id: uuid.UUID | None) -> None:
await db.execute(
sa_update(FaqItem)
.where(FaqItem.id == item_id)
.values(best_reply_id=reply_id, updated_at=func.now())
)
await db.commit()
async def set_status(db: AsyncSession, item_id: uuid.UUID, status: str, resolved_by_confirm: bool | None = None) -> None:
values: dict[str, object] = {"status": status, "updated_at": func.now()}
if resolved_by_confirm is not None:
values["resolved_by_confirm"] = resolved_by_confirm
await db.execute(
sa_update(FaqItem)
.where(FaqItem.id == item_id)
.values(**values)
)
await db.commit()
async def count_items_by_category(db: AsyncSession, category_id: uuid.UUID) -> int:
result = await db.execute(select(func.count()).select_from(FaqItem).where(FaqItem.category_id == category_id))
return int(result.scalar_one())
+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()