newtw3
This commit is contained in:
@@ -143,6 +143,49 @@ def init_db():
|
||||
except Exception:
|
||||
pass
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_acc ON chat_logs(acc_id)")
|
||||
|
||||
# ---- customer_orders 表 ----
|
||||
if _is_mysql():
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS customer_orders (
|
||||
id INTEGER PRIMARY KEY AUTO_INCREMENT,
|
||||
customer_id VARCHAR(128) NOT NULL,
|
||||
acc_id VARCHAR(128) DEFAULT '',
|
||||
order_id VARCHAR(64) NOT NULL,
|
||||
order_status VARCHAR(64) DEFAULT '',
|
||||
product_title VARCHAR(512) DEFAULT '',
|
||||
amount DECIMAL(10,2) DEFAULT 0,
|
||||
quantity INTEGER DEFAULT 0,
|
||||
buyer_note TEXT DEFAULT '',
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
""")
|
||||
idx_rows2 = conn.execute("SHOW INDEX FROM customer_orders").fetchall()
|
||||
exists2 = {str(r.get("Key_name", "")) for r in idx_rows2}
|
||||
if "idx_co_customer" not in exists2:
|
||||
conn.execute("CREATE INDEX idx_co_customer ON customer_orders(customer_id)")
|
||||
if "idx_co_order" not in exists2:
|
||||
conn.execute("CREATE UNIQUE INDEX idx_co_order ON customer_orders(order_id, order_status)")
|
||||
else:
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS customer_orders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
customer_id TEXT NOT NULL,
|
||||
acc_id TEXT DEFAULT '',
|
||||
order_id TEXT NOT NULL,
|
||||
order_status TEXT DEFAULT '',
|
||||
product_title TEXT DEFAULT '',
|
||||
amount REAL DEFAULT 0,
|
||||
quantity INTEGER DEFAULT 0,
|
||||
buyer_note TEXT DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
)
|
||||
""")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS idx_co_customer ON customer_orders(customer_id)")
|
||||
conn.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_co_order ON customer_orders(order_id, order_status)")
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
@@ -351,3 +394,63 @@ def get_latest_messages(limit: int = 20) -> List[Dict]:
|
||||
ORDER BY id DESC LIMIT ?
|
||||
"""), (limit,)).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
# ========== 订单相关 ==========
|
||||
|
||||
def upsert_order(
|
||||
customer_id: str,
|
||||
order_id: str,
|
||||
order_status: str = "",
|
||||
acc_id: str = "",
|
||||
product_title: str = "",
|
||||
amount: float = 0.0,
|
||||
quantity: int = 0,
|
||||
buyer_note: str = "",
|
||||
):
|
||||
"""写入或更新一条订单记录(按 order_id + order_status 去重)"""
|
||||
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
with _get_conn() as conn:
|
||||
if _is_mysql():
|
||||
conn.execute(
|
||||
"INSERT INTO customer_orders "
|
||||
"(customer_id, acc_id, order_id, order_status, product_title, amount, quantity, buyer_note, created_at, updated_at) "
|
||||
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) "
|
||||
"ON DUPLICATE KEY UPDATE customer_id=VALUES(customer_id), acc_id=VALUES(acc_id), "
|
||||
"product_title=VALUES(product_title), amount=VALUES(amount), quantity=VALUES(quantity), "
|
||||
"buyer_note=VALUES(buyer_note), updated_at=VALUES(updated_at)",
|
||||
(customer_id, acc_id, order_id, order_status, product_title, amount, quantity, buyer_note, ts, ts),
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
_sql("INSERT OR REPLACE INTO customer_orders "
|
||||
"(customer_id, acc_id, order_id, order_status, product_title, amount, quantity, buyer_note, created_at, updated_at) "
|
||||
"VALUES (?,?,?,?,?,?,?,?,?,?)"),
|
||||
(customer_id, acc_id, order_id, order_status, product_title, amount, quantity, buyer_note, ts, ts),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def get_customer_orders(customer_id: str, limit: int = 10) -> List[Dict]:
|
||||
"""查询某客户的订单记录(按时间倒序)"""
|
||||
with _get_conn() as conn:
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT order_id, order_status, product_title, amount, quantity, buyer_note, created_at, updated_at
|
||||
FROM customer_orders
|
||||
WHERE customer_id = ?
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT ?
|
||||
"""), (customer_id, limit)).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
def get_order_by_id(order_id: str) -> List[Dict]:
|
||||
"""按订单号查询所有状态变更记录"""
|
||||
with _get_conn() as conn:
|
||||
rows = conn.execute(_sql("""
|
||||
SELECT customer_id, order_id, order_status, product_title, amount, quantity, buyer_note, created_at, updated_at
|
||||
FROM customer_orders
|
||||
WHERE order_id = ?
|
||||
ORDER BY updated_at ASC
|
||||
"""), (order_id,)).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
Reference in New Issue
Block a user