diff --git a/backend/app/core/database.py b/backend/app/core/database.py index 130594d..a8192b9 100644 --- a/backend/app/core/database.py +++ b/backend/app/core/database.py @@ -9,6 +9,9 @@ engine = create_async_engine( max_overflow=10, # 超出 pool_size 时最多再创建的连接数 pool_recycle=3600, # 连接回收时间(秒),防止 MySQL 8 小时断连 pool_pre_ping=True, # 每次取出连接前先 ping 检测可用性 + connect_args={ + "server_settings": {"TimeZone": "Asia/Shanghai"}, # PG 会话级北京时间 + }, ) AsyncSessionLocal = async_sessionmaker( diff --git a/backend/app/services/dashboard_service.py b/backend/app/services/dashboard_service.py index 8786908..f2fe875 100644 --- a/backend/app/services/dashboard_service.py +++ b/backend/app/services/dashboard_service.py @@ -73,6 +73,7 @@ async def get_recent_activity(db: AsyncSession, limit: int = 10) -> list[RecentA from app.models.task_log import TaskLog from app.models.task import Task from app.models.product import Product + from app.core.time_utils import BEIJING_TZ stmt = ( select(TaskLog, Task.task_name, Product.serial_number) @@ -84,14 +85,33 @@ async def get_recent_activity(db: AsyncSession, limit: int = 10) -> list[RecentA result = await db.execute(stmt) rows = result.all() + # 收集 operator_id → 批量翻译中文姓名 + operator_ids = list({row[0].operator_id for row in rows if row[0].operator_id}) + name_map: dict[str, str] = {} + if operator_ids: + from app.services.mom_cache import get_display_names + name_map = get_display_names(operator_ids) + activities: list[RecentActivity] = [] for log, task_name, product_sn in rows: action_label = _action_label(log.action_type) - time_str = log.created_at.strftime("%m-%d %H:%M") if log.created_at else "" + # 强制转北京时间显示 + t = log.created_at + if t: + if t.tzinfo is None: + t = t.replace(tzinfo=BEIJING_TZ) + else: + t = t.astimezone(BEIJING_TZ) + time_str = t.strftime("%m-%d %H:%M") + else: + time_str = "" + # operator_id: 优先显示中文姓名 → 英文用户名兜底 → 无记录时为空 + op = log.operator_id + op_display = name_map.get(op, op or "") activities.append(RecentActivity( action=action_label, task_name=task_name or "", - operator=log.operator_id or "系统", + operator=op_display, product_sn=product_sn or "", time=time_str, remark=log.remark,