100 lines
4.5 KiB
Python
100 lines
4.5 KiB
Python
"""月度运势读取与内置模板。
|
||
|
||
取值顺序(device / web 共用):
|
||
1. 管理端写的指定年月(scope='ym', period='2026-11');
|
||
2. 管理端写的通用月度(scope='month', period='11');
|
||
3. 内置模板(只在前端/设备端兜底展示,不入库,管理端看不到)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from sqlalchemy import select
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
||
from app.models import Fortune
|
||
|
||
# 内置兜底模板:按「元素 × 月份主题」拼装,保证 12 星座 × 12 月不重样。
|
||
# 管理端写入了对应条目后,就不再走这份兜底。
|
||
MONTH_THEMES = {
|
||
1: ("收心规划", "对上一年的复盘会带来意外的清晰感", "Take stock of last year — it brings unexpected clarity"),
|
||
2: ("温养关系", "亲近的人需要你多一句肯定的回应", "People close to you need one more word of affirmation"),
|
||
3: ("开动新局", "酝酿已久的想法值得在本月落地", "Long-simmering ideas deserve to land this month"),
|
||
4: ("稳步深耕", "别被热闹的选项带走,深耕手头的事", "Don't chase the shiny options — deepen what's at hand"),
|
||
5: ("弹性调整", "计划外的变动其实是校准方向的机会", "Off-plan changes are chances to recalibrate"),
|
||
6: ("广结善缘", "一次随手的分享会带来新的连接", "A casual share brings a new connection"),
|
||
7: ("劳逸平衡", "身体发出的信号比日程表更值得听", "Listen to your body over your calendar"),
|
||
8: ("果敢决断", "犹豫太久的事,本月给出明确答案", "Give a clear answer to what you've long postponed"),
|
||
9: ("查漏补缺", "细节里的隐患会在本月浮出水面", "Hidden details surface this month — fix them early"),
|
||
10: ("收获沉淀", "前几个月的投入开始给出复利", "Months of investment start paying compounding returns"),
|
||
11: ("深谋远虑", "为明年埋线:学习、储蓄、健康都算", "Plant seeds for next year: learning, savings, health"),
|
||
12: ("收束归零", "给未完成的事一个体面的收尾", "Give unfinished business a graceful close"),
|
||
}
|
||
|
||
ELEMENT_ADVICE = {
|
||
"火": ("节奏可以快,但别省略热身", "Keep the pace fast, but never skip the warm-up"),
|
||
"土": ("把大目标切成可核对的周清单", "Slice big goals into weekly checkable lists"),
|
||
"风": ("把想法讲出来,讨论是最好的打磨", "Talk it out — discussion is the best polish"),
|
||
"水": ("记录情绪的波动,它是重要的数据", "Track your moods; they are important data"),
|
||
}
|
||
|
||
|
||
def generic_fortune(sign_name: str, element: str, month: int, lang: str = "zh") -> dict:
|
||
"""没入库也没有管理端文案时的兜底,按元素+月份拼一段。"""
|
||
theme_zh, theme_body_zh, theme_body_en = MONTH_THEMES.get(month, MONTH_THEMES[1])
|
||
adv_zh, adv_en = ELEMENT_ADVICE.get(element, ELEMENT_ADVICE["风"])
|
||
if lang == "en":
|
||
return {
|
||
"scope": "generic",
|
||
"title": f"{theme_body_en.split('—')[0].strip()}" if "—" in theme_body_en else theme_body_en,
|
||
"content": (
|
||
f"{sign_name}, the theme of this month is “{theme_body_en}”. "
|
||
f"{adv_en}."
|
||
),
|
||
}
|
||
return {
|
||
"scope": "generic",
|
||
"title": f"本月主题:{theme_zh}",
|
||
"content": (
|
||
f"{theme_body_zh}。{adv_zh}。"
|
||
),
|
||
}
|
||
|
||
|
||
async def for_user(
|
||
db: AsyncSession,
|
||
sign_code: str | None,
|
||
year: int,
|
||
month: int,
|
||
lang: str = "zh",
|
||
) -> dict | None:
|
||
"""按优先级取某星座当月运势。sign_code 为空(用户没填生日)返回 None。"""
|
||
if not sign_code:
|
||
return None
|
||
|
||
rows = (
|
||
await db.execute(
|
||
select(Fortune).where(
|
||
Fortune.zodiac_code == sign_code,
|
||
Fortune.enabled.is_(True),
|
||
Fortune.scope.in_(("ym", "month")),
|
||
Fortune.period.in_((f"{year}-{month:02d}", f"{month:02d}")),
|
||
)
|
||
)
|
||
).scalars().all()
|
||
|
||
pick = next((r for r in rows if r.scope == "ym"), None) or next(
|
||
(r for r in rows if r.scope == "month"), None
|
||
)
|
||
if pick is None:
|
||
return None
|
||
|
||
text = pick.content if lang != "en" or not pick.content_en else pick.content_en
|
||
title = pick.title if lang != "en" or not pick.title_en else pick.title_en
|
||
return {
|
||
"scope": pick.scope,
|
||
"period": pick.period,
|
||
"title": title,
|
||
"content": text,
|
||
"updated_at": pick.updated_at.isoformat() if pick.updated_at else None,
|
||
}
|