Files

74 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
from typing import List
from app.core.base_crud import CRUDBase
from app.api.v1.module_common.goodname.model import UserNameRecordModel
from app.api.v1.module_common.goodname.schema import NameRecordCreate, NameRecordUpdate
from app.api.v1.module_system.auth.schema import AuthSchema
class NameRecordCRUD(CRUDBase[UserNameRecordModel, NameRecordCreate, NameRecordUpdate]):
"""起名记录 CRUD"""
def __init__(self, auth: AuthSchema) -> None:
super().__init__(UserNameRecordModel, auth)
async def get_good_names_for_display(self, limit: int = 10) -> List[dict]:
"""
获取佳名赏析列表(用于前端展示)
参数:
- limit: 返回数量
返回:
- List[dict]: 格式化的佳名列表
"""
records = await self.list(
search={"status": "0"},
order_by=[{"created_time": "desc"}]
)
result = []
for record in records[:limit]:
result.append({
"name": record.name,
"source": record.source or "",
"desc": record.source_text or record.meaning or ""
})
return result
async def get_random_names(self, count: int = 3) -> List[dict]:
"""
随机获取佳名
参数:
- count: 返回数量
返回:
- List[dict]: 随机佳名列表
"""
import random
records = await self.list(
search={"status": "0"},
order_by=[{"id": "asc"}]
)
if not records:
return []
# 随机选择
selected = random.sample(list(records), min(count, len(records)))
result = []
for record in selected:
result.append({
"name": record.name,
"source": record.source or "",
"desc": record.source_text or record.meaning or ""
})
return result