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:
+10
@@ -12,6 +12,7 @@ import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.CreatePddGoodsDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.PddGoodsFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.UpdatePddGoodsDTO;
|
||||
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;
|
||||
|
||||
@@ -67,4 +68,13 @@ public class PddGoodsController {
|
||||
PddGoodsVO vo = pddGoodsService.getPddGoodsDetail(id);
|
||||
return ApiResponse.success("获取成功", vo);
|
||||
}
|
||||
|
||||
@Operation(summary = "同步商品", description = "按渠道店铺调用 pdd.goods.list.get,同步上架+下架商品及SKU到本地")
|
||||
@PostMapping("/sync/{pddShopId}")
|
||||
@RequirePermission(value = "biz:pdd:goods:manage", description = "同步拼多多商品", allowSuper = true)
|
||||
public ApiResponse<PddGoodsSyncResultVO> syncGoodsByPddShop(
|
||||
@Parameter(description = "渠道店铺ID(channel_pdd_shop.id)") @PathVariable Long pddShopId) {
|
||||
PddGoodsSyncResultVO result = pddGoodsService.syncGoodsByPddShop(pddShopId);
|
||||
return ApiResponse.success("同步完成", result);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -12,12 +12,13 @@ import java.util.List;
|
||||
@Schema(description = "创建拼多多商品DTO")
|
||||
public class CreatePddGoodsDTO {
|
||||
|
||||
@Schema(description = "店铺ID(ec_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "店铺ID不能为空")
|
||||
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "渠道店铺ID不能为空")
|
||||
private Long shopId;
|
||||
|
||||
@Schema(description = "拼多多授权ID")
|
||||
private Long pddAuthId;
|
||||
@Schema(description = "拼多多店铺ID(mall_id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "mall_id不能为空")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotBlank(message = "商品ID不能为空")
|
||||
|
||||
+4
-1
@@ -7,9 +7,12 @@ import lombok.Data;
|
||||
@Schema(description = "拼多多商品过滤DTO")
|
||||
public class PddGoodsFilterDTO {
|
||||
|
||||
@Schema(description = "店铺ID")
|
||||
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)")
|
||||
private Long shopId;
|
||||
|
||||
@Schema(description = "拼多多店铺ID(mall_id)")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "商品名称")
|
||||
private String goodsName;
|
||||
|
||||
|
||||
+5
-4
@@ -12,12 +12,13 @@ import java.util.List;
|
||||
@Schema(description = "更新拼多多商品DTO")
|
||||
public class UpdatePddGoodsDTO {
|
||||
|
||||
@Schema(description = "店铺ID(ec_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "店铺ID不能为空")
|
||||
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "渠道店铺ID不能为空")
|
||||
private Long shopId;
|
||||
|
||||
@Schema(description = "拼多多授权ID")
|
||||
private Long pddAuthId;
|
||||
@Schema(description = "拼多多店铺ID(mall_id)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "mall_id不能为空")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "拼多多商品ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotBlank(message = "商品ID不能为空")
|
||||
|
||||
+4
-2
@@ -15,11 +15,13 @@ public class PddGoodsPO {
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
|
||||
/** 渠道店铺ID(channel_pdd_shop.id) */
|
||||
@TableField("shop_id")
|
||||
private Long shopId;
|
||||
|
||||
@TableField("pdd_auth_id")
|
||||
private Long pddAuthId;
|
||||
/** 拼多多店铺 mall_id */
|
||||
@TableField("mall_id")
|
||||
private Long mallId;
|
||||
|
||||
@TableField("goods_id")
|
||||
private String goodsId;
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(description = "拼多多商品同步结果")
|
||||
public class PddGoodsSyncResultVO {
|
||||
|
||||
@Schema(description = "渠道店铺ID")
|
||||
private Long pddShopId;
|
||||
|
||||
@Schema(description = "拼多多 mall_id")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "本次拉取并落库的商品数(去重后)")
|
||||
private Integer syncedCount;
|
||||
|
||||
@Schema(description = "新增数")
|
||||
private Integer createdCount;
|
||||
|
||||
@Schema(description = "更新数")
|
||||
private Integer updatedCount;
|
||||
|
||||
@Schema(description = "SKU 落库条数")
|
||||
private Integer skuCount;
|
||||
|
||||
@Schema(description = "调用列表接口页次数")
|
||||
private Integer pageCount;
|
||||
}
|
||||
+3
-3
@@ -16,11 +16,11 @@ public class PddGoodsVO {
|
||||
@Schema(description = "公司ID")
|
||||
private Long companyId;
|
||||
|
||||
@Schema(description = "店铺ID")
|
||||
@Schema(description = "渠道店铺ID(channel_pdd_shop.id)")
|
||||
private Long shopId;
|
||||
|
||||
@Schema(description = "拼多多授权ID")
|
||||
private Long pddAuthId;
|
||||
@Schema(description = "拼多多店铺ID(mall_id)")
|
||||
private Long mallId;
|
||||
|
||||
@Schema(description = "拼多多商品ID")
|
||||
private String goodsId;
|
||||
|
||||
+8
@@ -7,6 +7,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.CreatePddGo
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.PddGoodsFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.UpdatePddGoodsDTO;
|
||||
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;
|
||||
|
||||
public interface PddGoodsService extends IService<PddGoodsPO> {
|
||||
@@ -20,4 +21,11 @@ public interface PddGoodsService extends IService<PddGoodsPO> {
|
||||
PageResultVO<PddGoodsVO> getPddGoodsPage(PageQueryDTO<PddGoodsFilterDTO> pageQueryDTO);
|
||||
|
||||
PddGoodsVO getPddGoodsDetail(Long id);
|
||||
|
||||
/**
|
||||
* 按渠道店铺调用 pdd.goods.list.get 全量同步(上架+下架)到 channel_pdd_goods / sku。
|
||||
*
|
||||
* @param pddShopId channel_pdd_shop.id
|
||||
*/
|
||||
PddGoodsSyncResultVO syncGoodsByPddShop(Long pddShopId);
|
||||
}
|
||||
|
||||
+228
-9
@@ -4,12 +4,16 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
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.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.mapper.PddAuthMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.model.po.PddAuthPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.mapper.PddGoodsMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.mapper.PddGoodsSkuMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.CreatePddGoodsDTO;
|
||||
@@ -19,22 +23,45 @@ 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.po.PddGoodsSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.PddGoodsSkuVO;
|
||||
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.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.utils.CurrentUserContext;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO> implements PddGoodsService {
|
||||
|
||||
private static final int LIST_PAGE_SIZE = 100;
|
||||
private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final PddGoodsSkuMapper pddGoodsSkuMapper;
|
||||
private final PddShopMapper pddShopMapper;
|
||||
private final PddAuthMapper pddAuthMapper;
|
||||
private final PddGoodsPopApiClient goodsPopApiClient;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public PddGoodsServiceImpl(PddGoodsSkuMapper pddGoodsSkuMapper, CurrentUserContext currentUserContext) {
|
||||
public PddGoodsServiceImpl(PddGoodsSkuMapper pddGoodsSkuMapper,
|
||||
PddShopMapper pddShopMapper,
|
||||
PddAuthMapper pddAuthMapper,
|
||||
PddGoodsPopApiClient goodsPopApiClient,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.pddGoodsSkuMapper = pddGoodsSkuMapper;
|
||||
this.pddShopMapper = pddShopMapper;
|
||||
this.pddAuthMapper = pddAuthMapper;
|
||||
this.goodsPopApiClient = goodsPopApiClient;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@@ -44,7 +71,7 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
LambdaQueryWrapper<PddGoodsPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PddGoodsPO::getCompanyId, companyId)
|
||||
wrapper.eq(PddGoodsPO::getShopId, createPddGoodsDTO.getShopId())
|
||||
.eq(PddGoodsPO::getGoodsId, createPddGoodsDTO.getGoodsId());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("该商品已存在");
|
||||
@@ -71,12 +98,12 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
PddGoodsPO existing = this.getById(id);
|
||||
if (existing == null) {
|
||||
if (existing == null || !companyId.equals(existing.getCompanyId())) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<PddGoodsPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PddGoodsPO::getCompanyId, companyId)
|
||||
wrapper.eq(PddGoodsPO::getShopId, updatePddGoodsDTO.getShopId())
|
||||
.eq(PddGoodsPO::getGoodsId, updatePddGoodsDTO.getGoodsId())
|
||||
.ne(PddGoodsPO::getId, id);
|
||||
if (this.count(wrapper) > 0) {
|
||||
@@ -100,14 +127,15 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
skus.forEach(pddGoodsSkuMapper::insert);
|
||||
}
|
||||
|
||||
return convertToVO(goods);
|
||||
return convertToVO(this.getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deletePddGoods(Long id) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
PddGoodsPO existing = this.getById(id);
|
||||
if (existing == null) {
|
||||
if (existing == null || !companyId.equals(existing.getCompanyId())) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
|
||||
@@ -130,6 +158,9 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
if (filter.getShopId() != null) {
|
||||
wrapper.eq(PddGoodsPO::getShopId, filter.getShopId());
|
||||
}
|
||||
if (filter.getMallId() != null) {
|
||||
wrapper.eq(PddGoodsPO::getMallId, filter.getMallId());
|
||||
}
|
||||
if (filter.getGoodsName() != null && !filter.getGoodsName().isEmpty()) {
|
||||
wrapper.like(PddGoodsPO::getGoodsName, filter.getGoodsName());
|
||||
}
|
||||
@@ -143,13 +174,14 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
wrapper.eq(PddGoodsPO::getIsMoreSku, filter.getIsMoreSku());
|
||||
}
|
||||
|
||||
wrapper.orderByDesc(PddGoodsPO::getCreateTime);
|
||||
wrapper.orderByDesc(PddGoodsPO::getIsOnsale)
|
||||
.orderByDesc(PddGoodsPO::getPddCreatedAt);
|
||||
|
||||
IPage<PddGoodsPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
IPage<PddGoodsPO> result = this.page(page, wrapper);
|
||||
|
||||
List<PddGoodsVO> voList = result.getRecords().stream()
|
||||
.map(this::convertToVO)
|
||||
.map(this::convertToBriefVO)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageResultVO.create(voList, result.getTotal(),
|
||||
@@ -158,13 +190,193 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
|
||||
@Override
|
||||
public PddGoodsVO getPddGoodsDetail(Long id) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
PddGoodsPO goods = this.getById(id);
|
||||
if (goods == null) {
|
||||
if (goods == null || !companyId.equals(goods.getCompanyId())) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
return convertToVO(goods);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PddGoodsSyncResultVO syncGoodsByPddShop(Long pddShopId) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (pddShopId == null) {
|
||||
throw new BusinessException("渠道店铺ID不能为空");
|
||||
}
|
||||
|
||||
PddShopPO pddShop = pddShopMapper.selectById(pddShopId);
|
||||
if (pddShop == null || !companyId.equals(pddShop.getCompanyId())) {
|
||||
throw new BusinessException("拼多多渠道店铺不存在");
|
||||
}
|
||||
if (pddShop.getPddAuthId() == null) {
|
||||
throw new BusinessException("该渠道店铺尚未绑定授权,请先完成授权");
|
||||
}
|
||||
PddAuthPO auth = pddAuthMapper.selectById(pddShop.getPddAuthId());
|
||||
if (auth == null || auth.getStatus() == null || auth.getStatus() != 1
|
||||
|| !StringUtils.hasText(auth.getAccessToken())) {
|
||||
throw new BusinessException("拼多多授权无效或已失效,请重新授权");
|
||||
}
|
||||
|
||||
Long mallId = pddShop.getMallId();
|
||||
String accessToken = auth.getAccessToken();
|
||||
LocalDateTime syncTime = LocalDateTime.now();
|
||||
|
||||
int pageCount = 0;
|
||||
int created = 0;
|
||||
int updated = 0;
|
||||
int skuCount = 0;
|
||||
Set<String> seenGoodsIds = new HashSet<>();
|
||||
|
||||
// 列表接口 is_onsale / outer_id / goods_name 三选一,需分别拉上架与下架
|
||||
for (int isOnsale : List.of(1, 0)) {
|
||||
int page = 1;
|
||||
Integer total = null;
|
||||
while (true) {
|
||||
PddGoodsPopApiClient.GoodsListPage listPage;
|
||||
try {
|
||||
listPage = goodsPopApiClient.fetchGoodsList(
|
||||
mallId, accessToken, page, LIST_PAGE_SIZE, isOnsale);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("同步拼多多商品失败 pddShopId={} mallId={} isOnsale={} page={}",
|
||||
pddShopId, mallId, isOnsale, page, e);
|
||||
throw new BusinessException("同步拼多多商品失败: " + e.getMessage());
|
||||
}
|
||||
pageCount++;
|
||||
if (total == null) {
|
||||
total = listPage.totalCount();
|
||||
}
|
||||
List<PddGoodsPopApiClient.GoodsListItem> items = listPage.goodsList();
|
||||
if (items == null || items.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
for (PddGoodsPopApiClient.GoodsListItem item : items) {
|
||||
if (item.goodsId() == null) {
|
||||
continue;
|
||||
}
|
||||
String goodsId = String.valueOf(item.goodsId());
|
||||
if (!seenGoodsIds.add(goodsId)) {
|
||||
continue;
|
||||
}
|
||||
UpsertResult ur = upsertGoodsItem(companyId, pddShopId, mallId, item, syncTime);
|
||||
if (ur.created()) {
|
||||
created++;
|
||||
} else {
|
||||
updated++;
|
||||
}
|
||||
skuCount += ur.skuCount();
|
||||
}
|
||||
if (items.size() < LIST_PAGE_SIZE) {
|
||||
break;
|
||||
}
|
||||
if (total != null && page * LIST_PAGE_SIZE >= total) {
|
||||
break;
|
||||
}
|
||||
page++;
|
||||
}
|
||||
}
|
||||
|
||||
return PddGoodsSyncResultVO.builder()
|
||||
.pddShopId(pddShopId)
|
||||
.mallId(mallId)
|
||||
.syncedCount(seenGoodsIds.size())
|
||||
.createdCount(created)
|
||||
.updatedCount(updated)
|
||||
.skuCount(skuCount)
|
||||
.pageCount(pageCount)
|
||||
.build();
|
||||
}
|
||||
|
||||
private UpsertResult upsertGoodsItem(Long companyId,
|
||||
Long pddShopId,
|
||||
Long mallId,
|
||||
PddGoodsPopApiClient.GoodsListItem item,
|
||||
LocalDateTime syncTime) {
|
||||
String goodsId = String.valueOf(item.goodsId());
|
||||
PddGoodsPO existing = this.getOne(new LambdaQueryWrapper<PddGoodsPO>()
|
||||
.eq(PddGoodsPO::getShopId, pddShopId)
|
||||
.eq(PddGoodsPO::getGoodsId, goodsId)
|
||||
.last("LIMIT 1"));
|
||||
|
||||
boolean created = existing == null;
|
||||
PddGoodsPO goods = existing != null ? existing : new PddGoodsPO();
|
||||
goods.setCompanyId(companyId);
|
||||
goods.setShopId(pddShopId);
|
||||
goods.setMallId(mallId);
|
||||
goods.setGoodsId(goodsId);
|
||||
goods.setGoodsName(StringUtils.hasText(item.goodsName()) ? item.goodsName() : goodsId);
|
||||
goods.setGoodsQuantity(toInt(item.goodsQuantity()));
|
||||
goods.setGoodsReserveQuantity(toInt(item.goodsReserveQuantity()));
|
||||
goods.setImageUrl(blankToNull(item.imageUrl()));
|
||||
goods.setThumbUrl(blankToNull(item.thumbUrl()));
|
||||
goods.setIsMoreSku(item.isMoreSku() != null ? item.isMoreSku() : 0);
|
||||
goods.setIsOnsale(item.isOnsale() != null ? item.isOnsale() : 0);
|
||||
goods.setPddCreatedAt(epochSecToLocalDateTime(item.createdAtEpochSec()));
|
||||
goods.setRawResponse(item.rawJson());
|
||||
goods.setLastSyncTime(syncTime);
|
||||
|
||||
if (created) {
|
||||
this.save(goods);
|
||||
} else {
|
||||
this.updateById(goods);
|
||||
pddGoodsSkuMapper.delete(new LambdaQueryWrapper<PddGoodsSkuPO>()
|
||||
.eq(PddGoodsSkuPO::getGoodsRowId, goods.getId()));
|
||||
}
|
||||
|
||||
int skuInserted = 0;
|
||||
if (item.skuList() != null) {
|
||||
for (PddGoodsPopApiClient.GoodsListSku sku : item.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.setSpecDetails(blankToNull(sku.specDetailsJson()));
|
||||
po.setSkuQuantity(toInt(sku.skuQuantity()));
|
||||
po.setReserveQuantity(toInt(sku.reserveQuantity()));
|
||||
po.setIsSkuOnsale(sku.isSkuOnsale() != null ? sku.isSkuOnsale() : 0);
|
||||
po.setOuterGoodsId(blankToNull(sku.outerGoodsId()));
|
||||
po.setOuterId(blankToNull(sku.outerId()));
|
||||
pddGoodsSkuMapper.insert(po);
|
||||
skuInserted++;
|
||||
}
|
||||
}
|
||||
return new UpsertResult(created, skuInserted);
|
||||
}
|
||||
|
||||
private record UpsertResult(boolean created, int skuCount) {
|
||||
}
|
||||
|
||||
private static Integer toInt(Long v) {
|
||||
if (v == null) {
|
||||
return 0;
|
||||
}
|
||||
if (v > Integer.MAX_VALUE) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
if (v < Integer.MIN_VALUE) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
return v.intValue();
|
||||
}
|
||||
|
||||
private static String blankToNull(String s) {
|
||||
return StringUtils.hasText(s) ? s.trim() : null;
|
||||
}
|
||||
|
||||
private static LocalDateTime epochSecToLocalDateTime(Long epochSec) {
|
||||
if (epochSec == null || epochSec <= 0) {
|
||||
return null;
|
||||
}
|
||||
return LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSec), ZONE);
|
||||
}
|
||||
|
||||
private PddGoodsSkuPO convertToSkuPO(CreatePddGoodsSkuDTO dto, Long goodsRowId, Long companyId) {
|
||||
PddGoodsSkuPO sku = new PddGoodsSkuPO();
|
||||
BeanCopyUtils.copyProperties(dto, sku);
|
||||
@@ -173,6 +385,13 @@ public class PddGoodsServiceImpl extends ServiceImpl<PddGoodsMapper, PddGoodsPO>
|
||||
return sku;
|
||||
}
|
||||
|
||||
private PddGoodsVO convertToBriefVO(PddGoodsPO goods) {
|
||||
PddGoodsVO vo = new PddGoodsVO();
|
||||
BeanCopyUtils.copyProperties(goods, vo);
|
||||
vo.setSkus(null);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private PddGoodsVO convertToVO(PddGoodsPO goods) {
|
||||
PddGoodsVO vo = new PddGoodsVO();
|
||||
BeanCopyUtils.copyProperties(goods, vo);
|
||||
|
||||
+1
@@ -15,6 +15,7 @@ public final class PddPopApiTypes {
|
||||
|
||||
// ---------- 商品 ----------
|
||||
public static final String GOODS_INFORMATION_GET = "pdd.goods.information.get";
|
||||
public static final String GOODS_LIST_GET = "pdd.goods.list.get";
|
||||
|
||||
// ---------- 店铺 ----------
|
||||
public static final String MALL_INFO_GET = "pdd.mall.info.get";
|
||||
|
||||
+119
-5
@@ -1,8 +1,10 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsInformationGetRequest;
|
||||
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsListGetRequest;
|
||||
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsInformationGetResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsListGetResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -14,14 +16,19 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品域 POP:{@value PddPopApiTypes#GOODS_INFORMATION_GET}。
|
||||
* 商品域 POP:{@value PddPopApiTypes#GOODS_INFORMATION_GET}、{@value PddPopApiTypes#GOODS_LIST_GET}。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PddGoodsPopApiClient {
|
||||
|
||||
private final PddPopInvokeSupport invokeSupport;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public PddGoodsPopApiClient(PddPopInvokeSupport invokeSupport, ObjectMapper objectMapper) {
|
||||
this.invokeSupport = invokeSupport;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public GoodsDetail fetchGoodsInformation(Long mallId, String accessToken, Long goodsId) throws Exception {
|
||||
if (goodsId == null) {
|
||||
@@ -32,11 +39,78 @@ public class PddGoodsPopApiClient {
|
||||
request.setGoodsId(goodsId);
|
||||
PddGoodsInformationGetResponse response = client.syncInvoke(request, accessToken);
|
||||
invokeSupport.assertNoError(response, PddPopApiTypes.GOODS_INFORMATION_GET);
|
||||
return map(response);
|
||||
return mapInformation(response);
|
||||
});
|
||||
}
|
||||
|
||||
private static GoodsDetail map(PddGoodsInformationGetResponse response) {
|
||||
/**
|
||||
* 商品列表分页。isOnsale:0 下架 / 1 上架;与 outer_id、goods_name 三选一,优先级 is_onsale 最高。
|
||||
*/
|
||||
public GoodsListPage fetchGoodsList(Long mallId,
|
||||
String accessToken,
|
||||
int page,
|
||||
int pageSize,
|
||||
Integer isOnsale) throws Exception {
|
||||
return invokeSupport.invoke(PddPopApiTypes.GOODS_LIST_GET, mallId, client -> {
|
||||
PddGoodsListGetRequest request = new PddGoodsListGetRequest();
|
||||
request.setPage(Math.max(page, 1));
|
||||
request.setPageSize(Math.min(Math.max(pageSize, 1), 100));
|
||||
if (isOnsale != null) {
|
||||
request.setIsOnsale(isOnsale);
|
||||
}
|
||||
PddGoodsListGetResponse response = client.syncInvoke(request, accessToken);
|
||||
invokeSupport.assertNoError(response, PddPopApiTypes.GOODS_LIST_GET);
|
||||
return mapList(response);
|
||||
});
|
||||
}
|
||||
|
||||
private GoodsListPage mapList(PddGoodsListGetResponse response) {
|
||||
if (response == null || response.getGoodsListGetResponse() == null) {
|
||||
return new GoodsListPage(0, List.of());
|
||||
}
|
||||
var body = response.getGoodsListGetResponse();
|
||||
int total = body.getTotalCount() != null ? body.getTotalCount() : 0;
|
||||
List<GoodsListItem> items = new ArrayList<>();
|
||||
if (body.getGoodsList() != null) {
|
||||
for (var g : body.getGoodsList()) {
|
||||
if (g == null || g.getGoodsId() == null) {
|
||||
continue;
|
||||
}
|
||||
List<GoodsListSku> skus = new ArrayList<>();
|
||||
if (g.getSkuList() != null) {
|
||||
for (var s : g.getSkuList()) {
|
||||
if (s == null || s.getSkuId() == null) {
|
||||
continue;
|
||||
}
|
||||
skus.add(new GoodsListSku(
|
||||
s.getSkuId(),
|
||||
s.getSpec(),
|
||||
s.getSkuQuantity(),
|
||||
s.getReserveQuantity(),
|
||||
s.getIsSkuOnsale(),
|
||||
s.getOuterGoodsId(),
|
||||
s.getOuterId(),
|
||||
toJsonQuietly(s.getSpecDetails())));
|
||||
}
|
||||
}
|
||||
items.add(new GoodsListItem(
|
||||
g.getGoodsId(),
|
||||
g.getGoodsName(),
|
||||
g.getGoodsQuantity(),
|
||||
g.getGoodsReserveQuantity(),
|
||||
g.getImageUrl(),
|
||||
g.getThumbUrl(),
|
||||
g.getIsMoreSku(),
|
||||
g.getIsOnsale(),
|
||||
g.getCreatedAt(),
|
||||
Collections.unmodifiableList(skus),
|
||||
toJsonQuietly(g)));
|
||||
}
|
||||
}
|
||||
return new GoodsListPage(total, Collections.unmodifiableList(items));
|
||||
}
|
||||
|
||||
private GoodsDetail mapInformation(PddGoodsInformationGetResponse response) {
|
||||
if (response == null || response.getGoodsInfoGetResponse() == null
|
||||
|| response.getGoodsInfoGetResponse().getGoodsInfo() == null) {
|
||||
return null;
|
||||
@@ -65,6 +139,46 @@ public class PddGoodsPopApiClient {
|
||||
Collections.unmodifiableList(skus));
|
||||
}
|
||||
|
||||
private String toJsonQuietly(Object value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return objectMapper.writeValueAsString(value);
|
||||
} catch (Exception e) {
|
||||
log.warn("序列化拼多多商品字段失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public record GoodsListPage(int totalCount, List<GoodsListItem> goodsList) {
|
||||
}
|
||||
|
||||
public record GoodsListItem(
|
||||
Long goodsId,
|
||||
String goodsName,
|
||||
Long goodsQuantity,
|
||||
Long goodsReserveQuantity,
|
||||
String imageUrl,
|
||||
String thumbUrl,
|
||||
Integer isMoreSku,
|
||||
Integer isOnsale,
|
||||
Long createdAtEpochSec,
|
||||
List<GoodsListSku> skuList,
|
||||
String rawJson) {
|
||||
}
|
||||
|
||||
public record GoodsListSku(
|
||||
Long skuId,
|
||||
String spec,
|
||||
Long skuQuantity,
|
||||
Long reserveQuantity,
|
||||
Integer isSkuOnsale,
|
||||
String outerGoodsId,
|
||||
String outerId,
|
||||
String specDetailsJson) {
|
||||
}
|
||||
|
||||
public record GoodsDetail(
|
||||
Long goodsId,
|
||||
String goodsName,
|
||||
|
||||
+1
-1
@@ -143,7 +143,7 @@ public class PddShopServiceImpl extends ServiceImpl<PddShopMapper, PddShopPO> im
|
||||
IPage<PddShopPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
IPage<PddShopPO> result = this.page(page, wrapper);
|
||||
|
||||
List<PddShopVO> voList = toBriefVoList(result.getRecords());
|
||||
List<PddShopVO> voList = toVoList(result.getRecords(), true);
|
||||
|
||||
return PageResultVO.create(voList, result.getTotal(),
|
||||
(int) pageQueryDTO.getPage(), (int) pageQueryDTO.getPageSize());
|
||||
|
||||
Reference in New Issue
Block a user