把卡密自动发货从 engine.DeliveryService 收敛到统一的 automation.Center: WS 事件/计划任务/手动触发统一为 Task,trigger_key 持久化防重,付款发货、 评价赠品、超时求评价共用一套规则+动作编排。新增 internal/automation 包、 db.AutomationRules repository、00007 迁移。 同步删除被取代的并行死系统: - engine.DeliveryService + delivery_test.go(已无引用) - db.DeliveryRules/DeliveryRule + 3 个 Match 方法(生产零调用) - card_handlers 的 /delivery-rules CRUD(前端早已只走 automation-rules) - engine.SystemMessage + extractSystemMessage(dispatch 不再单独路由 message["3"]) - store.DeliveryRules 字段、Cards.IncrementDeliveryTimes - 00008 迁移 DROP delivery_rules/delivery_rule_variants + email_verifications/ captcha_codes/ai_item_cache/risk_control_logs + publish 批次 3 个死列 - isAPIPath 清理 ~15 个未挂载的前缀,stub_handlers.go 内联删除 - vite.config 删 /delivery-rules proxy 其它:orders.Upsert 注释改为诚实描述(version 列不参与并发控制); dispatch.cleanupDedupLocked 用 sort.Slice 取代 O(n²) 选择排序; 删自定义 min 用内置;deliveryRuleItemOwned 改名 itemOwnedByUser。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
174 lines
4.4 KiB
Go
174 lines
4.4 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
// ItemInfo 对应 item_info 表。
|
|
type ItemInfo struct {
|
|
ID int64
|
|
CookieID string
|
|
ItemID string
|
|
ItemTitle string
|
|
ItemDescription string
|
|
ItemCategory string
|
|
ItemPrice string
|
|
ItemDetail string
|
|
IsMultiSpec bool
|
|
MultiQuantityDelivery bool
|
|
}
|
|
|
|
// Card 对应 cards 表(发货用字段)。
|
|
type Card struct {
|
|
ID int64
|
|
Name string
|
|
Type string // api/text/data/image
|
|
APIConfig string // JSON
|
|
TextContent string
|
|
DataContent string
|
|
ImageURL string
|
|
Description string
|
|
Enabled bool
|
|
DelaySeconds int
|
|
IsMultiSpec bool
|
|
SpecName string
|
|
SpecValue string
|
|
UserID int64
|
|
}
|
|
|
|
// Items 商品信息操作。
|
|
type Items struct{ DB *sql.DB }
|
|
|
|
// Get 取某账号下某商品信息。不存在返回 ErrNotFound。
|
|
func (i *Items) Get(ctx context.Context, cookieID, itemID string) (*ItemInfo, error) {
|
|
var it ItemInfo
|
|
var isMulti, multiQty int
|
|
var title, desc, cat, price, detail sql.NullString
|
|
err := i.DB.QueryRowContext(ctx,
|
|
`SELECT id, cookie_id, item_id, item_title, item_description, item_category, item_price, item_detail,
|
|
is_multi_spec, multi_quantity_delivery
|
|
FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(
|
|
&it.ID, &it.CookieID, &it.ItemID, &title, &desc, &cat,
|
|
&price, &detail, &isMulti, &multiQty)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
it.ItemTitle = title.String
|
|
it.ItemDescription = desc.String
|
|
it.ItemCategory = cat.String
|
|
it.ItemPrice = price.String
|
|
it.ItemDetail = detail.String
|
|
it.IsMultiSpec = isMulti != 0
|
|
it.MultiQuantityDelivery = multiQty != 0
|
|
return &it, nil
|
|
}
|
|
|
|
// IsMultiSpec 是否多规格商品。
|
|
func (i *Items) IsMultiSpec(ctx context.Context, cookieID, itemID string) bool {
|
|
var v int
|
|
err := i.DB.QueryRowContext(ctx, `SELECT is_multi_spec FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(&v)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return v != 0
|
|
}
|
|
|
|
// MultiQuantityDelivery 是否开启多数量发货。
|
|
func (i *Items) MultiQuantityDelivery(ctx context.Context, cookieID, itemID string) bool {
|
|
var v int
|
|
err := i.DB.QueryRowContext(ctx, `SELECT multi_quantity_delivery FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(&v)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return v != 0
|
|
}
|
|
|
|
// Cards 卡券操作。
|
|
type Cards struct{ DB *sql.DB }
|
|
|
|
// ConsumeBatchData 消费一条批量数据卡券(data 类型),返回内容。
|
|
// ConsumeBatchData 取 data_content 第一行,删除已消费行,发完置空。
|
|
func (c *Cards) ConsumeBatchData(ctx context.Context, cardID int64) (string, error) {
|
|
tx, err := c.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
var dataContent sql.NullString
|
|
err = tx.QueryRowContext(ctx, `SELECT data_content FROM cards WHERE id=?`, cardID).Scan(&dataContent)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", ErrNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
if !dataContent.Valid || dataContent.String == "" {
|
|
return "", errors.New("卡券批量数据为空")
|
|
}
|
|
|
|
// 按行分割,取第一行非空作为发货内容,其余回写。
|
|
lines := splitLines(dataContent.String)
|
|
if len(lines) == 0 {
|
|
return "", errors.New("卡券批量数据无有效行")
|
|
}
|
|
content := lines[0]
|
|
remaining := ""
|
|
if len(lines) > 1 {
|
|
remaining = joinLines(lines[1:])
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `UPDATE cards SET data_content=? WHERE id=?`, remaining, cardID); err != nil {
|
|
return "", err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return "", err
|
|
}
|
|
return content, nil
|
|
}
|
|
|
|
// splitLines / joinLines 统一处理多行库存内容。
|
|
func splitLines(s string) []string {
|
|
var out []string
|
|
cur := []rune{}
|
|
for _, r := range s {
|
|
switch r {
|
|
case '\n':
|
|
out = append(out, string(cur))
|
|
cur = cur[:0]
|
|
case '\r':
|
|
out = append(out, string(cur))
|
|
cur = cur[:0]
|
|
// 跳过 \r,处理 \r\n
|
|
default:
|
|
cur = append(cur, r)
|
|
}
|
|
}
|
|
if len(cur) > 0 {
|
|
out = append(out, string(cur))
|
|
}
|
|
// 过滤空行。
|
|
res := out[:0]
|
|
for _, l := range out {
|
|
if l != "" {
|
|
res = append(res, l)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func joinLines(lines []string) string {
|
|
if len(lines) == 0 {
|
|
return ""
|
|
}
|
|
out := lines[0]
|
|
for _, l := range lines[1:] {
|
|
out += "\n" + l
|
|
}
|
|
return out
|
|
}
|