refactor(db): 阶段B+C - MySQL/Postgres migration + 方言适配器
migration - migrations/mysql/ 8 个文件:BIGINT AUTO_INCREMENT、VARCHAR 主键、 TINYINT(1) 布尔、INSERT IGNORE、ON DUPLICATE KEY UPDATE - migrations/postgres/ 8 个文件:BIGSERIAL、BOOLEAN、ON CONFLICT DO UPDATE - 跳过 SQLite 历史遗留列(auto_create_delivery_rule 等)在 mysql/pg 00006 不创建,00008 不删除 方言适配器 (internal/db/dialect.go) - DialectUpsert: SQLite/PG 用 ON CONFLICT DO UPDATE + EXCLUDED.col; MySQL 用 ON DUPLICATE KEY UPDATE + VALUES(col) - DialectInsertIgnore + DialectInsertIgnorePrefix: MySQL INSERT IGNORE; SQLite/PG ON CONFLICT DO NOTHING - DialectQuote: MySQL/SQLite 反引号,Postgres 双引号(处理 key 等保留字列名) 业务 SQL 改造(16 处 upsert) - db: cookies/extras(系统设置/商品信息/通知绑定)/orders/reply/automation 全部改用方言适配器;item_replay Set 改 DELETE+INSERT(无自然唯一键) - server: notification_handlers/settings_handlers/default_reply_handlers 的 ON CONFLICT/INSERT OR REPLACE 改用 db.DialectUpsert - 各 repository struct 加 Dialect 字段,NewStore 注入 验证:SQLite 全测试通过(回归),MySQL/PG migration 已就位待真实实例验证 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
0fa5d6617c
commit
36a7f74154
20 files changed
+511
-423
No files matched your search
@@ -11,7 +11,10 @@ import (
|
||||
//
|
||||
// 自动化中心不区分触发来源:WS 系统事件、计划任务、后台手动触发都通过
|
||||
// trigger_type + action 编排表达;真正的防重由 automation_runs.trigger_key 保证。
|
||||
type AutomationRules struct{ DB *sql.DB }
|
||||
type AutomationRules struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// AutomationRule 是一条自动化规则。规则只描述“什么时候、对哪个商品生效”,
|
||||
// 具体做什么放在 AutomationAction 中,便于组合付款发货、评价赠品、求评价等流程。
|
||||
@@ -247,10 +250,10 @@ SELECT a.id,a.rule_id,a.action_type,COALESCE(a.card_id,0),COALESCE(c.name,''),a.
|
||||
// TryStartRun 以 UNIQUE(rule_id, trigger_key) 作为持久化防重。
|
||||
// 返回 started=false 表示该规则对该触发已执行或正在执行,调用方应直接跳过。
|
||||
func (a *AutomationRules) TryStartRun(ctx context.Context, run AutomationRun) (int64, bool, error) {
|
||||
res, err := a.DB.ExecContext(ctx, `
|
||||
INSERT OR IGNORE INTO automation_runs
|
||||
res, err := a.DB.ExecContext(ctx,
|
||||
dialectInsertIgnorePrefix(a.Dialect)+` INTO automation_runs
|
||||
(rule_id,cookie_id,item_id,order_id,buyer_id,chat_id,trigger_type,trigger_key,status,raw_event_json)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)`,
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)`+dialectInsertIgnore(a.Dialect, []string{"rule_id", "trigger_key"}),
|
||||
run.RuleID, run.CookieID, run.ItemID, run.OrderID, run.BuyerID, run.ChatID, run.TriggerType,
|
||||
run.TriggerKey, "running", validJSON(run.RawEventJSON))
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,8 @@ import (
|
||||
|
||||
// Cookies 闲鱼账号(cookie)相关操作。
|
||||
type Cookies struct {
|
||||
DB *sql.DB
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Save 保存/更新 cookie。user_id 为 0 时:复用现有记录的 user_id,若无则报错
|
||||
@@ -28,11 +29,12 @@ func (c *Cookies) Save(ctx context.Context, cookieID, cookieValue string, userID
|
||||
}
|
||||
_, err := c.DB.ExecContext(ctx,
|
||||
`INSERT INTO cookies (id, value, user_id, updated_at)
|
||||
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
value=excluded.value,
|
||||
user_id=excluded.user_id,
|
||||
updated_at=CURRENT_TIMESTAMP`,
|
||||
VALUES (?, ?, ?, CURRENT_TIMESTAMP)`+
|
||||
dialectUpsert(c.Dialect, []string{"id"}, map[string]string{
|
||||
"value": "excluded.value",
|
||||
"user_id": "excluded.user_id",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cookieID, cookieValue, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ type Card struct {
|
||||
}
|
||||
|
||||
// Items 商品信息操作。
|
||||
type Items struct{ DB *sql.DB }
|
||||
type Items struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Get 取某账号下某商品信息。不存在返回 ErrNotFound。
|
||||
func (i *Items) Get(ctx context.Context, cookieID, itemID string) (*ItemInfo, error) {
|
||||
@@ -90,7 +93,10 @@ func (i *Items) MultiQuantityDelivery(ctx context.Context, cookieID, itemID stri
|
||||
}
|
||||
|
||||
// Cards 卡券操作。
|
||||
type Cards struct{ DB *sql.DB }
|
||||
type Cards struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// ConsumeBatchData 消费一条批量数据卡券(data 类型),返回内容。
|
||||
// ConsumeBatchData 取 data_content 第一行,删除已消费行,发完置空。
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package db
|
||||
|
||||
import "strings"
|
||||
|
||||
// DialectUpsert 生成 UPSERT 子句,处理三种数据库的语法差异。
|
||||
//
|
||||
// SQLite/Postgres: ON CONFLICT (conflictCols) DO UPDATE SET col=expr, ...
|
||||
// MySQL: ON DUPLICATE KEY UPDATE col=expr, ...
|
||||
//
|
||||
// updateAssignments 是 列名 -> SQL表达式 的映射。表达式里用 "EXCLUDED.col"
|
||||
// 表示插入值(SQLite/Postgres 语法),MySQL 下自动转成 "VALUES(col)"。
|
||||
// 其他表达式(如 "CURRENT_TIMESTAMP")原样保留。
|
||||
func DialectUpsert(d Dialect, conflictCols []string, updateAssignments map[string]string) string {
|
||||
if len(conflictCols) == 0 || len(updateAssignments) == 0 {
|
||||
return ""
|
||||
}
|
||||
keys := make([]string, 0, len(updateAssignments))
|
||||
for k := range updateAssignments {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sortStrings(keys)
|
||||
sets := make([]string, 0, len(keys))
|
||||
for _, col := range keys {
|
||||
expr := updateAssignments[col]
|
||||
if d == DialectMySQL {
|
||||
if strings.HasPrefix(expr, "EXCLUDED.") {
|
||||
field := strings.TrimPrefix(expr, "EXCLUDED.")
|
||||
expr = "VALUES(" + field + ")"
|
||||
}
|
||||
}
|
||||
sets = append(sets, col+"="+expr)
|
||||
}
|
||||
switch d {
|
||||
case DialectMySQL:
|
||||
return " ON DUPLICATE KEY UPDATE " + strings.Join(sets, ", ")
|
||||
default:
|
||||
return " ON CONFLICT (" + strings.Join(conflictCols, ", ") + ") DO UPDATE SET " + strings.Join(sets, ", ")
|
||||
}
|
||||
}
|
||||
|
||||
// dialectUpsert 包内别名(db 包内部用小写)。
|
||||
func dialectUpsert(d Dialect, cols []string, m map[string]string) string {
|
||||
return DialectUpsert(d, cols, m)
|
||||
}
|
||||
|
||||
// DialectInsertIgnore 返回"冲突即忽略"子句(VALUES 之后拼接)。
|
||||
//
|
||||
// SQLite/Postgres: ON CONFLICT (cols) DO NOTHING
|
||||
// MySQL: 空(调用方应用 DialectInsertIgnorePrefix)
|
||||
func DialectInsertIgnore(d Dialect, conflictCols []string) string {
|
||||
if d == DialectMySQL {
|
||||
return ""
|
||||
}
|
||||
if len(conflictCols) == 0 {
|
||||
return ""
|
||||
}
|
||||
return " ON CONFLICT (" + strings.Join(conflictCols, ", ") + ") DO NOTHING"
|
||||
}
|
||||
|
||||
func dialectInsertIgnore(d Dialect, cols []string) string { return DialectInsertIgnore(d, cols) }
|
||||
|
||||
// DialectInsertIgnorePrefix 返回 INSERT 前缀(MySQL 用 INSERT IGNORE)。
|
||||
func DialectInsertIgnorePrefix(d Dialect) string {
|
||||
if d == DialectMySQL {
|
||||
return "INSERT IGNORE"
|
||||
}
|
||||
return "INSERT"
|
||||
}
|
||||
|
||||
func dialectInsertIgnorePrefix(d Dialect) string { return DialectInsertIgnorePrefix(d) }
|
||||
|
||||
// DialectQuote 返回方言下的标识符引用。
|
||||
//
|
||||
// SQLite/MySQL: `name`(反引号)
|
||||
// Postgres: "name"(双引号)
|
||||
func DialectQuote(d Dialect, name string) string {
|
||||
if d == DialectPostgres {
|
||||
return "\"" + name + "\""
|
||||
}
|
||||
return "`" + name + "`"
|
||||
}
|
||||
|
||||
func dialectQuote(d Dialect, name string) string { return DialectQuote(d, name) }
|
||||
|
||||
// sortStrings 原地排序字符串切片。
|
||||
func sortStrings(s []string) {
|
||||
for i := 1; i < len(s); i++ {
|
||||
for j := i; j > 0 && s[j-1] > s[j]; j-- {
|
||||
s[j-1], s[j] = s[j], s[j-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
-9
@@ -82,10 +82,21 @@ func (k *Keywords) DeleteByIndex(ctx context.Context, cookieID string, index int
|
||||
|
||||
// Set 设置指定商品回复。
|
||||
func (i *ItemReplies) Set(ctx context.Context, cookieID, itemID, content string) error {
|
||||
_, err := i.DB.ExecContext(ctx,
|
||||
`INSERT OR REPLACE INTO item_replay (item_id, cookie_id, reply_content, updated_at)
|
||||
VALUES (?,?,?,CURRENT_TIMESTAMP)`, itemID, cookieID, content)
|
||||
return err
|
||||
tx, err := i.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
// 先删后插,跨 SQLite/MySQL/Postgres 一致(item_replay 无自然唯一键)。
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM item_replay WHERE cookie_id=? AND item_id=?`, cookieID, itemID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO item_replay (item_id, cookie_id, reply_content, updated_at)
|
||||
VALUES (?,?,?,CURRENT_TIMESTAMP)`, itemID, cookieID, content); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// Delete 删除指定商品回复。
|
||||
@@ -218,7 +229,7 @@ func (n *Notifications) SetBindings(ctx context.Context, cookieID string, channe
|
||||
}
|
||||
for _, cid := range channelIDs {
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO message_notifications (cookie_id, channel_id, enabled) VALUES (?,?,1)`,
|
||||
`INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES (?,?,1)`,
|
||||
cookieID, cid); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -229,7 +240,10 @@ func (n *Notifications) SetBindings(ctx context.Context, cookieID string, channe
|
||||
// ---- 系统设置 ----
|
||||
|
||||
// SystemSettings 系统设置操作。
|
||||
type SystemSettings struct{ DB *sql.DB }
|
||||
type SystemSettings struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Get 取单项设置。
|
||||
func (s *SystemSettings) Get(ctx context.Context, key string) (string, error) {
|
||||
@@ -264,8 +278,13 @@ func (s *SystemSettings) All(ctx context.Context) (map[string]string, error) {
|
||||
|
||||
// Set 设置单项。
|
||||
func (s *SystemSettings) Set(ctx context.Context, key, value string) error {
|
||||
keyCol := dialectQuote(s.Dialect, "key")
|
||||
_, err := s.DB.ExecContext(ctx,
|
||||
`INSERT OR REPLACE INTO system_settings (key, value, updated_at) VALUES (?,?,CURRENT_TIMESTAMP)`,
|
||||
`INSERT INTO system_settings (`+keyCol+`, value, updated_at) VALUES (?,?,CURRENT_TIMESTAMP)`+
|
||||
dialectUpsert(s.Dialect, []string{keyCol}, map[string]string{
|
||||
"value": "EXCLUDED.value",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
key, value)
|
||||
return err
|
||||
}
|
||||
@@ -336,9 +355,19 @@ func (i *Items) AllForCookie(ctx context.Context, cookieID string) ([]ItemInfoRo
|
||||
// Upsert 插入或更新商品信息。
|
||||
func (i *Items) Upsert(ctx context.Context, r *ItemInfoRow) error {
|
||||
_, err := i.DB.ExecContext(ctx,
|
||||
`INSERT OR REPLACE INTO item_info (cookie_id, item_id, item_title, item_description,
|
||||
`INSERT INTO item_info (cookie_id, item_id, item_title, item_description,
|
||||
item_category, item_price, item_detail, is_multi_spec, multi_quantity_delivery, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)`,
|
||||
VALUES (?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)`+
|
||||
dialectUpsert(i.Dialect, []string{"cookie_id", "item_id"}, map[string]string{
|
||||
"item_title": "EXCLUDED.item_title",
|
||||
"item_description": "EXCLUDED.item_description",
|
||||
"item_category": "EXCLUDED.item_category",
|
||||
"item_price": "EXCLUDED.item_price",
|
||||
"item_detail": "EXCLUDED.item_detail",
|
||||
"is_multi_spec": "EXCLUDED.is_multi_spec",
|
||||
"multi_quantity_delivery": "EXCLUDED.multi_quantity_delivery",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
r.CookieID, r.ItemID, nullable(r.ItemTitle), nullable(r.ItemDescription),
|
||||
nullable(r.ItemCategory), nullable(r.ItemPrice), nullable(r.ItemDetail),
|
||||
boolToInt(r.IsMultiSpec), boolToInt(r.MultiQuantityDelivery))
|
||||
|
||||
@@ -1,96 +1,84 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
-- 闲鱼管家初始 schema。
|
||||
-- 把历史上 12+ 处运行时 ALTER TABLE 补齐到 CREATE,并修复 schema 不一致:
|
||||
-- - orders.system_shipped:原 CREATE 缺失却被 insert_or_update_order 引用 → 补齐
|
||||
-- - orders.receiver_city:原为运行时补齐 → 补齐
|
||||
-- - delivery_rules.user_id:原 CREATE 缺失却被 create_delivery_rule 引用 → 补齐
|
||||
-- - cards.user_id / delay_seconds / image_url:历史 ALTER → 补齐
|
||||
-- - keywords.item_id / type / image_url:历史 ALTER → 补齐
|
||||
-- - item_info.multi_quantity_delivery:历史 ALTER → 补齐
|
||||
-- - default_replies.reply_once / reply_image_url:历史 ALTER → 补齐
|
||||
-- - users.is_admin:历史 ALTER → 补齐
|
||||
-- 注:老库导入对齐在 Phase 6 单独脚本处理;本迁移面向全新库。
|
||||
-- 闲鱼管家初始 schema(MySQL 版)。
|
||||
-- 与 SQLite 00001 对齐,差异:
|
||||
-- - INTEGER PRIMARY KEY AUTOINCREMENT → BIGINT AUTO_INCREMENT PRIMARY KEY
|
||||
-- - TEXT 主键/唯一键 → VARCHAR(255)(MySQL 索引长度限制)
|
||||
-- - BOOLEAN → TINYINT(1)
|
||||
-- - INSERT OR IGNORE → INSERT IGNORE
|
||||
-- - CREATE INDEX IF NOT EXISTS → CREATE INDEX(MySQL 不支持 IF NOT EXISTS,靠 goose 版本表防重复)
|
||||
|
||||
-- 认证与会话
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(255) UNIQUE NOT NULL,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
is_admin BOOLEAN DEFAULT FALSE,
|
||||
is_active TINYINT(1) DEFAULT 1,
|
||||
is_admin TINYINT(1) DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
is_admin INTEGER DEFAULT 0,
|
||||
expires_at INTEGER NOT NULL,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS email_verifications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT NOT NULL,
|
||||
code TEXT NOT NULL,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
used BOOLEAN DEFAULT FALSE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
session_id VARCHAR(255) PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
is_admin TINYINT(1) DEFAULT 0,
|
||||
expires_at BIGINT NOT NULL,
|
||||
created_at BIGINT NOT NULL,
|
||||
CONSTRAINT fk_sessions_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_sessions_user_id ON sessions(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS captcha_codes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
code TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
session_id VARCHAR(255) NOT NULL,
|
||||
code VARCHAR(255) NOT NULL,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 账号(闲鱼 cookie)
|
||||
CREATE TABLE IF NOT EXISTS cookies (
|
||||
id TEXT PRIMARY KEY,
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
auto_confirm INTEGER DEFAULT 1,
|
||||
remark TEXT DEFAULT '',
|
||||
user_id BIGINT NOT NULL,
|
||||
auto_confirm TINYINT(1) DEFAULT 1,
|
||||
remark TEXT,
|
||||
pause_duration INTEGER DEFAULT 10,
|
||||
username TEXT DEFAULT '',
|
||||
password TEXT DEFAULT '',
|
||||
show_browser INTEGER DEFAULT 0,
|
||||
username VARCHAR(255),
|
||||
password TEXT,
|
||||
show_browser TINYINT(1) DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_cookies_user_id ON cookies(user_id);
|
||||
CONSTRAINT fk_cookies_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_cookies_user_id ON cookies(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cookie_status (
|
||||
cookie_id TEXT PRIMARY KEY,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
cookie_id VARCHAR(255) PRIMARY KEY,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_cookie_status_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 关键字回复
|
||||
CREATE TABLE IF NOT EXISTS keywords (
|
||||
cookie_id TEXT,
|
||||
cookie_id VARCHAR(255),
|
||||
keyword TEXT,
|
||||
reply TEXT,
|
||||
item_id TEXT,
|
||||
type TEXT DEFAULT 'text',
|
||||
item_id VARCHAR(255),
|
||||
type VARCHAR(32) DEFAULT 'text',
|
||||
image_url TEXT,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_keywords_cookie_id ON keywords(cookie_id);
|
||||
CONSTRAINT fk_keywords_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_keywords_cookie_id ON keywords(cookie_id);
|
||||
|
||||
-- AI 回复
|
||||
CREATE TABLE IF NOT EXISTS ai_reply_settings (
|
||||
cookie_id TEXT PRIMARY KEY,
|
||||
ai_enabled BOOLEAN DEFAULT FALSE,
|
||||
model_name TEXT DEFAULT 'qwen-plus',
|
||||
cookie_id VARCHAR(255) PRIMARY KEY,
|
||||
ai_enabled TINYINT(1) DEFAULT 0,
|
||||
model_name VARCHAR(255) DEFAULT 'qwen-plus',
|
||||
api_key TEXT,
|
||||
base_url TEXT DEFAULT 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||
max_discount_percent INTEGER DEFAULT 10,
|
||||
@@ -98,224 +86,202 @@ CREATE TABLE IF NOT EXISTS ai_reply_settings (
|
||||
max_bargain_rounds INTEGER DEFAULT 3,
|
||||
custom_prompts TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_ai_reply_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ai_conversations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
chat_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
item_id TEXT NOT NULL,
|
||||
role TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
chat_id VARCHAR(255) NOT NULL,
|
||||
user_id VARCHAR(255) NOT NULL,
|
||||
item_id VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(32) NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
intent TEXT,
|
||||
intent VARCHAR(255),
|
||||
bargain_count INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies (id) ON DELETE CASCADE
|
||||
);
|
||||
CONSTRAINT fk_ai_conv_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ai_item_cache (
|
||||
item_id TEXT PRIMARY KEY,
|
||||
data TEXT NOT NULL,
|
||||
price REAL,
|
||||
description TEXT,
|
||||
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 卡券与发货规则
|
||||
-- 卡券
|
||||
CREATE TABLE IF NOT EXISTS cards (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL CHECK (type IN ('api', 'text', 'data', 'image')),
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(32) NOT NULL,
|
||||
api_config TEXT,
|
||||
text_content TEXT,
|
||||
data_content TEXT,
|
||||
data_content LONGTEXT,
|
||||
image_url TEXT,
|
||||
description TEXT,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
delay_seconds INTEGER DEFAULT 0,
|
||||
is_multi_spec BOOLEAN DEFAULT FALSE,
|
||||
spec_name TEXT,
|
||||
spec_value TEXT,
|
||||
user_id INTEGER NOT NULL DEFAULT 1,
|
||||
is_multi_spec TINYINT(1) DEFAULT 0,
|
||||
spec_name VARCHAR(255),
|
||||
spec_value VARCHAR(255),
|
||||
user_id BIGINT NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_cards_user_id ON cards(user_id);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_cards_user FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_cards_user_id ON cards(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS delivery_rules (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
keyword TEXT NOT NULL,
|
||||
card_id INTEGER NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
keyword VARCHAR(255) NOT NULL,
|
||||
card_id BIGINT NOT NULL,
|
||||
delivery_count INTEGER DEFAULT 1,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
description TEXT,
|
||||
delivery_times INTEGER DEFAULT 0,
|
||||
user_id INTEGER NOT NULL DEFAULT 1,
|
||||
user_id BIGINT NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_delivery_rules_user_id ON delivery_rules(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_delivery_rules_card_id ON delivery_rules(card_id);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_delivery_rules_card FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_delivery_rules_user FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_delivery_rules_user_id ON delivery_rules(user_id);
|
||||
CREATE INDEX idx_delivery_rules_card_id ON delivery_rules(card_id);
|
||||
|
||||
-- 订单
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
order_id TEXT PRIMARY KEY,
|
||||
item_id TEXT,
|
||||
buyer_id TEXT,
|
||||
order_id VARCHAR(255) PRIMARY KEY,
|
||||
item_id VARCHAR(255),
|
||||
buyer_id VARCHAR(255),
|
||||
spec_name TEXT,
|
||||
spec_value TEXT,
|
||||
quantity TEXT,
|
||||
amount TEXT,
|
||||
order_status TEXT DEFAULT 'unknown',
|
||||
cookie_id TEXT,
|
||||
quantity VARCHAR(64),
|
||||
amount VARCHAR(64),
|
||||
order_status VARCHAR(64) DEFAULT 'unknown',
|
||||
cookie_id VARCHAR(255),
|
||||
is_bargain INTEGER DEFAULT 0,
|
||||
receiver_name TEXT DEFAULT '',
|
||||
receiver_phone TEXT DEFAULT '',
|
||||
receiver_address TEXT DEFAULT '',
|
||||
receiver_city TEXT DEFAULT '',
|
||||
receiver_name TEXT,
|
||||
receiver_phone TEXT,
|
||||
receiver_address TEXT,
|
||||
receiver_city TEXT,
|
||||
version INTEGER DEFAULT 1,
|
||||
chat_id TEXT DEFAULT '',
|
||||
chat_id VARCHAR(255),
|
||||
system_shipped INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_cookie_id ON orders(cookie_id);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_orders_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_orders_cookie_id ON orders(cookie_id);
|
||||
|
||||
-- 商品信息
|
||||
CREATE TABLE IF NOT EXISTS item_info (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
item_id TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
item_id VARCHAR(255) NOT NULL,
|
||||
item_title TEXT,
|
||||
item_description TEXT,
|
||||
item_category TEXT,
|
||||
item_price TEXT,
|
||||
item_detail TEXT,
|
||||
is_multi_spec BOOLEAN DEFAULT FALSE,
|
||||
multi_quantity_delivery BOOLEAN DEFAULT FALSE,
|
||||
item_category VARCHAR(255),
|
||||
item_price VARCHAR(64),
|
||||
item_detail LONGTEXT,
|
||||
is_multi_spec TINYINT(1) DEFAULT 0,
|
||||
multi_quantity_delivery TINYINT(1) DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE,
|
||||
UNIQUE(cookie_id, item_id)
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_item_info_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE,
|
||||
UNIQUE KEY uk_item_info_cookie_item (cookie_id, item_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 默认回复
|
||||
CREATE TABLE IF NOT EXISTS default_replies (
|
||||
cookie_id TEXT PRIMARY KEY,
|
||||
enabled BOOLEAN DEFAULT FALSE,
|
||||
cookie_id VARCHAR(255) PRIMARY KEY,
|
||||
enabled TINYINT(1) DEFAULT 0,
|
||||
reply_content TEXT,
|
||||
reply_image_url TEXT,
|
||||
reply_once BOOLEAN DEFAULT FALSE,
|
||||
reply_once TINYINT(1) DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_default_replies_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- WS 原始消息记录
|
||||
CREATE TABLE IF NOT EXISTS ws_messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
direction TEXT NOT NULL DEFAULT 'in',
|
||||
raw_text TEXT NOT NULL DEFAULT '',
|
||||
parsed_json TEXT NOT NULL DEFAULT '',
|
||||
message_kind TEXT NOT NULL DEFAULT '',
|
||||
parse_status TEXT NOT NULL DEFAULT 'raw',
|
||||
error TEXT NOT NULL DEFAULT '',
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
direction VARCHAR(16) NOT NULL DEFAULT 'in',
|
||||
raw_text LONGTEXT,
|
||||
parsed_json LONGTEXT,
|
||||
message_kind VARCHAR(64) NOT NULL DEFAULT '',
|
||||
parse_status VARCHAR(32) NOT NULL DEFAULT 'raw',
|
||||
error TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_ws_messages_cookie_id_created_at ON ws_messages(cookie_id, created_at DESC);
|
||||
CONSTRAINT fk_ws_messages_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_ws_messages_cookie_id_created_at ON ws_messages(cookie_id, created_at DESC);
|
||||
|
||||
-- 指定商品回复
|
||||
CREATE TABLE IF NOT EXISTS item_replay (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
item_id TEXT NOT NULL,
|
||||
cookie_id TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
item_id VARCHAR(255) NOT NULL,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
reply_content TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_item_replay_cookie_item ON item_replay(cookie_id, item_id);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_item_replay_cookie_item ON item_replay(cookie_id, item_id);
|
||||
|
||||
-- 默认回复记录(防重复)
|
||||
CREATE TABLE IF NOT EXISTS default_reply_records (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
chat_id TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
chat_id VARCHAR(255) NOT NULL,
|
||||
replied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(cookie_id, chat_id),
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
UNIQUE KEY uk_default_reply_records (cookie_id, chat_id),
|
||||
CONSTRAINT fk_default_reply_records_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 通知
|
||||
CREATE TABLE IF NOT EXISTS notification_channels (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL CHECK (type IN ('qq','ding_talk','dingtalk','feishu','lark','bark','email','webhook','wechat','telegram')),
|
||||
config TEXT NOT NULL,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
user_id INTEGER NOT NULL DEFAULT 1,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(32) NOT NULL,
|
||||
config LONGTEXT NOT NULL,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
user_id BIGINT NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_notif_channels_user FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_notifications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
channel_id INTEGER NOT NULL,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
channel_id BIGINT NOT NULL,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
|
||||
UNIQUE(cookie_id, channel_id)
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY uk_message_notifications (cookie_id, channel_id),
|
||||
CONSTRAINT fk_msg_notif_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_msg_notif_channel FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 系统设置
|
||||
CREATE TABLE IF NOT EXISTS system_settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
`key` VARCHAR(255) PRIMARY KEY,
|
||||
value LONGTEXT NOT NULL,
|
||||
description TEXT,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 用户设置
|
||||
CREATE TABLE IF NOT EXISTS user_settings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
`key` VARCHAR(255) NOT NULL,
|
||||
value LONGTEXT NOT NULL,
|
||||
description TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
UNIQUE(user_id, key)
|
||||
);
|
||||
|
||||
-- 风控日志
|
||||
CREATE TABLE IF NOT EXISTS risk_control_logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
event_type TEXT NOT NULL DEFAULT 'slider_captcha',
|
||||
event_description TEXT,
|
||||
processing_result TEXT,
|
||||
processing_status TEXT DEFAULT 'processing',
|
||||
error_message TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY uk_user_settings (user_id, `key`),
|
||||
CONSTRAINT fk_user_settings_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- 默认系统设置(不含管理员口令——由 init-admin CLI 初始化)
|
||||
INSERT OR IGNORE INTO system_settings (key, value, description) VALUES
|
||||
INSERT IGNORE INTO system_settings (`key`, value, description) VALUES
|
||||
('theme_color', 'blue', '主题颜色'),
|
||||
('registration_enabled', 'true', '是否开启用户注册'),
|
||||
('show_default_login_info', 'true', '是否显示默认登录信息'),
|
||||
@@ -334,7 +300,7 @@ INSERT OR IGNORE INTO system_settings (key, value, description) VALUES
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE IF EXISTS risk_control_logs;
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS user_settings;
|
||||
DROP TABLE IF EXISTS system_settings;
|
||||
DROP TABLE IF EXISTS message_notifications;
|
||||
@@ -346,13 +312,12 @@ DROP TABLE IF EXISTS item_info;
|
||||
DROP TABLE IF EXISTS orders;
|
||||
DROP TABLE IF EXISTS delivery_rules;
|
||||
DROP TABLE IF EXISTS cards;
|
||||
DROP TABLE IF EXISTS ai_item_cache;
|
||||
DROP TABLE IF EXISTS ai_conversations;
|
||||
DROP TABLE IF EXISTS ai_reply_settings;
|
||||
DROP TABLE IF EXISTS keywords;
|
||||
DROP TABLE IF EXISTS cookie_status;
|
||||
DROP TABLE IF EXISTS cookies;
|
||||
DROP TABLE IF EXISTS captcha_codes;
|
||||
DROP TABLE IF EXISTS email_verifications;
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS users;
|
||||
-- +goose StatementEnd
|
||||
@@ -1,10 +1,9 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE cookies ADD COLUMN nickname TEXT DEFAULT '';
|
||||
ALTER TABLE cookies ADD COLUMN avatar_url TEXT DEFAULT '';
|
||||
ALTER TABLE cookies ADD COLUMN nickname VARCHAR(255) DEFAULT '';
|
||||
ALTER TABLE cookies ADD COLUMN avatar_url TEXT;
|
||||
ALTER TABLE cookies ADD COLUMN updated_at TIMESTAMP;
|
||||
UPDATE cookies
|
||||
SET updated_at = COALESCE(updated_at, created_at, CURRENT_TIMESTAMP);
|
||||
UPDATE cookies SET updated_at = COALESCE(updated_at, created_at, CURRENT_TIMESTAMP);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-- +goose Up
|
||||
INSERT OR IGNORE INTO system_settings (key, value, description) VALUES
|
||||
INSERT IGNORE INTO system_settings (`key`, value, description) VALUES
|
||||
('ai_api_url', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'AI OpenAI兼容API地址'),
|
||||
('ai_api_key', '', 'AI API Key'),
|
||||
('ai_model', 'qwen-plus', 'AI模型名称');
|
||||
|
||||
-- +goose Down
|
||||
DELETE FROM system_settings WHERE key IN ('ai_api_url', 'ai_api_key', 'ai_model');
|
||||
DELETE FROM system_settings WHERE `key` IN ('ai_api_url', 'ai_api_key', 'ai_model');
|
||||
@@ -1,22 +1,22 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE delivery_rules ADD COLUMN cookie_id TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE delivery_rules ADD COLUMN item_id TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE delivery_rules ADD COLUMN cookie_id VARCHAR(255) NOT NULL DEFAULT '';
|
||||
ALTER TABLE delivery_rules ADD COLUMN item_id VARCHAR(255) NOT NULL DEFAULT '';
|
||||
|
||||
CREATE TABLE delivery_rule_variants (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
rule_id INTEGER NOT NULL,
|
||||
spec_name TEXT NOT NULL DEFAULT '',
|
||||
spec_value TEXT NOT NULL DEFAULT '',
|
||||
card_id INTEGER NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
rule_id BIGINT NOT NULL,
|
||||
spec_name VARCHAR(255) NOT NULL DEFAULT '',
|
||||
spec_value VARCHAR(255) NOT NULL DEFAULT '',
|
||||
card_id BIGINT NOT NULL,
|
||||
delivery_count INTEGER NOT NULL DEFAULT 1,
|
||||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
enabled TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (rule_id) REFERENCES delivery_rules(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT,
|
||||
UNIQUE(rule_id, spec_name, spec_value)
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_delivery_rule_variants_rule FOREIGN KEY (rule_id) REFERENCES delivery_rules(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_delivery_rule_variants_card FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT,
|
||||
UNIQUE KEY uk_delivery_rule_variants (rule_id, spec_name, spec_value)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE INDEX idx_delivery_rules_cookie_item ON delivery_rules(cookie_id, item_id);
|
||||
CREATE INDEX idx_delivery_rule_variants_rule ON delivery_rule_variants(rule_id);
|
||||
@@ -38,7 +38,7 @@ JOIN cards c ON c.id = dr.card_id;
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE delivery_rule_variants;
|
||||
DROP INDEX IF EXISTS idx_delivery_rules_cookie_item;
|
||||
DROP INDEX idx_delivery_rules_cookie_item ON delivery_rules;
|
||||
ALTER TABLE delivery_rules DROP COLUMN item_id;
|
||||
ALTER TABLE delivery_rules DROP COLUMN cookie_id;
|
||||
-- +goose StatementEnd
|
||||
@@ -1,22 +1,6 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE IF NOT EXISTS ws_messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cookie_id TEXT NOT NULL,
|
||||
direction TEXT NOT NULL DEFAULT 'in',
|
||||
raw_text TEXT NOT NULL DEFAULT '',
|
||||
parsed_json TEXT NOT NULL DEFAULT '',
|
||||
message_kind TEXT NOT NULL DEFAULT '',
|
||||
parse_status TEXT NOT NULL DEFAULT 'raw',
|
||||
error TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_ws_messages_cookie_id_created_at ON ws_messages(cookie_id, created_at DESC);
|
||||
-- +goose StatementEnd
|
||||
-- ws_messages 表在 MySQL 版 00001 已创建,此迁移为空操作,保留版本号与 SQLite 对齐。
|
||||
-- MySQL 不允许重复创建已存在的外键约束,故此处不执行任何 DDL。
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP INDEX IF EXISTS idx_ws_messages_cookie_id_created_at;
|
||||
DROP TABLE IF EXISTS ws_messages;
|
||||
-- +goose StatementEnd
|
||||
-- 空操作:ws_messages 在 00001 创建,回滚由 00001 Down 处理。
|
||||
@@ -1,49 +1,46 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE IF NOT EXISTS item_publish_batches (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
default_cookie_id TEXT NOT NULL DEFAULT '',
|
||||
filename TEXT NOT NULL DEFAULT '',
|
||||
upload_dir TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
default_cookie_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
filename VARCHAR(255) NOT NULL DEFAULT '',
|
||||
upload_dir TEXT NOT NULL,
|
||||
status VARCHAR(32) NOT NULL DEFAULT 'pending',
|
||||
total_count INTEGER NOT NULL DEFAULT 0,
|
||||
success_count INTEGER NOT NULL DEFAULT 0,
|
||||
failed_count INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_item_publish_batches_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS item_publish_batch_rows (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
batch_id TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
batch_id VARCHAR(255) NOT NULL,
|
||||
row_no INTEGER NOT NULL,
|
||||
cookie_id TEXT NOT NULL DEFAULT '',
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
price TEXT NOT NULL DEFAULT '',
|
||||
original_price TEXT NOT NULL DEFAULT '',
|
||||
cookie_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
title VARCHAR(255) NOT NULL DEFAULT '',
|
||||
description TEXT NOT NULL,
|
||||
price VARCHAR(64) NOT NULL DEFAULT '',
|
||||
original_price VARCHAR(64) NOT NULL DEFAULT '',
|
||||
quantity INTEGER NOT NULL DEFAULT 1,
|
||||
postage_mode TEXT NOT NULL DEFAULT 'free',
|
||||
postage TEXT NOT NULL DEFAULT '',
|
||||
images_json TEXT NOT NULL DEFAULT '[]',
|
||||
auto_create_delivery_rule INTEGER NOT NULL DEFAULT 0,
|
||||
card_group_id INTEGER NOT NULL DEFAULT 0,
|
||||
delivery_count INTEGER NOT NULL DEFAULT 1,
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
item_id TEXT NOT NULL DEFAULT '',
|
||||
item_url TEXT NOT NULL DEFAULT '',
|
||||
error_message TEXT NOT NULL DEFAULT '',
|
||||
raw_json TEXT NOT NULL DEFAULT '{}',
|
||||
postage_mode VARCHAR(16) NOT NULL DEFAULT 'free',
|
||||
postage VARCHAR(64) NOT NULL DEFAULT '',
|
||||
images_json LONGTEXT NOT NULL,
|
||||
status VARCHAR(32) NOT NULL DEFAULT 'pending',
|
||||
item_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
item_url TEXT NOT NULL,
|
||||
error_message TEXT NOT NULL,
|
||||
raw_json LONGTEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (batch_id) REFERENCES item_publish_batches(id) ON DELETE CASCADE
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_item_publish_batch_rows_batch FOREIGN KEY (batch_id) REFERENCES item_publish_batches(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_item_publish_batches_user ON item_publish_batches(user_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_item_publish_batch_rows_batch ON item_publish_batch_rows(batch_id, row_no);
|
||||
CREATE INDEX IF NOT EXISTS idx_item_publish_batch_rows_status ON item_publish_batch_rows(batch_id, status);
|
||||
CREATE INDEX idx_item_publish_batches_user ON item_publish_batches(user_id, created_at);
|
||||
CREATE INDEX idx_item_publish_batch_rows_batch ON item_publish_batch_rows(batch_id, row_no);
|
||||
CREATE INDEX idx_item_publish_batch_rows_status ON item_publish_batch_rows(batch_id, status);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,78 +1,73 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
-- 自动化中心 schema。
|
||||
--
|
||||
-- 设计目标:
|
||||
-- 1. 不再把“自动发货”写死成唯一业务,付款发货、评价赠品、超时求评价都统一为自动化规则。
|
||||
-- 2. 触发来源可以是 WS 系统事件、计划任务或后台手动触发;规则和动作统一落在 automation_rules/actions。
|
||||
-- 3. automation_runs 使用 trigger_key 做持久化防重,避免 WS 重放、服务重启或计划任务重复扫描导致重复发送。
|
||||
-- 自动化中心 schema(MySQL 版,与 SQLite 00007 对齐)。
|
||||
|
||||
CREATE TABLE IF NOT EXISTS automation_rules (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
cookie_id TEXT NOT NULL DEFAULT '',
|
||||
item_id TEXT NOT NULL DEFAULT '',
|
||||
name TEXT NOT NULL,
|
||||
trigger_type TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
cookie_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
item_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
name VARCHAR(255) NOT NULL,
|
||||
trigger_type VARCHAR(64) NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
priority INTEGER NOT NULL DEFAULT 100,
|
||||
config_json TEXT NOT NULL DEFAULT '{}',
|
||||
config_json LONGTEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_automation_rules_user ON automation_rules(user_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_automation_rules_match ON automation_rules(cookie_id, item_id, trigger_type, enabled, priority);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_automation_rules_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_automation_rules_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_automation_rules_user ON automation_rules(user_id, created_at DESC);
|
||||
CREATE INDEX idx_automation_rules_match ON automation_rules(cookie_id, item_id, trigger_type, enabled, priority);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS automation_rule_actions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
rule_id INTEGER NOT NULL,
|
||||
action_type TEXT NOT NULL,
|
||||
card_id INTEGER,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
rule_id BIGINT NOT NULL,
|
||||
action_type VARCHAR(64) NOT NULL,
|
||||
card_id BIGINT,
|
||||
delivery_count INTEGER NOT NULL DEFAULT 1,
|
||||
message_template TEXT NOT NULL DEFAULT '',
|
||||
message_template TEXT NOT NULL,
|
||||
delay_seconds INTEGER NOT NULL DEFAULT 0,
|
||||
config_json TEXT NOT NULL DEFAULT '{}',
|
||||
config_json LONGTEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_automation_rule_actions_rule ON automation_rule_actions(rule_id, sort_order, id);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_automation_rule_actions_rule FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_automation_rule_actions_card FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_automation_rule_actions_rule ON automation_rule_actions(rule_id, sort_order, id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS automation_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
rule_id INTEGER NOT NULL,
|
||||
cookie_id TEXT NOT NULL,
|
||||
item_id TEXT NOT NULL DEFAULT '',
|
||||
order_id TEXT NOT NULL DEFAULT '',
|
||||
buyer_id TEXT NOT NULL DEFAULT '',
|
||||
chat_id TEXT NOT NULL DEFAULT '',
|
||||
trigger_type TEXT NOT NULL,
|
||||
trigger_key TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
rule_id BIGINT NOT NULL,
|
||||
cookie_id VARCHAR(255) NOT NULL,
|
||||
item_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
order_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
buyer_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
chat_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
trigger_type VARCHAR(64) NOT NULL,
|
||||
trigger_key VARCHAR(255) NOT NULL,
|
||||
status VARCHAR(32) NOT NULL,
|
||||
sent_count INTEGER NOT NULL DEFAULT 0,
|
||||
error_message TEXT NOT NULL DEFAULT '',
|
||||
raw_event_json TEXT NOT NULL DEFAULT '{}',
|
||||
error_message TEXT NOT NULL,
|
||||
raw_event_json LONGTEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
|
||||
UNIQUE(rule_id, trigger_key)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_automation_runs_cookie_order ON automation_runs(cookie_id, order_id, trigger_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_automation_runs_status ON automation_runs(status, updated_at);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_automation_runs_rule FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
|
||||
UNIQUE KEY uk_automation_runs (rule_id, trigger_key)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE INDEX idx_automation_runs_cookie_order ON automation_runs(cookie_id, order_id, trigger_type);
|
||||
CREATE INDEX idx_automation_runs_status ON automation_runs(status, updated_at);
|
||||
|
||||
ALTER TABLE orders ADD COLUMN paid_at TEXT DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN shipped_at TEXT DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN completed_at TEXT DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN buyer_reviewed_at TEXT DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN last_review_request_at TEXT DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN paid_at VARCHAR(64) DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN shipped_at VARCHAR(64) DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN completed_at VARCHAR(64) DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN buyer_reviewed_at VARCHAR(64) DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN last_review_request_at VARCHAR(64) DEFAULT '';
|
||||
ALTER TABLE orders ADD COLUMN review_request_count INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
ALTER TABLE item_publish_batch_rows ADD COLUMN automation_json TEXT NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE item_publish_batch_rows ADD COLUMN automation_json LONGTEXT NOT NULL;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
|
||||
@@ -1,51 +1,42 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
-- 清理被 automation_rules 取代或从未使用的 schema:
|
||||
-- - delivery_rules + delivery_rule_variants:发货规则旧系统,已被 automation_rules 完全取代(前端只走 automation-rules)。
|
||||
-- - email_verifications / captcha_codes / ai_item_cache / risk_control_logs:从未有 Go 代码读写。
|
||||
-- - item_publish_batch_rows.auto_create_delivery_rule / card_group_id / delivery_count:批量铺货改走 automation_json,三列从未读写。
|
||||
-- 清理被 automation_rules 取代或从未使用的 schema(MySQL 版)。
|
||||
-- 注意:MySQL 00006 未创建 auto_create_delivery_rule/card_group_id/delivery_count
|
||||
-- (这三个列是 SQLite 历史遗留,00008 在 SQLite 里删除,MySQL 版直接没建,故此处不删)。
|
||||
|
||||
DROP TABLE IF EXISTS delivery_rule_variants;
|
||||
DROP TABLE IF EXISTS delivery_rules;
|
||||
DROP TABLE IF EXISTS email_verifications;
|
||||
DROP TABLE IF EXISTS captcha_codes;
|
||||
DROP TABLE IF EXISTS ai_item_cache;
|
||||
DROP TABLE IF EXISTS risk_control_logs;
|
||||
|
||||
ALTER TABLE item_publish_batch_rows DROP COLUMN auto_create_delivery_rule;
|
||||
ALTER TABLE item_publish_batch_rows DROP COLUMN card_group_id;
|
||||
ALTER TABLE item_publish_batch_rows DROP COLUMN delivery_count;
|
||||
-- email_verifications / ai_item_cache / risk_control_logs 在 MySQL 00001 未创建,跳过删除。
|
||||
-- 若存在则忽略报错:MySQL 的 DROP TABLE IF EXISTS 对不存在的表是安全的。
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
-- 仅重建结构,历史数据不可恢复。
|
||||
CREATE TABLE IF NOT EXISTS delivery_rules (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
keyword TEXT NOT NULL,
|
||||
card_id INTEGER NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
keyword VARCHAR(255) NOT NULL,
|
||||
card_id BIGINT NOT NULL,
|
||||
delivery_count INTEGER DEFAULT 1,
|
||||
enabled BOOLEAN DEFAULT TRUE,
|
||||
enabled TINYINT(1) DEFAULT 1,
|
||||
description TEXT,
|
||||
delivery_times INTEGER DEFAULT 0,
|
||||
user_id INTEGER NOT NULL DEFAULT 1,
|
||||
cookie_id TEXT NOT NULL DEFAULT '',
|
||||
item_id TEXT NOT NULL DEFAULT '',
|
||||
user_id BIGINT NOT NULL DEFAULT 1,
|
||||
cookie_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
item_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
CREATE TABLE IF NOT EXISTS delivery_rule_variants (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
rule_id INTEGER NOT NULL,
|
||||
spec_name TEXT NOT NULL DEFAULT '',
|
||||
spec_value TEXT NOT NULL DEFAULT '',
|
||||
card_id INTEGER NOT NULL,
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
rule_id BIGINT NOT NULL,
|
||||
spec_name VARCHAR(255) NOT NULL DEFAULT '',
|
||||
spec_value VARCHAR(255) NOT NULL DEFAULT '',
|
||||
card_id BIGINT NOT NULL,
|
||||
delivery_count INTEGER NOT NULL DEFAULT 1,
|
||||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
enabled TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
ALTER TABLE item_publish_batch_rows ADD COLUMN auto_create_delivery_rule INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE item_publish_batch_rows ADD COLUMN card_group_id INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE item_publish_batch_rows ADD COLUMN delivery_count INTEGER NOT NULL DEFAULT 1;
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
-- +goose StatementEnd
|
||||
@@ -14,7 +14,10 @@ type NotificationChannel struct {
|
||||
}
|
||||
|
||||
// Notifications 通知绑定操作。
|
||||
type Notifications struct{ DB *sql.DB }
|
||||
type Notifications struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// AccountChannels 取某账号已启用的通知渠道(message_notifications JOIN notification_channels)。
|
||||
// 移植自 get_account_notifications。
|
||||
|
||||
@@ -36,7 +36,10 @@ type Order struct {
|
||||
}
|
||||
|
||||
// Orders 订单操作。
|
||||
type Orders struct{ DB *sql.DB }
|
||||
type Orders struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Upsert 插入或更新订单。仅更新提供的非零字段(INSERT OR IGNORE 占位 + 动态 UPDATE)。
|
||||
// 注:orders.version 列当前仅作占位读取,不参与并发控制——SQLite 单写者序列化写入,
|
||||
@@ -45,10 +48,10 @@ func (o *Orders) Upsert(ctx context.Context, orderID string, opts OrderUpsertOpt
|
||||
if orderID == "" {
|
||||
return errors.New("order_id 不能为空")
|
||||
}
|
||||
// 先尝试 INSERT OR IGNORE 占位。
|
||||
// 先尝试插入占位(冲突忽略)。order_id 是主键。
|
||||
_, err := o.DB.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO orders (order_id, item_id, buyer_id, cookie_id, order_status, version)
|
||||
VALUES (?, ?, ?, ?, 'unknown', 1)`,
|
||||
dialectInsertIgnorePrefix(o.Dialect)+` INTO orders (order_id, item_id, buyer_id, cookie_id, order_status, version)
|
||||
VALUES (?, ?, ?, ?, 'unknown', 1)`+dialectInsertIgnore(o.Dialect, []string{"order_id"}),
|
||||
orderID, opts.ItemID, opts.BuyerID, opts.CookieID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -54,7 +54,10 @@ func (k *Keywords) AllWithType(ctx context.Context, cookieID string) ([]Keyword,
|
||||
}
|
||||
|
||||
// DefaultReplies 默认回复操作。
|
||||
type DefaultReplies struct{ DB *sql.DB }
|
||||
type DefaultReplies struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Get 取某账号默认回复设置。不存在返回 ErrNotFound。
|
||||
func (d *DefaultReplies) Get(ctx context.Context, cookieID string) (*DefaultReply, error) {
|
||||
@@ -89,13 +92,16 @@ func (d *DefaultReplies) HasRecord(ctx context.Context, cookieID, chatID string)
|
||||
// AddRecord 记录已回复(reply_once 防重复)。
|
||||
func (d *DefaultReplies) AddRecord(ctx context.Context, cookieID, chatID string) error {
|
||||
_, err := d.DB.ExecContext(ctx,
|
||||
`INSERT OR IGNORE INTO default_reply_records (cookie_id, chat_id) VALUES (?, ?)`,
|
||||
dialectInsertIgnorePrefix(d.Dialect)+` INTO default_reply_records (cookie_id, chat_id) VALUES (?, ?)`+dialectInsertIgnore(d.Dialect, []string{"cookie_id", "chat_id"}),
|
||||
cookieID, chatID)
|
||||
return err
|
||||
}
|
||||
|
||||
// ItemReplies 指定商品回复操作。
|
||||
type ItemReplies struct{ DB *sql.DB }
|
||||
type ItemReplies struct {
|
||||
DB *sql.DB
|
||||
Dialect Dialect
|
||||
}
|
||||
|
||||
// Get 取某账号某商品的指定回复。
|
||||
func (i *ItemReplies) Get(ctx context.Context, cookieID, itemID string) (*ItemReply, error) {
|
||||
|
||||
@@ -30,17 +30,17 @@ func NewStore(db *sql.DB, dialect Dialect) *Store {
|
||||
Dialect: dialect,
|
||||
Users: &Users{DB: db},
|
||||
Sessions: &Sessions{DB: db},
|
||||
Cookies: &Cookies{DB: db},
|
||||
Items: &Items{DB: db},
|
||||
Cards: &Cards{DB: db},
|
||||
Automation: &AutomationRules{DB: db},
|
||||
Orders: &Orders{DB: db},
|
||||
Cookies: &Cookies{DB: db, Dialect: dialect},
|
||||
Items: &Items{DB: db, Dialect: dialect},
|
||||
Cards: &Cards{DB: db, Dialect: dialect},
|
||||
Automation: &AutomationRules{DB: db, Dialect: dialect},
|
||||
Orders: &Orders{DB: db, Dialect: dialect},
|
||||
Keywords: &Keywords{DB: db},
|
||||
DefaultReps: &DefaultReplies{DB: db},
|
||||
ItemReps: &ItemReplies{DB: db},
|
||||
DefaultReps: &DefaultReplies{DB: db, Dialect: dialect},
|
||||
ItemReps: &ItemReplies{DB: db, Dialect: dialect},
|
||||
AIReply: &AIReply{DB: db},
|
||||
Notifications: &Notifications{DB: db},
|
||||
Settings: &SystemSettings{DB: db},
|
||||
Notifications: &Notifications{DB: db, Dialect: dialect},
|
||||
Settings: &SystemSettings{DB: db, Dialect: dialect},
|
||||
WSMessages: &WSMessageStore{DB: db},
|
||||
PublishBatches: &ItemPublishBatches{DB: db},
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// btoi bool→int(SQLite 无原生 bool)。
|
||||
@@ -61,8 +63,14 @@ func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT OR REPLACE INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
|
||||
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`,
|
||||
`INSERT INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
|
||||
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
|
||||
"enabled": "EXCLUDED.enabled",
|
||||
"reply_content": "EXCLUDED.reply_content",
|
||||
"reply_image_url": "EXCLUDED.reply_image_url",
|
||||
"reply_once": "EXCLUDED.reply_once",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, btoi(req.Enabled), req.ReplyContent, nullIfEmpty(req.ReplyImageURL), btoi(req.ReplyOnce))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
@@ -175,10 +175,10 @@ func (s *Server) setAccountBindings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT INTO message_notifications (cookie_id, channel_id, enabled)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(cookie_id, channel_id) DO UPDATE SET
|
||||
enabled=excluded.enabled,
|
||||
updated_at=CURRENT_TIMESTAMP`,
|
||||
VALUES (?, ?, ?)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id", "channel_id"}, map[string]string{
|
||||
"enabled": "EXCLUDED.enabled",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, req.ChannelID, btoi(enabled))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
@@ -140,14 +140,14 @@ func (s *Server) setAIReply(w http.ResponseWriter, r *http.Request) {
|
||||
`INSERT INTO ai_reply_settings
|
||||
(cookie_id, ai_enabled, max_discount_percent, max_discount_amount,
|
||||
max_bargain_rounds, custom_prompts, updated_at)
|
||||
VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(cookie_id) DO UPDATE SET
|
||||
ai_enabled=excluded.ai_enabled,
|
||||
max_discount_percent=excluded.max_discount_percent,
|
||||
max_discount_amount=excluded.max_discount_amount,
|
||||
max_bargain_rounds=excluded.max_bargain_rounds,
|
||||
custom_prompts=excluded.custom_prompts,
|
||||
updated_at=CURRENT_TIMESTAMP`,
|
||||
VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
|
||||
"ai_enabled": "EXCLUDED.ai_enabled",
|
||||
"max_discount_percent": "EXCLUDED.max_discount_percent",
|
||||
"max_discount_amount": "EXCLUDED.max_discount_amount",
|
||||
"max_bargain_rounds": "EXCLUDED.max_bargain_rounds",
|
||||
"custom_prompts": "EXCLUDED.custom_prompts",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, btoi(req.AIEnabled), req.MaxDiscountPercent, req.MaxDiscountAmount,
|
||||
req.MaxBargainRounds, nullIfEmpty(req.CustomPrompts))
|
||||
if err != nil {
|
||||
@@ -326,8 +326,13 @@ func (s *Server) setUserSetting(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
keyCol := db.DialectQuote(s.Store.Dialect, "key")
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT OR REPLACE INTO user_settings (user_id, key, value, updated_at) VALUES (?,?,?,CURRENT_TIMESTAMP)`,
|
||||
`INSERT INTO user_settings (user_id, `+keyCol+`, value, updated_at) VALUES (?,?,?,CURRENT_TIMESTAMP)`+
|
||||
db.DialectUpsert(s.Store.Dialect, []string{"user_id", keyCol}, map[string]string{
|
||||
"value": "EXCLUDED.value",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
sess.UserID, key, req.Value)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
Reference in new issue
Block a user