Files
Ydisks-Xianyu-Helper/internal/db/orders_ext.go
T
2026-08-15 15:26:01 +08:00

457 lines
17 KiB
Go

package db
import (
"context"
"database/sql"
"errors"
"sort"
"strings"
)
// OrderRow 订单列表展示行(含 item_title)。
type OrderRow struct {
OrderID string
ItemID string
ItemTitle string
ItemDetail string
BuyerID string
SpecName string
SpecValue string
Quantity string
Amount string
OrderStatus string
CookieID string
IsBargain int
SystemShipped bool
ReceiverName string
ReceiverPhone string
ReceiverAddr string
ReceiverCity string
CreatedAt string
UpdatedAt string
}
// OrderListFilter 是订单列表分页查询条件。
type OrderListFilter struct {
UserID int64
CookieID string
Status string
Search string
Limit int
Offset int
}
// ListForUser 按用户隔离分页查询订单,并带出商品标题/详情。
func (o *Orders) ListForUser(ctx context.Context, f OrderListFilter) ([]OrderRow, int, error) {
if f.Limit <= 0 {
f.Limit = 20
}
// where 保存where,供当前处理流程使用
where := []string{"c.user_id=?", "o.deleted_at IS NULL"}
// args 保存args,供当前处理流程使用
args := []any{f.UserID}
if f.CookieID != "" {
where = append(where, "o.cookie_id=?")
args = append(args, f.CookieID)
}
if // statuses 保存statuses,供当前处理流程使用
statuses := normalizedStatusCandidates(f.Status); len(statuses) > 0 {
// placeholders 保存placeholders,供当前处理流程使用
placeholders := make([]string, 0, len(statuses))
// st 表示当前遍历过程中的st
for _, st := range statuses {
placeholders = append(placeholders, "?")
args = append(args, st)
}
where = append(where, "o.order_status IN ("+strings.Join(placeholders, ",")+")")
}
if // search 保存搜索,供当前处理流程使用
search := strings.ToLower(strings.TrimSpace(f.Search)); search != "" {
// pattern 保存pattern,供当前处理流程使用
pattern := "%" + search + "%"
where = append(where, `(LOWER(o.order_id) LIKE ? OR LOWER(COALESCE(o.item_id,'')) LIKE ?
OR LOWER(COALESCE(o.buyer_id,'')) LIKE ? OR LOWER(COALESCE(i.item_title,'')) LIKE ?
OR LOWER(COALESCE(o.receiver_name,'')) LIKE ? OR LOWER(COALESCE(o.receiver_phone,'')) LIKE ?)`)
for // i 保存i,供当前处理流程使用
i := 0; i < 6; i++ {
args = append(args, pattern)
}
}
// whereSQL 保存whereSQL,供当前处理流程使用
whereSQL := strings.Join(where, " AND ")
// total 保存总数,供当前处理流程使用
var total int
if // err 保存err,供当前处理流程使用
err := o.DB.QueryRowContext(ctx,
`SELECT COUNT(*)
FROM orders o
JOIN cookies c ON c.id=o.cookie_id
LEFT JOIN item_info i ON i.cookie_id=o.cookie_id AND i.item_id=o.item_id
WHERE `+whereSQL, args...).Scan(&total); err != nil {
return nil, 0, err
}
// queryArgs 保存查询Args,供当前处理流程使用
queryArgs := append([]any{}, args...)
queryArgs = append(queryArgs, f.Limit, f.Offset)
// rows、err 保存rows、err,供当前处理流程使用
rows, err := o.DB.QueryContext(ctx,
`SELECT o.order_id, o.item_id, COALESCE(i.item_title,''), COALESCE(i.item_detail,''),
o.buyer_id, o.spec_name, o.spec_value, o.quantity, o.amount,
o.order_status, o.cookie_id, o.is_bargain, o.system_shipped,
o.receiver_name, o.receiver_phone, o.receiver_address, o.receiver_city,
o.created_at, o.updated_at
FROM orders o
JOIN cookies c ON c.id=o.cookie_id
LEFT JOIN item_info i ON i.cookie_id=o.cookie_id AND i.item_id=o.item_id
WHERE `+whereSQL+`
ORDER BY o.created_at DESC, o.order_id DESC
LIMIT ? OFFSET ?`, queryArgs...)
if err != nil {
return nil, 0, err
}
defer rows.Close()
// out 保存out,供当前处理流程使用
out := []OrderRow{}
for rows.Next() {
// r 保存r,供当前处理流程使用
var r OrderRow
// itemID、itemTitle、itemDetail、buyerID、specName、specValue、qty、amount、receiverName、receiverPhone、receiverAddr、receiverCity 保存商品ID、itemTitle、itemDetail、buyerID、specName、specValue、qty、amount、receiverName、receiverPhone、receiverAddr、receiverCity,供当前处理流程使用
var itemID, itemTitle, itemDetail, buyerID, specName, specValue, qty, amount, receiverName, receiverPhone, receiverAddr, receiverCity sql.NullString
// isBargain、sysShipped 保存isBargain、sysShipped,供当前处理流程使用
var isBargain, sysShipped int
if // err 保存err,供当前处理流程使用
err := rows.Scan(&r.OrderID, &itemID, &itemTitle, &itemDetail, &buyerID, &specName, &specValue, &qty, &amount,
&r.OrderStatus, &r.CookieID, &isBargain, &sysShipped, &receiverName, &receiverPhone, &receiverAddr,
&receiverCity, &r.CreatedAt, &r.UpdatedAt); err != nil {
return nil, 0, err
}
r.ItemID = itemID.String
r.ItemTitle = itemTitle.String
r.ItemDetail = itemDetail.String
r.BuyerID = buyerID.String
r.SpecName = specName.String
r.SpecValue = specValue.String
r.Quantity = qty.String
r.Amount = amount.String
r.IsBargain = isBargain
r.SystemShipped = sysShipped != 0
r.ReceiverName = receiverName.String
r.ReceiverPhone = receiverPhone.String
r.ReceiverAddr = receiverAddr.String
r.ReceiverCity = receiverCity.String
out = append(out, r)
}
return out, total, rows.Err()
}
// normalizedStatusCandidates 负责normalized状态Candidates相关处理。
func normalizedStatusCandidates(status string) []string {
status = strings.TrimSpace(status)
if status == "" || status == "all" {
return nil
}
switch NormalizeOrderStatus(status) {
case "processing":
return []string{"processing", "1"}
case "pending_ship":
return []string{"pending_ship", "paid", "2"}
case "shipped":
return []string{"shipped", "3"}
case "completed":
return []string{"completed", "4", "11"}
case "refunding":
return []string{"refunding", "5", "7", "9"}
case "cancelled":
return []string{"cancelled", "6", "8", "10", "12"}
case "unknown":
return []string{status}
default:
return []string{status}
}
}
// ByCookie 取某账号的订单(limit 上限)。
func (o *Orders) ByCookie(ctx context.Context, cookieID string, limit int) ([]OrderRow, error) {
if limit <= 0 {
limit = 1000
}
return o.ByCookiePage(ctx, cookieID, limit, 0)
}
// ByCookiePage 分页读取账号订单,供需要完整扫描的后台任务使用。
func (o *Orders) ByCookiePage(ctx context.Context, cookieID string, limit, offset int) ([]OrderRow, error) {
if limit <= 0 {
limit = 500
}
if offset < 0 {
offset = 0
}
// rows、err 保存rows、err,供当前处理流程使用
rows, err := o.DB.QueryContext(ctx,
`SELECT order_id, item_id, buyer_id, spec_name, spec_value, quantity, amount,
order_status, is_bargain, system_shipped, receiver_name, receiver_phone,
receiver_address, receiver_city, created_at, updated_at
FROM orders WHERE cookie_id=? AND deleted_at IS NULL ORDER BY created_at DESC,order_id DESC LIMIT ? OFFSET ?`, cookieID, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
// out 保存out,供当前处理流程使用
var out []OrderRow
for rows.Next() {
// r 保存r,供当前处理流程使用
var r OrderRow
// itemID、buyerID、specName、specValue、qty、amount、receiverName、receiverPhone、receiverAddr、receiverCity 保存商品ID、buyerID、specName、specValue、qty、amount、receiverName、receiverPhone、receiverAddr、receiverCity,供当前处理流程使用
var itemID, buyerID, specName, specValue, qty, amount, receiverName, receiverPhone, receiverAddr, receiverCity sql.NullString
// isBargain、sysShipped 保存isBargain、sysShipped,供当前处理流程使用
var isBargain, sysShipped int
if // err 保存err,供当前处理流程使用
err := rows.Scan(&r.OrderID, &itemID, &buyerID, &specName, &specValue, &qty, &amount,
&r.OrderStatus, &isBargain, &sysShipped, &receiverName, &receiverPhone, &receiverAddr,
&receiverCity, &r.CreatedAt, &r.UpdatedAt); err != nil {
return nil, err
}
r.ItemID = itemID.String
r.BuyerID = buyerID.String
r.SpecName = specName.String
r.SpecValue = specValue.String
r.Quantity = qty.String
r.Amount = amount.String
r.IsBargain = isBargain
r.SystemShipped = sysShipped != 0
r.ReceiverName = receiverName.String
r.ReceiverPhone = receiverPhone.String
r.ReceiverAddr = receiverAddr.String
r.ReceiverCity = receiverCity.String
r.CookieID = cookieID
out = append(out, r)
}
return out, rows.Err()
}
// SoftDeleteMissingForCookie 将本次完整卖家订单同步中未出现的本地订单逻辑删除。
// activeIDs 为空表示线上已确认没有任何卖家订单;调用方必须确保同步完整成功后再调用。
// SoftDeleteMissingForCookie 负责SoftDeleteMissingFor登录凭证相关处理。
func (o *Orders) SoftDeleteMissingForCookie(ctx context.Context, cookieID string, activeIDs map[string]struct{}) (int, error) {
if strings.TrimSpace(cookieID) == "" {
return 0, errors.New("cookie_id 不能为空")
}
// args 保存批量 UPDATE 的参数,首个参数是账号 ID。
args := []any{cookieID}
// query 保存批量逻辑删除 SQL。
query := `UPDATE orders SET deleted_at=CURRENT_TIMESTAMP, updated_at=CURRENT_TIMESTAMP
WHERE cookie_id=? AND deleted_at IS NULL`
if len(activeIDs) > 0 {
// activeOrderIDs 按稳定顺序保存线上仍存在的订单 ID,便于日志和测试复现。
activeOrderIDs := make([]string, 0, len(activeIDs))
// orderID 表示当前线上仍存在的订单标识。
for orderID := range activeIDs {
activeOrderIDs = append(activeOrderIDs, orderID)
}
sort.Strings(activeOrderIDs)
// placeholders 保存 NOT IN 子句所需的占位符。
placeholders := make([]string, len(activeOrderIDs))
// i、orderID 分别表示占位符序号和线上订单标识。
for i, orderID := range activeOrderIDs {
placeholders[i] = "?"
args = append(args, orderID)
}
query += ` AND order_id NOT IN (` + strings.Join(placeholders, ",") + `)`
}
// result、err 保存批量逻辑删除结果及错误。
result, err := o.DB.ExecContext(ctx, query, args...)
if err != nil {
return 0, err
}
// deleted 保存本次批量逻辑删除的订单数量。
deleted, err := result.RowsAffected()
if err != nil {
return 0, err
}
return int(deleted), nil
}
// OrderStatusMap 将数字状态码转换为文本状态。
var OrderStatusMap = map[string]string{
"paid": "pending_ship",
"1": "processing", "2": "pending_ship", "3": "shipped", "4": "completed",
"5": "refunding", "6": "cancelled", "7": "refunding", "8": "cancelled",
"9": "refunding", "10": "cancelled", "11": "completed", "12": "cancelled",
}
// NormalizeOrderStatus 数字码归一为文本。
func NormalizeOrderStatus(s string) string {
if // t、ok 保存t、ok,供当前处理流程使用
t, ok := OrderStatusMap[s]; ok {
return t
}
if s == "" {
return "unknown"
}
return s
}
// AllTitles 取全部 item_id → item_title 映射(订单列表用)。
func (i *Items) AllTitles(ctx context.Context) (map[string]string, error) {
// rows、err 保存rows、err,供当前处理流程使用
rows, err := i.DB.QueryContext(ctx, `SELECT item_id, item_title FROM item_info`)
if err != nil {
return nil, err
}
defer rows.Close()
// m 保存m,供当前处理流程使用
m := make(map[string]string)
for rows.Next() {
// id、title 保存id、title,供当前处理流程使用
var id, title sql.NullString
if // err 保存err,供当前处理流程使用
err := rows.Scan(&id, &title); err != nil {
return nil, err
}
m[id.String] = title.String
}
return m, rows.Err()
}
// 卡券 CRUD 辅助。
// CardFull 卡券完整信息(CRUD 用)。
type CardFull struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
APIConfig string `json:"api_config"`
TextContent string `json:"text_content"`
DataContent string `json:"data_content"`
ImageURL string `json:"image_url"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
DelaySeconds int `json:"delay_seconds"`
IsMultiSpec bool `json:"is_multi_spec"`
SpecName string `json:"spec_name"`
SpecValue string `json:"spec_value"`
UserID int64 `json:"user_id"`
}
// ExistsOwned 判断卡密组是否属于指定用户。
func (c *Cards) ExistsOwned(ctx context.Context, cardID, userID int64) (bool, error) {
// exists 保存exists,供当前处理流程使用
var exists bool
// err 保存err,供当前处理流程使用
err := c.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM cards WHERE id=? AND user_id=?)`, cardID, userID).Scan(&exists)
return exists, err
}
// Get 取单个卡券。
func (c *Cards) Get(ctx context.Context, cardID int64) (*CardFull, error) {
// cf 保存cf,供当前处理流程使用
var cf CardFull
// enabled、isMultiSpec 保存enabled、isMultiSpec,供当前处理流程使用
var enabled, isMultiSpec int
// apiCfg、textContent、dataContent、imageURL、specName、specValue、desc 保存apiCfg、textContent、dataContent、imageURL、specName、specValue、desc,供当前处理流程使用
var apiCfg, textContent, dataContent, imageURL, specName, specValue, desc sql.NullString
// err 保存err,供当前处理流程使用
err := c.DB.QueryRowContext(ctx,
`SELECT id, name, type, api_config, text_content, data_content, image_url, description,
enabled, delay_seconds, is_multi_spec, spec_name, spec_value, user_id
FROM cards WHERE id=?`, cardID).Scan(
&cf.ID, &cf.Name, &cf.Type, &apiCfg, &textContent, &dataContent, &imageURL, &desc,
&enabled, &cf.DelaySeconds, &isMultiSpec, &specName, &specValue, &cf.UserID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
return nil, err
}
cf.APIConfig = apiCfg.String
cf.TextContent = textContent.String
cf.DataContent = dataContent.String
cf.ImageURL = imageURL.String
cf.Description = desc.String
cf.Enabled = enabled != 0
cf.IsMultiSpec = isMultiSpec != 0
cf.SpecName = specName.String
cf.SpecValue = specValue.String
return &cf, nil
}
// AllForUser 取某用户全部卡券。
func (c *Cards) AllForUser(ctx context.Context, userID int64) ([]CardFull, error) {
// rows、err 保存rows、err,供当前处理流程使用
rows, err := c.DB.QueryContext(ctx,
`SELECT id, name, type, api_config, text_content, data_content, image_url, description,
enabled, delay_seconds, is_multi_spec, spec_name, spec_value, user_id
FROM cards WHERE user_id=? ORDER BY id DESC`, userID)
if err != nil {
return nil, err
}
defer rows.Close()
// out 保存out,供当前处理流程使用
var out []CardFull
for rows.Next() {
// cf 保存cf,供当前处理流程使用
var cf CardFull
// enabled、isMultiSpec 保存enabled、isMultiSpec,供当前处理流程使用
var enabled, isMultiSpec int
// apiCfg、textContent、dataContent、imageURL、specName、specValue、desc 保存apiCfg、textContent、dataContent、imageURL、specName、specValue、desc,供当前处理流程使用
var apiCfg, textContent, dataContent, imageURL, specName, specValue, desc sql.NullString
if // err 保存err,供当前处理流程使用
err := rows.Scan(&cf.ID, &cf.Name, &cf.Type, &apiCfg, &textContent, &dataContent, &imageURL, &desc,
&enabled, &cf.DelaySeconds, &isMultiSpec, &specName, &specValue, &cf.UserID); err != nil {
return nil, err
}
cf.APIConfig = apiCfg.String
cf.TextContent = textContent.String
cf.DataContent = dataContent.String
cf.ImageURL = imageURL.String
cf.Description = desc.String
cf.Enabled = enabled != 0
cf.IsMultiSpec = isMultiSpec != 0
cf.SpecName = specName.String
cf.SpecValue = specValue.String
out = append(out, cf)
}
return out, rows.Err()
}
// Create 创建卡券,返回新 ID。
func (c *Cards) Create(ctx context.Context, cf *CardFull) (int64, error) {
return insertReturningID(ctx, c.DB, c.Dialect,
`INSERT INTO cards (name, type, api_config, text_content, data_content, image_url, description,
enabled, delay_seconds, is_multi_spec, spec_name, spec_value, user_id)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)`,
cf.Name, cf.Type, nullable(cf.APIConfig), nullable(cf.TextContent), nullable(cf.DataContent),
nullable(cf.ImageURL), nullable(cf.Description), boolToInt(cf.Enabled), cf.DelaySeconds,
boolToInt(cf.IsMultiSpec), nullable(cf.SpecName), nullable(cf.SpecValue), cf.UserID)
}
// Update 更新卡券。
func (c *Cards) Update(ctx context.Context, cf *CardFull) error {
// err 保存err,供当前处理流程使用
_, err := c.DB.ExecContext(ctx,
`UPDATE cards SET name=?, type=?, api_config=?, text_content=?, data_content=?, image_url=?,
description=?, enabled=?, delay_seconds=?, is_multi_spec=?, spec_name=?, spec_value=?, updated_at=CURRENT_TIMESTAMP
WHERE id=?`,
cf.Name, cf.Type, nullable(cf.APIConfig), nullable(cf.TextContent), nullable(cf.DataContent),
nullable(cf.ImageURL), nullable(cf.Description), boolToInt(cf.Enabled), cf.DelaySeconds,
boolToInt(cf.IsMultiSpec), nullable(cf.SpecName), nullable(cf.SpecValue), cf.ID)
return err
}
// Delete 删除卡券。
func (c *Cards) Delete(ctx context.Context, cardID int64) error {
// err 保存err,供当前处理流程使用
_, err := c.DB.ExecContext(ctx, `DELETE FROM cards WHERE id=?`, cardID)
return err
}
// nullable 负责nullable相关处理。
func nullable(s string) any {
if s == "" {
return nil
}
return s
}