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
@@ -0,0 +1,166 @@
-- ============================================================================
-- 商品目录收敛(幂等,可在开发库反复执行前先清 schema_migrations.011)
-- 1) 补齐乐玩独有品英文主 sku
-- 2) 硬删除全部 LEWAN_* / lewan_* 镜像(含已软删行)
-- 3) 统一积分价为 1元=100积分(按权威目录绝对值校准)
-- ============================================================================
-- 1) 乐玩独有 → 英文主商品
WITH lewan_only(new_sku, display_name, category, legacy_sku, default_price) AS (
VALUES
('pack_dragon_palace', '龙宫之主大礼包', '礼包', 'LEWAN_46524A53', 80000),
('suit_dune_song', '套装-沙丘之歌', '套装', 'LEWAN_F3B83B85', 10200),
('suit_jester_face', '套装-花面小丑', '套装', 'LEWAN_B3891176', 20000)
)
INSERT INTO products (code, name, category, status)
SELECT v.new_sku, v.display_name, v.category, 'active'
FROM lewan_only v
ON CONFLICT (code) DO NOTHING;
WITH lewan_only(new_sku, display_name, category, legacy_sku, default_price) AS (
VALUES
('pack_dragon_palace', '龙宫之主大礼包', '礼包', 'LEWAN_46524A53', 80000),
('suit_dune_song', '套装-沙丘之歌', '套装', 'LEWAN_F3B83B85', 10200),
('suit_jester_face', '套装-花面小丑', '套装', 'LEWAN_B3891176', 20000)
),
self_merchant AS (
SELECT id FROM merchants WHERE code = 'self-operated' LIMIT 1
),
legacy AS (
SELECT
v.new_sku,
v.display_name,
v.default_price,
mp.price_amount,
mp.cost_amount,
mp.fulfillment_config,
p.id AS product_id
FROM lewan_only v
JOIN products p ON p.code = v.new_sku
LEFT JOIN merchant_products mp
ON mp.sku = v.legacy_sku
AND mp.merchant_id = (SELECT id FROM self_merchant)
)
INSERT INTO merchant_products (
merchant_id, product_id, sku, display_name,
price_amount, cost_amount, currency, stock, status, fulfillment_config
)
SELECT
sm.id,
l.product_id,
l.new_sku,
l.display_name,
COALESCE(NULLIF(l.price_amount, 0), l.default_price),
COALESCE(NULLIF(l.cost_amount, 0), l.default_price),
'POINT',
-1,
'active',
COALESCE(
NULLIF(l.fulfillment_config, ''),
CASE l.new_sku
WHEN 'pack_dragon_palace' THEN
'{"routing_mode":"lewan","priority":["lewan","dlc"],"dlc_good_id":"","lewan_product_id":"46524a53-068d-4bee-97ff-d5e7873818e0","lewan_name":"龙宫之主大礼包","delivery_type":"LINK_BINDING"}'
WHEN 'suit_dune_song' THEN
'{"routing_mode":"lewan","priority":["lewan","dlc"],"dlc_good_id":"","lewan_product_id":"f3b83b85-c664-4440-94ac-a4b0cd7cf0f3","lewan_name":"套装-沙丘之歌","delivery_type":"LINK_BINDING"}'
WHEN 'suit_jester_face' THEN
'{"routing_mode":"lewan","priority":["lewan","dlc"],"dlc_good_id":"","lewan_product_id":"b3891176-5ffa-40cb-8c58-7e46eac3f381","lewan_name":"套装-花面小丑","delivery_type":"LINK_BINDING"}'
END
)
FROM legacy l
CROSS JOIN self_merchant sm
ON CONFLICT (merchant_id, sku) DO UPDATE
SET
display_name = EXCLUDED.display_name,
price_amount = EXCLUDED.price_amount,
cost_amount = EXCLUDED.cost_amount,
fulfillment_config = COALESCE(NULLIF(merchant_products.fulfillment_config, ''), EXCLUDED.fulfillment_config),
status = 'active',
deleted_at = NULL;
-- 已持有对应 LEWAN_* 的其它商户同步英文 sku
WITH mapping(new_sku, legacy_sku) AS (
VALUES
('pack_dragon_palace', 'LEWAN_46524A53'),
('suit_dune_song', 'LEWAN_F3B83B85'),
('suit_jester_face', 'LEWAN_B3891176')
),
template AS (
SELECT mp.*
FROM merchant_products mp
JOIN merchants m ON m.id = mp.merchant_id AND m.code = 'self-operated'
JOIN mapping map ON map.new_sku = mp.sku
WHERE mp.deleted_at IS NULL
)
INSERT INTO merchant_products (
merchant_id, product_id, sku, display_name,
price_amount, cost_amount, currency, stock, status, fulfillment_config
)
SELECT
legacy.merchant_id,
t.product_id,
t.sku,
t.display_name,
COALESCE(NULLIF(legacy.price_amount, 0), t.price_amount),
COALESCE(NULLIF(legacy.cost_amount, 0), t.cost_amount),
'POINT',
-1,
'active',
COALESCE(NULLIF(t.fulfillment_config, ''), legacy.fulfillment_config)
FROM merchant_products legacy
JOIN mapping map ON map.legacy_sku = legacy.sku
JOIN template t ON t.sku = map.new_sku
JOIN merchants m ON m.id = legacy.merchant_id AND m.code <> 'self-operated'
ON CONFLICT (merchant_id, sku) DO NOTHING;
-- 2) 硬删除全部 LEWAN_* 镜像(含软删)
DELETE FROM merchant_products WHERE sku ILIKE 'lewan_%';
DELETE FROM products WHERE code ILIKE 'lewan_%';
-- 3) 权威价格校准:1元=100积分(docs/商品目录.md 2026-09-14)
WITH prices(sku, amount) AS (
VALUES
('suit_alan_walker', 3600),
('suit_shadow_gothic', 8880),
('top_black_elite_trainer', 3600),
('m416_hamster_gray', 16800),
('bag_cute_bear', 28800),
('suit_dual_fluffy', 11240),
('suit_pink_sheep', 18800),
('suit_first_peach', 9880),
('suit_romantic_destiny', 3600),
('pack_western_cowboy', 32000),
('smoke_pink_sheep', 4880),
('frag_pink_sheep', 4880),
('suit_hamster_gray', 12800),
('suit_cute_bear', 16800),
('bag_pink_sheep', 16800),
('helmet_pink_sheep', 16800),
('bag_hamster_gray', 16800),
('helmet_hamster_gray', 16800),
('suit_western_mystery', 11500),
('helmet_panda_treasure', 23400),
('suit_panda_round', 23400),
('suit_panda_tuan', 23400),
('pack_lava_ranger', 9600),
('suit_sand_dancer', 8400),
('pack_star_roam_outfit', 83600),
('pack_star_roam_weapon', 51600),
('suit_cloud_bear', 13480),
('honor_medal_x2', 200),
('honor_medal_x30', 3000),
('honor_medal_x90', 9000),
('lucky_coin_x2', 200),
('lucky_coin_x30', 3000),
('lucky_coin_x90', 9000),
('pack_dragon_palace', 80000),
('suit_dune_song', 10200),
('suit_jester_face', 20000)
)
UPDATE merchant_products mp
SET
price_amount = p.amount,
cost_amount = p.amount,
updated_at = NOW()
FROM prices p
WHERE mp.sku = p.sku
AND mp.deleted_at IS NULL;
+51 -36
View File
@@ -72,7 +72,8 @@ func (h *DeliveryHandler) Submit(c *gin.Context) {
response.OK(c, result)
}
// ImageProxy 代理图片并支持本地持久化缓存,杜绝跨域污染和远端加载失败。
// ImageProxy 代理外链图片并落盘缓存(仅无本地 SKU 图时的兜底)。
// 正式商品图请使用 frontend/public/images/products/{sku}.webp(go run ./cmd/sync_product_images)。
func (h *DeliveryHandler) ImageProxy(c *gin.Context) {
rawURL := strings.TrimSpace(c.Query("url"))
if rawURL == "" || (!strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://")) {
@@ -80,33 +81,57 @@ func (h *DeliveryHandler) ImageProxy(c *gin.Context) {
return
}
// 1. 优先检查本地官方商品图片缓存
for _, base := range []string{"data/uploads/products", "../data/uploads/products", "uploads/products"} {
if strings.Contains(rawURL, "/products/") {
parts := strings.Split(rawURL, "/products/")
if len(parts) > 1 {
sub := strings.Split(parts[1], "/")
if len(sub) > 0 && sub[0] != "" {
localPath := filepath.Join(base, sub[0]+".webp")
if info, err := os.Stat(localPath); err == nil && info.Size() > 0 {
c.Header("Cache-Control", "public, max-age=2592000, immutable")
c.File(localPath)
return
}
}
sku := strings.TrimSpace(c.Query("sku"))
publicDirs := []string{
"../frontend/public/images/products",
"frontend/public/images/products",
"public/images/products",
}
uploadDirs := []string{"data/uploads/products", "../data/uploads/products", "uploads/products"}
tryLocal := func(dirs []string, name string) bool {
if name == "" {
return false
}
for _, base := range dirs {
localPath := filepath.Join(base, name+".webp")
if info, err := os.Stat(localPath); err == nil && info.Size() > 0 {
c.Header("Cache-Control", "public, max-age=2592000, immutable")
c.File(localPath)
return true
}
}
// 按 URL SHA256 哈希查本地缓存
hash := sha256.Sum256([]byte(rawURL))
hashPath := filepath.Join(base, hex.EncodeToString(hash[:])+".webp")
if info, err := os.Stat(hashPath); err == nil && info.Size() > 0 {
c.Header("Cache-Control", "public, max-age=2592000, immutable")
c.File(hashPath)
return
}
return false
}
// 2. 本地无缓存时请求远端
// 1. 优先 SKU 命名的静态缓存
if tryLocal(publicDirs, sku) {
return
}
// 2. 历史:URL 中的乐玩 UUID → 先查 public,再查 uploads
var uuidName string
if strings.Contains(rawURL, "/products/") {
parts := strings.Split(rawURL, "/products/")
if len(parts) > 1 {
sub := strings.Split(parts[1], "/")
if len(sub) > 0 && sub[0] != "" {
uuidName = sub[0]
}
}
}
if tryLocal(publicDirs, uuidName) || tryLocal(uploadDirs, uuidName) {
return
}
// 3. URL hash 缓存(uploads 运行时兜底)
hash := sha256.Sum256([]byte(rawURL))
hashName := hex.EncodeToString(hash[:])
if tryLocal(uploadDirs, hashName) {
return
}
// 4. 本地无缓存时请求远端
req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodGet, rawURL, nil)
if err != nil {
response.BadRequest(c, "构建请求失败")
@@ -131,20 +156,10 @@ func (h *DeliveryHandler) ImageProxy(c *gin.Context) {
return
}
// 3. 异步/同步回写到本地磁盘缓存
// 5. 仅将未知外链写入 uploads 哈希缓存(不再按 UUID 污染正式目录)
cacheDir := "data/uploads/products"
_ = os.MkdirAll(cacheDir, 0755)
if strings.Contains(rawURL, "/products/") {
parts := strings.Split(rawURL, "/products/")
if len(parts) > 1 {
sub := strings.Split(parts[1], "/")
if len(sub) > 0 && sub[0] != "" {
_ = os.WriteFile(filepath.Join(cacheDir, sub[0]+".webp"), data, 0644)
}
}
}
hash := sha256.Sum256([]byte(rawURL))
_ = os.WriteFile(filepath.Join(cacheDir, hex.EncodeToString(hash[:])+".webp"), data, 0644)
_ = os.WriteFile(filepath.Join(cacheDir, hashName+".webp"), data, 0644)
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
+20 -2
View File
@@ -56,8 +56,16 @@ func mapKeys(values map[string]struct{}) []string {
func (h *MerchantHandler) ListProducts(c *gin.Context) {
page, size := pageParams(c)
list, total, err := h.merchantSvc.ListMerchantProducts(middleware.GetMerchantID(c), page, size, false)
list, total, err := h.merchantSvc.ListMerchantProductsWithOptions(middleware.GetMerchantID(c), page, size, service.ListMerchantProductsOptions{
Status: strings.TrimSpace(c.Query("status")),
Keyword: strings.TrimSpace(c.Query("keyword")),
ExcludeLegacyLewanSKU: true, // LEWAN_* 镜像已废弃,列表永不返回
})
if err != nil {
if err.Error() == "无效的商品状态" {
response.BadRequest(c, err.Error())
return
}
response.ServerError(c, err.Error())
return
}
@@ -86,6 +94,16 @@ func (h *MerchantHandler) UpdateProductStatus(c *gin.Context) {
response.OK(c, nil)
}
// DeleteProduct DELETE /api/merchant/products/:id — 仅已下架可删(软删除)。
func (h *MerchantHandler) DeleteProduct(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
if err := h.merchantSvc.DeleteMerchantProduct(middleware.GetMerchantID(c), uint(id), middleware.GetUserID(c)); err != nil {
response.BadRequest(c, err.Error())
return
}
response.OK(c, nil)
}
func (h *MerchantHandler) ListOrders(c *gin.Context) {
page, size := pageParams(c)
orderSource := strings.TrimSpace(c.Query("order_source"))
@@ -103,7 +121,7 @@ func (h *MerchantHandler) ListOrders(c *gin.Context) {
// ListManualOrderProducts returns active products for order creation without granting product-management access.
func (h *MerchantHandler) ListManualOrderProducts(c *gin.Context) {
list, _, err := h.merchantSvc.ListMerchantProducts(middleware.GetMerchantID(c), 1, 100, true)
list, _, err := h.merchantSvc.ListMerchantProductsForClient(middleware.GetMerchantID(c), 1, 100)
if err != nil {
response.ServerError(c, err.Error())
return
+7 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"strconv"
"strings"
"affiliate_dash/internal/middleware"
"affiliate_dash/internal/model"
@@ -30,7 +31,7 @@ func (h *OpenV1Handler) ListProducts(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
openlog.Info(c, "list_products start page=%d size=%d", page, size)
list, total, err := h.merchantSvc.ListMerchantProducts(middleware.GetMerchantID(c), page, size, true)
list, total, err := h.merchantSvc.ListMerchantProductsForClient(middleware.GetMerchantID(c), page, size)
if err != nil {
openlog.Warn(c, "list_products fail err=%v", err)
response.ServerError(c, err.Error())
@@ -71,6 +72,11 @@ func (h *OpenV1Handler) CreateOrder(c *gin.Context) {
}
data = decoded
}
if service.IsLegacyLewanMirrorSKU(req.SKU) {
openlog.Warn(c, "create_order reject legacy lewan sku=%s", strings.TrimSpace(req.SKU))
response.BadRequest(c, "请使用英文主商品 sku 下单,勿使用 LEWAN_ 镜像 sku")
return
}
client := middleware.GetAPIClient(c)
openlog.Info(c, "create_order start client_order_no=%s sku=%s quantity=%d buyer=%s",
req.ClientOrderNo, req.SKU, req.Quantity, req.BuyerReference)
+1
View File
@@ -166,6 +166,7 @@ func Setup(h *Handlers) *gin.Engine {
merchant.GET("", h.Merchant.Current)
merchant.GET("/products", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureProducts), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionProductsManage), h.Merchant.ListProducts)
merchant.PATCH("/products/:id/status", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureProducts), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionProductsManage), h.Merchant.UpdateProductStatus)
merchant.DELETE("/products/:id", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureProducts), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionProductsManage), h.Merchant.DeleteProduct)
merchant.GET("/orders", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureOrders), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionOrdersManage), h.Merchant.ListOrders)
merchant.GET("/orders/products", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureOrders), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionOrdersManage), h.Merchant.ListManualOrderProducts)
merchant.POST("/orders/manual", middleware.RequireMerchantFeature(h.OpenDB, model.MerchantFeatureOrders), middleware.RequireMerchantPermissions(h.Tenant, model.PermissionOrdersManage), h.Merchant.CreateManualOrder)
+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