Goals can now choose between milestone-based progress (existing) and cumulative checkin-based progress (new). Cumulative mode supports cross-unit conversion (e.g. kcal → g fat) via a configurable conversion rate. New GoalCheckin model stores daily inputs; progress auto-recalculates on every checkin C/U/D. Backup import/export covers the new table. Frontend GoalDialog, GoalDetailPage and GoalPage cards adapt to show cumulative progress or milestone progress based on track_type. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
120 lines
5.0 KiB
Python
120 lines
5.0 KiB
Python
import uuid as _uuid
|
||
from sqlalchemy import Column, Integer, String, Text, Date, DateTime, Boolean, Float, ForeignKey, Table, desc
|
||
from sqlalchemy.orm import relationship
|
||
from app.database import Base
|
||
from app.utils.datetime import utcnow
|
||
|
||
|
||
# 目标-任务关联表(多对多)
|
||
goal_tasks = Table(
|
||
"goal_tasks",
|
||
Base.metadata,
|
||
Column("goal_id", Integer, ForeignKey("goals.id"), primary_key=True),
|
||
Column("task_id", Integer, ForeignKey("tasks.id"), primary_key=True),
|
||
)
|
||
|
||
|
||
class Goal(Base):
|
||
"""长期目标模型"""
|
||
__tablename__ = "goals"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
uuid = Column(String(36), default=lambda: str(_uuid.uuid4()), unique=True, index=True)
|
||
title = Column(String(200), nullable=False)
|
||
description = Column(Text, nullable=True)
|
||
status = Column(String(20), default="active") # active/paused/completed/abandoned
|
||
track_type = Column(String(20), default="milestone") # "milestone" | "cumulative"
|
||
progress = Column(Integer, default=0) # 0-100,里程碑模式从里程碑自动计算,累计模式从打卡汇总计算
|
||
target_value = Column(Float, nullable=True) # 累计模式:目标值(目标单位)
|
||
target_unit = Column(String(20), nullable=True) # 累计模式:目标单位,如 "g"、"kg"
|
||
input_unit = Column(String(20), nullable=True) # 累计模式:打卡输入单位,如 "kcal"、"次"
|
||
conversion_rate = Column(Float, default=1.0) # 累计模式:换算率(多少输入单位 = 1 目标单位)
|
||
current_value = Column(Float, default=0) # 累计模式:累计打卡值(输入单位)
|
||
target_date = Column(Date, nullable=True)
|
||
completed_at = Column(DateTime, nullable=True)
|
||
category_id = Column(Integer, ForeignKey("categories.id"), nullable=True)
|
||
color = Column(String(20), default="#FFB7C5")
|
||
icon = Column(String(50), default="flag")
|
||
sort_order = Column(Integer, default=0)
|
||
is_deleted = Column(Boolean, default=False)
|
||
sync_version = Column(Integer, default=1)
|
||
created_at = Column(DateTime, default=utcnow)
|
||
updated_at = Column(DateTime, default=utcnow, onupdate=utcnow)
|
||
|
||
# 关联关系
|
||
category = relationship("Category")
|
||
steps = relationship(
|
||
"GoalStep", back_populates="goal",
|
||
cascade="all, delete-orphan",
|
||
order_by="GoalStep.sort_order",
|
||
)
|
||
reviews = relationship(
|
||
"GoalReview", back_populates="goal",
|
||
cascade="all, delete-orphan",
|
||
order_by=lambda: desc(GoalReview.created_at),
|
||
)
|
||
checkins = relationship(
|
||
"GoalCheckin", back_populates="goal",
|
||
cascade="all, delete-orphan",
|
||
order_by=lambda: desc(GoalCheckin.checkin_date),
|
||
)
|
||
tasks = relationship("Task", secondary=goal_tasks, back_populates="goals")
|
||
|
||
|
||
class GoalStep(Base):
|
||
"""目标阶段/里程碑模型(step_type 区分类型)"""
|
||
__tablename__ = "goal_steps"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
uuid = Column(String(36), default=lambda: str(_uuid.uuid4()), unique=True, index=True)
|
||
goal_id = Column(Integer, ForeignKey("goals.id"), nullable=False)
|
||
parent_id = Column(Integer, ForeignKey("goal_steps.id"), nullable=True)
|
||
title = Column(String(200), nullable=False)
|
||
step_type = Column(String(20), nullable=False) # "phase" | "milestone"
|
||
status = Column(String(20), default="pending") # pending/in_progress/completed
|
||
target_date = Column(Date, nullable=True)
|
||
reached_at = Column(DateTime, nullable=True)
|
||
sort_order = Column(Integer, default=0)
|
||
is_deleted = Column(Boolean, default=False)
|
||
sync_version = Column(Integer, default=1)
|
||
created_at = Column(DateTime, default=utcnow)
|
||
|
||
# 关联关系
|
||
goal = relationship("Goal", back_populates="steps")
|
||
parent = relationship("GoalStep", remote_side=[id], backref="children")
|
||
|
||
|
||
class GoalReview(Base):
|
||
"""目标复盘记录模型"""
|
||
__tablename__ = "goal_reviews"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
uuid = Column(String(36), default=lambda: str(_uuid.uuid4()), unique=True, index=True)
|
||
goal_id = Column(Integer, ForeignKey("goals.id"), nullable=False)
|
||
content = Column(Text, nullable=False)
|
||
rating = Column(Integer, nullable=True) # 1-5 自评
|
||
is_deleted = Column(Boolean, default=False)
|
||
sync_version = Column(Integer, default=1)
|
||
created_at = Column(DateTime, default=utcnow)
|
||
|
||
# 关联关系
|
||
goal = relationship("Goal", back_populates="reviews")
|
||
|
||
|
||
class GoalCheckin(Base):
|
||
"""目标累计打卡记录模型"""
|
||
__tablename__ = "goal_checkins"
|
||
|
||
id = Column(Integer, primary_key=True, index=True)
|
||
uuid = Column(String(36), default=lambda: str(_uuid.uuid4()), unique=True, index=True)
|
||
goal_id = Column(Integer, ForeignKey("goals.id"), nullable=False)
|
||
value = Column(Float, nullable=False)
|
||
note = Column(Text, nullable=True)
|
||
checkin_date = Column(Date, nullable=False)
|
||
is_deleted = Column(Boolean, default=False)
|
||
sync_version = Column(Integer, default=1)
|
||
created_at = Column(DateTime, default=utcnow)
|
||
|
||
# 关联关系
|
||
goal = relationship("Goal", back_populates="checkins")
|