refactor(catalog): 废弃 LEWAN_ 镜像,统一英文 SKU 与积分价,商品图按 SKU 缓存
对外目录只保留英文主 sku;迁移硬删镜像并校准 1元=100积分;发货页/凭证优先使用 local {sku}.webp。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user