refactor(catalog): 废弃 LEWAN_ 镜像,统一英文 SKU 与积分价,商品图按 SKU 缓存

对外目录只保留英文主 sku;迁移硬删镜像并校准 1元=100积分;发货页/凭证优先使用 local {sku}.webp。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-09-14 20:00:26 +08:00
co-authored by Cursor
parent bf9614569e
commit df1a170d44
57 changed files with 1052 additions and 171 deletions
+69 -35
View File
@@ -336,7 +336,10 @@ type SyncLewanProductInput struct {
DeliveryType string `json:"delivery_type"`
}
// SyncLewanProductToCatalog 导入或关联乐玩商品至平台商品目录
// SyncLewanProductToCatalog 将乐玩商品关联到英文主 sku 目录。
// - 禁止新建 LEWAN_* 镜像 sku
// - sku 为空时按 display_name 匹配已有主商品并挂接乐玩履约
// - 新商品必须显式传入英文 sku
func (s *MerchantService) SyncLewanProductToCatalog(in SyncLewanProductInput, actorUserID uint) (*ProductCatalogItem, error) {
if in.LewanProductID == "" {
return nil, errors.New("乐玩商品ID不能为空")
@@ -353,18 +356,38 @@ func (s *MerchantService) SyncLewanProductToCatalog(in SyncLewanProductInput, ac
}
sku := strings.TrimSpace(in.SKU)
if IsLegacyLewanMirrorSKU(sku) {
return errors.New("禁止使用 LEWAN_ 前缀 sku,请填写英文主商品 sku,或留空以按中文名关联已有商品")
}
var mp model.MerchantProduct
if sku == "" {
// 自动根据乐玩 UUID 派生 SKU
cleanID := strings.ReplaceAll(in.LewanProductID, "-", "")
if len(cleanID) > 12 {
cleanID = cleanID[:12]
// 按中文名关联已有主商品(排除 LEWAN_*)
if err := tx.Where(
"merchant_id = ? AND display_name = ? AND sku NOT ILIKE ? AND status = ?",
selfMerchant.ID, in.Name, "lewan_%", model.ProductStatusActive,
).Order("id ASC").First(&mp).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("未找到同名英文主商品,请填写英文 sku 新建(如 suit_xxx / pack_xxx)")
}
return err
}
sku = "lewan_" + cleanID
sku = mp.SKU
} else {
if err := tx.Where("merchant_id = ? AND sku = ?", selfMerchant.ID, sku).First(&mp).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
mp = model.MerchantProduct{}
}
}
// 检查 Product
var product model.Product
if err := tx.Where("code = ?", sku).First(&product).Error; err != nil {
if mp.ID > 0 {
if err := tx.Where("id = ?", mp.ProductID).First(&product).Error; err != nil {
return err
}
} else if err := tx.Where("code = ?", sku).First(&product).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
product = model.Product{
Code: sku,
@@ -397,45 +420,56 @@ func (s *MerchantService) SyncLewanProductToCatalog(in SyncLewanProductInput, ac
routing.DLCGoodID = builtinDLC
routing.RoutingMode = RoutingModePriority
}
// 保留已有 dlc_good_id(若原配置有)
if mp.ID > 0 {
if existing := ParseProductRoutingConfig(mp.FulfillmentConfig, sku); existing.DLCGoodID != "" && routing.DLCGoodID == "" {
routing.DLCGoodID = existing.DLCGoodID
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 {
if mp.ID == 0 {
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 {
// 更新现有配置
_ = tx.Model(&mp).Update("fulfillment_config", configStr).Error
updates := map[string]interface{}{
"fulfillment_config": configStr,
}
if in.Price > 0 {
updates["price_amount"] = in.Price
updates["cost_amount"] = in.Price
}
if err := tx.Model(&mp).Updates(updates).Error; err != nil {
return err
}
// 同步所有商户同 SKU 的履约配置
_ = tx.Model(&model.MerchantProduct{}).Where("sku = ?", sku).Update("fulfillment_config", configStr).Error
}
res = &ProductCatalogItem{
ID: mp.ID,
ProductID: product.ID,
SKU: sku,
DisplayName: mp.DisplayName,
DisplayName: fallbackName(mp.DisplayName, in.Name),
Category: product.Category,
PriceAmount: mp.PriceAmount,
CostAmount: mp.CostAmount,
@@ -39,3 +39,10 @@ func fallbackName(value, fallback string) string {
}
return fallback
}
// IsLegacyLewanMirrorSKU 判断是否为历史乐玩镜像 sku(LEWAN_*/lewan_*)。
// 对外目录应使用英文主 sku;乐玩只作为履约配置挂接。
func IsLegacyLewanMirrorSKU(sku string) bool {
normalized := strings.ToLower(strings.TrimSpace(sku))
return strings.HasPrefix(normalized, "lewan_")
}
@@ -0,0 +1,23 @@
package service
import "testing"
func TestIsLegacyLewanMirrorSKU(t *testing.T) {
cases := []struct {
sku string
want bool
}{
{"LEWAN_8EED3610", true},
{"lewan_abc", true},
{"Lewan_x", true},
{"suit_shadow_gothic", false},
{"pack_dragon_palace", false},
{"", false},
{" LEWAN_X ", true},
}
for _, tc := range cases {
if got := IsLegacyLewanMirrorSKU(tc.sku); got != tc.want {
t.Fatalf("IsLegacyLewanMirrorSKU(%q)=%v want %v", tc.sku, got, tc.want)
}
}
}
+57 -1
View File
@@ -83,11 +83,45 @@ func (s *MerchantService) CreateMerchantProduct(merchantID uint, in CreateMercha
return merchantProduct, nil
}
type ListMerchantProductsOptions struct {
ActiveOnly bool
ExcludeLegacyLewanSKU bool
Status string // active / inactive;空表示不限(ActiveOnly 优先)
Keyword string
}
func (s *MerchantService) ListMerchantProducts(merchantID uint, page, size int, activeOnly bool) ([]model.MerchantProduct, int64, error) {
return s.ListMerchantProductsWithOptions(merchantID, page, size, ListMerchantProductsOptions{
ActiveOnly: activeOnly,
})
}
// ListMerchantProductsForClient 商户开放 API / 手工下单选品:排除 LEWAN_* 镜像,只返回英文主 sku。
func (s *MerchantService) ListMerchantProductsForClient(merchantID uint, page, size int) ([]model.MerchantProduct, int64, error) {
return s.ListMerchantProductsWithOptions(merchantID, page, size, ListMerchantProductsOptions{
ActiveOnly: true,
ExcludeLegacyLewanSKU: true,
})
}
func (s *MerchantService) ListMerchantProductsWithOptions(merchantID uint, page, size int, opts ListMerchantProductsOptions) ([]model.MerchantProduct, int64, error) {
page, size = normalizePage(page, size)
tx := s.db.Model(&model.MerchantProduct{}).Where("merchant_id = ?", merchantID)
if activeOnly {
status := strings.TrimSpace(opts.Status)
if opts.ActiveOnly {
tx = tx.Where("status = ?", model.ProductStatusActive)
} else if status != "" {
if status != model.ProductStatusActive && status != model.ProductStatusInactive {
return nil, 0, errors.New("无效的商品状态")
}
tx = tx.Where("status = ?", status)
}
if opts.ExcludeLegacyLewanSKU {
tx = tx.Where("sku NOT ILIKE ?", "lewan_%")
}
if keyword := strings.TrimSpace(opts.Keyword); keyword != "" {
like := "%" + keyword + "%"
tx = tx.Where("(sku ILIKE ? OR display_name ILIKE ?)", like, like)
}
var total int64
if err := tx.Count(&total).Error; err != nil {
@@ -98,6 +132,28 @@ func (s *MerchantService) ListMerchantProducts(merchantID uint, page, size int,
return products, total, err
}
// DeleteMerchantProduct 软删除商户商品;仅允许删除已下架商品,避免误删在售品。
func (s *MerchantService) DeleteMerchantProduct(merchantID, id uint, actorUserID uint) error {
return s.db.Transaction(func(tx *gorm.DB) error {
var item model.MerchantProduct
if err := tx.Where("id = ? AND merchant_id = ?", id, merchantID).First(&item).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("商品不存在")
}
return err
}
if item.Status != model.ProductStatusInactive {
return errors.New("请先下架后再删除")
}
if err := tx.Delete(&item).Error; err != nil {
return err
}
return writeAudit(tx, &merchantID, &actorUserID, nil, "merchant_product.delete", "merchant_product", fmt.Sprint(id), map[string]string{
"sku": item.SKU,
})
})
}
type UpdateMerchantProductInput struct {
DisplayName *string
PriceAmount *int64