一、多渠道商品管理的核心痛点

在深入方案之前,先梳理一下多平台卖家在商品管理上最常遇到的问题:

┌─────────────────────────────────────────────────────────────────────┐
│                  多渠道商品管理四大痛点                                │
├──────────────┬──────────────────────────────────────────────────────┤
│  SKU混乱      │ 同一商品在不同平台编码不同,无法统一管理               │
├──────────────┼──────────────────────────────────────────────────────┤
│  属性映射错    │ 颜色尺码等属性在各平台定义不同,数据对不上             │
├──────────────┼──────────────────────────────────────────────────────┤
│  库存扣错      │ 商品对应关系错误导致A商品卖出扣了B商品库存             │
├──────────────┼──────────────────────────────────────────────────────┤
│  批量操作难    │ 上千个SKU要逐个配置对应关系,效率极低                 │
└──────────────┴──────────────────────────────────────────────────────┘

这些问题的根源在于:各电商平台的商品模型不同、属性体系不同、编码规范不同。淘宝用num_iid和sku_id,京东用ware_id和sku_id,拼多多用goods_id和sku_id。同一个商品在不同平台的SKU编码可能完全不同,如果没有统一的映射机制,就无法实现库存同步、订单处理、利润核算等核心功能。


二、商品管理系统整体架构

┌────────────────────────────────────────────────────────────────────────┐
│                      商品管理系统架构                                    │
│                                                                        │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐              │
│  │ 淘 宝    │  │ 京 东    │  │ 拼多多   │  │ 抖 音    │  ...         │
│  │ 商品API  │  │ 商品API  │  │ 商品API  │  │ 商品API  │              │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘  └────┬─────┘              │
│       │              │              │              │                    │
│  ┌────┴──────────────┴──────────────┴──────────────┴───────┐          │
│  │              平台适配层(Adapter)                        │          │
│  │  · 商品模型转换    · 属性映射    · 协议适配               │          │
│  └────────────────────────┬────────────────────────────────┘          │
│                           │                                            │
│  ┌────────────────────────┴────────────────────────────────┐          │
│  │              商品映射引擎                                 │          │
│  │  · 自动匹配    · 手工匹配    · 批量导入                  │          │
│  │  · 属性同步    · 套餐生成    · 冲突检测                  │          │
│  └────────────────────────┬────────────────────────────────┘          │
│                           │                                            │
│  ┌────────────────────────┴────────────────────────────────┐          │
│  │              商品数据层                                   │          │
│  │  · 本地商品档案    · 平台商品映射    · 属性字典           │          │
│  │  · SKU编码体系     · 套餐组合关系    · 往来单位编码       │          │
│  └─────────────────────────────────────────────────────────┘          │
└────────────────────────────────────────────────────────────────────────┘

整个系统分为三层:

平台适配层负责屏蔽各平台API的差异。每个平台有独立的适配器,将平台特有的商品模型转换为系统内部的统一模型。

商品映射引擎是核心业务层。它负责建立和维护本地商品与平台商品的对应关系,支持自动匹配、手工匹配、批量导入等多种方式。

商品数据层维护本地商品档案、映射关系、属性字典等核心数据。


三、统一商品模型设计

各平台的商品模型差异是管理混乱的根源。设计一个合理的统一模型是第一步。

3.1 平台差异分析

┌──────────┬────────────┬────────────┬────────────┬────────────┐
│  平台     │  商品ID    │  SKU ID    │  属性体系   │  编码规范   │
├──────────┼────────────┼────────────┼────────────┼────────────┤
│  淘宝     │  num_iid   │  sku_id    │  自定义属性 │  商家编码   │
│  京东     │  ware_id   │  sku_id    │  标准+自定义│  商品编号   │
│  拼多多   │  goods_id  │  sku_id    │  规格模板   │  外部编码   │
│  抖音     │  product_id│  sku_id    │  规格模板   │  商家编码   │
│  快手     │  item_id   │  sku_id    │  自定义属性 │  商品编码   │
└──────────┴────────────┴────────────┴────────────┴────────────┘

3.2 统一模型定义

/**
 * 统一商品模型
 * 将各平台差异化的商品模型统一为标准结构
 */
