|
@@ -0,0 +1,62 @@
|
|
|
|
|
+package com.payment.platform.module.payment.enterprise.scheduler;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.payment.platform.module.payment.enterprise.entity.EnterpriseEntity;
|
|
|
|
|
+import com.payment.platform.module.payment.enterprise.mapper.EnterpriseMapper;
|
|
|
|
|
+import com.payment.platform.module.payment.enterprise.service.AlipayEnterpriseService;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.OffsetDateTime;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 企业名称同步重试任务 — 入驻激活后支付宝查详情可能因产品未签约临时失败,周期重试
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class EnterpriseNameSyncScheduler {
|
|
|
|
|
+
|
|
|
|
|
+ private final EnterpriseMapper enterpriseMapper;
|
|
|
|
|
+ private final AlipayEnterpriseService alipayEnterpriseService;
|
|
|
|
|
+
|
|
|
|
|
+ private static final int MAX_RETRY = 20;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(fixedDelay = 10 * 60 * 1000) // 每 10 分钟
|
|
|
|
|
+ public void retryNameSync() {
|
|
|
|
|
+ List<EnterpriseEntity> list = enterpriseMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<EnterpriseEntity>()
|
|
|
|
|
+ .eq(EnterpriseEntity::getNameSyncStatus, "0")
|
|
|
|
|
+ .lt(EnterpriseEntity::getNameSyncRetryCount, MAX_RETRY)
|
|
|
|
|
+ .le(EnterpriseEntity::getNameSyncNextTime, OffsetDateTime.now())
|
|
|
|
|
+ .last("LIMIT 50"));
|
|
|
|
|
+ if (list.isEmpty()) return;
|
|
|
|
|
+
|
|
|
|
|
+ log.info("[企业名称同步] 待重试: {} 条", list.size());
|
|
|
|
|
+ int success = 0;
|
|
|
|
|
+ for (EnterpriseEntity entity : list) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Object> info = alipayEnterpriseService.queryEnterpriseDetail(entity.getEnterpriseId());
|
|
|
|
|
+ if (info != null && !info.isEmpty()) {
|
|
|
|
|
+ if (info.get("enterprise_name") != null) entity.setName((String) info.get("enterprise_name"));
|
|
|
|
|
+ if (info.get("enterprise_alias") != null) entity.setShortName((String) info.get("enterprise_alias"));
|
|
|
|
|
+ if (info.get("account_id") != null) entity.setAccountId((String) info.get("account_id"));
|
|
|
|
|
+ entity.setNameSyncStatus("1");
|
|
|
|
|
+ success++;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ int retry = entity.getNameSyncRetryCount() != null ? entity.getNameSyncRetryCount() + 1 : 1;
|
|
|
|
|
+ entity.setNameSyncRetryCount(retry);
|
|
|
|
|
+ // 指数退避: 10min, 20min, 40min, 80min... 最大 12h
|
|
|
|
|
+ long delay = Math.min(10L * (1L << Math.min(retry, 6)), 12 * 60);
|
|
|
|
|
+ entity.setNameSyncNextTime(OffsetDateTime.now().plusMinutes(delay));
|
|
|
|
|
+ }
|
|
|
|
|
+ enterpriseMapper.updateIgnoreTenant(entity);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("[企业名称同步] 完成: 成功 {}/{}", success, list.size());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|