功能:接入乐玩开放平台双渠道路由,并修复渠道锁定与 Webhook 可靠性

在保持 DLC 默认零破坏的前提下,支持 priority/仅乐玩/仅 DLC 履约策略与后台 GUI 配置;同步补齐 Prepare 渠道落库、乐玩订单取消、Webhook 验签与失败重试。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-09-11 15:48:03 +08:00
co-authored by Cursor
parent a314f3f51b
commit f7f48d0feb
22 changed files with 6489 additions and 189 deletions
+216 -20
View File
@@ -1,8 +1,10 @@
package service
import (
"encoding/json"
"errors"
"fmt"
"strings"
"affiliate_dash/internal/model"
@@ -11,16 +13,18 @@ import (
)
type ProductCatalogItem struct {
ID uint `json:"id"`
ProductID uint `json:"product_id"`
SKU string `json:"sku"`
DisplayName string `json:"display_name"`
Category string `json:"category"`
PriceAmount int64 `json:"price_amount"`
CostAmount int64 `json:"cost_amount"`
Currency string `json:"currency"`
Stock int64 `json:"stock"`
Status string `json:"status"`
ID uint `json:"id"`
ProductID uint `json:"product_id"`
SKU string `json:"sku"`
DisplayName string `json:"display_name"`
Category string `json:"category"`
PriceAmount int64 `json:"price_amount"`
CostAmount int64 `json:"cost_amount"`
Currency string `json:"currency"`
Stock int64 `json:"stock"`
Status string `json:"status"`
FulfillmentConfig string `json:"fulfillment_config,omitempty"`
Routing ProductRoutingConfig `json:"routing"`
}
// ListProductCatalog 返回自营商户的全部可售商品,作为平台默认商品目录供分配。
@@ -42,17 +46,20 @@ func (s *MerchantService) ListProductCatalog() ([]ProductCatalogItem, error) {
if p.Product != nil {
category = p.Product.Category
}
routing := ParseProductRoutingConfig(p.FulfillmentConfig, p.SKU)
items = append(items, ProductCatalogItem{
ID: p.ID,
ProductID: p.ProductID,
SKU: p.SKU,
DisplayName: p.DisplayName,
Category: category,
PriceAmount: p.PriceAmount,
CostAmount: p.CostAmount,
Currency: p.Currency,
Stock: p.Stock,
Status: p.Status,
ID: p.ID,
ProductID: p.ProductID,
SKU: p.SKU,
DisplayName: p.DisplayName,
Category: category,
PriceAmount: p.PriceAmount,
CostAmount: p.CostAmount,
Currency: p.Currency,
Stock: p.Stock,
Status: p.Status,
FulfillmentConfig: p.FulfillmentConfig,
Routing: routing,
})
}
return items, nil
@@ -260,3 +267,192 @@ func ensureProduct(tx *gorm.DB, in CreateMerchantProductInput) (*model.Product,
}
return product, nil
}
// UpdateProductRouting 更新平台目录商品的履约渠道与优先级配置
func (s *MerchantService) UpdateProductRouting(catalogID uint, routing ProductRoutingConfig, actorUserID uint) error {
switch routing.RoutingMode {
case RoutingModePriority, RoutingModeLewan, RoutingModeDLC:
default:
return errors.New("routing_mode 仅支持 priority / lewan / dlc")
}
if routing.RoutingMode == RoutingModePriority {
if len(routing.Priority) == 0 {
routing.Priority = []string{ProviderLewan, ProviderDLC}
}
for _, name := range routing.Priority {
if name != ProviderLewan && name != ProviderDLC {
return errors.New("priority 仅支持 lewan / dlc")
}
}
}
if routing.RoutingMode == RoutingModeLewan || routing.RoutingMode == RoutingModePriority {
if strings.TrimSpace(routing.LewanProductID) == "" {
return errors.New("乐玩渠道需配置 lewan_product_id")
}
}
if routing.DeliveryType != "" && routing.DeliveryType != "LINK_BINDING" && routing.DeliveryType != "EMAIL_BINDING" {
return errors.New("delivery_type 仅支持 LINK_BINDING / EMAIL_BINDING")
}
configBytes, err := json.Marshal(routing)
if err != nil {
return err
}
configStr := string(configBytes)
return s.db.Transaction(func(tx *gorm.DB) error {
var item model.MerchantProduct
if err := tx.Preload("Product").Where("id = ?", catalogID).First(&item).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("目录商品不存在")
}
return err
}
// 更新自营模板
if err := tx.Model(&model.MerchantProduct{}).Where("id = ?", item.ID).Update("fulfillment_config", configStr).Error; err != nil {
return err
}
// 同步更新所有商户同 SKU 商品的履约配置,保证全平台生效
if item.SKU != "" {
_ = tx.Model(&model.MerchantProduct{}).Where("sku = ?", item.SKU).Update("fulfillment_config", configStr).Error
}
return writeAudit(tx, &item.MerchantID, &actorUserID, nil, "catalog.routing.update", "merchant_product", fmt.Sprint(catalogID), map[string]string{
"sku": item.SKU,
"routing_mode": routing.RoutingMode,
"priority": strings.Join(routing.Priority, ","),
})
})
}
type SyncLewanProductInput struct {
LewanProductID string `json:"lewan_product_id"`
Name string `json:"name"`
Category string `json:"category"`
SKU string `json:"sku"`
Price int64 `json:"price"`
DeliveryType string `json:"delivery_type"`
}
// SyncLewanProductToCatalog 导入或关联乐玩商品至平台商品目录
func (s *MerchantService) SyncLewanProductToCatalog(in SyncLewanProductInput, actorUserID uint) (*ProductCatalogItem, error) {
if in.LewanProductID == "" {
return nil, errors.New("乐玩商品ID不能为空")
}
if in.Name == "" {
return nil, errors.New("商品名称不能为空")
}
var res *ProductCatalogItem
err := s.db.Transaction(func(tx *gorm.DB) error {
var selfMerchant model.Merchant
if err := tx.Where("code = ?", model.MerchantCodeSelfOperated).First(&selfMerchant).Error; err != nil {
return errors.New("自营商户未初始化")
}
sku := strings.TrimSpace(in.SKU)
if sku == "" {
// 自动根据乐玩 UUID 派生 SKU
cleanID := strings.ReplaceAll(in.LewanProductID, "-", "")
if len(cleanID) > 12 {
cleanID = cleanID[:12]
}
sku = "lewan_" + cleanID
}
// 检查 Product
var product model.Product
if err := tx.Where("code = ?", sku).First(&product).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
product = model.Product{
Code: sku,
Name: in.Name,
Category: in.Category,
Status: model.ProductStatusActive,
}
if err := tx.Create(&product).Error; err != nil {
return err
}
} else {
return err
}
}
routing := ProductRoutingConfig{
RoutingMode: RoutingModeLewan,
Priority: []string{ProviderLewan, ProviderDLC},
LewanProductID: in.LewanProductID,
LewanName: in.Name,
DeliveryType: in.DeliveryType,
}
if routing.DeliveryType == "" {
routing.DeliveryType = "LINK_BINDING"
}
if routing.DeliveryType != "LINK_BINDING" && routing.DeliveryType != "EMAIL_BINDING" {
return errors.New("delivery_type 仅支持 LINK_BINDING / EMAIL_BINDING")
}
if builtinDLC := deliveryGoodID("dlc", sku); builtinDLC != "" {
routing.DLCGoodID = builtinDLC
routing.RoutingMode = RoutingModePriority
}
configBytes, _ := json.Marshal(routing)
configStr := string(configBytes)
// 检查自营商户商品
var mp model.MerchantProduct
if err := tx.Where("merchant_id = ? AND sku = ?", selfMerchant.ID, sku).First(&mp).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
price := in.Price
if price <= 0 {
price = 1000
}
mp = model.MerchantProduct{
MerchantID: selfMerchant.ID,
ProductID: product.ID,
SKU: sku,
DisplayName: in.Name,
PriceAmount: price,
CostAmount: price,
Currency: "POINT",
Stock: -1,
Status: model.ProductStatusActive,
FulfillmentConfig: configStr,
}
if err := tx.Create(&mp).Error; err != nil {
return err
}
} else {
return err
}
} else {
// 更新现有配置
_ = tx.Model(&mp).Update("fulfillment_config", configStr).Error
}
res = &ProductCatalogItem{
ID: mp.ID,
ProductID: product.ID,
SKU: sku,
DisplayName: mp.DisplayName,
Category: product.Category,
PriceAmount: mp.PriceAmount,
CostAmount: mp.CostAmount,
Currency: mp.Currency,
Stock: mp.Stock,
Status: mp.Status,
FulfillmentConfig: configStr,
Routing: routing,
}
return writeAudit(tx, &selfMerchant.ID, &actorUserID, nil, "catalog.lewan.sync", "merchant_product", fmt.Sprint(mp.ID), map[string]string{
"lewan_product_id": in.LewanProductID,
"sku": sku,
})
})
if err != nil {
return nil, err
}
return res, nil
}