@Data
@Builder
public class UnifiedProductModel {
    
    /** 系统内部商品ID */
    private Long systemProductId;
    
    /** 系统内部SKU编码(全局唯一) */
    private String systemSkuCode;
    
    /** 商品名称 */
    private String productName;
    
    /** 商品编号(商家自定义) */
    private String productCode;
    
    /** 条码(EAN/UPC) */
    private String barcode;
    
    /** 品牌 */
    private String brand;
    
    /** 分类 */
    private String category;
    
    /** 规格属性(如:白色/M码) */
    private String specification;
    
    /** 计量单位 */
    private String unit;
    
    /** 成本价 */
    private BigDecimal costPrice;
    
    /** 销售价 */
    private BigDecimal salePrice;
    
    /** 商品状态(启用/停用) */
    private Integer status;
    
    /** 创建时间 */
    private LocalDateTime createTime;
    
    /** 更新时间 */
    private LocalDateTime updateTime;
}

/**
 * 平台商品映射模型
 * 维护本地商品与平台商品的对应关系
 */
@Data
@Builder
public class PlatformProductMapping {
    
    /** 映射ID */
    private Long id;
    
    /** 系统商品ID */
    private Long systemProductId;
    
    /** 系统SKU编码 */
    private String systemSkuCode;
    
    /** 平台编码(如 TAOBAO, JD, PDD) */
    private String platformCode;
    
    /** 店铺ID */
    private String shopId;
    
    /** 平台商品ID */
    private String platformProductId;
    
    /** 平台SKU ID */
    private String platformSkuId;
    
    /** 平台商品标题 */
    private String platformTitle;
    
    /** 平台商品链接 */
    private String platformUrl;
    
    /** 平台SKU属性(如:白色/M码) */
    private String platformSkuAttrs;
    
    /** 映射状态(已绑定/未绑定/已失效) */
    private Integer mappingStatus;
    
    /** 最后同步时间 */
    private LocalDateTime lastSyncTime;
    
    /** 创建时间 */
    private LocalDateTime createTime;
}

3.3 适配器模式实现

每个平台实现一个适配器,负责商品数据的双向转换:

/**
 * 平台商品适配器接口
 */
public interface PlatformProductAdapter {
    
    /** 获取支持的平台编码 */
    String getPlatformCode();
    
    /** 拉取平台商品列表 */
    List<PlatformProductModel> pullProducts(String shopId, int pageNo, int pageSize);
    
    /** 将平台商品转换为统一模型 */
    UnifiedProductModel fromPlatform(PlatformProductModel platformProduct);
    
    /** 将统一模型转换为平台格式 */
    PlatformProductModel toPlatform(UnifiedProductModel model);
    
    /** 推送商品到平台 */
    SyncResult pushProduct(PlatformProductModel product);
}

/**
 * 淘宝商品适配器实现
 */
@Component
public class TaobaoProductAdapter implements PlatformProductAdapter {
    
    @Override
    public String getPlatformCode() {
        return "TAOBAO";
    }
    
    @Override
    public List<PlatformProductModel> pullProducts(String shopId, int pageNo, int pageSize) {
        // 调用淘宝API拉取商品列表
        return taobaoApiClient.getProducts(shopId, pageNo, pageSize);
    }
    
    @Override
    public UnifiedProductModel fromPlatform(PlatformProductModel data) {
        return UnifiedProductModel.builder()
            .productName(data.getTitle())
            .productCode(data.getOuterId())           // 商家编码
            .barcode(data.getBarcode())
            .specification(data.getSkuProperties())   // SKU属性
            .salePrice(data.getPrice())
            .build();
    }
    
    @Override
    public PlatformProductModel toPlatform(UnifiedProductModel model) {
        PlatformProductModel product = new PlatformProductModel();
        product.setTitle(model.getProductName());
        product.setOuterId(model.getProductCode());
        product.setBarcode(model.getBarcode());
        product.setPrice(model.getSalePrice());
        return product;
    }
}

四、商品映射策略

商品映射是多渠道管理的核心。系统支持三种映射方式,覆盖不同场景。

4.1 映射策略总览

