feat:添加了月度计划 # 我要混提交

This commit is contained in:
minecraft1024a
2025-08-21 12:32:48 +08:00
parent 7894cd81b0
commit b1b08c1814
13 changed files with 512 additions and 15 deletions

View File

@@ -0,0 +1,68 @@
# mmc/src/common/database/monthly_plan_db.py
import datetime
from typing import List
from src.common.database.sqlalchemy_models import MonthlyPlan, get_db_session
from src.common.logger import get_logger
logger = get_logger("monthly_plan_db")
def add_new_plans(plans: List[str], month: str):
"""
批量添加新生成的月度计划到数据库。
:param plans: 计划内容列表。
:param month: 目标月份,格式为 "YYYY-MM"
"""
with get_db_session() as session:
try:
new_plan_objects = [
MonthlyPlan(plan_text=plan, target_month=month)
for plan in plans
]
session.add_all(new_plan_objects)
session.commit()
logger.info(f"成功向数据库添加了 {len(new_plan_objects)}{month} 的月度计划。")
except Exception as e:
logger.error(f"添加月度计划时发生错误: {e}")
session.rollback()
raise
def get_active_plans_for_month(month: str) -> List[MonthlyPlan]:
"""
获取指定月份所有未被软删除的计划。
:param month: 目标月份,格式为 "YYYY-MM"
:return: MonthlyPlan 对象列表。
"""
with get_db_session() as session:
try:
plans = session.query(MonthlyPlan).filter(
MonthlyPlan.target_month == month,
MonthlyPlan.is_deleted == False
).all()
return plans
except Exception as e:
logger.error(f"查询 {month} 的有效月度计划时发生错误: {e}")
return []
def soft_delete_plans(plan_ids: List[int]):
"""
将指定ID的计划标记为软删除。
:param plan_ids: 需要软删除的计划ID列表。
"""
if not plan_ids:
return
with get_db_session() as session:
try:
session.query(MonthlyPlan).filter(
MonthlyPlan.id.in_(plan_ids)
).update({"is_deleted": True}, synchronize_session=False)
session.commit()
logger.info(f"成功软删除了 {len(plan_ids)} 条月度计划。")
except Exception as e:
logger.error(f"软删除月度计划时发生错误: {e}")
session.rollback()
raise

View File

@@ -509,6 +509,19 @@ class CacheEntries(Base):
Index('idx_cache_entries_created_at', 'created_at'),
)
class MonthlyPlan(Base):
"""月层计划模型"""
__tablename__ = 'monthly_plans'
id = Column(Integer, primary_key=True, autoincrement=True)
plan_text = Column(Text, nullable=False)
target_month = Column(String(7), nullable=False, index=True) # "YYYY-MM"
is_deleted = Column(Boolean, nullable=False, default=False, index=True)
created_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
__table_args__ = (
Index('idx_monthlyplan_target_month_is_deleted', 'target_month', 'is_deleted'),
)
# 数据库引擎和会话管理
_engine = None