Files
Ydisks-Xianyu-Helper/internal/db/orders.go
T
2026-06-28 11:54:39 +08:00

194 lines
5.1 KiB
Go

package db
import (
"context"
"database/sql"
"errors"
)
// Order 对应 orders 表。
type Order struct {
OrderID string
ItemID string
BuyerID string
SpecName string
SpecValue string
Quantity string
Amount string
OrderStatus string
CookieID string
IsBargain int
ReceiverName string
ReceiverPhone string
ReceiverAddr string
ReceiverCity string
Version int
ChatID string
SystemShipped bool
CreatedAt string
UpdatedAt string
}
// Orders 订单操作。
type Orders struct{ DB *sql.DB }
// Upsert 插入或更新订单。仅更新提供的非零字段,使用 version 乐观锁。
// 对应 Python insert_or_update_order 的核心行为。
func (o *Orders) Upsert(ctx context.Context, orderID string, opts OrderUpsertOpts) error {
if orderID == "" {
return errors.New("order_id 不能为空")
}
// 先尝试 INSERT OR IGNORE 占位。
_, err := o.DB.ExecContext(ctx,
`INSERT OR IGNORE INTO orders (order_id, item_id, buyer_id, cookie_id, order_status, version)
VALUES (?, ?, ?, ?, 'unknown', 1)`,
orderID, opts.ItemID, opts.BuyerID, opts.CookieID)
if err != nil {
return err
}
// 动态构造 UPDATE。
set := []string{}
args := []any{}
if opts.SystemShipped != nil {
set = append(set, "system_shipped=?")
args = append(args, boolToInt(*opts.SystemShipped))
}
if opts.ChatID != "" {
set = append(set, "chat_id=?")
args = append(args, opts.ChatID)
}
if opts.ItemID != "" {
set = append(set, "item_id=?")
args = append(args, opts.ItemID)
}
if opts.BuyerID != "" {
set = append(set, "buyer_id=?")
args = append(args, opts.BuyerID)
}
if opts.CookieID != "" {
set = append(set, "cookie_id=?")
args = append(args, opts.CookieID)
}
if opts.OrderStatus != "" {
set = append(set, "order_status=?")
args = append(args, opts.OrderStatus)
}
if opts.SpecName != "" {
set = append(set, "spec_name=?")
args = append(args, opts.SpecName)
}
if opts.SpecValue != "" {
set = append(set, "spec_value=?")
args = append(args, opts.SpecValue)
}
if opts.Quantity != "" {
set = append(set, "quantity=?")
args = append(args, opts.Quantity)
}
if opts.Amount != "" {
set = append(set, "amount=?")
args = append(args, opts.Amount)
}
if opts.ReceiverName != "" {
set = append(set, "receiver_name=?")
args = append(args, opts.ReceiverName)
}
if opts.ReceiverPhone != "" {
set = append(set, "receiver_phone=?")
args = append(args, opts.ReceiverPhone)
}
if opts.ReceiverAddr != "" {
set = append(set, "receiver_address=?")
args = append(args, opts.ReceiverAddr)
}
if opts.ReceiverCity != "" {
set = append(set, "receiver_city=?")
args = append(args, opts.ReceiverCity)
}
if len(set) == 0 {
return nil // 无字段可更新
}
set = append(set, "updated_at=CURRENT_TIMESTAMP")
args = append(args, orderID)
q := `UPDATE orders SET ` + joinSet(set) + ` WHERE order_id=?`
_, err = o.DB.ExecContext(ctx, q, args...)
return err
}
// OrderUpsertOpts Upsert 的可选字段。
type OrderUpsertOpts struct {
ItemID string
BuyerID string
CookieID string
OrderStatus string
SpecName string
SpecValue string
Quantity string
Amount string
ReceiverName string
ReceiverPhone string
ReceiverAddr string
ReceiverCity string
ChatID string
SystemShipped *bool
}
// Get 按 order_id 查询。
func (o *Orders) Get(ctx context.Context, orderID string) (*Order, error) {
var ord Order
var isBargain, version, sysShipped int
var itemID, buyerID, specName, specValue, qty, amount, status, cookieID, receiverName, receiverPhone, receiverAddr, receiverCity, chatID, createdAt, updatedAt sql.NullString
err := o.DB.QueryRowContext(ctx,
`SELECT order_id, item_id, buyer_id, spec_name, spec_value, quantity, amount,
order_status, cookie_id, is_bargain, receiver_name, receiver_phone, receiver_address,
receiver_city, version, chat_id, system_shipped, created_at, updated_at
FROM orders WHERE order_id=?`, orderID).Scan(
&ord.OrderID, &itemID, &buyerID, &specName, &specValue, &qty, &amount,
&status, &cookieID, &isBargain, &receiverName, &receiverPhone, &receiverAddr,
&receiverCity, &version, &chatID, &sysShipped, &createdAt, &updatedAt)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
return nil, err
}
ord.ItemID = itemID.String
ord.BuyerID = buyerID.String
ord.SpecName = specName.String
ord.SpecValue = specValue.String
ord.Quantity = qty.String
ord.Amount = amount.String
ord.OrderStatus = status.String
ord.CookieID = cookieID.String
ord.IsBargain = isBargain
ord.ReceiverName = receiverName.String
ord.ReceiverPhone = receiverPhone.String
ord.ReceiverAddr = receiverAddr.String
ord.ReceiverCity = receiverCity.String
ord.Version = version
ord.ChatID = chatID.String
ord.SystemShipped = sysShipped != 0
ord.CreatedAt = createdAt.String
ord.UpdatedAt = updatedAt.String
return &ord, nil
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
func joinSet(parts []string) string {
out := ""
for i, p := range parts {
if i > 0 {
out += ", "
}
out += p
}
return out
}