┌─────────────────────────────────────────────────────────────────────┐
│                    三种商品映射策略                                    │
├──────────────┬──────────────────────────────────────────────────────┤
│  自动匹配     │ 基于编码/条码自动建立对应关系,适合标准化商品          │
├──────────────┼──────────────────────────────────────────────────────┤
│  手工匹配     │ 人工选择绑定,适合非标品或编码不规范的商品             │
├──────────────┼──────────────────────────────────────────────────────┤
│  批量导入     │ 通过Excel模板批量建立映射,适合大批量商品初始化        │
└──────────────┴──────────────────────────────────────────────────────┘

4.2 自动匹配实现

自动匹配的前提是:本地商品和平台商品都设置了编码(商家编码、条码等),且编码一致。

/**
 * 自动匹配服务
 */
@Service
@Slf4j
public class AutoMatchService {
    
    @Autowired
    private ProductService productService;
    
    @Autowired
    private PlatformProductMappingService mappingService;
    
    /**
     * 自动匹配指定店铺的平台商品
     */
    public AutoMatchResult autoMatch(String shopId, String platformCode) {
        log.info("开始自动匹配: shop={}, platform={}", shopId, platformCode);
        
        // 1. 拉取平台商品
        List<PlatformProductModel> platformProducts = platformAdapterMap
            .get(platformCode + "Adapter")
            .pullProducts(shopId, 1, Integer.MAX_VALUE);
        
        // 2. 获取本地所有商品
        List<UnifiedProductModel> localProducts = productService.getAll();
        
        // 构建本地商品索引(按编码)
        Map<String, UnifiedProductModel> localIndex = buildLocalIndex(localProducts);
        
        int matched = 0;
        int failed = 0;
        List<MatchConflict> conflicts = new ArrayList<>();
        
        // 3. 逐个匹配
        for (PlatformProductModel platformProduct : platformProducts) {
            String outerId = platformProduct.getOuterId();
            
            if (StringUtils.isBlank(outerId)) {
                failed++;
                continue;
            }
            
            // 匹配优先级:商家编码 > 条码 > SKU编号
            UnifiedProductModel matchedProduct = matchByPriority(outerId, 
                platformProduct.getBarcode(), platformProduct.getSkuCode(), 
                localIndex);
            
            if (matchedProduct != null) {
                // 建立映射关系
                PlatformProductMapping mapping = PlatformProductMapping.builder()
                    .systemProductId(matchedProduct.getSystemProductId())
                    .systemSkuCode(matchedProduct.getSystemSkuCode())
                    .platformCode(platformCode)
                    .shopId(shopId)
                    .platformProductId(platformProduct.getProductId())
                    .platformSkuId(platformProduct.getSkuId())
                    .platformTitle(platformProduct.getTitle())
                    .mappingStatus(1) // 已绑定
                    .build();
                
                mappingService.save(mapping);
                matched++;
            } else {
                failed++;
            }
        }
        
        log.info("自动匹配完成: 成功{}条, 失败{}条, 冲突{}条", 
                matched, failed, conflicts.size());
        
        return new AutoMatchResult(matched, failed, conflicts);
    }
    
    /**
     * 按优先级匹配
     */
    private UnifiedProductModel matchByPriority(String outerId, String barcode, 
                                               String skuCode, 
                                               Map<String, UnifiedProductModel> index) {
        // 优先级1:商家编码
        if (StringUtils.isNotBlank(outerId) && index.containsKey(outerId)) {
            return index.get(outerId);
        }
        
        // 优先级2:条码
        if (StringUtils.isNotBlank(barcode) && index.containsKey(barcode)) {
            return index.get(barcode);
        }
        
        // 优先级3:SKU编号
        if (StringUtils.isNotBlank(skuCode) && index.containsKey(skuCode)) {
            return index.get(skuCode);
        }
        
        return null;
    }
    
    /**
     * 构建本地商品索引
     */
    private Map<String, UnifiedProductModel> buildLocalIndex(
            List<UnifiedProductModel> products) {
        Map<String, UnifiedProductModel> index = new HashMap<>();
        
        for (UnifiedProductModel product : products) {
            // 按商家编码索引
            if (StringUtils.isNotBlank(product.getProductCode())) {
                index.put(product.getProductCode(), product);
            }
            // 按条码索引
            if (StringUtils.isNotBlank(product.getBarcode())) {
                index.put(product.getBarcode(), product);
            }
            // 按SKU编码索引
            if (StringUtils.isNotBlank(product.getSystemSkuCode())) {
                index.put(product.getSystemSkuCode(), product);
            }
        }
        
        return index;
    }
}

