根因: ProductMessage.created_at 使用 datetime.utcnow (UTC),
与其他所有模型使用的 get_beijing_time 不一致。
看板留言抽屉显示的时间比实际晚8小时。
修复:
1. message.py: DateTime → DateTime(timezone=True)
default 从 datetime.utcnow → get_beijing_time
2. dashboard_service.py: 兜底转换已有数据
naive UTC → replace(tzinfo=UTC) → astimezone(Beijing)
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""产品协同留言板模型"""
|
|
import uuid
|
|
from datetime import datetime
|
|
from sqlalchemy import String, Text, DateTime, ForeignKey
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.models.base import Base
|
|
from app.core.time_utils import get_beijing_time
|
|
|
|
|
|
class ProductMessage(Base):
|
|
__tablename__ = "product_messages"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4,
|
|
)
|
|
product_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
ForeignKey("products.id", ondelete="CASCADE"),
|
|
index=True,
|
|
nullable=False,
|
|
comment="所属产品 ID",
|
|
)
|
|
operator_id: Mapped[str] = mapped_column(
|
|
String(50), nullable=False, comment="留言人姓名或工号",
|
|
)
|
|
content: Mapped[str] = mapped_column(
|
|
Text, nullable=False, comment="留言内容",
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=get_beijing_time, comment="留言时间(北京时间)",
|
|
)
|