diff --git a/api.py b/api.py index ab505c2..53dfe27 100644 --- a/api.py +++ b/api.py @@ -1384,8 +1384,8 @@ def list_experts(category: str | None = None, authorization: str | None = Header cur = conn.cursor() expert_ids = [r["id"] for r in results] cur.execute( - "SELECT DISTINCT expert_id FROM expert_consultations " - "WHERE user_phone = %s AND expert_id = ANY(%s)", + "SELECT DISTINCT expert_id FROM expert_services " + "WHERE user_phone = %s AND expert_id = ANY(%s) AND status IN ('pending', 'active')", (phone, expert_ids), ) connected_ids = {r[0] for r in cur.fetchall()} @@ -2750,31 +2750,41 @@ def dashboard_timeline(authorization: str | None = Header(default=None)): @app.get("/team") def my_team(authorization: str | None = Header(default=None)): - """我的团队:咨询过的专家自动沉淀""" + """我的团队:已建立服务联系的专家(pending/active),含咨询统计""" phone = get_current_phone(authorization) if not phone: raise HTTPException(401, "未登录") conn = _matter_dsn() try: cur = conn.cursor() - # 从 expert_consultations 聚合关系 + # 从 expert_services 查已建立联系的专家,左连 expert_consultations 获取咨询统计 cur.execute( "SELECT e.id, e.name, e.avatar_url, e.category, e.title, e.expertise, " - "COUNT(c.id) as cnt, MAX(c.replied_at) as last_at, " - "(SELECT c2.topic FROM expert_consultations c2 WHERE c2.expert_id = e.id AND c2.user_phone = %s ORDER BY c2.created_at DESC LIMIT 1) as last_topic " - "FROM experts e JOIN expert_consultations c ON c.expert_id = e.id " - "WHERE c.user_phone = %s " - "GROUP BY e.id, e.name, e.avatar_url, e.category, e.title, e.expertise " - "ORDER BY last_at DESC NULLS LAST", - (phone, phone), + "es.status, es.created_at, " + "COALESCE(cnt.c, 0) as consultation_count, " + "cnt.last_at, cnt.last_topic " + "FROM expert_services es " + "JOIN experts e ON es.expert_id = e.id " + "LEFT JOIN LATERAL (" + " SELECT COUNT(*) as c, MAX(replied_at) as last_at, " + " (SELECT topic FROM expert_consultations c2 " + " WHERE c2.expert_id = es.expert_id AND c2.user_phone = es.user_phone " + " ORDER BY c2.created_at DESC LIMIT 1) as last_topic " + " FROM expert_consultations c " + " WHERE c.expert_id = es.expert_id AND c.user_phone = es.user_phone" + ") cnt ON true " + "WHERE es.user_phone = %s AND es.status IN ('pending', 'active') " + "ORDER BY es.created_at DESC", + (phone,), ) rows = cur.fetchall() results = [{ "expert_id": r[0], "name": r[1], "avatar_url": r[2], "category": r[3], - "title": r[4], "expertise": r[5], "online": False, # 在线状态待接入 - "consultation_count": r[6], - "last_consulted_at": r[7].isoformat() if r[7] else None, - "last_topic": r[8], "tags": r[5], + "title": r[4], "expertise": r[5], "online": False, + "service_status": r[6], + "consultation_count": r[8], + "last_consulted_at": r[9].isoformat() if r[9] else None, + "last_topic": r[10], "tags": r[5], } for r in rows] return {"results": results} finally: