可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。 登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。 安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。 自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。 前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。 测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。 验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
632 lines
21 KiB
Go
632 lines
21 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strconv"
|
|
)
|
|
|
|
// ---- 关键字 CRUD ----
|
|
|
|
// KeywordRow keywords 表完整行(含自增 id,用于按索引删除)。
|
|
type KeywordRow struct {
|
|
ID int64 `json:"id"`
|
|
CookieID string
|
|
Keyword string
|
|
Reply string
|
|
ItemID string
|
|
Type string
|
|
ImageURL string
|
|
}
|
|
|
|
func (k *Keywords) UpdateByID(ctx context.Context, row KeywordRow) error {
|
|
kwType := row.Type
|
|
if kwType == "" {
|
|
kwType = "text"
|
|
}
|
|
res, err := k.DB.ExecContext(ctx, `UPDATE keywords
|
|
SET keyword=?,reply=?,item_id=?,type=?,image_url=?
|
|
WHERE id=? AND cookie_id=?`,
|
|
row.Keyword, row.Reply, nullable(row.ItemID), kwType, nullable(row.ImageURL), row.ID, row.CookieID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affected, err := res.RowsAffected(); err == nil && affected == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (k *Keywords) DeleteByID(ctx context.Context, cookieID string, id int64) error {
|
|
res, err := k.DB.ExecContext(ctx, `DELETE FROM keywords WHERE id=? AND cookie_id=?`, id, cookieID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affected, err := res.RowsAffected(); err == nil && affected == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AllRows 取某账号所有关键字(含 id)。
|
|
func (k *Keywords) AllRows(ctx context.Context, cookieID string) ([]KeywordRow, error) {
|
|
rows, err := k.DB.QueryContext(ctx,
|
|
`SELECT id, keyword, reply, COALESCE(item_id,''), COALESCE(type,'text'), COALESCE(image_url,'')
|
|
FROM keywords WHERE cookie_id=? ORDER BY id`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []KeywordRow
|
|
for rows.Next() {
|
|
var r KeywordRow
|
|
if err := rows.Scan(&r.ID, &r.Keyword, &r.Reply, &r.ItemID, &r.Type, &r.ImageURL); err != nil {
|
|
return nil, err
|
|
}
|
|
r.CookieID = cookieID
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// Add 添加关键字。
|
|
func (k *Keywords) Add(ctx context.Context, cookieID, keyword, reply, itemID, kwType, imageURL string) (int64, error) {
|
|
if kwType == "" {
|
|
kwType = "text"
|
|
}
|
|
return insertReturningID(ctx, k.DB, k.Dialect,
|
|
`INSERT INTO keywords (cookie_id, keyword, reply, item_id, type, image_url) VALUES (?,?,?,?,?,?)`,
|
|
cookieID, keyword, reply, nullable(itemID), kwType, nullable(imageURL))
|
|
}
|
|
|
|
// ReplaceForCookie 覆盖某账号的全部关键词。
|
|
func (k *Keywords) ReplaceForCookie(ctx context.Context, cookieID string, rows []KeywordRow) error {
|
|
tx, err := k.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM keywords WHERE cookie_id=?`, cookieID); err != nil {
|
|
return err
|
|
}
|
|
for _, row := range rows {
|
|
kwType := row.Type
|
|
if kwType == "" {
|
|
kwType = "text"
|
|
}
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO keywords (cookie_id, keyword, reply, item_id, type, image_url) VALUES (?,?,?,?,?,?)`,
|
|
cookieID, row.Keyword, row.Reply, nullable(row.ItemID), kwType, nullable(row.ImageURL)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// DeleteByIndex 按索引(0-based,按 id 顺序)删除某账号的一个关键字。
|
|
func (k *Keywords) DeleteByIndex(ctx context.Context, cookieID string, index int) error {
|
|
rows, err := k.DB.QueryContext(ctx,
|
|
`SELECT id FROM keywords WHERE cookie_id=? ORDER BY id`, cookieID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
var ids []int64
|
|
for rows.Next() {
|
|
var id int64
|
|
if err := rows.Scan(&id); err != nil {
|
|
return err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
if index < 0 || index >= len(ids) {
|
|
return ErrNotFound
|
|
}
|
|
_, err = k.DB.ExecContext(ctx, `DELETE FROM keywords WHERE id=? AND cookie_id=?`, ids[index], cookieID)
|
|
return err
|
|
}
|
|
|
|
// ---- 指定商品回复 (item_replay) CRUD ----
|
|
|
|
// ItemReplies 已在 reply.go 定义 Get。补 Set/Delete。
|
|
|
|
// Set 设置指定商品回复。
|
|
func (i *ItemReplies) Set(ctx context.Context, cookieID, itemID, content string) error {
|
|
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 删除指定商品回复。
|
|
func (i *ItemReplies) Delete(ctx context.Context, cookieID, itemID string) error {
|
|
_, err := i.DB.ExecContext(ctx,
|
|
`DELETE FROM item_replay WHERE cookie_id=? AND item_id=?`, cookieID, itemID)
|
|
return err
|
|
}
|
|
|
|
// AllForUser 取某账号所有指定商品回复。
|
|
func (i *ItemReplies) AllForUser(ctx context.Context, cookieID string) ([]ItemReply, error) {
|
|
rows, err := i.DB.QueryContext(ctx,
|
|
`SELECT item_id, cookie_id, reply_content FROM item_replay WHERE cookie_id=?`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ItemReply
|
|
for rows.Next() {
|
|
var r ItemReply
|
|
if err := rows.Scan(&r.ItemID, &r.CookieID, &r.ReplyContent); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// ---- 通知渠道 CRUD ----
|
|
|
|
// NotificationChannelRow 通知渠道完整行(含 id)。
|
|
type NotificationChannelRow struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Config string `json:"config"`
|
|
EventTypes string `json:"event_types,omitempty"`
|
|
Enabled bool `json:"enabled"`
|
|
UserID int64 `json:"user_id,omitempty"`
|
|
}
|
|
|
|
// AllChannelsForUser 取某用户全部通知渠道。
|
|
func (n *Notifications) AllChannelsForUser(ctx context.Context, userID int64) ([]NotificationChannelRow, error) {
|
|
rows, err := n.DB.QueryContext(ctx,
|
|
`SELECT id, name, type, config, COALESCE(event_types,''), enabled, COALESCE(user_id,1) FROM notification_channels
|
|
WHERE user_id=? ORDER BY id DESC`, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []NotificationChannelRow
|
|
for rows.Next() {
|
|
var c NotificationChannelRow
|
|
var enabled int
|
|
if err := rows.Scan(&c.ID, &c.Name, &c.Type, &c.Config, &c.EventTypes, &enabled, &c.UserID); err != nil {
|
|
return nil, err
|
|
}
|
|
c.Config, err = n.codec.decrypt("notification-config", strconv.FormatInt(c.UserID, 10), c.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Enabled = enabled != 0
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// CreateChannel 创建通知渠道。
|
|
func (n *Notifications) CreateChannel(ctx context.Context, c *NotificationChannelRow) (int64, error) {
|
|
config, err := n.codec.encrypt("notification-config", strconv.FormatInt(c.UserID, 10), c.Config)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return insertReturningID(ctx, n.DB, n.Dialect,
|
|
`INSERT INTO notification_channels (name, type, config, event_types, enabled, user_id) VALUES (?,?,?,?,?,?)`,
|
|
c.Name, c.Type, config, c.EventTypes, boolToInt(c.Enabled), c.UserID)
|
|
}
|
|
|
|
// UpdateChannel 更新通知渠道。
|
|
func (n *Notifications) UpdateChannel(ctx context.Context, c *NotificationChannelRow) error {
|
|
if c.UserID == 0 {
|
|
if err := n.DB.QueryRowContext(ctx, `SELECT COALESCE(user_id,1) FROM notification_channels WHERE id=?`, c.ID).Scan(&c.UserID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
config, err := n.codec.encrypt("notification-config", strconv.FormatInt(c.UserID, 10), c.Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = n.DB.ExecContext(ctx,
|
|
`UPDATE notification_channels SET name=?, type=?, config=?, event_types=?, enabled=?, updated_at=CURRENT_TIMESTAMP WHERE id=?`,
|
|
c.Name, c.Type, config, c.EventTypes, boolToInt(c.Enabled), c.ID)
|
|
return err
|
|
}
|
|
|
|
// GetChannelRowForUser 按用户取单个通知渠道。未找到返回 nil。
|
|
func (n *Notifications) GetChannelRowForUser(ctx context.Context, id, userID int64) (*NotificationChannelRow, error) {
|
|
row := n.DB.QueryRowContext(ctx,
|
|
`SELECT id, name, type, config, COALESCE(event_types,''), enabled, COALESCE(user_id,1)
|
|
FROM notification_channels WHERE id=? AND user_id=?`, id, userID)
|
|
var c NotificationChannelRow
|
|
var enabled int
|
|
if err := row.Scan(&c.ID, &c.Name, &c.Type, &c.Config, &c.EventTypes, &enabled, &c.UserID); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
c.Enabled = enabled != 0
|
|
config, err := n.codec.decrypt("notification-config", strconv.FormatInt(c.UserID, 10), c.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Config = config
|
|
return &c, nil
|
|
}
|
|
|
|
// UpdateChannelForUser 更新指定用户拥有的通知渠道。
|
|
func (n *Notifications) UpdateChannelForUser(ctx context.Context, c *NotificationChannelRow, userID int64) error {
|
|
config, err := n.codec.encrypt("notification-config", strconv.FormatInt(userID, 10), c.Config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := n.DB.ExecContext(ctx,
|
|
`UPDATE notification_channels
|
|
SET name=?, type=?, config=?, event_types=?, enabled=?, updated_at=CURRENT_TIMESTAMP
|
|
WHERE id=? AND user_id=?`,
|
|
c.Name, c.Type, config, c.EventTypes, boolToInt(c.Enabled), c.ID, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows, err := res.RowsAffected()
|
|
if err == nil && rows == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteChannel 删除通知渠道。
|
|
func (n *Notifications) DeleteChannel(ctx context.Context, id int64) error {
|
|
_, err := n.DB.ExecContext(ctx, `DELETE FROM notification_channels WHERE id=?`, id)
|
|
return err
|
|
}
|
|
|
|
// DeleteChannelForUser 删除指定用户拥有的通知渠道。
|
|
func (n *Notifications) DeleteChannelForUser(ctx context.Context, id, userID int64) error {
|
|
res, err := n.DB.ExecContext(ctx, `DELETE FROM notification_channels WHERE id=? AND user_id=?`, id, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rows, err := res.RowsAffected()
|
|
if err == nil && rows == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetChannel 按 ID 取单个通知渠道(含 config)。未找到返回 nil。
|
|
func (n *Notifications) GetChannel(ctx context.Context, id int64) (*NotificationChannel, error) {
|
|
row := n.DB.QueryRowContext(ctx,
|
|
`SELECT id, name, type, config, COALESCE(event_types,''), COALESCE(user_id,1) FROM notification_channels WHERE id=?`, id)
|
|
var c NotificationChannel
|
|
var userID int64
|
|
if err := row.Scan(&c.ID, &c.Name, &c.Type, &c.Config, &c.EventTypes, &userID); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
config, err := n.codec.decrypt("notification-config", strconv.FormatInt(userID, 10), c.Config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Config = config
|
|
return &c, nil
|
|
}
|
|
|
|
// AccountBindings 取某账号已绑定的通知渠道 ID 列表。
|
|
func (n *Notifications) AccountBindings(ctx context.Context, cookieID string) ([]int64, error) {
|
|
rows, err := n.DB.QueryContext(ctx,
|
|
`SELECT mn.channel_id
|
|
FROM message_notifications mn
|
|
JOIN cookies c ON c.id=mn.cookie_id
|
|
JOIN notification_channels nc ON nc.id=mn.channel_id AND nc.user_id=c.user_id
|
|
WHERE mn.cookie_id=? AND mn.enabled=1`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []int64
|
|
for rows.Next() {
|
|
var id int64
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// SetBindings 设置某账号的通知渠道绑定(覆盖式)。
|
|
func (n *Notifications) SetBindings(ctx context.Context, cookieID string, channelIDs []int64) error {
|
|
tx, err := n.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
var userID int64
|
|
if err := tx.QueryRowContext(ctx, `SELECT user_id FROM cookies WHERE id=?`, cookieID).Scan(&userID); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
for _, channelID := range channelIDs {
|
|
var exists bool
|
|
if err := tx.QueryRowContext(ctx,
|
|
`SELECT EXISTS(SELECT 1 FROM notification_channels WHERE id=? AND user_id=?)`,
|
|
channelID, userID).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if !exists {
|
|
return ErrForbidden
|
|
}
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM message_notifications WHERE cookie_id=?`, cookieID); err != nil {
|
|
return err
|
|
}
|
|
for _, cid := range channelIDs {
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO message_notifications (cookie_id, channel_id, enabled) VALUES (?,?,1)`,
|
|
cookieID, cid); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ---- 系统设置 ----
|
|
|
|
// SystemSettings 系统设置操作。
|
|
type SystemSettings struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
codec *secretCodec
|
|
}
|
|
|
|
// Get 取单项设置。
|
|
func (s *SystemSettings) Get(ctx context.Context, key string) (string, error) {
|
|
var v string
|
|
keyCol := dialectQuote(s.Dialect, "key")
|
|
err := s.DB.QueryRowContext(ctx, `SELECT value FROM system_settings WHERE `+keyCol+`=?`, key).Scan(&v)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
if isSensitiveSettingKey(key) {
|
|
return s.codec.decrypt("system-setting", key, v)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// All 取全部设置(key→value)。
|
|
func (s *SystemSettings) All(ctx context.Context) (map[string]string, error) {
|
|
keyCol := dialectQuote(s.Dialect, "key")
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT `+keyCol+`, value FROM system_settings`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
m := make(map[string]string)
|
|
for rows.Next() {
|
|
var k, v string
|
|
if err := rows.Scan(&k, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
if isSensitiveSettingKey(k) {
|
|
v, err = s.codec.decrypt("system-setting", k, v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
m[k] = v
|
|
}
|
|
return m, rows.Err()
|
|
}
|
|
|
|
// Set 设置单项。
|
|
func (s *SystemSettings) Set(ctx context.Context, key, value string) error {
|
|
if isSensitiveSettingKey(key) {
|
|
encrypted, err := s.codec.encrypt("system-setting", key, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
value = encrypted
|
|
}
|
|
keyCol := dialectQuote(s.Dialect, "key")
|
|
_, err := s.DB.ExecContext(ctx,
|
|
`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
|
|
}
|
|
|
|
// SetMany 在单个事务中原子保存多项设置。
|
|
func (s *SystemSettings) SetMany(ctx context.Context, values map[string]string) error {
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
keyCol := dialectQuote(s.Dialect, "key")
|
|
query := `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",
|
|
})
|
|
for key, value := range values {
|
|
if isSensitiveSettingKey(key) {
|
|
encrypted, err := s.codec.encrypt("system-setting", key, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
value = encrypted
|
|
}
|
|
if _, err := tx.ExecContext(ctx, query, key, value); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// PublicSystemKeys 是公开设置键白名单(前端登录页等无需登录可读)。
|
|
var PublicSystemKeys = map[string]bool{
|
|
"theme_color": true,
|
|
}
|
|
|
|
// Public 取公开设置子集。
|
|
func (s *SystemSettings) Public(ctx context.Context) (map[string]string, error) {
|
|
all, err := s.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[string]string)
|
|
for k := range PublicSystemKeys {
|
|
if v, ok := all[k]; ok {
|
|
out[k] = v
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ---- 物品 (item_info) CRUD ----
|
|
|
|
// ItemInfoRow item_info 完整行。
|
|
type ItemInfoRow struct {
|
|
ID int64
|
|
CookieID string
|
|
ItemID string
|
|
ItemTitle string
|
|
ItemDescription string
|
|
ItemCategory string
|
|
ItemPrice string
|
|
ItemDetail string
|
|
IsMultiSpec bool
|
|
MultiQuantityDelivery bool
|
|
}
|
|
|
|
// AllForCookie 取某账号全部商品。
|
|
func (i *Items) AllForCookie(ctx context.Context, cookieID string) ([]ItemInfoRow, error) {
|
|
rows, err := i.DB.QueryContext(ctx,
|
|
`SELECT id, cookie_id, item_id, COALESCE(item_title,''), COALESCE(item_description,''),
|
|
COALESCE(item_category,''), COALESCE(item_price,''), COALESCE(item_detail,''),
|
|
is_multi_spec, COALESCE(multi_quantity_delivery,0)
|
|
FROM item_info WHERE cookie_id=? ORDER BY id DESC`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ItemInfoRow
|
|
for rows.Next() {
|
|
var r ItemInfoRow
|
|
var isMulti, multiQty int
|
|
if err := rows.Scan(&r.ID, &r.CookieID, &r.ItemID, &r.ItemTitle, &r.ItemDescription,
|
|
&r.ItemCategory, &r.ItemPrice, &r.ItemDetail, &isMulti, &multiQty); err != nil {
|
|
return nil, err
|
|
}
|
|
r.IsMultiSpec = isMulti != 0
|
|
r.MultiQuantityDelivery = multiQty != 0
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// Upsert 插入或更新商品信息。
|
|
func (i *Items) Upsert(ctx context.Context, r *ItemInfoRow) error {
|
|
_, err := i.DB.ExecContext(ctx,
|
|
`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)`+
|
|
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))
|
|
return err
|
|
}
|
|
|
|
// UpsertBasic 插入或补全商品基础信息,不覆盖已有的多规格/多数量发货设置。
|
|
func (i *Items) UpsertBasic(ctx context.Context, r *ItemInfoRow) error {
|
|
return i.upsertBasic(ctx, i.DB, r)
|
|
}
|
|
|
|
func (i *Items) UpsertBasicTx(ctx context.Context, tx *sql.Tx, r *ItemInfoRow) error {
|
|
return i.upsertBasic(ctx, tx, r)
|
|
}
|
|
|
|
func (i *Items) upsertBasic(ctx context.Context, execer sqlExecer, r *ItemInfoRow) error {
|
|
// 三种数据库的条件 upsert:非空才覆盖,空值保留旧值。
|
|
// SQLite/Postgres 用 EXCLUDED.col 引用插入值;MySQL 用 VALUES(col)。
|
|
var conflictClause string
|
|
switch i.Dialect {
|
|
case DialectMySQL:
|
|
conflictClause = ` ON DUPLICATE KEY UPDATE
|
|
item_title=CASE WHEN VALUES(item_title) IS NOT NULL AND VALUES(item_title) != '' THEN VALUES(item_title) ELSE item_info.item_title END,
|
|
item_description=CASE WHEN VALUES(item_description) IS NOT NULL AND VALUES(item_description) != '' THEN VALUES(item_description) ELSE item_info.item_description END,
|
|
item_category=CASE WHEN VALUES(item_category) IS NOT NULL AND VALUES(item_category) != '' THEN VALUES(item_category) ELSE item_info.item_category END,
|
|
item_price=CASE WHEN VALUES(item_price) IS NOT NULL AND VALUES(item_price) != '' THEN VALUES(item_price) ELSE item_info.item_price END,
|
|
item_detail=CASE WHEN VALUES(item_detail) IS NOT NULL AND VALUES(item_detail) != '' THEN VALUES(item_detail) ELSE item_info.item_detail END,
|
|
updated_at=CURRENT_TIMESTAMP`
|
|
default:
|
|
conflictClause = ` ON CONFLICT(cookie_id, item_id) DO UPDATE SET
|
|
item_title=CASE WHEN EXCLUDED.item_title IS NOT NULL AND EXCLUDED.item_title != '' THEN EXCLUDED.item_title ELSE item_info.item_title END,
|
|
item_description=CASE WHEN EXCLUDED.item_description IS NOT NULL AND EXCLUDED.item_description != '' THEN EXCLUDED.item_description ELSE item_info.item_description END,
|
|
item_category=CASE WHEN EXCLUDED.item_category IS NOT NULL AND EXCLUDED.item_category != '' THEN EXCLUDED.item_category ELSE item_info.item_category END,
|
|
item_price=CASE WHEN EXCLUDED.item_price IS NOT NULL AND EXCLUDED.item_price != '' THEN EXCLUDED.item_price ELSE item_info.item_price END,
|
|
item_detail=CASE WHEN EXCLUDED.item_detail IS NOT NULL AND EXCLUDED.item_detail != '' THEN EXCLUDED.item_detail ELSE item_info.item_detail END,
|
|
updated_at=CURRENT_TIMESTAMP`
|
|
}
|
|
_, err := execer.ExecContext(ctx,
|
|
`INSERT INTO item_info (cookie_id, item_id, item_title, item_description,
|
|
item_category, item_price, item_detail, updated_at)
|
|
VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)`+conflictClause,
|
|
r.CookieID, r.ItemID, nullable(r.ItemTitle), nullable(r.ItemDescription),
|
|
nullable(r.ItemCategory), nullable(r.ItemPrice), nullable(r.ItemDetail))
|
|
return err
|
|
}
|
|
|
|
// Delete 删除商品。
|
|
func (i *Items) Delete(ctx context.Context, cookieID, itemID string) error {
|
|
_, err := i.DB.ExecContext(ctx, `DELETE FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID)
|
|
return err
|
|
}
|
|
|
|
// SetMultiSpec 设置多规格开关。
|
|
func (i *Items) SetMultiSpec(ctx context.Context, cookieID, itemID string, on bool) error {
|
|
_, err := i.DB.ExecContext(ctx,
|
|
`UPDATE item_info SET is_multi_spec=?, updated_at=CURRENT_TIMESTAMP WHERE cookie_id=? AND item_id=?`,
|
|
boolToInt(on), cookieID, itemID)
|
|
return err
|
|
}
|
|
|
|
// SetMultiQuantity 设置多数量发货开关。
|
|
func (i *Items) SetMultiQuantity(ctx context.Context, cookieID, itemID string, on bool) error {
|
|
_, err := i.DB.ExecContext(ctx,
|
|
`UPDATE item_info SET multi_quantity_delivery=?, updated_at=CURRENT_TIMESTAMP WHERE cookie_id=? AND item_id=?`,
|
|
boolToInt(on), cookieID, itemID)
|
|
return err
|
|
}
|