mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
feat: 新增拼多多商品关联内部商品
This commit is contained in:
+3
@@ -20,6 +20,9 @@ public class CreatePddGoodsDTO {
|
||||
@NotNull(message = "mall_id不能为空")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "内部商品ID(ec_product.id);不传则按首条 SKU.outer_id 解析", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotBlank(message = "商品ID不能为空")
|
||||
private String goodsId;
|
||||
|
||||
+3
@@ -13,6 +13,9 @@ public class PddGoodsFilterDTO {
|
||||
@Schema(description = "拼多多店铺ID(mall_id)")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "内部商品ID(ec_product.id)")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "商品名称")
|
||||
private String goodsName;
|
||||
|
||||
|
||||
+3
@@ -20,6 +20,9 @@ public class UpdatePddGoodsDTO {
|
||||
@NotNull(message = "mall_id不能为空")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "内部商品ID(ec_product.id);不传则按首条 SKU.outer_id 解析", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotBlank(message = "商品ID不能为空")
|
||||
private String goodsId;
|
||||
|
||||
+4
@@ -23,6 +23,10 @@ public class PddGoodsPO {
|
||||
@TableField("mall_id")
|
||||
private Long mallId;
|
||||
|
||||
/** 内部商品ID(ec_product.id) */
|
||||
@TableField("product_id")
|
||||
private Long productId;
|
||||
|
||||
@TableField("goods_id")
|
||||
private String goodsId;
|
||||
|
||||
|
||||
+6
@@ -31,6 +31,12 @@ public class PddGoodsSyncResultVO {
|
||||
@Schema(description = "SKU 落库条数")
|
||||
private Integer skuCount;
|
||||
|
||||
@Schema(description = "因无法匹配内部商品而跳过的数")
|
||||
private Integer skippedCount;
|
||||
|
||||
@Schema(description = "同步时自动创建内部商品的数量")
|
||||
private Integer autoCreatedProductCount;
|
||||
|
||||
@Schema(description = "调用列表接口页次数")
|
||||
private Integer pageCount;
|
||||
}
|
||||
|
||||
+6
@@ -22,6 +22,12 @@ public class PddGoodsVO {
|
||||
@Schema(description = "拼多多店铺ID(mall_id)")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "内部商品ID(ec_product.id)")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "内部商品名称")
|
||||
private String productName;
|
||||
|
||||
@Schema(description = "拼多多商品ID")
|
||||
private String goodsId;
|
||||
|
||||
|
||||
+9
@@ -9,6 +9,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.UpdatePddGo
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.po.PddGoodsPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsSyncResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods.PddGoodsPopApiClient;
|
||||
|
||||
public interface PddGoodsService extends IService<PddGoodsPO> {
|
||||
|
||||
@@ -28,4 +29,12 @@ public interface PddGoodsService extends IService<PddGoodsPO> {
|
||||
* @param pddShopId channel_pdd_shop.id
|
||||
*/
|
||||
PddGoodsSyncResultVO syncGoodsByPddShop(Long pddShopId);
|
||||
|
||||
/**
|
||||
* 按商品详情 upsert 渠道商品及 SKU,并绑定内部商品(product_id 必填)。
|
||||
*
|
||||
* @param pddShopId channel_pdd_shop.id
|
||||
*/
|
||||
void upsertFromGoodsDetail(Long companyId, Long pddShopId, Long mallId,
|
||||
Long productId, PddGoodsPopApiClient.GoodsDetail detail);
|
||||
}
|
||||
|
||||
+235
-6
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -26,17 +27,25 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsSkuV
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsSyncResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsService;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.service.PddProductAutoCreateService;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods.PddGoodsPopApiClient;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductSkuMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -51,17 +60,26 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
private final PddShopMapper pddShopMapper;
|
||||
private final PddAuthTokenService authTokenService;
|
||||
private final PddGoodsPopApiClient goodsPopApiClient;
|
||||
private final ProductSkuMapper productSkuMapper;
|
||||
private final ProductMapper productMapper;
|
||||
private final PddProductAutoCreateService productAutoCreateService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public PddGoodsServiceImpl(PddGoodsSkuMapper pddGoodsSkuMapper,
|
||||
PddShopMapper pddShopMapper,
|
||||
PddAuthTokenService authTokenService,
|
||||
PddGoodsPopApiClient goodsPopApiClient,
|
||||
ProductSkuMapper productSkuMapper,
|
||||
ProductMapper productMapper,
|
||||
@Lazy PddProductAutoCreateService productAutoCreateService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.pddGoodsSkuMapper = pddGoodsSkuMapper;
|
||||
this.pddShopMapper = pddShopMapper;
|
||||
this.authTokenService = authTokenService;
|
||||
this.goodsPopApiClient = goodsPopApiClient;
|
||||
this.productSkuMapper = productSkuMapper;
|
||||
this.productMapper = productMapper;
|
||||
this.productAutoCreateService = productAutoCreateService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@@ -77,9 +95,14 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
throw new BusinessException("该商品已存在");
|
||||
}
|
||||
|
||||
Long productId = resolveProductIdForManual(
|
||||
companyId, createPddGoodsDTO.getProductId(), createPddGoodsDTO.getSkus());
|
||||
requireOwnedProduct(companyId, productId);
|
||||
|
||||
PddGoodsPO goods = new PddGoodsPO();
|
||||
BeanCopyUtils.copyProperties(createPddGoodsDTO, goods);
|
||||
goods.setCompanyId(companyId);
|
||||
goods.setProductId(productId);
|
||||
this.save(goods);
|
||||
|
||||
if (createPddGoodsDTO.getSkus() != null && !createPddGoodsDTO.getSkus().isEmpty()) {
|
||||
@@ -110,10 +133,18 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
throw new BusinessException("该商品已存在");
|
||||
}
|
||||
|
||||
Long productId = resolveProductIdForManual(
|
||||
companyId, updatePddGoodsDTO.getProductId(), updatePddGoodsDTO.getSkus());
|
||||
if (productId == null) {
|
||||
productId = existing.getProductId();
|
||||
}
|
||||
requireOwnedProduct(companyId, productId);
|
||||
|
||||
PddGoodsPO goods = new PddGoodsPO();
|
||||
BeanCopyUtils.copyProperties(updatePddGoodsDTO, goods);
|
||||
goods.setId(id);
|
||||
goods.setCompanyId(companyId);
|
||||
goods.setProductId(productId);
|
||||
this.updateById(goods);
|
||||
|
||||
LambdaQueryWrapper<PddGoodsSkuPO> deleteWrapper = new LambdaQueryWrapper<>();
|
||||
@@ -161,6 +192,9 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
if (filter.getMallId() != null) {
|
||||
wrapper.eq(PddGoodsPO::getMallId, filter.getMallId());
|
||||
}
|
||||
if (filter.getProductId() != null) {
|
||||
wrapper.eq(PddGoodsPO::getProductId, filter.getProductId());
|
||||
}
|
||||
if (filter.getGoodsName() != null && !filter.getGoodsName().isEmpty()) {
|
||||
wrapper.like(PddGoodsPO::getGoodsName, filter.getGoodsName());
|
||||
}
|
||||
@@ -180,8 +214,9 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
IPage<PddGoodsPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
IPage<PddGoodsPO> result = this.page(page, wrapper);
|
||||
|
||||
Map<Long, String> productNames = loadProductNames(result.getRecords());
|
||||
List<PddGoodsVO> voList = result.getRecords().stream()
|
||||
.map(this::convertToBriefVO)
|
||||
.map(g -> convertToBriefVO(g, productNames))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageResultVO.create(voList, result.getTotal(),
|
||||
@@ -222,6 +257,8 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
int created = 0;
|
||||
int updated = 0;
|
||||
int skuCount = 0;
|
||||
int skipped = 0;
|
||||
int autoCreated = 0;
|
||||
Set<String> seenGoodsIds = new HashSet<>();
|
||||
|
||||
// 列表接口 is_onsale / outer_id / goods_name 三选一,需分别拉上架与下架
|
||||
@@ -256,7 +293,15 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
if (!seenGoodsIds.add(goodsId)) {
|
||||
continue;
|
||||
}
|
||||
UpsertResult ur = upsertGoodsItem(companyId, pddShopId, mallId, item, syncTime);
|
||||
UpsertResult ur = upsertGoodsItem(
|
||||
companyId, pddShopId, mallId, accessToken, item, syncTime);
|
||||
if (ur.skipped()) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (ur.autoCreatedInternal()) {
|
||||
autoCreated++;
|
||||
}
|
||||
if (ur.created()) {
|
||||
created++;
|
||||
} else {
|
||||
@@ -277,17 +322,95 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
return PddGoodsSyncResultVO.builder()
|
||||
.pddShopId(pddShopId)
|
||||
.mallId(mallId)
|
||||
.syncedCount(seenGoodsIds.size())
|
||||
.syncedCount(created + updated)
|
||||
.createdCount(created)
|
||||
.updatedCount(updated)
|
||||
.skuCount(skuCount)
|
||||
.skippedCount(skipped)
|
||||
.autoCreatedProductCount(autoCreated)
|
||||
.pageCount(pageCount)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void upsertFromGoodsDetail(Long companyId, Long pddShopId, Long mallId,
|
||||
Long productId, PddGoodsPopApiClient.GoodsDetail detail) {
|
||||
if (companyId == null || pddShopId == null || productId == null
|
||||
|| detail == null || detail.goodsId() == null) {
|
||||
throw new BusinessException("渠道商品落库参数不完整");
|
||||
}
|
||||
requireOwnedProduct(companyId, productId);
|
||||
|
||||
String goodsId = String.valueOf(detail.goodsId());
|
||||
PddGoodsPO existing = this.getOne(new LambdaQueryWrapper<PddGoodsPO>()
|
||||
.eq(PddGoodsPO::getShopId, pddShopId)
|
||||
.eq(PddGoodsPO::getGoodsId, goodsId)
|
||||
.last("LIMIT 1"));
|
||||
|
||||
LocalDateTime syncTime = LocalDateTime.now();
|
||||
boolean created = existing == null;
|
||||
PddGoodsPO goods = existing != null ? existing : new PddGoodsPO();
|
||||
goods.setCompanyId(companyId);
|
||||
goods.setShopId(pddShopId);
|
||||
goods.setMallId(mallId);
|
||||
goods.setProductId(productId);
|
||||
goods.setGoodsId(goodsId);
|
||||
goods.setGoodsName(StringUtils.hasText(detail.goodsName()) ? detail.goodsName().trim() : goodsId);
|
||||
if (goods.getGoodsQuantity() == null) {
|
||||
goods.setGoodsQuantity(0);
|
||||
}
|
||||
if (goods.getGoodsReserveQuantity() == null) {
|
||||
goods.setGoodsReserveQuantity(0);
|
||||
}
|
||||
String imageUrl = blankToNull(detail.resolveImageUrl());
|
||||
if (imageUrl != null) {
|
||||
goods.setImageUrl(imageUrl);
|
||||
if (!StringUtils.hasText(goods.getThumbUrl())) {
|
||||
goods.setThumbUrl(imageUrl);
|
||||
}
|
||||
}
|
||||
int skuSize = detail.skuList() != null ? detail.skuList().size() : 0;
|
||||
goods.setIsMoreSku(skuSize > 1 ? 1 : 0);
|
||||
if (goods.getIsOnsale() == null) {
|
||||
goods.setIsOnsale(0);
|
||||
}
|
||||
goods.setLastSyncTime(syncTime);
|
||||
|
||||
if (created) {
|
||||
this.save(goods);
|
||||
} else {
|
||||
this.updateById(goods);
|
||||
pddGoodsSkuMapper.delete(new LambdaQueryWrapper<PddGoodsSkuPO>()
|
||||
.eq(PddGoodsSkuPO::getGoodsRowId, goods.getId()));
|
||||
}
|
||||
|
||||
if (detail.skuList() == null) {
|
||||
return;
|
||||
}
|
||||
for (PddGoodsPopApiClient.SkuDetail sku : detail.skuList()) {
|
||||
if (sku == null || sku.skuId() == null) {
|
||||
continue;
|
||||
}
|
||||
PddGoodsSkuPO po = new PddGoodsSkuPO();
|
||||
po.setGoodsRowId(goods.getId());
|
||||
po.setCompanyId(companyId);
|
||||
po.setGoodsId(goodsId);
|
||||
po.setSkuId(String.valueOf(sku.skuId()));
|
||||
po.setSpec(blankToNull(sku.spec()));
|
||||
po.setSkuQuantity(0);
|
||||
po.setReserveQuantity(0);
|
||||
po.setIsSkuOnsale(0);
|
||||
po.setOuterGoodsId(blankToNull(sku.outerGoodsId()));
|
||||
po.setOuterId(blankToNull(sku.outerId()));
|
||||
pddGoodsSkuMapper.insert(po);
|
||||
}
|
||||
}
|
||||
|
||||
private UpsertResult upsertGoodsItem(Long companyId,
|
||||
Long pddShopId,
|
||||
Long mallId,
|
||||
String accessToken,
|
||||
PddGoodsPopApiClient.GoodsListItem item,
|
||||
LocalDateTime syncTime) {
|
||||
String goodsId = String.valueOf(item.goodsId());
|
||||
@@ -296,11 +419,33 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
.eq(PddGoodsPO::getGoodsId, goodsId)
|
||||
.last("LIMIT 1"));
|
||||
|
||||
boolean autoCreatedInternal = false;
|
||||
Long productId = resolveProductIdFromOuterIds(companyId, firstOuterIdFromApiSkus(item.skuList()));
|
||||
if (productId == null && existing != null) {
|
||||
productId = existing.getProductId();
|
||||
}
|
||||
if (productId == null) {
|
||||
try {
|
||||
productId = productAutoCreateService.ensureInternalProductByFetching(
|
||||
companyId, mallId, accessToken, item.goodsId());
|
||||
autoCreatedInternal = productId != null;
|
||||
} catch (Exception e) {
|
||||
log.warn("同步时自动建内部商品失败 shopId={} goodsId={}: {}",
|
||||
pddShopId, goodsId, e.getMessage());
|
||||
}
|
||||
}
|
||||
if (productId == null) {
|
||||
log.warn("跳过拼多多商品:无法匹配或创建内部商品 shopId={} goodsId={} firstOuterId={}",
|
||||
pddShopId, goodsId, firstOuterIdFromApiSkus(item.skuList()));
|
||||
return new UpsertResult(false, 0, true, false);
|
||||
}
|
||||
|
||||
boolean created = existing == null;
|
||||
PddGoodsPO goods = existing != null ? existing : new PddGoodsPO();
|
||||
goods.setCompanyId(companyId);
|
||||
goods.setShopId(pddShopId);
|
||||
goods.setMallId(mallId);
|
||||
goods.setProductId(productId);
|
||||
goods.setGoodsId(goodsId);
|
||||
goods.setGoodsName(StringUtils.hasText(item.goodsName()) ? item.goodsName() : goodsId);
|
||||
goods.setGoodsQuantity(toInt(item.goodsQuantity()));
|
||||
@@ -343,10 +488,85 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
skuInserted++;
|
||||
}
|
||||
}
|
||||
return new UpsertResult(created, skuInserted);
|
||||
return new UpsertResult(created, skuInserted, false, autoCreatedInternal);
|
||||
}
|
||||
|
||||
private record UpsertResult(boolean created, int skuCount) {
|
||||
private Long resolveProductIdForManual(Long companyId, Long explicitProductId,
|
||||
List<CreatePddGoodsSkuDTO> skus) {
|
||||
if (explicitProductId != null) {
|
||||
return explicitProductId;
|
||||
}
|
||||
String firstOuterId = null;
|
||||
if (skus != null) {
|
||||
for (CreatePddGoodsSkuDTO sku : skus) {
|
||||
if (sku != null && StringUtils.hasText(sku.getOuterId())) {
|
||||
firstOuterId = sku.getOuterId().trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Long productId = resolveProductIdFromOuterIds(companyId, firstOuterId);
|
||||
if (productId == null) {
|
||||
throw new BusinessException("无法关联内部商品:请传入 productId,或保证首条 SKU 的 outerId 能匹配内部 sku_code");
|
||||
}
|
||||
return productId;
|
||||
}
|
||||
|
||||
private Long resolveProductIdFromOuterIds(Long companyId, String outerId) {
|
||||
if (!StringUtils.hasText(outerId) || companyId == null) {
|
||||
return null;
|
||||
}
|
||||
ProductSkuPO sku = productSkuMapper.selectOne(new LambdaQueryWrapper<ProductSkuPO>()
|
||||
.eq(ProductSkuPO::getCompanyId, companyId)
|
||||
.eq(ProductSkuPO::getSkuCode, outerId.trim())
|
||||
.last("LIMIT 1"));
|
||||
return sku != null ? sku.getProductId() : null;
|
||||
}
|
||||
|
||||
private static String firstOuterIdFromApiSkus(List<PddGoodsPopApiClient.GoodsListSku> skus) {
|
||||
if (skus == null) {
|
||||
return null;
|
||||
}
|
||||
for (PddGoodsPopApiClient.GoodsListSku sku : skus) {
|
||||
if (sku != null && StringUtils.hasText(sku.outerId())) {
|
||||
return sku.outerId().trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void requireOwnedProduct(Long companyId, Long productId) {
|
||||
if (productId == null) {
|
||||
throw new BusinessException("内部商品ID不能为空");
|
||||
}
|
||||
ProductPO product = productMapper.selectById(productId);
|
||||
if (product == null || !Objects.equals(product.getCompanyId(), companyId)) {
|
||||
throw new BusinessException("内部商品不存在或无权访问");
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Long, String> loadProductNames(List<PddGoodsPO> goodsList) {
|
||||
if (goodsList == null || goodsList.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
List<Long> ids = goodsList.stream()
|
||||
.map(PddGoodsPO::getProductId)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
if (ids.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<Long, String> map = new HashMap<>();
|
||||
for (ProductPO p : productMapper.selectBatchIds(ids)) {
|
||||
if (p != null && p.getId() != null) {
|
||||
map.put(p.getId(), p.getName());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private record UpsertResult(boolean created, int skuCount, boolean skipped, boolean autoCreatedInternal) {
|
||||
}
|
||||
|
||||
private static Integer toInt(Long v) {
|
||||
@@ -381,9 +601,12 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
return sku;
|
||||
}
|
||||
|
||||
private PddGoodsVO convertToBriefVO(PddGoodsPO goods) {
|
||||
private PddGoodsVO convertToBriefVO(PddGoodsPO goods, Map<Long, String> productNames) {
|
||||
PddGoodsVO vo = new PddGoodsVO();
|
||||
BeanCopyUtils.copyProperties(goods, vo);
|
||||
if (goods.getProductId() != null) {
|
||||
vo.setProductName(productNames.get(goods.getProductId()));
|
||||
}
|
||||
vo.setSkus(null);
|
||||
return vo;
|
||||
}
|
||||
@@ -391,6 +614,12 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
private PddGoodsVO convertToVO(PddGoodsPO goods) {
|
||||
PddGoodsVO vo = new PddGoodsVO();
|
||||
BeanCopyUtils.copyProperties(goods, vo);
|
||||
if (goods.getProductId() != null) {
|
||||
ProductPO product = productMapper.selectById(goods.getProductId());
|
||||
if (product != null) {
|
||||
vo.setProductName(product.getName());
|
||||
}
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<PddGoodsSkuPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PddGoodsSkuPO::getGoodsRowId, goods.getId());
|
||||
|
||||
+56
-13
@@ -12,6 +12,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.mapper.PddOrderItemMa
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.model.po.PddOrderItemPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.model.PddOrderSyncShopContext;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.service.PddOrderSyncContextResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsService;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods.PddGoodsPopApiClient;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.category.model.po.CategoryPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.category.service.CategoryService;
|
||||
@@ -21,7 +22,8 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
|
||||
/**
|
||||
* 按平台 goodsId 拉详情,自动创建内部商品 + 全部 SKU(已存在则跳过),并回绑 orphan 明细。
|
||||
* 按平台 goodsId 拉详情,自动创建内部商品 + 全部 SKU(已存在则跳过),
|
||||
* 并 upsert 渠道商品 channel_pdd_goods(绑定 product_id)。
|
||||
* 类目:PDD 一级/叶子 → 内部一级/二级;按公司内叶子类目名复用,不重复创建。
|
||||
*/
|
||||
@Slf4j
|
||||
@@ -31,6 +33,7 @@ public class PddProductAutoCreateService {
|
||||
|
||||
private final PddOrderSyncContextResolver contextResolver;
|
||||
private final PddGoodsPopApiClient goodsPopApiClient;
|
||||
private final PddGoodsService pddGoodsService;
|
||||
private final PddOrderItemMapper pddOrderItemMapper;
|
||||
private final CategoryService categoryService;
|
||||
private final ProductService productService;
|
||||
@@ -45,31 +48,70 @@ public class PddProductAutoCreateService {
|
||||
PddOrderSyncShopContext ctx = contextResolver.resolve(message.getCompanyId(), message.getShopId());
|
||||
PddGoodsPopApiClient.GoodsDetail detail = goodsPopApiClient.fetchGoodsInformation(
|
||||
ctx.getMallId(), ctx.getAccessToken(), message.getGoodsId());
|
||||
if (detail == null || detail.goodsId() == null) {
|
||||
log.warn("自动建品:商品详情为空 companyId={} goodsId={}",
|
||||
Long productId = ensureInternalProduct(message.getCompanyId(), detail);
|
||||
if (productId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx.getPddShop() == null || ctx.getPddShop().getId() == null) {
|
||||
log.warn("自动建品:缺少渠道店铺,跳过 channel_pdd_goods 落库 companyId={} goodsId={}",
|
||||
message.getCompanyId(), message.getGoodsId());
|
||||
return;
|
||||
}
|
||||
pddGoodsService.upsertFromGoodsDetail(
|
||||
message.getCompanyId(),
|
||||
ctx.getPddShop().getId(),
|
||||
ctx.getMallId(),
|
||||
productId,
|
||||
detail);
|
||||
log.info("自动建品已绑定渠道商品 companyId={} pddShopId={} goodsId={} productId={}",
|
||||
message.getCompanyId(), ctx.getPddShop().getId(), message.getGoodsId(), productId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取商品详情并确保内部商品/SKU,返回 productId;失败返回 null。
|
||||
* 不写 channel_pdd_goods(供列表同步等调用方自行落库)。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long ensureInternalProductByFetching(Long companyId, Long mallId, String accessToken, Long goodsId)
|
||||
throws Exception {
|
||||
if (companyId == null || goodsId == null) {
|
||||
return null;
|
||||
}
|
||||
PddGoodsPopApiClient.GoodsDetail detail = goodsPopApiClient.fetchGoodsInformation(
|
||||
mallId, accessToken, goodsId);
|
||||
return ensureInternalProduct(companyId, detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据已拉取的详情确保内部商品与 SKU,返回 productId;失败返回 null。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long ensureInternalProduct(Long companyId, PddGoodsPopApiClient.GoodsDetail detail) {
|
||||
if (companyId == null || detail == null || detail.goodsId() == null) {
|
||||
log.warn("自动建品:商品详情为空 companyId={}", companyId);
|
||||
return null;
|
||||
}
|
||||
|
||||
String productCode = detail.resolveProductCode();
|
||||
if (!StringUtils.hasText(productCode)) {
|
||||
log.warn("自动建品:无法解析商品编码 goodsId={}", detail.goodsId());
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
String productName = StringUtils.hasText(detail.goodsName())
|
||||
? detail.goodsName().trim()
|
||||
: productCode;
|
||||
|
||||
String imageUrl = resolveProductImageUrl(detail, message.getCompanyId(), message.getGoodsId());
|
||||
Long categoryId = ensureCategoryId(message.getCompanyId(), detail.goodsCategory(), detail.lastCategory());
|
||||
ProductPO product = ensureProduct(message.getCompanyId(), productCode, productName, imageUrl, categoryId);
|
||||
String imageUrl = resolveProductImageUrl(detail, companyId, detail.goodsId());
|
||||
Long categoryId = ensureCategoryId(companyId, detail.goodsCategory(), detail.lastCategory());
|
||||
ProductPO product = ensureProduct(companyId, productCode, productName, imageUrl, categoryId);
|
||||
if (product == null || product.getId() == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (detail.skuList() == null || detail.skuList().isEmpty()) {
|
||||
log.info("自动建品:商品无 SKU 列表 goodsId={}", detail.goodsId());
|
||||
return;
|
||||
log.info("自动建品:商品无 SKU 列表 goodsId={} productId={}", detail.goodsId(), product.getId());
|
||||
return product.getId();
|
||||
}
|
||||
|
||||
int created = 0;
|
||||
@@ -83,20 +125,21 @@ public class PddProductAutoCreateService {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
ProductSkuPO existing = productSkuService.getByCompanyIdAndSkuCode(message.getCompanyId(), skuCode);
|
||||
ProductSkuPO existing = productSkuService.getByCompanyIdAndSkuCode(companyId, skuCode);
|
||||
if (existing != null) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (createSku(message.getCompanyId(), product.getId(), skuCode, skuDetail, imageUrl)) {
|
||||
if (createSku(companyId, product.getId(), skuCode, skuDetail, imageUrl)) {
|
||||
created++;
|
||||
} else {
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
log.info("自动建品完成 companyId={} goodsId={} productId={} categoryId={} createdSku={} skipped={} hasImage={}",
|
||||
message.getCompanyId(), detail.goodsId(), product.getId(), categoryId, created, skipped,
|
||||
companyId, detail.goodsId(), product.getId(), categoryId, created, skipped,
|
||||
StringUtils.hasText(imageUrl));
|
||||
return product.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user