4.3 手工匹配实现

当自动匹配失败(编码缺失、不一致等),需要手工匹配:

/**
 * 手工匹配服务
 */
@Service
public class ManualMatchService {
    
    @Autowired
    private PlatformProductMappingService mappingService;
    
    /**
     * 手工绑定平台商品到本地商品
     */
    public void manualBind(String shopId, String platformCode, 
                          String platformSkuId, Long systemProductId) {
        // 1. 检查是否已存在映射
        PlatformProductMapping existing = mappingService
            .getByPlatformSku(shopId, platformCode, platformSkuId);
        
        if (existing != null && existing.getMappingStatus() == 1) {
            throw new BusinessException("该商品已绑定,请先解绑");
        }
        
        // 2. 获取本地商品信息
        UnifiedProductModel localProduct = productService.getById(systemProductId);
        if (localProduct == null) {
            throw new BusinessException("本地商品不存在");
        }
        
        // 3. 创建或更新映射
        PlatformProductMapping mapping = existing != null ? existing : 
            PlatformProductMapping.builder().build();
        
        mapping.setSystemProductId(systemProductId);
        mapping.setSystemSkuCode(localProduct.getSystemSkuCode());
        mapping.setPlatformCode(platformCode);
        mapping.setShopId(shopId);
        mapping.setPlatformSkuId(platformSkuId);
        mapping.setMappingStatus(1); // 已绑定
        
        mappingService.saveOrUpdate(mapping);
    }
    
    /**
     * 解绑平台商品
     */
    public void unbind(String shopId, String platformCode, String platformSkuId) {
        PlatformProductMapping mapping = mappingService
            .getByPlatformSku(shopId, platformCode, platformSkuId);
        
        if (mapping == null) {
            throw new BusinessException("映射关系不存在");
        }
        
        mapping.setMappingStatus(2); // 已解绑
        mappingService.update(mapping);
    }
    
    /**
     * 快速新建并绑定
     * 当本地没有对应商品时,快速创建本地商品并建立映射
     */
    public void quickCreateAndBind(String shopId, String platformCode, 
                                   String platformSkuId, 
                                   PlatformProductModel platformProduct) {
        // 1. 创建本地商品
        UnifiedProductModel newProduct = UnifiedProductModel.builder()
            .productName(platformProduct.getTitle())
            .productCode(platformProduct.getOuterId())
            .barcode(platformProduct.getBarcode())
            .salePrice(platformProduct.getPrice())
            .status(1) // 启用
            .build();
        
        Long productId = productService.create(newProduct);
        
        // 2. 建立映射
        manualBind(shopId, platformCode, platformSkuId, productId);
    }
}

4.4 批量导入实现

对于大批量商品初始化,手工匹配效率太低。系统支持通过Excel模板批量导入映射关系:

/**
 * 批量导入服务
 */
@Service
@Slf4j
public class BatchImportService {
    
    @Autowired
    private PlatformProductMappingService mappingService;
    
