@@ -9,8 +9,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.http.* ;
import org.springframework.stereotype.Service ;
import org.springframework.util.StringUtils ;
import org.springframework.web.client.RestTemplate ;
import javax.annotation.PostConstruct ;
import javax.annotation.PreDestroy ;
@@ -47,6 +49,8 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
@Autowired
private ITencentDocTokenService tokenService ;
private final RestTemplate restTemplate = new RestTemplate ( ) ;
/**
* 延迟时间(分钟),可通过配置文件修改
*/
@@ -257,13 +261,19 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
try {
log . info ( " 开始执行批量同步... " ) ;
// 获取配置信息
String fileId = tencentDocConfig . getF ileId( ) ;
String sheetId = tencentDocConfig . getS heetId( ) ;
Integer startRow = tencentDocConfig . getS tartRow( ) ;
// 从 Redis 读取配置信息(用户通过前端配置页面设置)
String fileId = redisCache . getCacheObject ( " tendoc:config:f ileId" ) ;
String sheetId = redisCache . getCacheObject ( " tendoc:config:s heetId" ) ;
Integer startRow = redisCache . getCacheObject ( " tendoc:config:s tartRow" ) ;
if ( startRow = = null ) {
startRow = 3 ; // 默认值
}
log . info ( " 读取配置 - fileId: {}, sheetId: {}, startRow: {} " , fileId , sheetId , startRow ) ;
if ( StringUtils . isEmpty ( fileId ) | | StringUtils . isEmpty ( sheetId ) ) {
log . error ( " 腾讯文档配置不完整,无法执行批量同步 " ) ;
log . error ( " 腾讯文档配置不完整,无法执行批量同步。请先在前端配置页面设置文件ID和工作表ID " ) ;
return ;
}
@@ -278,39 +288,32 @@ public class TencentDocDelayedPushServiceImpl implements ITencentDocDelayedPushS
) ;
log . info ( " ✓ 创建批量推送记录, 批次ID: {} " , batchId ) ;
// 调用批量同步接口, 传递批次ID
java . net . URL url = new java . net . URL ( " http://localhost:30313/jarvis-api/jarvis/tendoc/fillLogisticsByOrderNo?batchId= " + batchId ) ;
java . net . HttpURLConnection conn = ( java . net . HttpURLConnection ) url . openConnection ( ) ;
conn . setRequestMethod ( " POST " ) ;
conn . setRequestProperty ( " Content-Type " , " application/json " ) ;
conn . setDoOutput ( true ) ;
// 使用RestTemplate调用本地Controller( 内部调用, 无需认证)
String url = " http://localhost:30313/jarvis-api/jarvis/tendoc/fillLogisticsByOrderNo?batchId= " + batchId ;
// 发送空JSON对象
try ( java . io . OutputStream os = conn . getOutputStream ( ) ) {
byte [ ] input = " {} " . getBytes ( " utf-8 " ) ;
os . write ( input , 0 , input . length ) ;
}
HttpHeaders headers = new HttpHeaders ( ) ;
headers . setContentType ( MediaType . APPLICATION_JSON ) ;
int responseCode = conn . getResponseCode ( ) ;
log . info ( " 批量同步调用完成,响应码: {} " , responseCode ) ;
// 构造请求体
String requestBody = " {} " ;
if ( responseCode = = 200 ) {
// 读取响应
try ( java . io . BufferedReader br = new java . io . BufferedReader (
new java . io . InputStreamReader ( conn . getInputStream ( ) , " utf-8 " ) ) ) {
StringBuilder response = new StringBuilder ( ) ;
String responseLine ;
while ( ( responseLine = br . readLine ( ) ) ! = null ) {
response . append ( responseLine . trim ( ) ) ;
}
log . info ( " 批量同步结果: {} " , response . toString ( ) ) ;
}
} else {
log . error ( " 批量同步调用失败,响应码: {} " , responseCode ) ;
// 更新批量推送记录为失败状态
HttpEntity < String > request = new HttpEntity < > ( requestBody , headers ) ;
try {
log . info ( " 调用批量同步API: {} " , url ) ;
ResponseEntity < String> response = restTemplate . postForEntity ( url , request , String. class ) ;
log . info ( " 批量同步调用完成, HTTP状态码: {} " , response . getStatusCodeValue ( ) ) ;
log . info ( " 批量同步结果: {} " , response . getBody ( ) ) ;
// 注意: 由于是内部调用, 401错误说明需要添加@Anonymous注解
// 暂时记录日志, 后续需要修改Controller添加@Anonymous注解
} catch ( Exception ex ) {
log . error ( " 批量同步调用失败 " , ex ) ;
if ( batchId ! = null ) {
batchPushService . updateBatchPushRecord ( batchId , " FAILED " , 0 , 0 , 0 ,
null , " 批量同步调用失败,响应码 : " + responseCode ) ;
null , " 批量同步调用失败: " + ex . getMessage ( ) ) ;
}
}