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:
+5
@@ -41,6 +41,11 @@ public final class OrderMqConstants {
|
||||
public static final String QUEUE_BI_STAT_INC = "bi.stat.inc";
|
||||
public static final String QUEUE_BI_STAT_INC_DLQ = "bi.stat.inc.dlq";
|
||||
|
||||
/** 转换未绑定时异步拉商品详情并自动建品/回绑 */
|
||||
public static final String RK_ORDER_PRODUCT_AUTO_CREATE_PDD = "order.product.auto-create.pdd";
|
||||
public static final String QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD = "order.product.auto-create.pdd";
|
||||
public static final String QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD_DLQ = "order.product.auto-create.pdd.dlq";
|
||||
|
||||
private OrderMqConstants() {
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -104,6 +104,27 @@ public class RabbitBiConfig {
|
||||
.with(OrderMqConstants.RK_ORDER_CONVERT_PDD);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue orderProductAutoCreatePddQueue() {
|
||||
return QueueBuilder.durable(OrderMqConstants.QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD)
|
||||
.withArgument("x-dead-letter-exchange", "")
|
||||
.withArgument("x-dead-letter-routing-key", OrderMqConstants.QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD_DLQ)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue orderProductAutoCreatePddDlq() {
|
||||
return QueueBuilder.durable(OrderMqConstants.QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD_DLQ).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Binding orderProductAutoCreatePddBinding(Queue orderProductAutoCreatePddQueue,
|
||||
TopicExchange orderTopicExchange) {
|
||||
return BindingBuilder.bind(orderProductAutoCreatePddQueue)
|
||||
.to(orderTopicExchange)
|
||||
.with(OrderMqConstants.RK_ORDER_PRODUCT_AUTO_CREATE_PDD);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Queue biStatFullQueue() {
|
||||
return QueueBuilder.durable(OrderMqConstants.QUEUE_BI_STAT_FULL)
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.consumer;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.common.mq.DelayedMqRepublisher;
|
||||
import top.xyquan.ecomdmbbackend.common.mq.OrderMqConstants;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.config.PddOrderConvertProperties;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging.PddProductAutoCreateMessage;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.service.PddProductAutoCreateService;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.PddPopRateLimitedException;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PddProductAutoCreateConsumer {
|
||||
|
||||
private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final PddProductAutoCreateService autoCreateService;
|
||||
private final PddOrderConvertProperties convertProperties;
|
||||
private final DelayedMqRepublisher delayedMqRepublisher;
|
||||
|
||||
@RabbitListener(queues = OrderMqConstants.QUEUE_ORDER_PRODUCT_AUTO_CREATE_PDD, concurrency = "1-2")
|
||||
public void onMessage(PddProductAutoCreateMessage message) {
|
||||
if (message == null || message.getCompanyId() == null
|
||||
|| message.getShopId() == null || message.getGoodsId() == null) {
|
||||
log.warn("自动建品消息非法: {}", message);
|
||||
return;
|
||||
}
|
||||
log.info("收到自动建品消息 companyId={} shopId={} goodsId={} messageId={}",
|
||||
message.getCompanyId(), message.getShopId(), message.getGoodsId(), message.getMessageId());
|
||||
try {
|
||||
autoCreateService.createFromGoods(message);
|
||||
} catch (PddPopRateLimitedException e) {
|
||||
delayAndRepublish(message, e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("自动建品失败 goodsId={}", message.getGoodsId(), e);
|
||||
delayAndRepublish(message, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void delayAndRepublish(PddProductAutoCreateMessage message, String reason) {
|
||||
int retry = message.getRetryCount() == null ? 0 : message.getRetryCount();
|
||||
if (retry >= convertProperties.getBusyMaxRetry()) {
|
||||
throw new IllegalStateException("自动建品重试超限 goodsId=" + message.getGoodsId()
|
||||
+ " reason=" + reason);
|
||||
}
|
||||
message.setRetryCount(retry + 1);
|
||||
message.setOccurredAt(OffsetDateTime.now(ZONE));
|
||||
delayedMqRepublisher.republish(
|
||||
OrderMqConstants.EXCHANGE,
|
||||
OrderMqConstants.RK_ORDER_PRODUCT_AUTO_CREATE_PDD,
|
||||
message,
|
||||
convertProperties.getBusyDelayMs());
|
||||
log.info("自动建品已延迟重投 goodsId={} retry={} reason={}",
|
||||
message.getGoodsId(), message.getRetryCount(), reason);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
/**
|
||||
* 转换未绑定时触发:按平台 goodsId 拉详情并自动建内部商品/SKU,再回绑 orphan 明细。
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PddProductAutoCreateMessage {
|
||||
|
||||
private String messageId;
|
||||
private Long companyId;
|
||||
private Long shopId;
|
||||
/** 拼多多商品 ID */
|
||||
private Long goodsId;
|
||||
private OffsetDateTime occurredAt;
|
||||
private Integer retryCount;
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.common.mq.OrderMqConstants;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PddProductAutoCreatePublisher {
|
||||
|
||||
private static final ZoneId ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
public void publish(Long companyId, Long shopId, Long goodsId) {
|
||||
if (companyId == null || shopId == null || goodsId == null) {
|
||||
return;
|
||||
}
|
||||
PddProductAutoCreateMessage msg = PddProductAutoCreateMessage.builder()
|
||||
.messageId(UUID.randomUUID().toString())
|
||||
.companyId(companyId)
|
||||
.shopId(shopId)
|
||||
.goodsId(goodsId)
|
||||
.occurredAt(OffsetDateTime.now(ZONE))
|
||||
.retryCount(0)
|
||||
.build();
|
||||
rabbitTemplate.convertAndSend(
|
||||
OrderMqConstants.EXCHANGE, OrderMqConstants.RK_ORDER_PRODUCT_AUTO_CREATE_PDD, msg);
|
||||
log.info("已投递自动建品消息 companyId={} shopId={} goodsId={}", companyId, shopId, goodsId);
|
||||
}
|
||||
}
|
||||
+45
-19
@@ -17,6 +17,8 @@ import top.xyquan.ecomdmbbackend.modules.bi.messaging.BiSkuStatDelta;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.messaging.BiStatMetricDelta;
|
||||
import top.xyquan.ecomdmbbackend.modules.bi.orderstat.support.OrderStatContributionCalculator;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging.PddBiCalcPublisher;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging.PddProductAutoCreatePublisher;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.support.PddChannelSkuCodeResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.support.PddOrderStatusMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.mapper.PddOrderItemMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.mapper.PddOrderMapper;
|
||||
@@ -64,6 +66,7 @@ public class PddOrderConvertService {
|
||||
private final ProductSkuService productSkuService;
|
||||
private final ProductSkuCostService productSkuCostService;
|
||||
private final PddBiCalcPublisher biCalcPublisher;
|
||||
private final PddProductAutoCreatePublisher productAutoCreatePublisher;
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
private final OrderStatContributionCalculator contributionCalculator;
|
||||
private final OrderStatusStatResolver orderStatusStatResolver;
|
||||
@@ -74,23 +77,34 @@ public class PddOrderConvertService {
|
||||
def.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
TransactionTemplate tt = new TransactionTemplate(transactionManager, def);
|
||||
// 必须等转换事务提交后再投递 BI full,否则消费者可能读到未提交快照(漏单)
|
||||
Set<LocalDate> biDates = tt.execute(status -> convertFullDay(message));
|
||||
if (biDates != null) {
|
||||
for (LocalDate d : biDates) {
|
||||
// 必须等转换事务提交后再投递 BI / 自动建品,否则消费者可能读到未提交快照
|
||||
ConvertBatchResult result = tt.execute(status -> convertFullDay(message));
|
||||
if (result != null) {
|
||||
for (LocalDate d : result.biDates()) {
|
||||
biCalcPublisher.publishFull(
|
||||
message.getCompanyId(), message.getShopId(), d, message.getSyncBatchId());
|
||||
}
|
||||
publishAutoCreate(message.getCompanyId(), message.getShopId(), result.pendingGoodsIds());
|
||||
}
|
||||
return;
|
||||
}
|
||||
convertInc(message);
|
||||
Set<Long> pendingGoodsIds = convertInc(message);
|
||||
publishAutoCreate(message.getCompanyId(), message.getShopId(), pendingGoodsIds);
|
||||
}
|
||||
|
||||
private void publishAutoCreate(Long companyId, Long shopId, Set<Long> goodsIds) {
|
||||
if (goodsIds == null || goodsIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (Long goodsId : goodsIds) {
|
||||
productAutoCreatePublisher.publish(companyId, shopId, goodsId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅做库写入;返回需全量重算的 BI 统计日(成交日 + 本批实际转换订单 order_time 涉及日)。
|
||||
* 仅做库写入;返回需全量重算的 BI 统计日,以及未绑定需异步建品的 goodsId。
|
||||
*/
|
||||
private Set<LocalDate> convertFullDay(OrderConvertMessage message) {
|
||||
private ConvertBatchResult convertFullDay(OrderConvertMessage message) {
|
||||
List<PddOrderPO> channelOrders = loadChannelOrders(message);
|
||||
LocalDateTime mappedAt = LocalDateTime.now(ZONE);
|
||||
Set<LocalDate> biDates = new TreeSet<>();
|
||||
@@ -112,17 +126,18 @@ public class PddOrderConvertService {
|
||||
if (biDates.isEmpty()) {
|
||||
biDates.add(LocalDate.now(ZONE));
|
||||
}
|
||||
return biDates;
|
||||
return new ConvertBatchResult(biDates, bindCtx.pendingGoodsIds());
|
||||
}
|
||||
|
||||
/**
|
||||
* 增量:消息带渠道镜像主键列表;按 id 查渠道单。
|
||||
* 内部单用 platform_code + source_order_id 定位:无则新增;有则仅当渠道更新时间更新时覆盖,并产出 BI delta。
|
||||
* @return 未绑定需异步建品的 goodsId
|
||||
*/
|
||||
private void convertInc(OrderConvertMessage message) {
|
||||
private Set<Long> convertInc(OrderConvertMessage message) {
|
||||
List<ChannelOrderRef> refs = message.getOrders() != null ? message.getOrders() : List.of();
|
||||
if (refs.isEmpty()) {
|
||||
return;
|
||||
return Set.of();
|
||||
}
|
||||
OrderStatusStatSets stat = orderStatusStatResolver.resolve(message.getCompanyId());
|
||||
LocalDateTime mappedAt = LocalDateTime.now(ZONE);
|
||||
@@ -173,6 +188,7 @@ public class PddOrderConvertService {
|
||||
OffsetDateTime internalOrderUpdateTime = internalUpdated.atZone(ZONE).toOffsetDateTime();
|
||||
emitIncDeltas(message, channel.getId(), neu.getId(), internalOrderUpdateTime, oldContrib, newContrib);
|
||||
}
|
||||
return bindCtx.pendingGoodsIds();
|
||||
}
|
||||
|
||||
private OrderPO findInternalBySource(PddOrderPO channel) {
|
||||
@@ -394,9 +410,9 @@ public class PddOrderConvertService {
|
||||
|
||||
Long productId = null;
|
||||
Long skuId = null;
|
||||
String skuCode = null;
|
||||
if (StringUtils.hasText(ci.getOuterId())) {
|
||||
ProductSkuPO sku = bindCtx.skuByCode().get(ci.getOuterId().trim());
|
||||
String skuCode = PddChannelSkuCodeResolver.resolveSkuCode(ci);
|
||||
if (StringUtils.hasText(skuCode)) {
|
||||
ProductSkuPO sku = bindCtx.skuByCode().get(skuCode);
|
||||
if (sku != null) {
|
||||
productId = sku.getProductId();
|
||||
skuId = sku.getId();
|
||||
@@ -407,11 +423,16 @@ public class PddOrderConvertService {
|
||||
item.setShippingCost(cost.getShippingCost());
|
||||
item.setOtherCost(cost.getOtherCost());
|
||||
}
|
||||
} else {
|
||||
Long goodsId = PddChannelSkuCodeResolver.parseGoodsId(ci.getGoodsId());
|
||||
if (goodsId != null) {
|
||||
bindCtx.pendingGoodsIds().add(goodsId);
|
||||
}
|
||||
}
|
||||
}
|
||||
item.setProductId(productId);
|
||||
item.setProductSkuId(skuId);
|
||||
item.setSkuCode(skuCode != null ? skuCode : ci.getOuterId());
|
||||
item.setSkuCode(skuCode);
|
||||
if (item.getProductCost() == null) {
|
||||
item.setProductCost(BigDecimal.ZERO);
|
||||
}
|
||||
@@ -444,8 +465,9 @@ public class PddOrderConvertService {
|
||||
|
||||
Set<String> codes = new HashSet<>();
|
||||
for (PddOrderItemPO item : allItems) {
|
||||
if (StringUtils.hasText(item.getOuterId())) {
|
||||
codes.add(item.getOuterId().trim());
|
||||
String code = PddChannelSkuCodeResolver.resolveSkuCode(item);
|
||||
if (StringUtils.hasText(code)) {
|
||||
codes.add(code);
|
||||
}
|
||||
}
|
||||
Map<String, ProductSkuPO> skuByCode = productSkuService.listByCompanyIdAndSkuCodes(companyId, codes);
|
||||
@@ -454,16 +476,20 @@ public class PddOrderConvertService {
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
Map<Long, ProductSkuCostPO> costs = productSkuCostService.getCurrentCosts(skuIds);
|
||||
return new SkuBindContext(itemsByOrderId, skuByCode, costs);
|
||||
return new SkuBindContext(itemsByOrderId, skuByCode, costs, new HashSet<>());
|
||||
}
|
||||
|
||||
private record ConvertBatchResult(Set<LocalDate> biDates, Set<Long> pendingGoodsIds) {
|
||||
}
|
||||
|
||||
private record SkuBindContext(
|
||||
Map<Long, List<PddOrderItemPO>> itemsByOrderId,
|
||||
Map<String, ProductSkuPO> skuByCode,
|
||||
Map<Long, ProductSkuCostPO> costBySkuId
|
||||
Map<Long, ProductSkuCostPO> costBySkuId,
|
||||
Set<Long> pendingGoodsIds
|
||||
) {
|
||||
static SkuBindContext empty() {
|
||||
return new SkuBindContext(Map.of(), Map.of(), Map.of());
|
||||
return new SkuBindContext(Map.of(), Map.of(), Map.of(), new HashSet<>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging.PddProductAutoCreateMessage;
|
||||
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.pop.goods.PddGoodsPopApiClient;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
|
||||
/**
|
||||
* 按平台 goodsId 拉详情,自动创建内部商品 + 全部 SKU(已存在则跳过),并回绑 orphan 明细。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PddProductAutoCreateService {
|
||||
|
||||
private final PddOrderSyncContextResolver contextResolver;
|
||||
private final PddGoodsPopApiClient goodsPopApiClient;
|
||||
private final ProductService productService;
|
||||
private final ProductSkuService productSkuService;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void createFromGoods(PddProductAutoCreateMessage message) throws Exception {
|
||||
if (message == null || message.getCompanyId() == null
|
||||
|| message.getShopId() == null || message.getGoodsId() == null) {
|
||||
return;
|
||||
}
|
||||
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={}",
|
||||
message.getCompanyId(), message.getGoodsId());
|
||||
return;
|
||||
}
|
||||
|
||||
String productCode = detail.resolveProductCode();
|
||||
if (!StringUtils.hasText(productCode)) {
|
||||
log.warn("自动建品:无法解析商品编码 goodsId={}", detail.goodsId());
|
||||
return;
|
||||
}
|
||||
String productName = StringUtils.hasText(detail.goodsName())
|
||||
? detail.goodsName().trim()
|
||||
: productCode;
|
||||
|
||||
ProductPO product = ensureProduct(message.getCompanyId(), productCode, productName, detail.imageUrl());
|
||||
if (product == null || product.getId() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (detail.skuList() == null || detail.skuList().isEmpty()) {
|
||||
log.info("自动建品:商品无 SKU 列表 goodsId={}", detail.goodsId());
|
||||
return;
|
||||
}
|
||||
|
||||
int created = 0;
|
||||
int skipped = 0;
|
||||
for (PddGoodsPopApiClient.SkuDetail skuDetail : detail.skuList()) {
|
||||
if (skuDetail == null) {
|
||||
continue;
|
||||
}
|
||||
String skuCode = skuDetail.resolveSkuCode();
|
||||
if (!StringUtils.hasText(skuCode)) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
ProductSkuPO existing = productSkuService.getByCompanyIdAndSkuCode(message.getCompanyId(), skuCode);
|
||||
if (existing != null) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (createSku(message.getCompanyId(), product.getId(), skuCode, skuDetail, detail.imageUrl())) {
|
||||
created++;
|
||||
} else {
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
log.info("自动建品完成 companyId={} goodsId={} productId={} createdSku={} skipped={}",
|
||||
message.getCompanyId(), detail.goodsId(), product.getId(), created, skipped);
|
||||
}
|
||||
|
||||
private ProductPO ensureProduct(Long companyId, String productCode, String productName, String imageUrl) {
|
||||
ProductPO existing = productService.getByCompanyIdAndProductCode(companyId, productCode);
|
||||
if (existing != null) {
|
||||
if (!StringUtils.hasText(existing.getMainImageUrl()) && StringUtils.hasText(imageUrl)
|
||||
&& existing.getMainImageId() == null) {
|
||||
existing.setMainImageUrl(imageUrl.trim());
|
||||
productService.updateById(existing);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
ProductPO product = new ProductPO();
|
||||
product.setCompanyId(companyId);
|
||||
product.setProductCode(productCode);
|
||||
product.setName(productName);
|
||||
product.setMainImageUrl(StringUtils.hasText(imageUrl) ? imageUrl.trim() : null);
|
||||
try {
|
||||
productService.save(product);
|
||||
return product;
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
log.info("自动建品商品并发冲突,重查 productCode={}", productCode);
|
||||
return productService.getByCompanyIdAndProductCode(companyId, productCode);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean createSku(Long companyId, Long productId, String skuCode,
|
||||
PddGoodsPopApiClient.SkuDetail skuDetail, String fallbackImg) {
|
||||
ProductSkuPO sku = new ProductSkuPO();
|
||||
sku.setCompanyId(companyId);
|
||||
sku.setProductId(productId);
|
||||
sku.setSkuCode(skuCode);
|
||||
String name = StringUtils.hasText(skuDetail.spec()) ? skuDetail.spec().trim() : skuCode;
|
||||
sku.setName(name);
|
||||
String img = StringUtils.hasText(skuDetail.skuImg()) ? skuDetail.skuImg().trim() : null;
|
||||
if (!StringUtils.hasText(img) && StringUtils.hasText(fallbackImg)) {
|
||||
img = fallbackImg.trim();
|
||||
}
|
||||
sku.setImageUrl(img);
|
||||
sku.setStatus(1);
|
||||
sku.setSortOrder(0);
|
||||
sku.setRemark("channel_auto:pdd");
|
||||
try {
|
||||
// save 会触发 orphan 回绑
|
||||
return productSkuService.save(sku);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
log.info("自动建品 SKU 并发冲突,跳过 skuCode={}", skuCode);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.support;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.model.po.PddOrderItemPO;
|
||||
|
||||
/**
|
||||
* 转换/自动建品共用的商家编码解析。
|
||||
*/
|
||||
public final class PddChannelSkuCodeResolver {
|
||||
|
||||
private PddChannelSkuCodeResolver() {
|
||||
}
|
||||
|
||||
/** outer_id 优先;否则 PDD:{sku_id};都没有则 null */
|
||||
public static String resolveSkuCode(PddOrderItemPO item) {
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
if (StringUtils.hasText(item.getOuterId())) {
|
||||
return item.getOuterId().trim();
|
||||
}
|
||||
if (StringUtils.hasText(item.getSkuId())) {
|
||||
return "PDD:" + item.getSkuId().trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Long parseGoodsId(String goodsId) {
|
||||
if (!StringUtils.hasText(goodsId)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(goodsId.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -13,6 +13,9 @@ public final class PddPopApiTypes {
|
||||
public static final String ORDER_INCREMENT_GET = "pdd.order.number.list.increment.get";
|
||||
public static final String ORDER_INFORMATION_GET = "pdd.order.information.get";
|
||||
|
||||
// ---------- 商品 ----------
|
||||
public static final String GOODS_INFORMATION_GET = "pdd.goods.information.get";
|
||||
|
||||
// ---------- 店铺 ----------
|
||||
public static final String MALL_INFO_GET = "pdd.mall.info.get";
|
||||
}
|
||||
|
||||
+1
@@ -83,6 +83,7 @@ public class PddPopProperties {
|
||||
map.put(PddPopApiTypes.ORDER_LIST_GET, orderLike);
|
||||
map.put(PddPopApiTypes.ORDER_INCREMENT_GET, ApiRule.of(6000, 60, 800, 10));
|
||||
map.put(PddPopApiTypes.ORDER_INFORMATION_GET, ApiRule.of(6000, 60, 800, 10));
|
||||
map.put(PddPopApiTypes.GOODS_INFORMATION_GET, ApiRule.of(5000, 60, 800, 10));
|
||||
map.put(PddPopApiTypes.MALL_INFO_GET, ApiRule.of(6000, 60, 800, 10));
|
||||
return map;
|
||||
}
|
||||
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods;
|
||||
|
||||
import com.pdd.pop.sdk.http.api.pop.request.PddGoodsInformationGetRequest;
|
||||
import com.pdd.pop.sdk.http.api.pop.response.PddGoodsInformationGetResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.PddPopApiTypes;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.support.PddPopInvokeSupport;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品域 POP:{@value PddPopApiTypes#GOODS_INFORMATION_GET}。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class PddGoodsPopApiClient {
|
||||
|
||||
private final PddPopInvokeSupport invokeSupport;
|
||||
|
||||
public GoodsDetail fetchGoodsInformation(Long mallId, String accessToken, Long goodsId) throws Exception {
|
||||
if (goodsId == null) {
|
||||
return null;
|
||||
}
|
||||
return invokeSupport.invoke(PddPopApiTypes.GOODS_INFORMATION_GET, mallId, client -> {
|
||||
PddGoodsInformationGetRequest request = new PddGoodsInformationGetRequest();
|
||||
request.setGoodsId(goodsId);
|
||||
PddGoodsInformationGetResponse response = client.syncInvoke(request, accessToken);
|
||||
invokeSupport.assertNoError(response, PddPopApiTypes.GOODS_INFORMATION_GET);
|
||||
return map(response);
|
||||
});
|
||||
}
|
||||
|
||||
private static GoodsDetail map(PddGoodsInformationGetResponse response) {
|
||||
if (response == null || response.getGoodsInfoGetResponse() == null
|
||||
|| response.getGoodsInfoGetResponse().getGoodsInfo() == null) {
|
||||
return null;
|
||||
}
|
||||
var info = response.getGoodsInfoGetResponse().getGoodsInfo();
|
||||
List<SkuDetail> skus = new ArrayList<>();
|
||||
if (info.getSkuList() != null) {
|
||||
for (var item : info.getSkuList()) {
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
skus.add(new SkuDetail(
|
||||
item.getSkuId(),
|
||||
item.getSpec(),
|
||||
item.getOuterId(),
|
||||
item.getOuterGoodsId(),
|
||||
item.getSkuImg()));
|
||||
}
|
||||
}
|
||||
return new GoodsDetail(
|
||||
info.getGoodsId(),
|
||||
info.getGoodsName(),
|
||||
info.getImageUrl(),
|
||||
Collections.unmodifiableList(skus));
|
||||
}
|
||||
|
||||
public record GoodsDetail(Long goodsId, String goodsName, String imageUrl, List<SkuDetail> skuList) {
|
||||
public String resolveProductCode() {
|
||||
if (skuList != null) {
|
||||
for (SkuDetail sku : skuList) {
|
||||
if (sku != null && StringUtils.hasText(sku.outerGoodsId())) {
|
||||
return sku.outerGoodsId().trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
return goodsId != null ? "PDD:" + goodsId : null;
|
||||
}
|
||||
}
|
||||
|
||||
public record SkuDetail(Long skuId, String spec, String outerId, String outerGoodsId, String skuImg) {
|
||||
public String resolveSkuCode() {
|
||||
if (StringUtils.hasText(outerId)) {
|
||||
return outerId.trim();
|
||||
}
|
||||
if (skuId != null) {
|
||||
return "PDD:" + skuId;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -33,10 +33,13 @@ public interface OrderMapper extends BaseMapper<OrderPO> {
|
||||
@Param("startTime") LocalDateTime startTime,
|
||||
@Param("endTime") LocalDateTime endTime);
|
||||
|
||||
/** 新建 SKU 后挂 orphan 明细 */
|
||||
/** 新建 SKU 后挂 orphan 明细(同时补 product_id) */
|
||||
@Update("""
|
||||
UPDATE ec_order_item i
|
||||
SET i.product_sku_id = #{productSkuId}, i.update_time = NOW()
|
||||
INNER JOIN ec_product_sku s ON s.id = #{productSkuId}
|
||||
SET i.product_sku_id = #{productSkuId},
|
||||
i.product_id = s.product_id,
|
||||
i.update_time = NOW()
|
||||
WHERE i.company_id = #{companyId} AND i.sku_code = #{skuCode} AND i.product_sku_id IS NULL
|
||||
""")
|
||||
int linkOrdersBySkuCodeWhenSkuIdNull(@Param("companyId") Long companyId,
|
||||
|
||||
+7
-1
@@ -45,11 +45,17 @@ public class ProductPO {
|
||||
private String productCode;
|
||||
|
||||
/**
|
||||
* 主图图片ID
|
||||
* 主图图片ID(系统文件)
|
||||
*/
|
||||
@TableField("main_image_id")
|
||||
private Long mainImageId;
|
||||
|
||||
/**
|
||||
* 主图外链 URL(渠道自动建品等)
|
||||
*/
|
||||
@TableField("main_image_url")
|
||||
private String mainImageUrl;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
||||
+4
@@ -38,6 +38,10 @@ public class ProductSkuPO {
|
||||
@TableField("image_id")
|
||||
private Long imageId;
|
||||
|
||||
/** 渠道外链图;查询时若有 imageId 则用系统文件 URL 覆盖 */
|
||||
@TableField("image_url")
|
||||
private String imageUrl;
|
||||
|
||||
@TableField("status")
|
||||
private Integer status;
|
||||
|
||||
|
||||
+3
@@ -31,6 +31,9 @@ public class ProductSkuVO {
|
||||
@Schema(description = "SKU编码")
|
||||
private String skuCode;
|
||||
|
||||
@Schema(description = "SKU图URL(有系统图 id 时优先解析系统图)")
|
||||
private String imageUrl;
|
||||
|
||||
@Schema(description = "商品成本(当前生效)")
|
||||
private BigDecimal productCost;
|
||||
|
||||
|
||||
+9
-4
@@ -132,8 +132,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
public ProductPO getByCompanyIdAndProductCode(Long companyId, String productCode) {
|
||||
if (companyId == null || productCode == null || productCode.isBlank()) return null;
|
||||
LambdaQueryWrapper<ProductPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getProductCode, productCode.trim());
|
||||
return this.getOne(w);
|
||||
w.eq(ProductPO::getCompanyId, companyId).eq(ProductPO::getProductCode, productCode.trim())
|
||||
.last("LIMIT 1");
|
||||
return this.getOne(w, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -566,14 +567,18 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
ProductVO productVO = new ProductVO();
|
||||
BeanCopyUtils.copyProperties(product, productVO);
|
||||
|
||||
// 查询主图文件信息并生成访问URL
|
||||
// 查询主图:有系统文件 id 则覆盖;否则用渠道外链 url
|
||||
if (product.getMainImageId() != null) {
|
||||
SysFilePO mainImageFile = sysFileMapper.selectById(product.getMainImageId());
|
||||
if (mainImageFile != null && mainImageFile.getDeleteTime() == null) {
|
||||
String mainImageUrl = SysFileUtils.getFileAccessUrl(mainImageFile.getStoragePath(), mainImageFile.getStorageType());
|
||||
String mainImageUrl = SysFileUtils.getFileAccessUrl(
|
||||
mainImageFile.getStoragePath(), mainImageFile.getStorageType());
|
||||
productVO.setMainImage(mainImageUrl);
|
||||
}
|
||||
}
|
||||
if (productVO.getMainImage() == null || productVO.getMainImage().isBlank()) {
|
||||
productVO.setMainImage(product.getMainImageUrl());
|
||||
}
|
||||
|
||||
// 查询类目名称
|
||||
if (product.getCategoryId() != null) {
|
||||
|
||||
+17
-2
@@ -25,7 +25,10 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuCostService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.mapper.SysFileMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.file.model.po.SysFilePO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import top.xyquan.ecomdmbbackend.utils.SysFileUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collection;
|
||||
@@ -46,14 +49,16 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
private final ProductService productService;
|
||||
private final ProductSkuCostService productSkuCostService;
|
||||
private final OrderMapper orderMapper;
|
||||
private final SysFileMapper sysFileMapper;
|
||||
|
||||
public ProductSkuServiceImpl(CurrentUserContext currentUserContext, ProductService productService,
|
||||
ProductSkuCostService productSkuCostService,
|
||||
OrderMapper orderMapper) {
|
||||
ProductSkuCostService productSkuCostService, OrderMapper orderMapper,
|
||||
SysFileMapper sysFileMapper) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
this.productService = productService;
|
||||
this.productSkuCostService = productSkuCostService;
|
||||
this.orderMapper = orderMapper;
|
||||
this.sysFileMapper = sysFileMapper;
|
||||
}
|
||||
|
||||
/** SKU 落库后尝试挂 orphan 订单;订单表不存在时跳过 */
|
||||
@@ -431,6 +436,16 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
vo.setShippingCost(cost.getShippingCost());
|
||||
vo.setOtherCost(cost.getOtherCost());
|
||||
}
|
||||
// 有系统图 id 则覆盖外链
|
||||
if (sku.getImageId() != null) {
|
||||
SysFilePO file = sysFileMapper.selectById(sku.getImageId());
|
||||
if (file != null && file.getDeleteTime() == null) {
|
||||
vo.setImageUrl(SysFileUtils.getFileAccessUrl(file.getStoragePath(), file.getStorageType()));
|
||||
}
|
||||
}
|
||||
if (vo.getImageUrl() == null || vo.getImageUrl().isBlank()) {
|
||||
vo.setImageUrl(sku.getImageUrl());
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user