    /**
     * 批量导入商品映射
     * @param file Excel文件
     * @param shopId 店铺ID
     * @param platformCode 平台编码
     */
    public BatchImportResult batchImport(MultipartFile file, String shopId, 
                                        String platformCode) {
        log.info("开始批量导入: shop={}, platform={}", shopId, platformCode);
        
        // 1. 解析Excel
        List<ImportRow> rows = parseExcel(file);
        
        int success = 0;
        int failed = 0;
        List<ImportError> errors = new ArrayList<>();
        
        // 2. 逐行处理
        for (int i = 0; i < rows.size(); i++) {
            ImportRow row = rows.get(i);
            int rowNum = i + 2; // Excel行号(从第2行开始)
            
            try {
                // 校验必填字段
                validateRow(row, rowNum);
                
                // 查找本地商品
                UnifiedProductModel localProduct = findLocalProduct(row);
                if (localProduct == null) {
                    errors.add(new ImportError(rowNum, "本地商品不存在: " + 
                        row.getLocalProductCode()));
                    failed++;
                    continue;
                }
                
                // 查找平台商品
                PlatformProductModel platformProduct = findPlatformProduct(
                    shopId, platformCode, row);
                if (platformProduct == null) {
                    errors.add(new ImportError(rowNum, "平台商品不存在: " + 
                        row.getPlatformSkuId()));
                    failed++;
                    continue;
                }
                
                // 创建映射
                PlatformProductMapping mapping = PlatformProductMapping.builder()
                    .systemProductId(localProduct.getSystemProductId())
                    .systemSkuCode(localProduct.getSystemSkuCode())
                    .platformCode(platformCode)
                    .shopId(shopId)
                    .platformProductId(platformProduct.getProductId())
                    .platformSkuId(platformProduct.getSkuId())
                    .platformTitle(platformProduct.getTitle())
                    .mappingStatus(1)
                    .build();
                
                mappingService.save(mapping);
                success++;
                
            } catch (Exception e) {
                errors.add(new ImportError(rowNum, e.getMessage()));
                failed++;
            }
        }
        
        log.info("批量导入完成: 成功{}条, 失败{}条", success, failed);
        
        return new BatchImportResult(success, failed, errors);
    }
    
    /**
     * 解析Excel文件
     */
    private List<ImportRow> parseExcel(MultipartFile file) {
        // 使用EasyExcel解析
        // 模板字段:平台商品ID | 平台SKU ID | 本地商品编码 | 本地SKU编码
        List<ImportRow> rows = new ArrayList<>();
        
        EasyExcel.read(file.getInputStream(), ImportRow.class, 
            new ReadListener<ImportRow>() {
                @Override
                public void invoke(ImportRow data, AnalysisContext context) {
                    rows.add(data);
                }
                
                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                    // 解析完成
                }
            }).sheet().doRead();
        
        return rows;
    }
}

五、属性同步与映射

商品属性(颜色、尺码、规格等)在各平台的定义方式不同,需要同步和映射。

5.1 属性字典设计

/**
 * 属性字典
 * 维护系统内部的标准属性值
 */
@Data
@Builder
public class AttributeDictionary {
    
    /** 属性ID */
    private Long id;
    
    /** 属性名称(如:颜色、尺码) */
    private String attributeName;
    
    /** 属性值列表 */
    private List<String> attributeValues;
    
    /** 属性类型(销售属性/非销售属性) */
    private Integer attributeType;
    
    /** 排序 */
    private Integer sortOrder;
}

/**
 * 平台属性映射
 * 将平台属性映射到系统标准属性
 */
@Data
@Builder
public class PlatformAttributeMapping {
    
    /** 映射ID */
    private Long id;
    
    /** 平台编码 */
    private String platformCode;
    
    /** 平台属性名(如:颜色分类、尺码) */
    private String platformAttrName;
    
    /** 平台属性值(如:白色、M) */
    private String platformAttrValue;
    
    /** 系统属性ID */
    private Long systemAttrId;
    
    /** 系统属性名 */
    private String systemAttrName;
    
    /** 系统属性值 */
    private String systemAttrValue;
}

5.2 属性同步实现

/**
 * 属性同步服务
 */
@Service
public class AttributeSyncService {
    
    @Autowired
    private PlatformAttributeMappingService mappingService;
    
    /**
     * 将平台属性转换为系统标准属性
     */
    public SystemAttribute fromPlatform(String platformCode, 
                                       List<PlatformAttribute> platformAttrs) {
        SystemAttribute systemAttr = new SystemAttribute();
        
        for (PlatformAttribute attr : platformAttrs) {
            // 查找属性映射
            PlatformAttributeMapping mapping = mappingService
                .getByPlatform(platformCode, attr.getName(), attr.getValue());
            
            if (mapping != null) {
                // 使用映射后的标准值
                systemAttr.addAttribute(mapping.getSystemAttrName(), 
                                       mapping.getSystemAttrValue());
            } else {
                // 未找到映射,直接使用原值
                systemAttr.addAttribute(attr.getName(), attr.getValue());
            }
        }
        
        return systemAttr;
    }
    
