fix: 看板动态时间改为北京时间 + 操作人显示中文姓名
1. 时区修复: - database.py: PG连接池会话级设置 TimeZone=Asia/Shanghai - dashboard_service.py: 格式化时间显式 astimezone(BEIJING_TZ) - 兜底: tzinfo为None时默认当作北京时间处理 2. 操作人中文姓名: - 收集所有operator_id → 调用mom_cache批量翻译 - 优先中文姓名 → 英文用户名兜底 - operator_id为None时不再显示"系统",留空
This commit is contained in:
@ -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(
|
||||
|
||||
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user