newtw
This commit is contained in:
@@ -113,6 +113,11 @@ def init_db():
|
||||
conn.execute("CREATE INDEX idx_ts ON chat_logs(timestamp)")
|
||||
if "idx_acc" not in exists:
|
||||
conn.execute("CREATE INDEX idx_acc ON chat_logs(acc_id)")
|
||||
# 添加 image_urls 列(如果不存在)
|
||||
try:
|
||||
conn.execute("ALTER TABLE chat_logs ADD COLUMN image_urls TEXT DEFAULT ''")
|
||||
except Exception:
|
||||
pass # 列已存在
|
||||
else:
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS chat_logs (
|
||||
@@ -133,6 +138,10 @@ def init_db():
|
||||
conn.execute("ALTER TABLE chat_logs ADD COLUMN acc_id TEXT DEFAULT ''")
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
conn.execute("ALTER TABLE chat_logs ADD COLUMN image_urls TEXT DEFAULT ''")
|
||||
except Exception:
|
||||
pass
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_acc ON chat_logs(acc_id)")
|
||||
conn.commit()
|
||||
|
||||
@@ -150,15 +159,16 @@ def log_message(
|
||||
acc_id: str = "", # 店铺账号ID
|
||||
platform: str = "",
|
||||
msg_type: int = 0,
|
||||
image_urls: str = "", # 图片URL列表,用\n分隔
|
||||
):
|
||||
"""记录一条聊天消息"""
|
||||
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
with _get_conn() as conn:
|
||||
conn.execute(
|
||||
_sql("INSERT INTO chat_logs "
|
||||
"(customer_id, customer_name, acc_id, platform, direction, message, msg_type, timestamp) "
|
||||
"VALUES (?,?,?,?,?,?,?,?)"),
|
||||
(customer_id, customer_name, acc_id, platform, direction, message, msg_type, ts),
|
||||
"(customer_id, customer_name, acc_id, platform, direction, message, msg_type, timestamp, image_urls) "
|
||||
"VALUES (?,?,?,?,?,?,?,?,?)"),
|
||||
(customer_id, customer_name, acc_id, platform, direction, message, msg_type, ts, image_urls),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
@@ -198,10 +208,10 @@ def get_customers(limit: int = 100) -> List[Dict]:
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
def get_conversation(customer_id: str, limit: int = 200) -> List[Dict]:
|
||||
def get_conversation(customer_id: str, limit: int = 200, acc_id: str = "") -> List[Dict]:
|
||||
"""返回某客户的最近对话记录(按时间升序)"""
|
||||
# 忽略 acc_id 过滤,实现全店铺记忆
|
||||
with _get_conn() as conn:
|
||||
# 核心修复:先取最新的 limit 条,再按时间正序排列
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT * FROM (
|
||||
SELECT id, direction, message, msg_type, timestamp, acc_id
|
||||
@@ -216,24 +226,15 @@ def get_conversation(customer_id: str, limit: int = 200) -> List[Dict]:
|
||||
|
||||
|
||||
def get_recent_conversation(customer_id: str, acc_id: str = "", limit: int = 10) -> List[Dict]:
|
||||
"""返回某客户近期对话(同店铺),用于企微推送保持连贯"""
|
||||
"""返回某客户近期对话,忽略 acc_id 过滤"""
|
||||
with _get_conn() as conn:
|
||||
if acc_id:
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT id, direction, message, timestamp, acc_id
|
||||
FROM chat_logs
|
||||
WHERE customer_id = ? AND acc_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"""), (customer_id, acc_id, limit)).fetchall()
|
||||
else:
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT id, direction, message, timestamp, acc_id
|
||||
FROM chat_logs
|
||||
WHERE customer_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"""), (customer_id, limit)).fetchall()
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT id, direction, message, timestamp, acc_id
|
||||
FROM chat_logs
|
||||
WHERE customer_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"""), (customer_id, limit)).fetchall()
|
||||
out = [dict(r) for r in reversed(rows)]
|
||||
return out
|
||||
|
||||
|
||||
Reference in New Issue
Block a user