    /**
     * 一键拉取平台属性到本地
     * 将平台属性自动添加到系统属性字典
     */
    public void pullPlatformAttributes(String platformCode, String shopId) {
        // 1. 拉取平台所有商品的属性
        List<PlatformProductModel> products = platformAdapterMap
            .get(platformCode + "Adapter")
            .pullProducts(shopId, 1, Integer.MAX_VALUE);
        
        // 2. 提取所有属性值
        Map<String, Set<String>> attrMap = new HashMap<>();
        
        for (PlatformProductModel product : products) {
            for (PlatformAttribute attr : product.getAttributes()) {
                attrMap.computeIfAbsent(attr.getName(), k -> new HashSet<>())
                       .add(attr.getValue());
            }
        }
        
        // 3. 创建或更新属性字典
        for (Map.Entry<String, Set<String>> entry : attrMap.entrySet()) {
            String attrName = entry.getKey();
            Set<String> attrValues = entry.getValue();
            
            AttributeDictionary dict = dictionaryService
                .getByPlatformAndName(platformCode, attrName);
            
            if (dict == null) {
                // 新建属性字典
                dict = AttributeDictionary.builder()
                    .attributeName(attrName)
                    .attributeValues(new ArrayList<>(attrValues))
                    .build();
                dictionaryService.create(dict);
            } else {
                // 追加新属性值
                List<String> existingValues = dict.getAttributeValues();
                for (String value : attrValues) {
                    if (!existingValues.contains(value)) {
                        existingValues.add(value);
                    }
                }
                dictionaryService.update(dict);
            }
        }
    }
}

六、套餐与组合商品管理

电商场景中,套餐和组合商品很常见(如:买一送一、套装销售)。系统需要支持自动或手动生成套餐。

6.1 套餐生成策略

/**
 * 套餐生成服务
 */
@Service
public class PackageService {
    
    /**
     * 根据平台商品自动生成套餐
     * 规则:平台商品名 + SKU名
     */
    public UnifiedProductModel autoGeneratePackage(
            PlatformProductModel platformProduct) {
        
        String packageName = platformProduct.getTitle() + " - " + 
                            platformProduct.getSkuName();
        
        return UnifiedProductModel.builder()
            .productName(packageName)
            .productCode("PKG-" + platformProduct.getSkuId())
            .productType(2) // 套餐类型
            .salePrice(platformProduct.getPrice())
            .build();
    }
    
    /**
     * 手动创建套餐
     * @param packageInfo 套餐信息
     * @param items 套餐明细(子商品列表)
     */
    public Long createPackage(PackageInfo packageInfo, List<PackageItem> items) {
        // 1. 创建套餐主商品
        UnifiedProductModel packageProduct = UnifiedProductModel.builder()
            .productName(packageInfo.getPackageName())
            .productCode(packageInfo.getPackageCode())
            .productType(2) // 套餐类型
            .salePrice(packageInfo.getSalePrice())
            .build();
        
        Long packageId = productService.create(packageProduct);
        
        // 2. 创建套餐明细
        for (PackageItem item : items) {
            PackageItemDetail detail = PackageItemDetail.builder()
                .packageId(packageId)
                .productId(item.getProductId())
                .skuCode(item.getSkuCode())
                .quantity(item.getQuantity())
                .build();
            
            packageItemService.create(detail);
        }
        
        return packageId;
    }
}

七、商品数据一致性保障

商品数据涉及多个系统和平台,一致性保障至关重要。

7.1 数据同步机制

/**
 * 商品数据同步服务
 * 确保本地商品数据与平台保持一致
 */
@Service
@Slf4j
public class ProductSyncService {
    
