feat: 累积功能变更 — 聊天集成、租户管理、小程序更新、ETL 增强、迁移脚本
包含多个会话的累积代码变更: - backend: AI 聊天服务、触发器调度、认证增强、WebSocket、调度器最小间隔 - admin-web: ETL 状态页、任务管理、调度配置、登录优化 - miniprogram: 看板页面、聊天集成、UI 组件、导航更新 - etl: DWS 新任务(finance_area_daily/board_cache)、连接器增强 - tenant-admin: 项目初始化 - db: 19 个迁移脚本(etl_feiqiu 11 + zqyy_app 8) - packages/shared: 枚举和工具函数更新 - tools: 数据库工具、报表生成、健康检查 - docs: PRD/架构/部署/合约文档更新 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import json
|
||||
import logging
|
||||
|
||||
from fastapi import HTTPException
|
||||
from app.trace.decorators import trace_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -52,16 +53,67 @@ def _record_history(
|
||||
|
||||
|
||||
|
||||
def ai_analyze_note(note_id: int) -> int | None:
|
||||
@trace_service(description_zh="ai_analyze_note", description_en="Ai Analyze Note")
|
||||
async def ai_analyze_note(note_id: int, site_id: int, member_id: int, content: str, user_name: str = "") -> int | None:
|
||||
"""
|
||||
AI 应用 6 备注分析接口(占位)。
|
||||
AI 应用 6 备注分析:调用百炼 Application API 获取评分。
|
||||
|
||||
P5 AI 集成层实现后替换此占位函数。
|
||||
当前返回 None 表示 AI 未就绪,跳过评分逻辑。
|
||||
CHANGE 2026-03-27 | 打通 AI 应用 6 调用链
|
||||
仅执行 App6 评分,不触发 App8 线索整合(后续统一处理)。
|
||||
返回 score(1-10),失败返回 None。
|
||||
"""
|
||||
return None
|
||||
try:
|
||||
from app.ai.config import AIConfig
|
||||
from app.ai.dashscope_client import DashScopeClient
|
||||
import json
|
||||
|
||||
config = AIConfig.from_env()
|
||||
client = DashScopeClient(api_key=config.api_key, workspace_id=config.workspace_id)
|
||||
|
||||
# 构建 prompt(简化版,直接传给百炼应用)
|
||||
prompt = json.dumps({
|
||||
"site_id": site_id,
|
||||
"member_id": member_id,
|
||||
"note_content": content,
|
||||
"noted_by_name": user_name,
|
||||
}, ensure_ascii=False)
|
||||
|
||||
result, tokens_used, _ = await client.call_app(config.app_id_6_note, prompt)
|
||||
score = result.get("score") if isinstance(result, dict) else None
|
||||
|
||||
if score is not None:
|
||||
score = max(1, min(10, int(score)))
|
||||
logger.info("App6 备注评分完成: note_id=%d score=%d tokens=%d", note_id, score, tokens_used)
|
||||
|
||||
return score
|
||||
except Exception:
|
||||
logger.warning("App6 备注评分失败: note_id=%d", note_id, exc_info=True)
|
||||
return None
|
||||
|
||||
|
||||
async def _async_ai_score(note_id: int, site_id: int, member_id: int, content: str) -> None:
|
||||
"""后台异步执行 AI 评分,不阻塞 API 响应。"""
|
||||
try:
|
||||
ai_score_val = await ai_analyze_note(
|
||||
note_id=note_id, site_id=site_id, member_id=member_id, content=content,
|
||||
)
|
||||
if ai_score_val is not None:
|
||||
conn = _get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE biz.notes SET ai_score = %s, updated_at = NOW() WHERE id = %s",
|
||||
(ai_score_val, note_id),
|
||||
)
|
||||
conn.commit()
|
||||
logger.info("AI 评分已写入: note_id=%d ai_score=%d", note_id, ai_score_val)
|
||||
finally:
|
||||
conn.close()
|
||||
except Exception:
|
||||
logger.warning("后台 AI 评分失败: note_id=%d", note_id, exc_info=True)
|
||||
|
||||
|
||||
@trace_service("创建备注", "Create note")
|
||||
async def create_note(
|
||||
site_id: int,
|
||||
user_id: int,
|
||||
@@ -71,6 +123,7 @@ async def create_note(
|
||||
task_id: int | None = None,
|
||||
rating_service_willingness: int | None = None,
|
||||
rating_revisit_likelihood: int | None = None,
|
||||
score: int | None = None,
|
||||
) -> dict:
|
||||
"""
|
||||
创建备注。
|
||||
@@ -91,6 +144,7 @@ async def create_note(
|
||||
for label, val in [
|
||||
("再次服务意愿评分", rating_service_willingness),
|
||||
("再来店可能性评分", rating_revisit_likelihood),
|
||||
("备注星星评分", score),
|
||||
]:
|
||||
if val is not None and (val < 1 or val > 5):
|
||||
raise HTTPException(
|
||||
@@ -139,17 +193,17 @@ async def create_note(
|
||||
INSERT INTO biz.notes
|
||||
(site_id, user_id, target_type, target_id, type,
|
||||
content, rating_service_willingness,
|
||||
rating_revisit_likelihood, task_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
rating_revisit_likelihood, task_id, score)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id, site_id, user_id, target_type, target_id,
|
||||
type, content, rating_service_willingness,
|
||||
rating_revisit_likelihood, task_id,
|
||||
ai_score, ai_analysis, created_at, updated_at
|
||||
ai_score, ai_analysis, created_at, updated_at, score
|
||||
""",
|
||||
(
|
||||
site_id, user_id, target_type, target_id, note_type,
|
||||
content, rating_service_willingness,
|
||||
rating_revisit_likelihood, task_id,
|
||||
rating_revisit_likelihood, task_id, score,
|
||||
),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
@@ -169,26 +223,11 @@ async def create_note(
|
||||
"ai_analysis": row[11],
|
||||
"created_at": row[12].isoformat() if row[12] else None,
|
||||
"updated_at": row[13].isoformat() if row[13] else None,
|
||||
"score": row[14],
|
||||
}
|
||||
|
||||
# 若 type='follow_up',触发 AI 分析并标记回访任务完成
|
||||
# 若 type='follow_up',标记回访任务完成(不依赖 AI 评分)
|
||||
if note_type == "follow_up" and task_id is not None:
|
||||
# 保留 AI 占位调用(P5 接入时调用链不变)
|
||||
ai_score = ai_analyze_note(note["id"])
|
||||
|
||||
if ai_score is not None:
|
||||
# 更新备注的 ai_score
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE biz.notes
|
||||
SET ai_score = %s, updated_at = NOW()
|
||||
WHERE id = %s
|
||||
""",
|
||||
(ai_score, note["id"]),
|
||||
)
|
||||
note["ai_score"] = ai_score
|
||||
|
||||
# 不论 ai_score 如何,有备注即标记回访任务完成(T4)
|
||||
if task_info and task_info["status"] == "active":
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -209,13 +248,17 @@ async def create_note(
|
||||
new_status="completed",
|
||||
old_task_type=task_info["task_type"],
|
||||
new_task_type=task_info["task_type"],
|
||||
detail={
|
||||
"note_id": note["id"],
|
||||
"ai_score": ai_score,
|
||||
},
|
||||
detail={"note_id": note["id"]},
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
|
||||
# CHANGE 2026-03-27 | AI 评分:后台异步执行,不阻塞 API 响应
|
||||
# 备注先返回给前端(aiScore=null),AI 评分完成后写入数据库
|
||||
# 前端下次加载页面时自动获取最新 aiScore
|
||||
import asyncio
|
||||
asyncio.create_task(_async_ai_score(note["id"], site_id, target_id, content))
|
||||
|
||||
return note
|
||||
|
||||
except HTTPException:
|
||||
@@ -228,6 +271,7 @@ async def create_note(
|
||||
conn.close()
|
||||
|
||||
|
||||
@trace_service("查询备注列表", "Get notes")
|
||||
async def get_notes(
|
||||
site_id: int, target_type: str, target_id: int
|
||||
) -> list[dict]:
|
||||
@@ -280,6 +324,7 @@ async def get_notes(
|
||||
conn.close()
|
||||
|
||||
|
||||
@trace_service("删除备注", "Delete note")
|
||||
async def delete_note(note_id: int, user_id: int, site_id: int) -> dict:
|
||||
"""
|
||||
删除备注。
|
||||
|
||||
Reference in New Issue
Block a user