upload project source code
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
@@ -0,0 +1,125 @@
|
||||
'''
|
||||
Author: caoziyuan ziyuan.cao@zhuying.com
|
||||
Date: 2025-12-23 15:20:07
|
||||
LastEditors: caoziyuan ziyuan.cao@zhuying.com
|
||||
LastEditTime: 2025-12-23 15:41:59
|
||||
FilePath: \naming-backend\app\api\v1\module_common\goodname\controller.py
|
||||
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
'''
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import List
|
||||
from fastapi import APIRouter, Query, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.common.response import SuccessResponse
|
||||
from app.core.dependencies import db_getter, get_current_user
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
from app.api.v1.module_common.goodname.crud import NameRecordCRUD
|
||||
from app.api.v1.module_common.goodname.schema import NameRecordCreate, NameRecordUpdate, NameRecordOut
|
||||
|
||||
# 定义路由
|
||||
GoodNameRouter = APIRouter(prefix="/goodname", tags=["佳名赏析"])
|
||||
|
||||
|
||||
@GoodNameRouter.get("/list", summary="获取佳名列表", description="获取佳名赏析列表(无需登录)")
|
||||
async def get_good_name_list(
|
||||
limit: int = Query(default=3, ge=1, le=50, description="返回数量"),
|
||||
db: AsyncSession = Depends(db_getter)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
获取佳名赏析列表
|
||||
|
||||
返回格式:
|
||||
[
|
||||
{"name": "清芷", "source": "楚辞", "desc": "岸芷汀兰,郁郁青青"},
|
||||
{"name": "云帆", "source": "行路难", "desc": "长风破浪会有时,直挂云帆济沧海"},
|
||||
...
|
||||
]
|
||||
"""
|
||||
auth = AuthSchema(db=db, check_data_scope=False)
|
||||
crud = NameRecordCRUD(auth)
|
||||
data = await crud.get_good_names_for_display(limit)
|
||||
return SuccessResponse(data=data)
|
||||
|
||||
|
||||
@GoodNameRouter.get("/random", summary="随机获取佳名", description="随机获取佳名赏析(无需登录)")
|
||||
async def get_random_good_names(
|
||||
count: int = Query(default=3, ge=1, le=10, description="返回数量"),
|
||||
db: AsyncSession = Depends(db_getter)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
随机获取佳名
|
||||
"""
|
||||
auth = AuthSchema(db=db, check_data_scope=False)
|
||||
crud = NameRecordCRUD(auth)
|
||||
data = await crud.get_random_names(count)
|
||||
return SuccessResponse(data=data)
|
||||
|
||||
|
||||
@GoodNameRouter.post("/create", summary="创建起名记录", description="创建起名记录(需要登录)")
|
||||
async def create_name_record(
|
||||
data: NameRecordCreate,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
创建起名记录
|
||||
"""
|
||||
crud = NameRecordCRUD(auth)
|
||||
result = await crud.create(data)
|
||||
return SuccessResponse(data={"id": result.id}, msg="创建成功")
|
||||
|
||||
|
||||
@GoodNameRouter.get("/my", summary="获取我的起名记录", description="获取当前用户的起名记录(需要登录)")
|
||||
async def get_my_name_records(
|
||||
page: int = Query(default=1, ge=1, description="页码"),
|
||||
size: int = Query(default=10, ge=1, le=100, description="每页数量"),
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
获取我的起名记录
|
||||
"""
|
||||
crud = NameRecordCRUD(auth)
|
||||
offset = (page - 1) * size
|
||||
result = await crud.page(
|
||||
offset=offset,
|
||||
limit=size,
|
||||
order_by=[{"created_time": "desc"}],
|
||||
search={"created_id": auth.user.id},
|
||||
out_schema=NameRecordOut
|
||||
)
|
||||
return SuccessResponse(data=result)
|
||||
|
||||
|
||||
@GoodNameRouter.put("/favorite/{record_id}", summary="收藏/取消收藏", description="收藏或取消收藏起名记录(需要登录)")
|
||||
async def toggle_favorite(
|
||||
record_id: int,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
收藏/取消收藏起名记录
|
||||
"""
|
||||
crud = NameRecordCRUD(auth)
|
||||
record = await crud.get(id=record_id)
|
||||
if not record:
|
||||
return SuccessResponse(msg="记录不存在", success=False)
|
||||
|
||||
# 切换收藏状态
|
||||
new_status = 0 if record.is_favorite == 1 else 1
|
||||
await crud.update(record_id, {"is_favorite": new_status})
|
||||
|
||||
msg = "收藏成功" if new_status == 1 else "已取消收藏"
|
||||
return SuccessResponse(msg=msg)
|
||||
|
||||
|
||||
@GoodNameRouter.delete("/delete/{record_id}", summary="删除起名记录", description="删除起名记录(需要登录)")
|
||||
async def delete_name_record(
|
||||
record_id: int,
|
||||
auth: AuthSchema = Depends(get_current_user)
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
删除起名记录
|
||||
"""
|
||||
crud = NameRecordCRUD(auth)
|
||||
await crud.delete([record_id])
|
||||
return SuccessResponse(msg="删除成功")
|
||||
@@ -0,0 +1,73 @@
|
||||
# -*- 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
|
||||
@@ -0,0 +1,57 @@
|
||||
'''
|
||||
Author: caoziyuan ziyuan.cao@zhuying.com
|
||||
Date: 2025-12-23 15:22:53
|
||||
LastEditors: caoziyuan ziyuan.cao@zhuying.com
|
||||
LastEditTime: 2025-12-23 15:41:35
|
||||
FilePath: \naming-backend\app\api\v1\module_common\goodname\model.py
|
||||
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
'''
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from sqlalchemy import String, Integer, Text, SmallInteger
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.core.base_model import ModelMixin, UserMixin
|
||||
|
||||
|
||||
class UserNameRecordModel(ModelMixin, UserMixin):
|
||||
"""用户起名记录表"""
|
||||
|
||||
__tablename__ = "biz_user_name_record"
|
||||
|
||||
# ========== 基本信息 ==========
|
||||
name: Mapped[str] = mapped_column(String(50), nullable=False, comment="起的名字")
|
||||
surname: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="姓氏")
|
||||
full_name: Mapped[str | None] = mapped_column(String(70), nullable=True, comment="完整姓名")
|
||||
|
||||
# ========== 名字来源 ==========
|
||||
source: Mapped[str | None] = mapped_column(String(100), nullable=True, comment="出处(如:诗经、楚辞)")
|
||||
source_text: Mapped[str | None] = mapped_column(Text, nullable=True, comment="原文/出处原句")
|
||||
meaning: Mapped[str | None] = mapped_column(Text, nullable=True, comment="名字含义/寓意")
|
||||
# ========== 起名对象信息 ==========
|
||||
gender: Mapped[int | None] = mapped_column(SmallInteger, default=0, nullable=True, comment="性别(0:未知 1:男 2:女)")
|
||||
birth_year: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="出生年份")
|
||||
birth_month: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="出生月份")
|
||||
birth_day: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="出生日期")
|
||||
birth_hour: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="出生时辰(0-23)")
|
||||
lunar_birth: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="农历生日")
|
||||
|
||||
# ========== 五行八字 ==========
|
||||
wuxing: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="五行属性(如:金木水火土)")
|
||||
bazi: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="八字")
|
||||
wuxing_lack: Mapped[str | None] = mapped_column(String(20), nullable=True, comment="五行缺失")
|
||||
|
||||
# ========== 起名类型 ==========
|
||||
name_type: Mapped[int | None] = mapped_column(SmallInteger, default=1, nullable=True, comment="起名类型(1:宝宝起名 2:成人改名 3:公司起名 4:店铺起名)")
|
||||
|
||||
# ========== 评分相关 ==========
|
||||
score: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="名字评分(0-100)")
|
||||
score_detail: Mapped[str | None] = mapped_column(Text, nullable=True, comment="评分详情(JSON)")
|
||||
|
||||
# ========== 用户操作 ==========
|
||||
is_favorite: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="是否收藏(0:否 1:是)")
|
||||
is_used: Mapped[int] = mapped_column(SmallInteger, default=0, nullable=False, comment="是否已使用(0:否 1:是)")
|
||||
|
||||
# ========== 来源渠道 ==========
|
||||
channel: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="来源渠道(如:小程序、APP、网页)")
|
||||
ip_address: Mapped[str | None] = mapped_column(String(50), nullable=True, comment="用户IP地址")
|
||||
@@ -0,0 +1,74 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class GoodNameItem(BaseModel):
|
||||
"""佳名赏析项(简化版,用于列表展示)"""
|
||||
name: str = Field(..., description="名字")
|
||||
source: str = Field(..., description="出处")
|
||||
desc: str = Field(..., description="原文/释义")
|
||||
|
||||
|
||||
class NameRecordCreate(BaseModel):
|
||||
"""创建起名记录"""
|
||||
name: str = Field(..., max_length=50, description="起的名字")
|
||||
surname: Optional[str] = Field(None, max_length=20, description="姓氏")
|
||||
full_name: Optional[str] = Field(None, max_length=70, description="完整姓名")
|
||||
source: Optional[str] = Field(None, max_length=100, description="出处")
|
||||
source_text: Optional[str] = Field(None, description="原文/出处原句")
|
||||
meaning: Optional[str] = Field(None, description="名字含义/寓意")
|
||||
gender: Optional[int] = Field(0, description="性别(0:未知 1:男 2:女)")
|
||||
birth_year: Optional[int] = Field(None, description="出生年份")
|
||||
birth_month: Optional[int] = Field(None, ge=1, le=12, description="出生月份")
|
||||
birth_day: Optional[int] = Field(None, ge=1, le=31, description="出生日期")
|
||||
birth_hour: Optional[int] = Field(None, ge=0, le=23, description="出生时辰")
|
||||
lunar_birth: Optional[str] = Field(None, max_length=50, description="农历生日")
|
||||
wuxing: Optional[str] = Field(None, max_length=20, description="五行属性")
|
||||
bazi: Optional[str] = Field(None, max_length=50, description="八字")
|
||||
wuxing_lack: Optional[str] = Field(None, max_length=20, description="五行缺失")
|
||||
name_type: Optional[int] = Field(1, description="起名类型(1:宝宝起名 2:成人改名 3:公司起名 4:店铺起名)")
|
||||
score: Optional[int] = Field(None, ge=0, le=100, description="名字评分")
|
||||
score_detail: Optional[str] = Field(None, description="评分详情(JSON)")
|
||||
channel: Optional[str] = Field(None, max_length=50, description="来源渠道")
|
||||
|
||||
|
||||
class NameRecordUpdate(BaseModel):
|
||||
"""更新起名记录"""
|
||||
name: Optional[str] = Field(None, max_length=50)
|
||||
surname: Optional[str] = Field(None, max_length=20)
|
||||
full_name: Optional[str] = Field(None, max_length=70)
|
||||
source: Optional[str] = Field(None, max_length=100)
|
||||
source_text: Optional[str] = None
|
||||
meaning: Optional[str] = None
|
||||
is_favorite: Optional[int] = Field(None, description="是否收藏(0:否 1:是)")
|
||||
is_used: Optional[int] = Field(None, description="是否已使用(0:否 1:是)")
|
||||
|
||||
|
||||
class NameRecordOut(BaseModel):
|
||||
"""起名记录输出"""
|
||||
id: int
|
||||
name: str
|
||||
surname: Optional[str] = None
|
||||
full_name: Optional[str] = None
|
||||
source: Optional[str] = None
|
||||
source_text: Optional[str] = None
|
||||
meaning: Optional[str] = None
|
||||
gender: Optional[int] = None
|
||||
birth_year: Optional[int] = None
|
||||
birth_month: Optional[int] = None
|
||||
birth_day: Optional[int] = None
|
||||
lunar_birth: Optional[str] = None
|
||||
wuxing: Optional[str] = None
|
||||
bazi: Optional[str] = None
|
||||
wuxing_lack: Optional[str] = None
|
||||
name_type: Optional[int] = None
|
||||
score: Optional[int] = None
|
||||
is_favorite: int = 0
|
||||
is_used: int = 0
|
||||
created_time: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -0,0 +1,50 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import List
|
||||
|
||||
|
||||
class GoodNameService:
|
||||
"""佳名赏析服务"""
|
||||
|
||||
# 佳名数据
|
||||
GOOD_NAMES = [
|
||||
{"name": "清芷", "source": "楚辞", "desc": "岸芷汀兰,郁郁青青"},
|
||||
{"name": "云帆", "source": "行路难", "desc": "长风破浪会有时,直挂云帆济沧海"},
|
||||
{"name": "望舒", "source": "离骚", "desc": "前望舒使先驱兮,后飞廉使奔属"},
|
||||
{"name": "思齐", "source": "论语", "desc": "见贤思齐焉,见不贤而内自省也"},
|
||||
{"name": "若华", "source": "楚辞", "desc": "桂树丛生兮山之幽,偃蹇连蜷兮枝相缭"},
|
||||
{"name": "嘉言", "source": "尚书", "desc": "嘉言罔攸伏,野无遗贤"},
|
||||
{"name": "明哲", "source": "诗经", "desc": "既明且哲,以保其身"},
|
||||
{"name": "子衿", "source": "诗经", "desc": "青青子衿,悠悠我心"},
|
||||
{"name": "静姝", "source": "诗经", "desc": "静女其姝,俟我于城隅"},
|
||||
{"name": "修远", "source": "离骚", "desc": "路漫漫其修远兮,吾将上下而求索"},
|
||||
{"name": "星汉", "source": "观沧海", "desc": "日月之行,若出其中;星汉灿烂,若出其里"},
|
||||
{"name": "霁月", "source": "世说新语", "desc": "光风霁月,坦荡胸怀"},
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def get_good_names(cls, limit: int = 10) -> List[dict]:
|
||||
"""
|
||||
获取佳名列表
|
||||
|
||||
参数:
|
||||
- limit: 返回数量,默认10条
|
||||
|
||||
返回:
|
||||
- List[dict]: 佳名列表
|
||||
"""
|
||||
return cls.GOOD_NAMES[:limit]
|
||||
|
||||
@classmethod
|
||||
def get_random_names(cls, count: int = 3) -> List[dict]:
|
||||
"""
|
||||
随机获取佳名
|
||||
|
||||
参数:
|
||||
- count: 返回数量,默认3条
|
||||
|
||||
返回:
|
||||
- List[dict]: 随机佳名列表
|
||||
"""
|
||||
import random
|
||||
return random.sample(cls.GOOD_NAMES, min(count, len(cls.GOOD_NAMES)))
|
||||
Reference in New Issue
Block a user