    /**
     * 定时同步平台商品数据
     * 每天凌晨2点执行
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void syncPlatformProducts() {
        log.info("开始同步平台商品数据...");
        
        List<ShopConfig> shops = shopConfigService.getAllActive();
        
        for (ShopConfig shop : shops) {
            try {
                syncShopProducts(shop);
            } catch (Exception e) {
                log.error("同步失败: shop={}", shop.getShopId(), e);
            }
        }
        
        log.info("平台商品同步完成");
    }
    
    /**
     * 同步指定店铺的商品
     */
    private void syncShopProducts(ShopConfig shop) {
        String shopId = shop.getShopId();
        String platformCode = shop.getPlatformCode();
        
        // 1. 拉取平台最新商品数据
        List<PlatformProductModel> platformProducts = platformAdapterMap
            .get(platformCode + "Adapter")
            .pullProducts(shopId, 1, Integer.MAX_VALUE);
        
        // 2. 获取本地映射关系
        List<PlatformProductMapping> mappings = mappingService
            .getByShopAndPlatform(shopId, platformCode);
        
        Map<String, PlatformProductMapping> mappingIndex = mappings.stream()
            .collect(Collectors.toMap(
                PlatformProductMapping::getPlatformSkuId, 
                m -> m
            ));
        
        int updated = 0;
        int added = 0;
        
        // 3. 对比更新
        for (PlatformProductModel platformProduct : platformProducts) {
            PlatformProductMapping mapping = mappingIndex
                .get(platformProduct.getSkuId());
            
            if (mapping != null) {
                // 已映射,更新平台信息
                mapping.setPlatformTitle(platformProduct.getTitle());
                mapping.setLastSyncTime(LocalDateTime.now());
                mappingService.update(mapping);
                updated++;
            } else {
                // 未映射,记录待处理
                pendingMatchService.add(shopId, platformCode, platformProduct);
                added++;
            }
        }
        
        log.info("店铺同步完成: shop={}, updated={}, added={}", 
                shopId, updated, added);
    }
}

7.2 变更通知机制

当商品信息变更时,需要通知相关系统(库存、订单、财务等):

/**
 * 商品变更事件发布
 */
@Component
public class ProductChangeEventPublisher {
    
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    
    /**
     * 发布商品变更事件
     */
    public void publishProductChange(Long productId, ChangeType changeType) {
        ProductChangeEvent event = new ProductChangeEvent();
        event.setProductId(productId);
        event.setChangeType(changeType);
        event.setTimestamp(System.currentTimeMillis());
        
        eventPublisher.publishEvent(event);
    }
    
    /**
     * 商品变更事件监听器
     */
    @Component
    public class ProductChangeListener {
        
        @Autowired
        private StockService stockService;
        
        @Autowired
        private OrderService orderService;
        
        /**
         * 监听商品变更事件
         */
        @EventListener
        public void onProductChange(ProductChangeEvent event) {
            log.info("收到商品变更事件: productId={}, type={}", 
                    event.getProductId(), event.getChangeType());
            
            switch (event.getChangeType()) {
                case PRICE_CHANGE:
                    // 价格变更,更新订单系统的价格校验规则
                    orderService.updatePriceValidation(event.getProductId());
                    break;
                    
                case STATUS_CHANGE:
                    // 状态变更(启用/停用),更新库存系统
                    stockService.handleProductStatusChange(event.getProductId());
                    break;
                    
                case INFO_CHANGE:
                    // 信息变更(名称、规格等),通知相关系统
                    notifyRelatedSystems(event.getProductId());
                    break;
            }
        }
    }
}

八、往来单位商品编码映射

在供应链场景中,供应商和客户可能有自己的商品编码体系。系统支持维护往来单位的商品编码映射,方便对账和开单。

/**
 * 往来单位商品编码映射
 */
@Data
@Builder
public class PartnerProductCode {
    
    /** 映射ID */
    private Long id;
    
    /** 系统商品ID */
    private Long systemProductId;
    
    /** 系统SKU编码 */
    private String systemSkuCode;
    
    /** 往来单位ID(供应商/客户) */
    private Long partnerId;
    
    /** 往来单位名称 */
    private String partnerName;
    
    /** 往来单位商品编码 */
    private String partnerProductCode;
    
    /** 往来单位商品名称 */
    private String partnerProductName;
    
    /** 创建时间 */
    private LocalDateTime createTime;
}

/**
 * 往来单位编码服务
 */
@Service
public class PartnerProductCodeService {
    
