Files
Ydisks-Xianyu-Helper/internal/automation/delivery_notifier.go
T
lettimepassby 7c4a944b43 新增:自动化规则支持拍下未付款自动改价
- 新增 order_created 触发器,识别买家拍下待付款交易卡片
- 新增 MTOP 订单改价接口 mtop.taobao.idle.trade.user.adjust.price(含 token 重签与凭证恢复)
- 新增 adjust_price 自动化动作,目标价格按整数分校验与执行,错误按三态收口
- 应用层规则校验限定改价动作只用于拍下未付款规则,可附带买家提醒文案
- 前端规则页新增拍下改价类型、目标价格与可选提醒编辑器
2026-08-20 10:48:51 +08:00

64 lines
2.3 KiB
Go

package automation
import (
"context"
"fmt"
"xianyu-go/internal/db"
)
// deliveryNotifier 将自动化运行状态转换为可选的多渠道发货结果通知,并通过回调读取构造期固定的通知器。
type deliveryNotifier struct {
// current 返回构造期固定的通知器实例;为空时不发送外部通知。
current func() Notifier
}
// notifyResult 根据规则执行结果发送通知,成功且实际发出内容时才发送成功通知。
// runID 与 status 会传给持久化 outbox,防止恢复扫描对同一运行重复排队。
func (n deliveryNotifier) notifyResult(ctx context.Context, task Task, runID int64, status string, sent int, errMsg string) {
// notifier 是当前可选的外部通知器。
notifier := n.current()
if notifier == nil {
return
}
// triggerName 是面向用户展示的自动化触发类型名称。
triggerName := map[string]string{
TriggerOrderCreated: "拍下改价",
TriggerOrderPaid: "付款发货",
TriggerBuyerReviewed: "评价赠品",
TriggerReviewMissingTimeout: "求评价",
}[task.TriggerType]
if triggerName == "" {
triggerName = task.TriggerType
}
if status == "success" {
if sent <= 0 {
return
}
// message 是成功通知正文。
message := fmt.Sprintf("✅ %s成功(订单 %s,已发送 %d 条)", triggerName, task.OrderID, sent)
notifier.NotifyAutomationRun(ctx, runID, task.AccountID, task.BuyerID, task.ItemID, status, message, task.ChatID)
return
}
// message 是失败或人工核对通知正文。
message := fmt.Sprintf("🚨 %s失败(订单 %s):%s", triggerName, task.OrderID, errMsg)
notifier.NotifyAutomationRun(ctx, runID, task.AccountID, task.BuyerID, task.ItemID, status, message, task.ChatID)
}
// notifyRunNeedsReview 通知运行需要人工核对,并复用统一结果通知格式。
func (n deliveryNotifier) notifyRunNeedsReview(ctx context.Context, run db.AutomationRun, reason string) {
if n.current() == nil {
return
}
// task 是从运行快照构造出的最小通知上下文。
task := Task{
AccountID: run.CookieID,
BuyerID: run.BuyerID,
ItemID: run.ItemID,
ChatID: run.ChatID,
OrderID: run.OrderID,
TriggerType: run.TriggerType,
}
n.notifyResult(ctx, task, run.ID, "needs_review", run.SentCount, "需要人工核对:"+reason)
}