This commit is contained in:
Leo
2026-02-08 11:35:38 +08:00
parent 549224a83f
commit 4c07dda3d7
2 changed files with 55 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import com.ruoyi.jarvis.config.TencentDocConfig;
import com.ruoyi.jarvis.service.ITencentDocBatchPushService;
import com.ruoyi.jarvis.service.ITencentDocDelayedPushService;
import com.ruoyi.jarvis.service.ITencentDocTokenService;
import com.ruoyi.jarvis.service.ITencentDocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
@@ -50,7 +51,13 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
@Autowired
private ITencentDocTokenService tokenService;
@Autowired
private ITencentDocService tencentDocService;
private ApplicationContext applicationContext;
private static final int READ_ROWS_WHEN_USE_ROW_TOTAL = 200;
private static final int MIN_START_ROW_WHEN_USE_ROW_TOTAL = 3;
/**
* 延迟时间(分钟),可通过配置文件修改
@@ -275,24 +282,46 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
Integer startRow = redisCache.getCacheObject(CONFIG_KEY_PREFIX + "startRow");
if (startRow == null) {
startRow = 3; // 默认值
startRow = MIN_START_ROW_WHEN_USE_ROW_TOTAL;
}
int batchStartRow = startRow;
int batchEndRow = startRow + READ_ROWS_WHEN_USE_ROW_TOTAL - 1;
// 先获取 rowTotal确保 batch 记录使用有效范围(避免 350 超出 324 行)
try {
String accessToken = tokenService.getValidAccessToken();
if (accessToken != null) {
int rowTotal = tencentDocService.getSheetRowTotal(accessToken, fileId, sheetId);
if (rowTotal > 0) {
if (startRow > rowTotal) {
batchStartRow = Math.max(MIN_START_ROW_WHEN_USE_ROW_TOTAL, rowTotal - READ_ROWS_WHEN_USE_ROW_TOTAL);
batchEndRow = Math.min(rowTotal, batchStartRow + READ_ROWS_WHEN_USE_ROW_TOTAL - 1);
log.info("配置起始行 {} 超出表尾 rowTotal={},修正为第 {} ~ {} 行", startRow, rowTotal, batchStartRow, batchEndRow);
} else {
batchEndRow = Math.min(rowTotal, startRow + READ_ROWS_WHEN_USE_ROW_TOTAL - 1);
}
}
}
} catch (Exception e) {
log.warn("获取 rowTotal 失败,使用配置起始行创建 batch: {}", e.getMessage());
}
log.info("读取配置 - fileId: {}, sheetId: {}, startRow: {}", fileId, sheetId, startRow);
log.info("读取配置 - fileId: {}, sheetId: {}, 创建 batch 范围: {} ~ {}", fileId, sheetId, batchStartRow, batchEndRow);
if (StringUtils.isEmpty(fileId) || StringUtils.isEmpty(sheetId)) {
log.error("腾讯文档配置不完整无法执行批量同步。请先在前端配置页面设置文件ID和工作表ID");
return;
}
// 创建批量推送记录
// 创建批量推送记录(使用有效范围)
batchId = batchPushService.createBatchPushRecord(
fileId,
sheetId,
"AUTO",
"DELAYED_TIMER",
startRow,
startRow + 199 // 接口单次只能读 200 行
batchStartRow,
batchEndRow
);
log.info("✓ 创建批量推送记录批次ID: {}", batchId);
@@ -342,8 +371,16 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
}
}
if (nextStartRow >= 1) {
redisCache.setCacheObject(CONFIG_KEY_PREFIX + "startRow", nextStartRow, 180, TimeUnit.DAYS);
log.info("✓ 已更新下次起始行到 Redis: startRow={}(下次定时同步从第 {} 行开始)", nextStartRow, nextStartRow);
// 若 nextStartRow 超出 rowTotal capped 避免下次仍超出表尾
Object rowTotalObj = dataMap.get("rowTotal");
int rowTotal = rowTotalObj instanceof Number ? ((Number) rowTotalObj).intValue() : 0;
int toWrite = nextStartRow;
if (rowTotal > 0 && nextStartRow > rowTotal) {
toWrite = Math.max(MIN_START_ROW_WHEN_USE_ROW_TOTAL, rowTotal - READ_ROWS_WHEN_USE_ROW_TOTAL);
log.info("nextStartRow={} 超出 rowTotal={}capped 为 {}", nextStartRow, rowTotal, toWrite);
}
redisCache.setCacheObject(CONFIG_KEY_PREFIX + "startRow", toWrite, 180, TimeUnit.DAYS);
log.info("✓ 已更新下次起始行到 Redis: startRow={}(下次定时同步从第 {} 行开始)", toWrite, toWrite);
}
}
}