    /**
     * 设置往来单位商品编码
     */
    public void setPartnerCode(Long partnerId, String systemSkuCode, 
                              String partnerCode) {
        PartnerProductCode existing = mapper
            .getByPartnerAndSku(partnerId, systemSkuCode);
        
        if (existing != null) {
            existing.setPartnerProductCode(partnerCode);
            mapper.update(existing);
        } else {
            PartnerProductCode newCode = PartnerProductCode.builder()
                .partnerId(partnerId)
                .systemSkuCode(systemSkuCode)
                .partnerProductCode(partnerCode)
                .build();
            mapper.insert(newCode);
        }
    }
    
    /**
     * 根据往来单位编码查找本地商品
     */
    public UnifiedProductModel findByPartnerCode(Long partnerId, 
                                                String partnerCode) {
        PartnerProductCode mapping = mapper
            .getByPartnerAndCode(partnerId, partnerCode);
        
        if (mapping != null) {
            return productService.getBySkuCode(mapping.getSystemSkuCode());
        }
        
        return null;
    }
}

九、技术选型建议

基于实战经验,以下是商品管理系统的技术选型建议:

┌──────────────┬────────────────────┬────────────────────────────────┐
│  组件         │  推荐方案          │  选型理由                      │
├──────────────┼────────────────────┼────────────────────────────────┤
│  Excel解析    │  EasyExcel         │  内存占用低,适合大文件处理     │
├──────────────┼────────────────────┼────────────────────────────────┤
│  缓存         │  Redis             │  商品数据缓存,提升查询性能     │
├──────────────┼────────────────────┼────────────────────────────────┤
│  数据库       │  MySQL 8.0         │  事务支持好,适合复杂查询       │
├──────────────┼────────────────────┼────────────────────────────────┤
│  搜索引擎     │  Elasticsearch     │  商品全文检索和复杂查询         │
├──────────────┼────────────────────┼────────────────────────────────┤
│  消息队列     │  RabbitMQ          │  商品变更事件通知               │
├──────────────┼────────────────────┼────────────────────────────────┤
│  定时任务     │  XXL-JOB           │  商品数据定时同步               │
└──────────────┴────────────────────┴────────────────────────────────┘

十、实战经验总结

在落地商品管理系统的过程中,有几个关键经验值得分享:

第一,编码规范是基础。 很多商家的商品管理混乱,根源在于没有统一的编码规范。建议在系统初始化时就建立严格的编码体系:商品编号、SKU编码、条码都要有明确的规则,并且在各平台保持一致。

第二,自动匹配能大幅提升效率。 对于标准化商品(如数码3C),自动匹配的成功率可以达到90%以上。关键是确保本地商品和平台商品的编码一致。对于非标品(如服装),可能需要手工匹配或批量导入。

第三,属性映射要足够灵活。 各平台的属性体系差异很大,淘宝用"颜色分类",京东用"颜色",拼多多用"规格"。属性映射要支持一对多、多对一的灵活配置,并且支持一键拉取平台属性到本地。

第四,套餐管理要自动化。 电商场景中套餐和组合商品很多,如果手工创建效率太低。系统应该支持根据平台商品自动生成套餐,并且自动建立套餐与子商品的关联关系。

网上管家婆在商品管理模块的设计上也遵循了这些原则。系统支持自动匹配、手工匹配、批量导入三种映射方式,属性映射支持灵活配置,套餐管理支持自动生成,往来单位编码映射方便供应链协同。整套商品管理体系能够覆盖绝大多数电商场景。


结语

多渠道商品管理看似简单——"把平台商品对应到本地商品",但实际落地时,编码规范、属性映射、批量操作、套餐管理、数据一致性每一个环节都有不少细节要处理。本文从实际业务痛点出发,分享了一套完整的商品管理方案,包括统一模型设计、三种映射策略、属性同步机制、套餐自动生成、数据一致性保障。

核心思路可以概括为:用统一模型屏蔽平台差异,用多种映射策略覆盖不同场景,用属性字典管理标准化属性,用事件机制保障数据一致性,用自动化工具提升效率。这套方案在多个行业的实际业务中验证过,能够覆盖绝大多数电商商品管理场景。


如果这篇文章对你有帮助,欢迎点赞、收藏、关注。后续会继续分享电商ERP系统的实战经验。

Logo

电商企业物流数字化转型必备!快递鸟 API 接口,72 小时快速完成物流系统集成。全流程实战1V1指导,营造开放的API技术生态圈。

更多推荐