feat: 看板重构 — 在制品看板 + 留言未读数
核心理念转变:
旧: 最近操作历史(谁做了什么)
新: 当前在制品状态(谁的活卡了多久)
后端:
- DashboardStats 新增 unread_messages(协同留言总数)
- 新增 GET /dashboard/wip-tasks(在制品端点)
- 按滞留时间升序排列,PENDING/WIP 任务倒计时
- 自动计算 duration_hours(已滞留小时数)
前端:
- 品质卡片: 新增留言未读数(紫色)
- 在制品看板: 替换原最近动态
每行显示: 状态徽标 | 任务名+负责人+身份证 | 滞留时长
- 滞留颜色: 48h+红 | 24h+橙 | 8h+黄 | <8h灰
- 系统通知可点击跳转
This commit is contained in:
@ -18,26 +18,30 @@ class DashboardStats(BaseModel):
|
||||
tasks_completed: int
|
||||
tasks_rejected: int
|
||||
tasks_rework: int
|
||||
# 通知
|
||||
# 通知 + 留言
|
||||
unread_notifications: int = 0
|
||||
unread_messages: int = 0
|
||||
|
||||
|
||||
class RecentActivity(BaseModel):
|
||||
action: str
|
||||
class WipTask(BaseModel):
|
||||
"""在制品 — 当前卡在手里的任务"""
|
||||
task_id: str
|
||||
task_name: str
|
||||
operator: str
|
||||
assignee: str # 中文姓名
|
||||
product_sn: str
|
||||
time: str
|
||||
remark: str | None = None
|
||||
status: str # PENDING / WIP
|
||||
received_at: str # 接收时间
|
||||
duration_hours: float # 已滞留小时数
|
||||
|
||||
|
||||
async def get_dashboard_stats(db: AsyncSession) -> DashboardStats:
|
||||
from app.models.product import Product
|
||||
from app.models.task import (
|
||||
Task, TASK_STATUS_PENDING, TASK_STATUS_WIP, TASK_STATUS_COMPLETED,
|
||||
TASK_STATUS_REJECTED, TASK_STATUS_ARCHIVED,
|
||||
TASK_STATUS_REJECTED,
|
||||
)
|
||||
from app.models.notification import Notification
|
||||
from app.models.message import ProductMessage
|
||||
|
||||
p_total = await db.scalar(select(func.count(Product.id)))
|
||||
p_pending = await db.scalar(select(func.count(Product.id)).where(Product.status == "pending"))
|
||||
@ -51,9 +55,11 @@ async def get_dashboard_stats(db: AsyncSession) -> DashboardStats:
|
||||
t_rejected = await db.scalar(select(func.count(Task.id)).where(Task.status == TASK_STATUS_REJECTED))
|
||||
t_rework = await db.scalar(select(func.count(Task.id)).where(Task.is_rework.is_(True)))
|
||||
|
||||
unread = await db.scalar(
|
||||
unread_notif = await db.scalar(
|
||||
select(func.count(Notification.id)).where(Notification.is_read.is_(False))
|
||||
)
|
||||
# 留言板未读数 — 全局(所有产品下的留言总数)
|
||||
unread_msg = await db.scalar(select(func.count(ProductMessage.id)))
|
||||
|
||||
return DashboardStats(
|
||||
products_total=p_total or 0,
|
||||
@ -66,80 +72,71 @@ async def get_dashboard_stats(db: AsyncSession) -> DashboardStats:
|
||||
tasks_completed=t_done or 0,
|
||||
tasks_rejected=t_rejected or 0,
|
||||
tasks_rework=t_rework or 0,
|
||||
unread_notifications=unread or 0,
|
||||
unread_notifications=unread_notif or 0,
|
||||
unread_messages=unread_msg or 0,
|
||||
)
|
||||
|
||||
|
||||
async def get_recent_activity(
|
||||
db: AsyncSession, limit: int = 10,
|
||||
since: datetime | None = None, until: datetime | None = None,
|
||||
) -> list[RecentActivity]:
|
||||
from app.models.task_log import TaskLog
|
||||
from app.models.task import Task
|
||||
async def get_wip_tasks(db: AsyncSession, limit: int = 20) -> list[WipTask]:
|
||||
"""
|
||||
在制品看板 — 当前所有 PENDING/WIP 状态的任务,
|
||||
按接收时间升序(最早接收的排最前 = 滞留最久)。
|
||||
"""
|
||||
from app.models.task import (
|
||||
Task, TASK_STATUS_PENDING, TASK_STATUS_WIP,
|
||||
)
|
||||
from app.models.product import Product
|
||||
from app.core.time_utils import BEIJING_TZ
|
||||
from app.core.time_utils import get_beijing_time, BEIJING_TZ
|
||||
|
||||
stmt = (
|
||||
select(TaskLog, Task.task_name, Product.serial_number, Task.assignee_id)
|
||||
.join(Task, TaskLog.task_id == Task.id)
|
||||
select(Task, Product.serial_number)
|
||||
.join(Product, Task.product_id == Product.id)
|
||||
.where(Task.status.in_([TASK_STATUS_PENDING, TASK_STATUS_WIP]))
|
||||
.order_by(Task.received_at.asc().nulls_first(), Task.created_at.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
if since:
|
||||
stmt = stmt.where(TaskLog.created_at >= since)
|
||||
if until:
|
||||
stmt = stmt.where(TaskLog.created_at <= until)
|
||||
stmt = stmt.order_by(TaskLog.created_at.desc()).limit(limit)
|
||||
result = await db.execute(stmt)
|
||||
rows = result.all()
|
||||
|
||||
# 收集 operator_id + 兜底 assignee_id → 批量翻译中文姓名
|
||||
raw_ids: set[str] = set()
|
||||
for row in rows:
|
||||
op = row[0].operator_id
|
||||
if op:
|
||||
raw_ids.add(op)
|
||||
elif row[3]: # assignee_id 兜底
|
||||
raw_ids.add(row[3])
|
||||
# 收集 assignee_id 批量翻译中文姓名
|
||||
raw_ids = list({t.assignee_id for t, _ in rows if t.assignee_id})
|
||||
name_map: dict[str, str] = {}
|
||||
if raw_ids:
|
||||
from app.services.mom_cache import get_display_names
|
||||
name_map = get_display_names(list(raw_ids))
|
||||
name_map = get_display_names(raw_ids)
|
||||
|
||||
activities: list[RecentActivity] = []
|
||||
for log, task_name, product_sn, task_assignee in rows:
|
||||
action_label = _action_label(log.action_type)
|
||||
# 强制转北京时间显示
|
||||
t = log.created_at
|
||||
if t:
|
||||
if t.tzinfo is None:
|
||||
t = t.replace(tzinfo=BEIJING_TZ)
|
||||
now = get_beijing_time()
|
||||
wip_list: list[WipTask] = []
|
||||
for task, product_sn in rows:
|
||||
# 计算滞留时长
|
||||
start = task.received_at or task.created_at
|
||||
if start:
|
||||
if start.tzinfo is None:
|
||||
start = start.replace(tzinfo=BEIJING_TZ)
|
||||
else:
|
||||
t = t.astimezone(BEIJING_TZ)
|
||||
time_str = t.strftime("%m-%d %H:%M")
|
||||
start = start.astimezone(BEIJING_TZ)
|
||||
hours = round((now - start).total_seconds() / 3600, 1)
|
||||
recv_str = start.strftime("%m-%d %H:%M")
|
||||
else:
|
||||
time_str = ""
|
||||
# operator_id: 优先显示中文姓名 → 英文用户名兜底 → 任务负责人兜底 → 空
|
||||
op = log.operator_id or task_assignee or ""
|
||||
op_display = name_map.get(op, op)
|
||||
activities.append(RecentActivity(
|
||||
action=action_label,
|
||||
task_name=task_name or "",
|
||||
operator=op_display,
|
||||
hours = 0
|
||||
recv_str = ""
|
||||
|
||||
wip_list.append(WipTask(
|
||||
task_id=str(task.id),
|
||||
task_name=task.task_name,
|
||||
assignee=name_map.get(task.assignee_id or "", task.assignee_id or "未分配"),
|
||||
product_sn=product_sn or "",
|
||||
time=time_str,
|
||||
remark=log.remark,
|
||||
status=task.status,
|
||||
received_at=recv_str,
|
||||
duration_hours=hours,
|
||||
))
|
||||
return activities
|
||||
return wip_list
|
||||
|
||||
|
||||
def _action_label(action_type: str) -> str:
|
||||
labels = {
|
||||
"create": "创建任务",
|
||||
"receive": "确认接收",
|
||||
"complete": "完成任务",
|
||||
"transfer": "完工转交",
|
||||
"reject": "品质驳回",
|
||||
"end": "结束分支",
|
||||
"create": "创建任务", "receive": "确认接收", "complete": "完成任务",
|
||||
"transfer": "完工转交", "reject": "品质驳回", "end": "结束分支",
|
||||
"recall": "撤回转交",
|
||||
}
|
||||
return labels.get(action_type, action_type)
|
||||
|
||||
Reference in New Issue
Block a user