refactor: 迁移增量同步开关到渠道侧

This commit is contained in:
2026-08-21 12:17:07 +08:00
parent e030ca6e0a
commit b6891b16e3
12 changed files with 159 additions and 64 deletions
@@ -9,35 +9,26 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.messaging.PddOrd
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.service.PddOrderFullSyncTaskService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO;
import top.xyquan.ecomdmbbackend.modules.ec.platform.model.enums.PlatformCode;
import top.xyquan.ecomdmbbackend.modules.ec.shop.mapper.ShopMapper;
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 定时将开启增量开关且已绑定渠道店的店铺投入增量同步队列。
* 定时将开启增量开关且已绑定内部店的渠道店投入增量同步队列。
*/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "pdd.order-sync", name = "incremental-job-enabled", havingValue = "true", matchIfMissing = true)
public class PddOrderIncrementalSyncJob {
private final ShopMapper shopMapper;
private final PddShopMapper pddShopMapper;
private final PddOrderFullSyncTaskService fullSyncTaskService;
private final PddOrderSyncPublisher syncPublisher;
public PddOrderIncrementalSyncJob(ShopMapper shopMapper,
PddShopMapper pddShopMapper,
public PddOrderIncrementalSyncJob(PddShopMapper pddShopMapper,
PddOrderFullSyncTaskService fullSyncTaskService,
PddOrderSyncPublisher syncPublisher) {
this.shopMapper = shopMapper;
this.pddShopMapper = pddShopMapper;
this.fullSyncTaskService = fullSyncTaskService;
this.syncPublisher = syncPublisher;
@@ -45,33 +36,20 @@ public class PddOrderIncrementalSyncJob {
@Scheduled(cron = "${pdd.order-sync.incremental-cron:0 */5 * * * ?}")
public void run() {
List<ShopPO> shops = shopMapper.selectList(new LambdaQueryWrapper<ShopPO>()
.eq(ShopPO::getPlatformCode, PlatformCode.PDD.getCode())
.eq(ShopPO::getOrderIncSyncEnabled, 1));
if (shops.isEmpty()) {
return;
}
Map<Long, ShopPO> shopById = shops.stream()
.filter(s -> s.getId() != null)
.collect(Collectors.toMap(ShopPO::getId, Function.identity(), (a, b) -> a));
List<PddShopPO> bound = pddShopMapper.selectList(new LambdaQueryWrapper<PddShopPO>()
.in(PddShopPO::getEcShopId, shopById.keySet())
List<PddShopPO> shops = pddShopMapper.selectList(new LambdaQueryWrapper<PddShopPO>()
.eq(PddShopPO::getOrderIncSyncEnabled, 1)
.isNotNull(PddShopPO::getEcShopId));
Set<Long> done = new HashSet<>();
for (PddShopPO pddShop : bound) {
for (PddShopPO pddShop : shops) {
if (pddShop.getId() == null || !done.add(pddShop.getId())) {
continue;
}
ShopPO shop = shopById.get(pddShop.getEcShopId());
if (shop == null) {
continue;
}
if (fullSyncTaskService.isIncrementalPaused(pddShop.getId())) {
log.debug("跳过出错暂停的增量店铺 channelShopId={}", pddShop.getId());
continue;
}
try {
syncPublisher.publishInc(shop.getCompanyId(), pddShop.getId());
syncPublisher.publishInc(pddShop.getCompanyId(), pddShop.getId());
} catch (Exception e) {
log.warn("投递增量同步失败 channelShopId={}: {}", pddShop.getId(), e.getMessage());
}
@@ -268,16 +268,16 @@ public class PddOrderFullSyncTaskService {
.eq(ShopPO::getCompanyId, companyId)
.in(ShopPO::getId, distinctIds));
Set<Long> owned = shops.stream().map(ShopPO::getId).collect(Collectors.toCollection(HashSet::new));
Map<Long, ShopPO> shopMap = shops.stream()
.collect(Collectors.toMap(ShopPO::getId, s -> s, (a, b) -> a));
Map<Long, Long> ecToChannel = pddShopMapper.selectList(new LambdaQueryWrapper<PddShopPO>()
Map<Long, PddShopPO> channelByEc = pddShopMapper.selectList(new LambdaQueryWrapper<PddShopPO>()
.eq(PddShopPO::getCompanyId, companyId)
.in(PddShopPO::getEcShopId, owned)
.isNotNull(PddShopPO::getEcShopId))
.stream()
.filter(p -> p.getEcShopId() != null && p.getId() != null)
.collect(Collectors.toMap(PddShopPO::getEcShopId, PddShopPO::getId, (a, b) -> a));
.collect(Collectors.toMap(PddShopPO::getEcShopId, p -> p, (a, b) -> a));
Map<Long, Long> ecToChannel = channelByEc.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getId(), (a, b) -> a));
Set<Long> channelIds = new HashSet<>(ecToChannel.values());
Map<Long, PddOrderFullSyncTaskPO> latestByChannel = loadLatestTasksByShop(companyId, channelIds);
@@ -288,11 +288,12 @@ public class PddOrderFullSyncTaskService {
if (!owned.contains(shopId)) {
continue;
}
ShopPO shop = shopMap.get(shopId);
Long channelId = ecToChannel.get(shopId);
PddShopPO channelShop = channelByEc.get(shopId);
PddOrderFullSyncTaskPO full = channelId == null ? null : latestByChannel.get(channelId);
PddOrderIncSyncPO inc = channelId == null ? null : incByChannel.get(channelId);
boolean enabled = shop != null && Integer.valueOf(1).equals(shop.getOrderIncSyncEnabled());
boolean enabled = channelShop != null
&& Integer.valueOf(1).equals(channelShop.getOrderIncSyncEnabled());
result.add(PddOrderSyncShopStatusVO.builder()
.shopId(shopId)
.fullTask(full == null ? null : toVo(full, shopId))
@@ -305,13 +306,14 @@ public class PddOrderFullSyncTaskService {
@Transactional(rollbackFor = Exception.class)
public PddOrderIncrementalStatusVO toggleIncremental(PddOrderIncrementalToggleDTO dto) {
PddOrderSyncShopContext ctx = contextResolver.resolveForCurrentCompany(dto.getShopId());
ShopPO shop = shopMapper.selectById(ctx.getInternalShopId());
if (shop == null) {
throw new BusinessException("店铺不存在");
PddShopPO channelShop = ctx.getPddShop();
if (channelShop == null || channelShop.getId() == null) {
throw new BusinessException("店铺尚未绑定拼多多外部店");
}
boolean enabled = Boolean.TRUE.equals(dto.getEnabled());
shop.setOrderIncSyncEnabled(enabled ? 1 : 0);
shopMapper.updateById(shop);
pddShopMapper.update(null, new LambdaUpdateWrapper<PddShopPO>()
.eq(PddShopPO::getId, channelShop.getId())
.set(PddShopPO::getOrderIncSyncEnabled, enabled ? 1 : 0));
PddOrderIncSyncPO state = getOrCreateIncState(ctx);
if (enabled && Integer.valueOf(PddOrderIncSyncPO.STATUS_PAUSED).equals(state.getStatus())) {
@@ -324,15 +326,22 @@ public class PddOrderFullSyncTaskService {
public PddOrderIncrementalStatusVO incrementalStatus(Long shopId) {
PddOrderSyncShopContext ctx = contextResolver.resolveForCurrentCompany(shopId);
ShopPO shop = shopMapper.selectById(ctx.getInternalShopId());
PddOrderIncSyncPO state = findIncState(ctx.getShopId());
boolean enabled = shop != null && Integer.valueOf(1).equals(shop.getOrderIncSyncEnabled());
PddShopPO channelShop = pddShopMapper.selectById(ctx.getShopId());
boolean enabled = channelShop != null
&& Integer.valueOf(1).equals(channelShop.getOrderIncSyncEnabled());
return toIncVo(ctx.getInternalShopId(), enabled, state);
}
public boolean isIncrementalEnabled(Long shopId) {
ShopPO shop = shopMapper.selectById(shopId);
return shop != null && Integer.valueOf(1).equals(shop.getOrderIncSyncEnabled());
/** @param internalShopId ec_shop.id */
public boolean isIncrementalEnabled(Long internalShopId) {
if (internalShopId == null) {
return false;
}
PddShopPO pddShop = pddShopMapper.selectOne(new LambdaQueryWrapper<PddShopPO>()
.eq(PddShopPO::getEcShopId, internalShopId)
.last("LIMIT 1"));
return pddShop != null && Integer.valueOf(1).equals(pddShop.getOrderIncSyncEnabled());
}
/** @param channelShopId channel_pdd_shop.id */
@@ -13,8 +13,6 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.mapper.PddOrderI
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.model.po.PddOrderIncSyncPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO;
import top.xyquan.ecomdmbbackend.modules.ec.shop.mapper.ShopMapper;
import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
/**
* 同步侧授权失效统一处置:失效 auth、关闭增量开关、游标置 PAUSED。
@@ -24,7 +22,6 @@ import top.xyquan.ecomdmbbackend.modules.ec.shop.model.po.ShopPO;
@RequiredArgsConstructor
public class PddOrderSyncAuthFailureHandler {
private final ShopMapper shopMapper;
private final PddAuthMapper pddAuthMapper;
private final PddShopMapper pddShopMapper;
private final PddOrderIncSyncMapper incSyncMapper;
@@ -68,11 +65,11 @@ public class PddOrderSyncAuthFailureHandler {
.set(PddAuthPO::getStatus, 0));
}
if (resolvedInternal != null) {
shopMapper.update(null, new LambdaUpdateWrapper<ShopPO>()
.eq(ShopPO::getId, resolvedInternal)
.eq(companyId != null, ShopPO::getCompanyId, companyId)
.set(ShopPO::getOrderIncSyncEnabled, 0));
if (resolvedChannel != null) {
pddShopMapper.update(null, new LambdaUpdateWrapper<PddShopPO>()
.eq(PddShopPO::getId, resolvedChannel)
.eq(companyId != null, PddShopPO::getCompanyId, companyId)
.set(PddShopPO::getOrderIncSyncEnabled, 0));
}
if (resolvedChannel != null && companyId != null) {
@@ -44,6 +44,10 @@ public class PddShopPO {
@TableField("mall_character")
private Integer mallCharacter;
/** 订单增量同步开关:0关 1开 */
@TableField("order_inc_sync_enabled")
private Integer orderIncSyncEnabled;
@TableField("last_sync_time")
private LocalDateTime lastSyncTime;
@@ -31,6 +31,9 @@ public class PddShopVO {
private Integer mallCharacter;
/** 订单增量同步开关:0关 1开 */
private Integer orderIncSyncEnabled;
private LocalDateTime lastSyncTime;
private LocalDateTime createTime;
@@ -215,6 +215,7 @@ public class PddShopServiceImpl extends ServiceImpl<PddShopMapper, PddShopPO> im
vo.setMallName(shop.getMallName());
vo.setLogo(shop.getLogo());
vo.setPddAuthId(shop.getPddAuthId());
vo.setOrderIncSyncEnabled(shop.getOrderIncSyncEnabled());
return vo;
}
@@ -50,12 +50,6 @@ public class ShopPO {
@TableField("platform_code")
private String platformCode;
/**
* 订单增量同步开关:0关 1开
*/
@TableField("order_inc_sync_enabled")
private Integer orderIncSyncEnabled;
/**
* 创建时间
*/