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 <[email protected]>
This commit is contained in:
ChristandClaude Opus 4.8 committed 2026-06-30 21:40:15 +08:00
1 parent 5546494577
commit 57f9090dd2
29 files changed
+2469 -2382

No files matched your search

-77
View File
@@ -155,80 +155,3 @@ func TestCardCRUD(t *testing.T) {
}
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
// TestDeliveryRuleCRUD 发货规则增删。
func TestDeliveryRuleCRUD(t *testing.T) {
srv, store, cleanup := newTestServer(t)
defer cleanup()
ctx := context.Background()
// 先建一个卡券供规则引用。
store.DB.ExecContext(ctx, `INSERT INTO cards (id,name,type,text_content,enabled,user_id) VALUES (1,'卡','text','c',1,1)`)
h := srv.Router()
cookie := loginHelper(t, h)
body := `{"keyword":"VIP","card_id":1,"delivery_count":1,"enabled":true}`
req := httptest.NewRequest(http.MethodPost, "/delivery-rules", strings.NewReader(body))
req.AddCookie(cookie)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("create status=%d body=%s", rec.Code, rec.Body.String())
}
// 列表。
req2 := httptest.NewRequest(http.MethodGet, "/delivery-rules", nil)
req2.AddCookie(cookie)
rec2 := httptest.NewRecorder()
h.ServeHTTP(rec2, req2)
var arr []map[string]any
json.Unmarshal(rec2.Body.Bytes(), &arr)
if len(arr) != 1 || arr[0]["keyword"] != "VIP" {
t.Fatalf("规则列表异常: %+v", arr)
}
}
func TestDeliveryRuleVariantCRUD(t *testing.T) {
srv, store, cleanup := newTestServer(t)
defer cleanup()
ctx := context.Background()
admin, _ := store.Users.GetByUsername(ctx, "admin")
_, _ = store.DB.ExecContext(ctx, `INSERT INTO cards (id,name,type,text_content,enabled,user_id) VALUES
(31,'30天库存','text','A',1,?),(32,'90天库存','text','B',1,?)`, admin.ID, admin.ID)
_, _ = store.DB.ExecContext(ctx, `INSERT INTO item_info (cookie_id,item_id,item_title,is_multi_spec) VALUES
('acc1','item-vip','视频会员',1)`)
h := srv.Router()
cookie := loginHelper(t, h)
body := `{"keyword":"视频会员","cookie_id":"acc1","item_id":"item-vip","enabled":true,"description":"会员发货","variants":[{"spec_name":"套餐","spec_value":"30天","card_id":31,"delivery_count":1,"enabled":true},{"spec_name":"套餐","spec_value":"90天","card_id":32,"delivery_count":2,"enabled":true}]}`
req := httptest.NewRequest(http.MethodPost, "/delivery-rules", strings.NewReader(body))
req.AddCookie(cookie)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("create status=%d body=%s", rec.Code, rec.Body.String())
}
listReq := httptest.NewRequest(http.MethodGet, "/delivery-rules", nil)
listReq.AddCookie(cookie)
listRec := httptest.NewRecorder()
h.ServeHTTP(listRec, listReq)
var rules []struct {
CookieID string `json:"cookie_id"`
ItemID string `json:"item_id"`
Variants []struct {
SpecValue string `json:"spec_value"`
CardID int64 `json:"card_id"`
DeliveryCount int `json:"delivery_count"`
} `json:"variants"`
}
if err := json.Unmarshal(listRec.Body.Bytes(), &rules); err != nil {
t.Fatal(err)
}
if len(rules) != 1 || rules[0].CookieID != "acc1" || rules[0].ItemID != "item-vip" || len(rules[0].Variants) != 2 {
t.Fatalf("规则返回异常: %+v body=%s", rules, listRec.Body.String())
}
if rules[0].Variants[1].SpecValue != "90天" || rules[0].Variants[1].CardID != 32 || rules[0].Variants[1].DeliveryCount != 2 {
t.Fatalf("规格返回异常: %+v", rules[0].Variants)
}
}