Files
Ydisks-Xianyu-Helper/internal/server/automation_handlers.go
T
ChristandClaude Opus 4.8 57f9090dd2 refactor: automation 中心落地 + 清理 delivery_rules/SystemMessage 死代码
把卡密自动发货从 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>
2026-06-30 21:40:15 +08:00

242 lines
8.3 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
"xianyu-go/internal/auth"
"xianyu-go/internal/automation"
"xianyu-go/internal/db"
)
func (s *Server) mountAutomation(r chi.Router) {
r.Get("/automation-rules", s.listAutomationRules)
r.Post("/automation-rules", s.createAutomationRule)
r.Put("/automation-rules/{rule_id}", s.updateAutomationRule)
r.Delete("/automation-rules/{rule_id}", s.deleteAutomationRule)
}
type automationActionRequest struct {
ActionType string `json:"action_type"`
CardID int64 `json:"card_id"`
DeliveryCount int `json:"delivery_count"`
MessageTemplate string `json:"message_template"`
DelaySeconds int `json:"delay_seconds"`
ConfigJSON string `json:"config_json"`
Enabled *bool `json:"enabled"`
SortOrder int `json:"sort_order"`
}
type automationRuleRequest struct {
CookieID string `json:"cookie_id"`
ItemID string `json:"item_id"`
Name string `json:"name"`
TriggerType string `json:"trigger_type"`
Enabled bool `json:"enabled"`
Priority int `json:"priority"`
ConfigJSON string `json:"config_json"`
Actions []automationActionRequest `json:"actions"`
}
func (s *Server) listAutomationRules(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
rules, err := s.Store.Automation.ListForUser(r.Context(), sess.UserID)
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询自动化规则失败")
return
}
out := make([]map[string]any, 0, len(rules))
for _, rule := range rules {
actions := make([]map[string]any, 0, len(rule.Actions))
for _, action := range rule.Actions {
actions = append(actions, map[string]any{
"id": action.ID, "action_type": action.ActionType, "card_id": action.CardID,
"card_name": action.CardName, "delivery_count": action.DeliveryCount,
"message_template": action.MessageTemplate, "delay_seconds": action.DelaySeconds,
"config_json": action.ConfigJSON, "enabled": action.Enabled, "sort_order": action.SortOrder,
})
}
out = append(out, map[string]any{
"id": rule.ID, "cookie_id": rule.CookieID, "item_id": rule.ItemID, "item_title": rule.ItemTitle,
"name": rule.Name, "trigger_type": rule.TriggerType, "enabled": rule.Enabled,
"priority": rule.Priority, "config_json": rule.ConfigJSON, "actions": actions,
"created_at": rule.CreatedAt, "updated_at": rule.UpdatedAt,
})
}
writeJSON(w, http.StatusOK, out)
}
func (s *Server) createAutomationRule(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
var req automationRuleRequest
if err := decodeJSON(r, &req); err != nil {
writeErr(w, http.StatusBadRequest, "请求格式错误")
return
}
in, err := s.normalizeAutomationRuleRequest(r, sess.UserID, req)
if err != nil {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
id, err := s.Store.Automation.Create(r.Context(), in)
if err != nil {
writeErr(w, http.StatusInternalServerError, "创建自动化规则失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
}
func (s *Server) updateAutomationRule(w http.ResponseWriter, r *http.Request) {
ruleID, err := strconv.ParseInt(chi.URLParam(r, "rule_id"), 10, 64)
if err != nil {
writeErr(w, http.StatusBadRequest, "无效规则ID")
return
}
sess := auth.SessionFromContext(r.Context())
var req automationRuleRequest
if err := decodeJSON(r, &req); err != nil {
writeErr(w, http.StatusBadRequest, "请求格式错误")
return
}
in, err := s.normalizeAutomationRuleRequest(r, sess.UserID, req)
if err != nil {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
if err := s.Store.Automation.Update(r.Context(), sess.UserID, ruleID, in); err != nil {
if err == db.ErrNotFound {
writeErr(w, http.StatusNotFound, "自动化规则不存在")
return
}
writeErr(w, http.StatusInternalServerError, "更新自动化规则失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func (s *Server) deleteAutomationRule(w http.ResponseWriter, r *http.Request) {
ruleID, err := strconv.ParseInt(chi.URLParam(r, "rule_id"), 10, 64)
if err != nil {
writeErr(w, http.StatusBadRequest, "无效规则ID")
return
}
sess := auth.SessionFromContext(r.Context())
if err := s.Store.Automation.Delete(r.Context(), sess.UserID, ruleID); err != nil {
if err == db.ErrNotFound {
writeErr(w, http.StatusNotFound, "自动化规则不存在")
return
}
writeErr(w, http.StatusInternalServerError, "删除自动化规则失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func (s *Server) normalizeAutomationRuleRequest(r *http.Request, userID int64, req automationRuleRequest) (db.AutomationRuleInput, error) {
req.CookieID = strings.TrimSpace(req.CookieID)
req.ItemID = strings.TrimSpace(req.ItemID)
req.Name = strings.TrimSpace(req.Name)
req.TriggerType = strings.TrimSpace(req.TriggerType)
switch req.TriggerType {
case automation.TriggerOrderPaid, automation.TriggerBuyerReviewed, automation.TriggerReviewMissingTimeout:
default:
return db.AutomationRuleInput{}, errStr("不支持的触发类型")
}
if req.CookieID == "" || !s.cookieOwnedByUser(r.Context(), userID, req.CookieID) {
return db.AutomationRuleInput{}, errStr("账号不存在或不属于当前用户")
}
if req.ItemID != "" && !s.itemOwnedByUser(r, userID, req.CookieID, req.ItemID) {
return db.AutomationRuleInput{}, errStr("商品不属于当前用户")
}
if req.Priority <= 0 {
req.Priority = 100
}
config := req.ConfigJSON
if config == "" {
config = "{}"
}
if !isJSONObject(config) {
return db.AutomationRuleInput{}, errStr("规则配置必须是 JSON 对象")
}
if len(req.Actions) == 0 {
return db.AutomationRuleInput{}, errStr("至少需要一个自动化动作")
}
if req.Name == "" {
req.Name = defaultAutomationRuleName(req.TriggerType, req.ItemID)
}
actions := make([]db.AutomationActionInput, 0, len(req.Actions))
for i, act := range req.Actions {
enabled := true
if act.Enabled != nil {
enabled = *act.Enabled
}
act.ActionType = strings.TrimSpace(act.ActionType)
switch act.ActionType {
case automation.ActionConfirmShipment:
case automation.ActionSendCard:
if act.CardID <= 0 {
return db.AutomationRuleInput{}, errStr("发送卡密动作必须选择卡密组")
}
if !s.cardOwnedByUser(r.Context(), userID, act.CardID) {
return db.AutomationRuleInput{}, errStr("卡密组不存在或不属于当前用户")
}
case automation.ActionSendText:
if strings.TrimSpace(act.MessageTemplate) == "" {
return db.AutomationRuleInput{}, errStr("发送文本动作必须填写文案")
}
default:
return db.AutomationRuleInput{}, errStr("不支持的动作类型")
}
if act.DeliveryCount <= 0 {
act.DeliveryCount = 1
}
if act.ConfigJSON == "" {
act.ConfigJSON = "{}"
}
if !isJSONObject(act.ConfigJSON) {
return db.AutomationRuleInput{}, errStr("动作配置必须是 JSON 对象")
}
actions = append(actions, db.AutomationActionInput{
ActionType: act.ActionType, CardID: act.CardID, DeliveryCount: act.DeliveryCount,
MessageTemplate: act.MessageTemplate, DelaySeconds: act.DelaySeconds, ConfigJSON: act.ConfigJSON,
Enabled: enabled, SortOrder: firstNonZero(act.SortOrder, i+1),
})
}
return db.AutomationRuleInput{
UserID: userID, CookieID: req.CookieID, ItemID: req.ItemID, Name: req.Name,
TriggerType: req.TriggerType, Enabled: req.Enabled, Priority: req.Priority,
ConfigJSON: config, Actions: actions,
}, nil
}
func defaultAutomationRuleName(triggerType, itemID string) string {
name := map[string]string{
automation.TriggerOrderPaid: "付款后自动发货",
automation.TriggerBuyerReviewed: "评价后发送赠品",
automation.TriggerReviewMissingTimeout: "超时未评价求评价",
}[triggerType]
if name == "" {
name = "自动化规则"
}
if strings.TrimSpace(itemID) != "" {
return name + " - " + strings.TrimSpace(itemID)
}
return name
}
func isJSONObject(s string) bool {
var m map[string]any
return json.Unmarshal([]byte(s), &m) == nil
}
func firstNonZero(v, fallback int) int {
if v != 0 {
return v
}
return fallback
}