This commit is contained in:
2026-06-14 09:25:59 +08:00
parent a0f441d8ae
commit 6fbf610277
39 changed files with 2492 additions and 2 deletions
+52
View File
@@ -64,6 +64,58 @@ class LeafService:
)
return [self._leaf_to_dict(l) for l in result.scalars().all()]
async def get_grove(self, user_id: str) -> dict:
"""获取情绪共鸣林:自己 + 好友今日的心情叶,聚合成俯瞰森林"""
from app.models.friend import Friend
today = date.today()
# 自己 + 好友 ID
friends_result = await self.db.execute(
select(Friend.friend_user_id).where(Friend.user_id == user_id)
)
visible_ids = [user_id] + [r[0] for r in friends_result.all()]
# 查今日所有人的叶子
result = await self.db.execute(
select(DailyMoodLeaf).where(
DailyMoodLeaf.user_id.in_(visible_ids),
DailyMoodLeaf.leaf_date == today,
)
)
leaves = result.scalars().all()
# 排布:自己在中心(position 0),好友按确定性环绕
import math
positioned = []
others = [l for l in leaves if l.user_id != user_id]
my_leaf = next((l for l in leaves if l.user_id == user_id), None)
if my_leaf:
positioned.append({
"is_self": True, "user_id": my_leaf.user_id,
"mood": my_leaf.mood, "leaf_seed": my_leaf.leaf_seed,
"angle": 0, "radius": 0,
})
n = len(others)
for i, l in enumerate(others):
angle = (2 * math.pi * i / n) if n > 0 else 0
positioned.append({
"is_self": False, "user_id": l.user_id,
"mood": l.mood, "leaf_seed": l.leaf_seed,
"angle": angle, "radius": 1,
})
# 聚合情绪天气
mood_counts: dict[str, int] = {}
for l in leaves:
mood_counts[l.mood or "unknown"] = mood_counts.get(l.mood or "unknown", 0) + 1
return {
"leaves": positioned,
"total": len(leaves),
"mood_counts": mood_counts,
"date": today.isoformat(),
}
def _leaf_to_dict(self, leaf: DailyMoodLeaf) -> dict:
return {
"id": leaf.id,