diff --git a/ecom-dmb-backend/sql/design/channel/channel_pdd_good_design.sql b/ecom-dmb-backend/sql/design/channel/channel_pdd_good_design.sql index 6458e077..f084b5dc 100644 --- a/ecom-dmb-backend/sql/design/channel/channel_pdd_good_design.sql +++ b/ecom-dmb-backend/sql/design/channel/channel_pdd_good_design.sql @@ -26,6 +26,7 @@ CREATE TABLE `channel_pdd_goods` ( `thumb_url` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品缩略图(thumb_url)', `is_more_sku` tinyint NOT NULL DEFAULT 0 COMMENT '是否多SKU 0否 1是(is_more_sku)', `is_onsale` tinyint NOT NULL DEFAULT 0 COMMENT '是否在架 0否 1是(is_onsale)', + `is_platform_deleted` tinyint NOT NULL DEFAULT 0 COMMENT '平台是否已删除 0否 1是(消息软标,不物理删)', `pdd_created_at` datetime NULL DEFAULT NULL COMMENT '拼多多创建时间(created_at)', `raw_response` json NULL COMMENT '商品完整 JSON(含未结构化扩展字段)', `last_sync_time` datetime NOT NULL COMMENT '最近一次同步时间', @@ -36,6 +37,7 @@ CREATE TABLE `channel_pdd_goods` ( INDEX `idx_channel_pdd_goods_company_mall`(`company_id` ASC, `mall_id` ASC) USING BTREE, INDEX `idx_channel_pdd_goods_mall_goods`(`mall_id` ASC, `goods_id` ASC) USING BTREE, INDEX `idx_channel_pdd_goods_onsale`(`is_onsale` ASC) USING BTREE, + INDEX `idx_channel_pdd_goods_platform_deleted`(`company_id` ASC, `is_platform_deleted` ASC) USING BTREE, INDEX `idx_channel_pdd_goods_sync`(`last_sync_time` ASC) USING BTREE, CONSTRAINT `fk_channel_pdd_goods_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `fk_channel_pdd_goods_shop` FOREIGN KEY (`shop_id`) REFERENCES `channel_pdd_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE diff --git a/ecom-dmb-backend/sql/design/channel/channel_pdd_msg_design.sql b/ecom-dmb-backend/sql/design/channel/channel_pdd_msg_design.sql new file mode 100644 index 00000000..40beb104 --- /dev/null +++ b/ecom-dmb-backend/sql/design/channel/channel_pdd_msg_design.sql @@ -0,0 +1,24 @@ +-- ======================================== +-- 拼多多消息入站(WS → 幂等落库 → 内部 MQ) +-- ======================================== +DROP TABLE IF EXISTS `channel_pdd_msg_inbox`; +CREATE TABLE `channel_pdd_msg_inbox` ( + `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', + `platform_msg_id` bigint UNSIGNED NOT NULL COMMENT '幂等键(SDK 不暴露 ServerMessage.id,用 type+mall+content 指纹)', + `msg_type` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '消息 type', + `mall_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '拼多多 mall_id', + `company_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '解析后的公司ID', + `channel_shop_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '解析后的渠道店ID', + `goods_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '业务键:商品ID', + `content_json` json NULL COMMENT '消息 content 原文', + `status` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'RECEIVED' COMMENT 'RECEIVED/QUEUED/SUCCESS/FAILED/IGNORED', + `fail_reason` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '失败原因', + `retry_count` int NOT NULL DEFAULT 0 COMMENT '业务处理重试次数', + `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入库时间', + `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`id`) USING BTREE, + UNIQUE KEY `uk_channel_pdd_msg_inbox_platform` (`platform_msg_id`) USING BTREE, + KEY `idx_channel_pdd_msg_inbox_status` (`status`, `id`) USING BTREE, + KEY `idx_channel_pdd_msg_inbox_mall_type` (`mall_id`, `msg_type`) USING BTREE, + KEY `idx_channel_pdd_msg_inbox_shop` (`channel_shop_id`, `id`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='拼多多消息入站幂等表' ROW_FORMAT=Dynamic; diff --git a/ecom-dmb-backend/sql/migration/V20260905__pdd_msg_inbox_and_goods_platform_deleted.sql b/ecom-dmb-backend/sql/migration/V20260905__pdd_msg_inbox_and_goods_platform_deleted.sql new file mode 100644 index 00000000..dd34721c --- /dev/null +++ b/ecom-dmb-backend/sql/migration/V20260905__pdd_msg_inbox_and_goods_platform_deleted.sql @@ -0,0 +1,27 @@ +-- 拼多多商品:平台删除标记(消息 Delete 软标,不物理删渠道行) +ALTER TABLE `channel_pdd_goods` + ADD COLUMN `is_platform_deleted` tinyint NOT NULL DEFAULT 0 + COMMENT '平台是否已删除 0否 1是' AFTER `is_onsale`, + ADD INDEX `idx_channel_pdd_goods_platform_deleted` (`company_id`, `is_platform_deleted`); + +-- 拼多多消息入站幂等表 +CREATE TABLE IF NOT EXISTS `channel_pdd_msg_inbox` ( + `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', + `platform_msg_id` bigint UNSIGNED NOT NULL COMMENT '幂等键(type+mall+content 指纹;SDK 不暴露 ServerMessage.id)', + `msg_type` varchar(128) NOT NULL COMMENT '消息 type,如 pdd_goods_GoodsUpdate', + `mall_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '拼多多 mall_id', + `company_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '解析后的公司ID', + `channel_shop_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '解析后的渠道店ID', + `goods_id` varchar(64) NULL DEFAULT NULL COMMENT '业务键:商品ID(可空)', + `content_json` json NULL COMMENT '消息 content 原文', + `status` varchar(32) NOT NULL DEFAULT 'RECEIVED' COMMENT 'RECEIVED/QUEUED/SUCCESS/FAILED/IGNORED', + `fail_reason` varchar(1000) NULL DEFAULT NULL COMMENT '失败原因', + `retry_count` int NOT NULL DEFAULT 0 COMMENT '业务处理重试次数', + `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入库时间', + `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`id`), + UNIQUE KEY `uk_channel_pdd_msg_inbox_platform` (`platform_msg_id`), + KEY `idx_channel_pdd_msg_inbox_status` (`status`, `id`), + KEY `idx_channel_pdd_msg_inbox_mall_type` (`mall_id`, `msg_type`), + KEY `idx_channel_pdd_msg_inbox_shop` (`channel_shop_id`, `id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='拼多多消息入站幂等表'; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/auth/service/impl/PddAuthServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/auth/service/impl/PddAuthServiceImpl.java index 025dbad2..3d61431b 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/auth/service/impl/PddAuthServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/auth/service/impl/PddAuthServiceImpl.java @@ -20,6 +20,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.service.PddAuthService import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.service.PddAuthTokenService; import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.support.PddOAuthTokenExchange; import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.PddAuthFailureException; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.pop.PddPmcPermitService; import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.mall.PddMallApiSnapshot; import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.mall.PddMallPopApiClient; import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper; @@ -55,6 +56,7 @@ public class PddAuthServiceImpl implements PddAuthService { private final PddShopMapper pddShopMapper; private final ShopMapper shopMapper; private final ObjectMapper objectMapper; + private final PddPmcPermitService pmcPermitService; public PddAuthServiceImpl(PddOAuthProperties properties, PddOAuthTokenExchange tokenExchange, @@ -65,7 +67,8 @@ public class PddAuthServiceImpl implements PddAuthService { PddAuthMapper pddAuthMapper, PddShopMapper pddShopMapper, ShopMapper shopMapper, - ObjectMapper objectMapper) { + ObjectMapper objectMapper, + PddPmcPermitService pmcPermitService) { this.properties = properties; this.tokenExchange = tokenExchange; this.authTokenService = authTokenService; @@ -76,6 +79,7 @@ public class PddAuthServiceImpl implements PddAuthService { this.pddShopMapper = pddShopMapper; this.shopMapper = shopMapper; this.objectMapper = objectMapper; + this.pmcPermitService = pmcPermitService; } /** @@ -188,6 +192,9 @@ public class PddAuthServiceImpl implements PddAuthService { } bindInternalShop(companyId, userId, shopId, pddShop.getId(), mallInfo); + // 开通商品 PMC 订阅(失败不影响授权结果) + pmcPermitService.permitAfterAuthBestEffort(pddShop, tokenBundle.getAccessToken()); + String shopName = firstNonBlank( mallInfo != null ? mallInfo.getMallName() : null, tokenBundle.getOwnerName(), diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/dto/PddGoodsFilterDTO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/dto/PddGoodsFilterDTO.java index 27da55ce..a011f16b 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/dto/PddGoodsFilterDTO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/dto/PddGoodsFilterDTO.java @@ -16,4 +16,9 @@ public class PddGoodsFilterDTO { private Integer isOnsale; private Integer isMoreSku; + + /** + * 是否包含平台已删除商品;默认 null/false 表示列表排除已删除。 + */ + private Boolean includePlatformDeleted; } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/po/PddGoodsPO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/po/PddGoodsPO.java index bf22cd8a..c9b50d2c 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/po/PddGoodsPO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/po/PddGoodsPO.java @@ -47,6 +47,10 @@ public class PddGoodsPO { @TableField("is_onsale") private Integer isOnsale; + /** 平台是否已删除:0否 1是(消息软标,不物理删) */ + @TableField("is_platform_deleted") + private Integer isPlatformDeleted; + @TableField("pdd_created_at") private LocalDateTime pddCreatedAt; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/vo/PddGoodsVO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/vo/PddGoodsVO.java index 09016c75..c0c58b5a 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/vo/PddGoodsVO.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/model/vo/PddGoodsVO.java @@ -32,6 +32,9 @@ public class PddGoodsVO { private Integer isOnsale; + /** 平台是否已删除 0否 1是 */ + private Integer isPlatformDeleted; + private LocalDateTime pddCreatedAt; private LocalDateTime lastSyncTime; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/PddGoodsService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/PddGoodsService.java index f5ecbf56..140f8e13 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/PddGoodsService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/PddGoodsService.java @@ -29,4 +29,10 @@ public interface PddGoodsService extends IService { */ void upsertFromGoodsDetail(Long companyId, Long pddShopId, Long mallId, PddGoodsPopApiClient.GoodsDetail detail); + + /** 消息:标记平台已删除(不物理删)。 */ + void markPlatformDeleted(Long companyId, Long channelShopId, String goodsId); + + /** 消息:仅更新本地在架标志。 */ + void updateOnsaleFlag(Long companyId, Long channelShopId, String goodsId, int isOnsale); } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/impl/PddGoodsServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/impl/PddGoodsServiceImpl.java index 0c89e0a6..16aae7cb 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/impl/PddGoodsServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/goods/service/impl/PddGoodsServiceImpl.java @@ -38,6 +38,7 @@ import java.time.ZoneId; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -102,6 +103,12 @@ public class PddGoodsServiceImpl extends ServiceImpl if (filter.getIsMoreSku() != null) { wrapper.eq(PddGoodsPO::getIsMoreSku, filter.getIsMoreSku()); } + // 默认排除平台已删除;显式 includePlatformDeleted=true 才展示 + if (!Boolean.TRUE.equals(filter.getIncludePlatformDeleted())) { + wrapper.and(w -> w.eq(PddGoodsPO::getIsPlatformDeleted, 0) + .or() + .isNull(PddGoodsPO::getIsPlatformDeleted)); + } wrapper.orderByDesc(PddGoodsPO::getIsOnsale) .orderByDesc(PddGoodsPO::getPddCreatedAt); @@ -267,6 +274,8 @@ public class PddGoodsServiceImpl extends ServiceImpl if (goods.getIsOnsale() == null) { goods.setIsOnsale(0); } + // 详情 upsert 视为平台商品仍存在,清除删除标记 + goods.setIsPlatformDeleted(0); goods.setLastSyncTime(syncTime); if (created) { @@ -299,6 +308,57 @@ public class PddGoodsServiceImpl extends ServiceImpl } } + @Override + @Transactional(rollbackFor = Exception.class) + public void markPlatformDeleted(Long companyId, Long channelShopId, String goodsId) { + if (companyId == null || channelShopId == null || !StringUtils.hasText(goodsId)) { + return; + } + PddGoodsPO goods = this.getOne(new LambdaQueryWrapper() + .eq(PddGoodsPO::getCompanyId, companyId) + .eq(PddGoodsPO::getShopId, channelShopId) + .eq(PddGoodsPO::getGoodsId, goodsId.trim()) + .last("LIMIT 1")); + if (goods == null) { + log.info("平台删除消息:本地无渠道商品,跳过 companyId={} shopId={} goodsId={}", + companyId, channelShopId, goodsId); + return; + } + if (Integer.valueOf(1).equals(goods.getIsPlatformDeleted())) { + return; + } + goods.setIsPlatformDeleted(1); + this.updateById(goods); + log.info("已标记平台删除 channelGoodsId={} goodsId={}", goods.getId(), goodsId); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateOnsaleFlag(Long companyId, Long channelShopId, String goodsId, int isOnsale) { + if (companyId == null || channelShopId == null || !StringUtils.hasText(goodsId)) { + return; + } + int flag = isOnsale == 1 ? 1 : 0; + PddGoodsPO goods = this.getOne(new LambdaQueryWrapper() + .eq(PddGoodsPO::getCompanyId, companyId) + .eq(PddGoodsPO::getShopId, channelShopId) + .eq(PddGoodsPO::getGoodsId, goodsId.trim()) + .last("LIMIT 1")); + if (goods == null) { + log.info("上下架消息:本地无渠道商品,跳过 companyId={} shopId={} goodsId={}", + companyId, channelShopId, goodsId); + return; + } + if (Integer.valueOf(1).equals(goods.getIsPlatformDeleted())) { + log.info("上下架消息:商品已平台删除,仍更新在架标志 goodsId={} isOnsale={}", goodsId, flag); + } + if (Objects.equals(goods.getIsOnsale(), flag)) { + return; + } + goods.setIsOnsale(flag); + this.updateById(goods); + } + private UpsertResult upsertGoodsItem(Long companyId, Long pddShopId, Long mallId, @@ -323,6 +383,7 @@ public class PddGoodsServiceImpl extends ServiceImpl goods.setThumbUrl(blankToNull(item.thumbUrl())); goods.setIsMoreSku(item.isMoreSku() != null ? item.isMoreSku() : 0); goods.setIsOnsale(item.isOnsale() != null ? item.isOnsale() : 0); + goods.setIsPlatformDeleted(0); goods.setPddCreatedAt(epochSecToLocalDateTime(item.createdAtEpochSec())); goods.setRawResponse(item.rawJson()); goods.setLastSyncTime(syncTime); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgMqConstants.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgMqConstants.java new file mode 100644 index 00000000..873439d1 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgMqConstants.java @@ -0,0 +1,19 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config; + +/** + * 拼多多消息内部 MQ 常量。 + */ +public final class PddMsgMqConstants { + + public static final String EXCHANGE = "ecom.order.topic"; + + public static final String RK_PROCESS = "pdd.msg.process"; + public static final String QUEUE_PROCESS = "pdd.msg.process"; + public static final String QUEUE_PROCESS_DLQ = "pdd.msg.process.dlq"; + + public static final int BUSY_MAX_RETRY = 20; + public static final long BUSY_DELAY_MS = 10_000L; + + private PddMsgMqConstants() { + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgProperties.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgProperties.java new file mode 100644 index 00000000..3bde2473 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgProperties.java @@ -0,0 +1,34 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * 拼多多消息(PMC WebSocket)配置。 + */ +@Data +@Component +@ConfigurationProperties(prefix = "pdd.msg") +public class PddMsgProperties { + + /** 是否启用 WS 客户端(多实例时建议仅一台开启) */ + private boolean enabled = false; + + /** 授权成功后是否自动 pmc.user.permit */ + private boolean autoPermit = true; + + /** + * 为店铺开通的消息 topics;空表示不传 topics(继承应用级订阅)。 + */ + private List goodsTopics = new ArrayList<>(List.of( + "pdd_goods_GoodsAdd", + "pdd_goods_GoodsUpdate", + "pdd_goods_GoodsOnShelf", + "pdd_goods_GoodsOffShelf", + "pdd_goods_GoodsDelete" + )); +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgRabbitConfig.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgRabbitConfig.java new file mode 100644 index 00000000..a1e0d2af --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/config/PddMsgRabbitConfig.java @@ -0,0 +1,36 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config; + +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.QueueBuilder; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 拼多多消息处理队列,复用订单 TopicExchange。 + */ +@Configuration +public class PddMsgRabbitConfig { + + @Bean + public Queue pddMsgProcessQueue() { + return QueueBuilder.durable(PddMsgMqConstants.QUEUE_PROCESS) + .withArgument("x-dead-letter-exchange", "") + .withArgument("x-dead-letter-routing-key", PddMsgMqConstants.QUEUE_PROCESS_DLQ) + .build(); + } + + @Bean + public Queue pddMsgProcessDlq() { + return QueueBuilder.durable(PddMsgMqConstants.QUEUE_PROCESS_DLQ).build(); + } + + @Bean + public Binding pddMsgProcessBinding(Queue pddMsgProcessQueue, TopicExchange orderTopicExchange) { + return BindingBuilder.bind(pddMsgProcessQueue) + .to(orderTopicExchange) + .with(PddMsgMqConstants.RK_PROCESS); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgDispatcher.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgDispatcher.java new file mode 100644 index 00000000..7c514d45 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgDispatcher.java @@ -0,0 +1,54 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 按 msg_type 分发到具体 Handler。 + */ +@Slf4j +@Component +public class PddMsgDispatcher { + + private final Map handlersByType; + + public PddMsgDispatcher(List handlers) { + Map map = new HashMap<>(); + if (handlers != null) { + for (PddMsgHandler handler : handlers) { + if (handler == null || !StringUtils.hasText(handler.supportType())) { + continue; + } + PddMsgHandler prev = map.put(handler.supportType(), handler); + if (prev != null) { + log.warn("重复注册拼多多消息 Handler type={} keep={}", + handler.supportType(), handler.getClass().getSimpleName()); + } + } + } + this.handlersByType = Map.copyOf(map); + log.info("拼多多消息 Handler 已注册 count={} types={}", + handlersByType.size(), handlersByType.keySet()); + } + + /** + * @return true 已处理;false 无 Handler(调用方应标记 IGNORED) + */ + public boolean dispatch(PddMsgInboxPO inbox) throws Exception { + if (inbox == null || !StringUtils.hasText(inbox.getMsgType())) { + return false; + } + PddMsgHandler handler = handlersByType.get(inbox.getMsgType()); + if (handler == null) { + return false; + } + handler.handle(inbox); + return true; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgHandler.java new file mode 100644 index 00000000..87f7acb1 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/dispatch/PddMsgHandler.java @@ -0,0 +1,14 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch; + +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +/** + * 拼多多消息业务 Handler(按 msg_type 分发)。 + */ +public interface PddMsgHandler { + + /** 支持的消息 type,如 pdd_goods_GoodsUpdate */ + String supportType(); + + void handle(PddMsgInboxPO inbox) throws Exception; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/AbstractPddGoodsUpsertMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/AbstractPddGoodsUpsertMsgHandler.java new file mode 100644 index 00000000..91ee785b --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/AbstractPddGoodsUpsertMsgHandler.java @@ -0,0 +1,62 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.model.po.PddAuthPO; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.service.PddAuthTokenService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgHandler; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; +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; + +/** + * 商品新建 / 更新:拉详情 upsert 渠道镜像(不自动建内部品)。 + */ +@Slf4j +@RequiredArgsConstructor +abstract class AbstractPddGoodsUpsertMsgHandler implements PddMsgHandler { + + private final PddGoodsService pddGoodsService; + private final PddGoodsPopApiClient goodsPopApiClient; + private final PddAuthTokenService authTokenService; + private final PddShopMapper pddShopMapper; + + @Override + public void handle(PddMsgInboxPO inbox) throws Exception { + requireShopBound(inbox); + String goodsId = inbox.getGoodsId(); + if (!StringUtils.hasText(goodsId)) { + throw new IllegalArgumentException("goods_id 为空 type=" + supportType()); + } + long goodsIdLong; + try { + goodsIdLong = Long.parseLong(goodsId.trim()); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("非法 goods_id=" + goodsId); + } + PddShopPO shop = pddShopMapper.selectById(inbox.getChannelShopId()); + if (shop == null || shop.getPddAuthId() == null) { + throw new IllegalStateException("渠道店不存在或未授权 shopId=" + inbox.getChannelShopId()); + } + PddAuthPO auth = authTokenService.ensureUsableById(shop.getPddAuthId()); + PddGoodsPopApiClient.GoodsDetail detail = goodsPopApiClient.fetchGoodsInformation( + shop.getMallId(), auth.getAccessToken(), goodsIdLong); + if (detail == null || detail.goodsId() == null) { + throw new IllegalStateException("拉取商品详情为空 goodsId=" + goodsId); + } + pddGoodsService.upsertFromGoodsDetail( + inbox.getCompanyId(), shop.getId(), shop.getMallId(), detail); + log.info("消息 upsert 渠道商品 type={} goodsId={} shopId={}", + supportType(), goodsId, shop.getId()); + } + + protected void requireShopBound(PddMsgInboxPO inbox) { + if (inbox.getCompanyId() == null || inbox.getChannelShopId() == null) { + throw new IllegalStateException("消息未绑定店铺 type=" + supportType() + + " inboxId=" + inbox.getId()); + } + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsAddMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsAddMsgHandler.java new file mode 100644 index 00000000..df8e2941 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsAddMsgHandler.java @@ -0,0 +1,26 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +import org.springframework.stereotype.Component; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.service.PddAuthTokenService; +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; + +/** 商品新建消息。 */ +@Component +public class PddGoodsAddMsgHandler extends AbstractPddGoodsUpsertMsgHandler { + + public static final String TYPE = "pdd_goods_GoodsAdd"; + + public PddGoodsAddMsgHandler(PddGoodsService pddGoodsService, + PddGoodsPopApiClient goodsPopApiClient, + PddAuthTokenService authTokenService, + PddShopMapper pddShopMapper) { + super(pddGoodsService, goodsPopApiClient, authTokenService, pddShopMapper); + } + + @Override + public String supportType() { + return TYPE; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsDeleteMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsDeleteMsgHandler.java new file mode 100644 index 00000000..6ebd4280 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsDeleteMsgHandler.java @@ -0,0 +1,37 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +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.goods.service.PddGoodsService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgHandler; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +/** 商品删除:软标 is_platform_deleted=1,不物理删。 */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddGoodsDeleteMsgHandler implements PddMsgHandler { + + public static final String TYPE = "pdd_goods_GoodsDelete"; + + private final PddGoodsService pddGoodsService; + + @Override + public String supportType() { + return TYPE; + } + + @Override + public void handle(PddMsgInboxPO inbox) { + if (inbox.getCompanyId() == null || inbox.getChannelShopId() == null + || !StringUtils.hasText(inbox.getGoodsId())) { + throw new IllegalStateException("删除消息缺店铺或 goods_id inboxId=" + inbox.getId()); + } + pddGoodsService.markPlatformDeleted( + inbox.getCompanyId(), inbox.getChannelShopId(), inbox.getGoodsId()); + log.info("消息平台删除标记 goodsId={} shopId={}", + inbox.getGoodsId(), inbox.getChannelShopId()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOffShelfMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOffShelfMsgHandler.java new file mode 100644 index 00000000..b73bfbe3 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOffShelfMsgHandler.java @@ -0,0 +1,36 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +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.goods.service.PddGoodsService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgHandler; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +/** 商品下架:仅改本地 is_onsale=0。 */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddGoodsOffShelfMsgHandler implements PddMsgHandler { + + public static final String TYPE = "pdd_goods_GoodsOffShelf"; + + private final PddGoodsService pddGoodsService; + + @Override + public String supportType() { + return TYPE; + } + + @Override + public void handle(PddMsgInboxPO inbox) { + if (inbox.getCompanyId() == null || inbox.getChannelShopId() == null + || !StringUtils.hasText(inbox.getGoodsId())) { + throw new IllegalStateException("下架消息缺店铺或 goods_id inboxId=" + inbox.getId()); + } + pddGoodsService.updateOnsaleFlag( + inbox.getCompanyId(), inbox.getChannelShopId(), inbox.getGoodsId(), 0); + log.info("消息下架 goodsId={} shopId={}", inbox.getGoodsId(), inbox.getChannelShopId()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOnShelfMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOnShelfMsgHandler.java new file mode 100644 index 00000000..c61f499c --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsOnShelfMsgHandler.java @@ -0,0 +1,36 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +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.goods.service.PddGoodsService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgHandler; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +/** 商品上架:仅改本地 is_onsale=1。 */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddGoodsOnShelfMsgHandler implements PddMsgHandler { + + public static final String TYPE = "pdd_goods_GoodsOnShelf"; + + private final PddGoodsService pddGoodsService; + + @Override + public String supportType() { + return TYPE; + } + + @Override + public void handle(PddMsgInboxPO inbox) { + if (inbox.getCompanyId() == null || inbox.getChannelShopId() == null + || !StringUtils.hasText(inbox.getGoodsId())) { + throw new IllegalStateException("上架消息缺店铺或 goods_id inboxId=" + inbox.getId()); + } + pddGoodsService.updateOnsaleFlag( + inbox.getCompanyId(), inbox.getChannelShopId(), inbox.getGoodsId(), 1); + log.info("消息上架 goodsId={} shopId={}", inbox.getGoodsId(), inbox.getChannelShopId()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsUpdateMsgHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsUpdateMsgHandler.java new file mode 100644 index 00000000..e2458d7e --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/handler/goods/PddGoodsUpdateMsgHandler.java @@ -0,0 +1,26 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.handler.goods; + +import org.springframework.stereotype.Component; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.service.PddAuthTokenService; +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; + +/** 商品更新消息。 */ +@Component +public class PddGoodsUpdateMsgHandler extends AbstractPddGoodsUpsertMsgHandler { + + public static final String TYPE = "pdd_goods_GoodsUpdate"; + + public PddGoodsUpdateMsgHandler(PddGoodsService pddGoodsService, + PddGoodsPopApiClient goodsPopApiClient, + PddAuthTokenService authTokenService, + PddShopMapper pddShopMapper) { + super(pddGoodsService, goodsPopApiClient, authTokenService, pddShopMapper); + } + + @Override + public String supportType() { + return TYPE; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/mapper/PddMsgInboxMapper.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/mapper/PddMsgInboxMapper.java new file mode 100644 index 00000000..b8755807 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/mapper/PddMsgInboxMapper.java @@ -0,0 +1,9 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +@Mapper +public interface PddMsgInboxMapper extends BaseMapper { +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/model/po/PddMsgInboxPO.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/model/po/PddMsgInboxPO.java new file mode 100644 index 00000000..1e4acc24 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/model/po/PddMsgInboxPO.java @@ -0,0 +1,63 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.time.LocalDateTime; + +/** + * 拼多多消息入站幂等行。 + */ +@Data +@TableName("channel_pdd_msg_inbox") +public class PddMsgInboxPO { + + public static final String STATUS_RECEIVED = "RECEIVED"; + public static final String STATUS_QUEUED = "QUEUED"; + public static final String STATUS_SUCCESS = "SUCCESS"; + public static final String STATUS_FAILED = "FAILED"; + public static final String STATUS_IGNORED = "IGNORED"; + + @TableId(value = "id", type = IdType.AUTO) + private Long id; + + @TableField("platform_msg_id") + private Long platformMsgId; + + @TableField("msg_type") + private String msgType; + + @TableField("mall_id") + private Long mallId; + + @TableField("company_id") + private Long companyId; + + @TableField("channel_shop_id") + private Long channelShopId; + + @TableField("goods_id") + private String goodsId; + + @TableField("content_json") + private String contentJson; + + @TableField("status") + private String status; + + @TableField("fail_reason") + private String failReason; + + @TableField("retry_count") + private Integer retryCount; + + @TableField(value = "create_time", fill = FieldFill.INSERT) + private LocalDateTime createTime; + + @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) + private LocalDateTime updateTime; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/PddMsgInboxService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/PddMsgInboxService.java new file mode 100644 index 00000000..3a062f69 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/PddMsgInboxService.java @@ -0,0 +1,26 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.service; + +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; + +/** + * 拼多多消息入站幂等服务。 + */ +public interface PddMsgInboxService { + + /** + * 幂等写入;已存在则返回已有行(不覆盖成功态)。 + * + * @return 入库行;若已 SUCCESS/IGNORED 则后续应跳过投递 + */ + PddMsgInboxPO receiveOrGet(PddMsgInboxPO draft); + + void markQueued(Long inboxId); + + void markSuccess(Long inboxId); + + void markIgnored(Long inboxId, String reason); + + void markFailed(Long inboxId, String reason, int retryCount); + + PddMsgInboxPO getById(Long inboxId); +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/impl/PddMsgInboxServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/impl/PddMsgInboxServiceImpl.java new file mode 100644 index 00000000..e733ce80 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/inbox/service/impl/PddMsgInboxServiceImpl.java @@ -0,0 +1,110 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.mapper.PddMsgInboxMapper; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.service.PddMsgInboxService; + +/** + * 拼多多消息入站幂等实现。 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class PddMsgInboxServiceImpl implements PddMsgInboxService { + + private final PddMsgInboxMapper inboxMapper; + + @Override + @Transactional(rollbackFor = Exception.class) + public PddMsgInboxPO receiveOrGet(PddMsgInboxPO draft) { + if (draft == null || draft.getPlatformMsgId() == null) { + throw new IllegalArgumentException("platformMsgId required"); + } + PddMsgInboxPO existing = findByPlatformMsgId(draft.getPlatformMsgId()); + if (existing != null) { + return existing; + } + if (!StringUtils.hasText(draft.getStatus())) { + draft.setStatus(PddMsgInboxPO.STATUS_RECEIVED); + } + if (draft.getRetryCount() == null) { + draft.setRetryCount(0); + } + try { + inboxMapper.insert(draft); + return draft; + } catch (DuplicateKeyException e) { + PddMsgInboxPO raced = findByPlatformMsgId(draft.getPlatformMsgId()); + if (raced != null) { + return raced; + } + throw e; + } + } + + @Override + public void markQueued(Long inboxId) { + updateStatus(inboxId, PddMsgInboxPO.STATUS_QUEUED, null, null); + } + + @Override + public void markSuccess(Long inboxId) { + updateStatus(inboxId, PddMsgInboxPO.STATUS_SUCCESS, null, null); + } + + @Override + public void markIgnored(Long inboxId, String reason) { + updateStatus(inboxId, PddMsgInboxPO.STATUS_IGNORED, truncate(reason), null); + } + + @Override + public void markFailed(Long inboxId, String reason, int retryCount) { + updateStatus(inboxId, PddMsgInboxPO.STATUS_FAILED, truncate(reason), retryCount); + } + + @Override + public PddMsgInboxPO getById(Long inboxId) { + if (inboxId == null) { + return null; + } + return inboxMapper.selectById(inboxId); + } + + private PddMsgInboxPO findByPlatformMsgId(Long platformMsgId) { + return inboxMapper.selectOne(new LambdaQueryWrapper() + .eq(PddMsgInboxPO::getPlatformMsgId, platformMsgId) + .last("LIMIT 1")); + } + + private void updateStatus(Long inboxId, String status, String failReason, Integer retryCount) { + if (inboxId == null) { + return; + } + PddMsgInboxPO po = inboxMapper.selectById(inboxId); + if (po == null) { + return; + } + po.setStatus(status); + if (failReason != null) { + po.setFailReason(failReason); + } + if (retryCount != null) { + po.setRetryCount(retryCount); + } + inboxMapper.updateById(po); + } + + private static String truncate(String reason) { + if (reason == null) { + return null; + } + return reason.length() > 1000 ? reason.substring(0, 1000) : reason; + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcMessageIngressHandler.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcMessageIngressHandler.java new file mode 100644 index 00000000..1942b7db --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcMessageIngressHandler.java @@ -0,0 +1,81 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.infra; + +import com.fasterxml.jackson.databind.JsonNode; +import com.pdd.pop.sdk.message.MessageHandler; +import com.pdd.pop.sdk.message.model.Message; +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.msg.inbox.model.po.PddMsgInboxPO; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.service.PddMsgInboxService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.messaging.PddMsgProcessPublisher; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.support.PddMsgContentSupport; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.support.PddMsgShopResolver; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO; + +/** + * PMC WS 入站:幂等写 inbox + 投递内部 MQ;抛错则 SDK Fail ACK 触发平台重投。 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddPmcMessageIngressHandler implements MessageHandler { + + private final PddMsgContentSupport contentSupport; + private final PddMsgShopResolver shopResolver; + private final PddMsgInboxService inboxService; + private final PddMsgProcessPublisher processPublisher; + + @Override + public void onMessage(Message message) throws Exception { + if (message == null || !StringUtils.hasText(message.getType())) { + log.warn("忽略空拼多多消息"); + return; + } + String msgType = message.getType().trim(); + String content = message.getContent(); + JsonNode contentNode = contentSupport.parseContent(content); + Long mallId = contentSupport.extractMallId(contentNode, message.getMallID()); + String goodsId = contentSupport.extractGoodsId(contentNode); + long platformMsgId = contentSupport.fingerprint(msgType, mallId, content); + + PddShopPO shop = shopResolver.resolveByMallId(mallId); + + PddMsgInboxPO draft = new PddMsgInboxPO(); + draft.setPlatformMsgId(platformMsgId); + draft.setMsgType(msgType); + draft.setMallId(mallId); + draft.setGoodsId(goodsId); + draft.setContentJson(content); + draft.setStatus(PddMsgInboxPO.STATUS_RECEIVED); + draft.setRetryCount(0); + if (shop != null) { + draft.setCompanyId(shop.getCompanyId()); + draft.setChannelShopId(shop.getId()); + } + + PddMsgInboxPO inbox = inboxService.receiveOrGet(draft); + if (PddMsgInboxPO.STATUS_SUCCESS.equals(inbox.getStatus()) + || PddMsgInboxPO.STATUS_IGNORED.equals(inbox.getStatus())) { + log.debug("拼多多消息已终态,跳过投递 inboxId={} status={}", + inbox.getId(), inbox.getStatus()); + return; + } + if (PddMsgInboxPO.STATUS_QUEUED.equals(inbox.getStatus())) { + // 已投递过:ACK 成功即可,避免重复塞队列(消费端幂等兜底) + return; + } + if (shop == null) { + inboxService.markIgnored(inbox.getId(), "未找到 mall_id=" + mallId + " 对应渠道店"); + log.info("拼多多消息无本地店铺,已忽略 type={} mallId={} inboxId={}", + msgType, mallId, inbox.getId()); + return; + } + + processPublisher.publish(inbox.getId()); + inboxService.markQueued(inbox.getId()); + log.info("拼多多消息已入站 type={} mallId={} goodsId={} inboxId={}", + msgType, mallId, goodsId, inbox.getId()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcWsClientLifecycle.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcWsClientLifecycle.java new file mode 100644 index 00000000..6f972b91 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/infra/PddPmcWsClientLifecycle.java @@ -0,0 +1,69 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.infra; + +import com.pdd.pop.sdk.message.WsClient; +import jakarta.annotation.PreDestroy; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.auth.config.PddOAuthProperties; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config.PddMsgProperties; + +/** + * 拼多多 PMC WebSocket 生命周期(默认关闭;多实例仅一台 enabled=true)。 + */ +@Slf4j +@Component +@ConditionalOnProperty(prefix = "pdd.msg", name = "enabled", havingValue = "true") +public class PddPmcWsClientLifecycle implements ApplicationRunner { + + private final PddOAuthProperties oauthProperties; + private final PddMsgProperties msgProperties; + private final PddPmcMessageIngressHandler ingressHandler; + + private volatile WsClient wsClient; + + public PddPmcWsClientLifecycle(PddOAuthProperties oauthProperties, + PddMsgProperties msgProperties, + PddPmcMessageIngressHandler ingressHandler) { + this.oauthProperties = oauthProperties; + this.msgProperties = msgProperties; + this.ingressHandler = ingressHandler; + } + + @Override + public void run(ApplicationArguments args) { + if (!msgProperties.isEnabled()) { + return; + } + String clientId = oauthProperties.getClientId(); + String clientSecret = oauthProperties.getClientSecret(); + if (!StringUtils.hasText(clientId) || !StringUtils.hasText(clientSecret)) { + log.error("pdd.msg.enabled=true 但 oauth clientId/secret 为空,跳过 WS 连接"); + return; + } + try { + WsClient client = new WsClient(clientId, clientSecret, ingressHandler); + client.connect(); + this.wsClient = client; + log.info("拼多多 PMC WebSocket 已连接 clientId={}", clientId); + } catch (Exception e) { + log.error("拼多多 PMC WebSocket 连接失败", e); + } + } + + @PreDestroy + public void destroy() { + WsClient client = this.wsClient; + if (client != null) { + try { + client.close(); + log.info("拼多多 PMC WebSocket 已关闭"); + } catch (Exception e) { + log.warn("关闭 PMC WebSocket 失败: {}", e.getMessage()); + } + } + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessConsumer.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessConsumer.java new file mode 100644 index 00000000..899eb733 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessConsumer.java @@ -0,0 +1,74 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.messaging; + +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.modules.channel.pdd.msg.config.PddMsgMqConstants; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgDispatcher; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.service.PddMsgInboxService; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.PddPopRateLimitedException; + +/** + * 拼多多消息业务消费:分发 Handler;限流延迟重投。 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddMsgProcessConsumer { + + private final PddMsgInboxService inboxService; + private final PddMsgDispatcher dispatcher; + private final DelayedMqRepublisher delayedMqRepublisher; + + @RabbitListener(queues = PddMsgMqConstants.QUEUE_PROCESS, concurrency = "1-2") + public void onMessage(PddMsgProcessMessage message) { + if (message == null || message.getInboxId() == null) { + log.warn("拼多多消息处理报文非法: {}", message); + return; + } + Long inboxId = message.getInboxId(); + PddMsgInboxPO inbox = inboxService.getById(inboxId); + if (inbox == null) { + log.warn("inbox 不存在 inboxId={}", inboxId); + return; + } + if (PddMsgInboxPO.STATUS_SUCCESS.equals(inbox.getStatus()) + || PddMsgInboxPO.STATUS_IGNORED.equals(inbox.getStatus())) { + return; + } + try { + boolean handled = dispatcher.dispatch(inbox); + if (!handled) { + inboxService.markIgnored(inboxId, "无对应 Handler: " + inbox.getMsgType()); + return; + } + inboxService.markSuccess(inboxId); + } catch (PddPopRateLimitedException e) { + delayAndRepublish(message, e.getMessage()); + } catch (Exception e) { + log.error("拼多多消息处理失败 inboxId={} type={}", inboxId, inbox.getMsgType(), e); + delayAndRepublish(message, e.getMessage()); + } + } + + private void delayAndRepublish(PddMsgProcessMessage message, String reason) { + int retry = message.getRetryCount(); + if (retry >= PddMsgMqConstants.BUSY_MAX_RETRY) { + inboxService.markFailed(message.getInboxId(), reason, retry); + log.error("拼多多消息处理重试超限 inboxId={} reason={}", message.getInboxId(), reason); + return; + } + message.setRetryCount(retry + 1); + inboxService.markFailed(message.getInboxId(), reason, message.getRetryCount()); + delayedMqRepublisher.republish( + PddMsgMqConstants.EXCHANGE, + PddMsgMqConstants.RK_PROCESS, + message, + PddMsgMqConstants.BUSY_DELAY_MS); + log.info("拼多多消息已延迟重投 inboxId={} retry={}", + message.getInboxId(), message.getRetryCount()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessMessage.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessMessage.java new file mode 100644 index 00000000..918e3acf --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessMessage.java @@ -0,0 +1,20 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.messaging; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +/** + * 拼多多消息内部处理投递体(按 inbox 主键)。 + */ +@Data +public class PddMsgProcessMessage implements Serializable { + + @Serial + private static final long serialVersionUID = 1L; + + private Long inboxId; + + private int retryCount; +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessPublisher.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessPublisher.java new file mode 100644 index 00000000..b381e34d --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/messaging/PddMsgProcessPublisher.java @@ -0,0 +1,36 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.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.modules.channel.pdd.msg.config.PddMsgMqConstants; + +/** + * 拼多多消息处理投递。 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddMsgProcessPublisher { + + private final RabbitTemplate rabbitTemplate; + + public void publish(Long inboxId) { + publish(inboxId, 0); + } + + public void publish(Long inboxId, int retryCount) { + if (inboxId == null) { + return; + } + PddMsgProcessMessage message = new PddMsgProcessMessage(); + message.setInboxId(inboxId); + message.setRetryCount(Math.max(0, retryCount)); + rabbitTemplate.convertAndSend( + PddMsgMqConstants.EXCHANGE, + PddMsgMqConstants.RK_PROCESS, + message); + log.debug("已投递拼多多消息处理 inboxId={} retry={}", inboxId, retryCount); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcPermitService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcPermitService.java new file mode 100644 index 00000000..2d78623d --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcPermitService.java @@ -0,0 +1,42 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.pop; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config.PddMsgProperties; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO; + +/** + * 授权成功后为店铺开通 PMC 消息订阅。 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class PddPmcPermitService { + + private final PddMsgProperties msgProperties; + private final PddPmcUserPermitPopApiClient permitPopApiClient; + + /** + * 尽力开通;失败只打日志,不影响授权主流程。 + */ + public void permitAfterAuthBestEffort(PddShopPO pddShop, String accessToken) { + if (!msgProperties.isAutoPermit()) { + return; + } + if (pddShop == null || pddShop.getMallId() == null || !StringUtils.hasText(accessToken)) { + return; + } + try { + boolean ok = permitPopApiClient.permitConfiguredTopics(pddShop.getMallId(), accessToken); + if (!ok) { + log.warn("pmc.user.permit 返回未成功 mallId={} shopId={}", + pddShop.getMallId(), pddShop.getId()); + } + } catch (Exception e) { + log.warn("pmc.user.permit 失败 mallId={} shopId={}: {}", + pddShop.getMallId(), pddShop.getId(), e.getMessage()); + } + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcUserPermitPopApiClient.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcUserPermitPopApiClient.java new file mode 100644 index 00000000..75aa50c7 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/pop/PddPmcUserPermitPopApiClient.java @@ -0,0 +1,66 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.pop; + +import com.pdd.pop.sdk.http.api.pop.request.PddPmcUserPermitRequest; +import com.pdd.pop.sdk.http.api.pop.response.PddPmcUserPermitResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.config.PddMsgProperties; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.PddPopApiTypes; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.support.PddPopInvokeSupport; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * 拼多多 PMC 用户消息订阅开通(pdd.pmc.user.permit)。 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddPmcUserPermitPopApiClient { + + private final PddPopInvokeSupport invokeSupport; + private final PddMsgProperties msgProperties; + + /** + * 为店铺开通消息 topics(空列表则不传 topics,继承应用级订阅)。 + */ + public boolean permit(Long mallId, String accessToken, List topics) throws Exception { + if (!StringUtils.hasText(accessToken)) { + throw new IllegalArgumentException("accessToken 为空"); + } + String topicsParam = null; + if (!CollectionUtils.isEmpty(topics)) { + topicsParam = topics.stream() + .filter(StringUtils::hasText) + .map(String::trim) + .distinct() + .collect(Collectors.joining(",")); + if (!StringUtils.hasText(topicsParam)) { + topicsParam = null; + } + } + final String topicsFinal = topicsParam; + return invokeSupport.invoke(PddPopApiTypes.PMC_USER_PERMIT, mallId, client -> { + PddPmcUserPermitRequest request = new PddPmcUserPermitRequest(); + if (topicsFinal != null) { + request.setTopics(topicsFinal); + } + PddPmcUserPermitResponse response = client.syncInvoke(request, accessToken); + invokeSupport.assertNoError(response, PddPopApiTypes.PMC_USER_PERMIT); + Boolean ok = response.getPmcUserPermitResponse() == null + ? null + : response.getPmcUserPermitResponse().getIsSuccess(); + log.info("pmc.user.permit mallId={} topics={} success={}", mallId, topicsFinal, ok); + return Boolean.TRUE.equals(ok); + }); + } + + /** 使用配置中的 goodsTopics。 */ + public boolean permitConfiguredTopics(Long mallId, String accessToken) throws Exception { + return permit(mallId, accessToken, msgProperties.getGoodsTopics()); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgContentSupport.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgContentSupport.java new file mode 100644 index 00000000..4d599947 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgContentSupport.java @@ -0,0 +1,87 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.support; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * 拼多多消息 content 解析与幂等指纹(SDK MessageHandler 不暴露 ServerMessage.id)。 + */ +@Component +public class PddMsgContentSupport { + + private final ObjectMapper objectMapper; + + public PddMsgContentSupport(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + /** + * 基于 type+mall+content 生成稳定 long,作为 inbox 幂等键。 + */ + public long fingerprint(String msgType, Long mallId, String content) { + String raw = (msgType == null ? "" : msgType) + + "|" + + (mallId == null ? "" : mallId) + + "|" + + (content == null ? "" : content); + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest(raw.getBytes(StandardCharsets.UTF_8)); + long value = 0L; + for (int i = 0; i < 8; i++) { + value = (value << 8) | (digest[i] & 0xffL); + } + // 清符号位,保证 JDBC 写入 MySQL UNSIGNED 时不为负 + value = value & Long.MAX_VALUE; + return value == 0L ? 1L : value; + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SHA-256 unavailable", e); + } + } + + public JsonNode parseContent(String content) { + if (!StringUtils.hasText(content)) { + return objectMapper.nullNode(); + } + try { + return objectMapper.readTree(content); + } catch (Exception e) { + return objectMapper.nullNode(); + } + } + + public String extractGoodsId(JsonNode content) { + if (content == null || content.isNull()) { + return null; + } + JsonNode node = content.get("goods_id"); + if (node == null || node.isNull()) { + return null; + } + if (node.isNumber()) { + return String.valueOf(node.asLong()); + } + String text = node.asText(null); + return StringUtils.hasText(text) ? text.trim() : null; + } + + public Long extractMallId(JsonNode content, Long messageMallId) { + if (messageMallId != null) { + return messageMallId; + } + if (content == null || content.isNull()) { + return null; + } + JsonNode node = content.get("mall_id"); + if (node == null || node.isNull() || !node.isNumber()) { + return null; + } + return node.asLong(); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgShopResolver.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgShopResolver.java new file mode 100644 index 00000000..d7a0de56 --- /dev/null +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/msg/support/PddMsgShopResolver.java @@ -0,0 +1,39 @@ +package top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.support; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper; +import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO; + +/** + * 按 mall_id 解析渠道店铺(消息入站用)。 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class PddMsgShopResolver { + + private final PddShopMapper pddShopMapper; + + /** + * mall_id 全局可能对应多公司;取最近更新的一条,并打 warn。 + */ + public PddShopPO resolveByMallId(Long mallId) { + if (mallId == null) { + return null; + } + java.util.List shops = pddShopMapper.selectList(new LambdaQueryWrapper() + .eq(PddShopPO::getMallId, mallId) + .orderByDesc(PddShopPO::getUpdateTime) + .last("LIMIT 2")); + if (shops == null || shops.isEmpty()) { + return null; + } + if (shops.size() > 1) { + log.warn("mall_id={} 匹配到多个渠道店,使用 id={}", mallId, shops.get(0).getId()); + } + return shops.get(0); + } +} diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/pop/PddPopApiTypes.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/pop/PddPopApiTypes.java index 429ca4bd..0081c68a 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/pop/PddPopApiTypes.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/pop/PddPopApiTypes.java @@ -27,4 +27,7 @@ public final class PddPopApiTypes { // ---------- 店铺 ---------- public static final String MALL_INFO_GET = "pdd.mall.info.get"; + + // ---------- 消息订阅 ---------- + public static final String PMC_USER_PERMIT = "pdd.pmc.user.permit"; } diff --git a/ecom-dmb-backend/src/main/resources/config/channel-pdd.yml b/ecom-dmb-backend/src/main/resources/config/channel-pdd.yml index 324f10cf..ade110f4 100644 --- a/ecom-dmb-backend/src/main/resources/config/channel-pdd.yml +++ b/ecom-dmb-backend/src/main/resources/config/channel-pdd.yml @@ -96,6 +96,22 @@ pdd: shop-window-seconds: 60 app-burst-limit: 800 app-burst-window-seconds: 10 + pdd.pmc.user.permit: + shop-limit: 600 + shop-window-seconds: 60 + app-burst-limit: 200 + app-burst-window-seconds: 10 + + # PMC 消息:WS 默认关闭(多实例仅一台开启);授权后自动 permit 商品 topics + msg: + enabled: true + auto-permit: true + goods-topics: + - pdd_goods_GoodsAdd + - pdd_goods_GoodsUpdate + - pdd_goods_GoodsOnShelf + - pdd_goods_GoodsOffShelf + - pdd_goods_GoodsDelete order-sync: overlap-seconds: 120 diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/channel/pdd/goods/types/index.ts b/ecom-dmb-frontend/apps/web-antd/src/api/channel/pdd/goods/types/index.ts index 7cd7d704..8b408999 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/channel/pdd/goods/types/index.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/channel/pdd/goods/types/index.ts @@ -42,6 +42,8 @@ export namespace PddGoodsVO { thumbUrl?: string; isMoreSku?: number; isOnsale?: number; + /** 平台是否已删除 0否 1是 */ + isPlatformDeleted?: number; pddCreatedAt?: string; lastSyncTime?: string; createTime?: string; diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsDetailDrawer.vue b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsDetailDrawer.vue index 4227bb1b..dfaf7ca3 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsDetailDrawer.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsDetailDrawer.vue @@ -124,6 +124,12 @@ defineExpose({ open, close }); {{ detail.goodsName }}
+ + 平台已删 + {{ detail.isOnsale === 1 ? '在架' : '下架' }} diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/index.vue index 53afe63d..12375afc 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/index.vue @@ -348,9 +348,20 @@ onMounted(async () => {