This commit is contained in:
Guoqs
2026-01-09 11:41:19 +08:00
parent 0febf69883
commit 0062aa2c1b
3 changed files with 35 additions and 9 deletions

View File

@@ -44,7 +44,7 @@ public class JcppPileSyncController extends BaseController {
*/
@ApiOperation("全量同步充电桩数据")
@PreAuthorize("@ss.hasPermi('jcpp:sync:full')")
@Log(title = "JCPP 充电桩同步", businessType = BusinessType.OTHER)
// @Log(title = "JCPP 充电桩同步", businessType = BusinessType.OTHER)
@PostMapping("/full")
public AjaxResult syncFull() {
try {

View File

@@ -24,7 +24,7 @@ jcpp:
# JCPP 同步接口地址
api-url: http://localhost:8180/api/sync
# 批量同步大小(每批充电桩数量)
batch-size: 500
batch-size: 1000
# 超时时间(毫秒)
timeout: 60000
# 是否启用自动增量同步

View File

@@ -58,7 +58,7 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
@Value("${jcpp.sync.api-url:http://localhost:8180/api/sync}")
private String jcppApiUrl;
@Value("${jcpp.sync.batch-size:500}")
@Value("${jcpp.sync.batch-size:1000}")
private int batchSize;
@Value("${jcpp.sync.timeout:60000}")
@@ -458,10 +458,14 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
*/
private List<JcppSyncResult> syncPilesToJcpp(List<JcppPileSyncDTO> pileDTOs) {
String url = jcppApiUrl + "/piles";
long methodStartTime = System.currentTimeMillis();
try {
// 获取访问令牌
long tokenStartTime = System.currentTimeMillis();
String token = jcppAuthService.getAccessToken();
long tokenTime = System.currentTimeMillis() - tokenStartTime;
log.info("【性能】获取访问令牌耗时: {} ms", tokenTime);
if (token == null || token.isEmpty()) {
log.error("无法获取 JCPP 访问令牌");
// 返回失败结果
@@ -473,6 +477,7 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
}
// 构建请求体
long buildStartTime = System.currentTimeMillis();
JSONObject requestBody = new JSONObject();
requestBody.put("piles", pileDTOs);
@@ -482,17 +487,22 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
headers.set("Authorization", "Bearer " + token);
HttpEntity<String> entity = new HttpEntity<>(requestBody.toJSONString(), headers);
long buildTime = System.currentTimeMillis() - buildStartTime;
log.info("【性能】构建充电桩请求体耗时: {} ms", buildTime);
// 发送请求
log.info("调用 JCPP 充电桩同步接口: {}, 数量: {}", url, pileDTOs.size());
long httpStartTime = System.currentTimeMillis();
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
long httpTime = System.currentTimeMillis() - httpStartTime;
log.info("【性能】JCPP 充电桩接口 HTTP 请求耗时: {} ms (网络+JCPP处理)", httpTime);
// 打印响应状态和内容
log.info("JCPP 充电桩同步接口响应 - 状态码: {}, 响应体: {}",
response.getStatusCode(), response.getBody());
// log.info("JCPP 充电桩同步接口响应 - 状态码: {}, 响应体: {}", response.getStatusCode(), response.getBody());
if (response.getStatusCode() == HttpStatus.OK) {
// 解析响应
long parseStartTime = System.currentTimeMillis();
JSONObject responseBody = JSON.parseObject(response.getBody());
// JCPP 响应格式:{ "success": true, "data": { "results": [...] } }
@@ -506,11 +516,14 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
log.warn("JCPP 充电桩同步响应中没有 data 字段");
results = new ArrayList<>();
}
long parseTime = System.currentTimeMillis() - parseStartTime;
log.info("【性能】解析充电桩响应耗时: {} ms", parseTime);
// 统计结果
long successCount = results != null ? results.stream().filter(JcppSyncResult::isSuccess).count() : 0;
long failCount = results != null ? results.size() - successCount : 0;
log.info("JCPP 充电桩同步结果 - 成功: {}, 失败: {}", successCount, failCount);
long totalTime = System.currentTimeMillis() - methodStartTime;
log.info("JCPP 充电桩同步结果 - 成功: {}, 失败: {}, 方法总耗时: {} ms", successCount, failCount, totalTime);
return results != null ? results : new ArrayList<>();
} else if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
@@ -614,10 +627,14 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
*/
private List<JcppSyncResult> syncGunsToJcpp(List<JcppGunSyncDTO> gunDTOs) {
String url = jcppApiUrl + "/guns";
long methodStartTime = System.currentTimeMillis();
try {
// 获取访问令牌
long tokenStartTime = System.currentTimeMillis();
String token = jcppAuthService.getAccessToken();
long tokenTime = System.currentTimeMillis() - tokenStartTime;
log.info("【性能】获取访问令牌耗时: {} ms", tokenTime);
if (token == null || token.isEmpty()) {
log.error("无法获取 JCPP 访问令牌");
// 返回失败结果
@@ -629,6 +646,7 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
}
// 构建请求体
long buildStartTime = System.currentTimeMillis();
JSONObject requestBody = new JSONObject();
requestBody.put("guns", gunDTOs);
@@ -638,17 +656,22 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
headers.set("Authorization", "Bearer " + token);
HttpEntity<String> entity = new HttpEntity<>(requestBody.toJSONString(), headers);
long buildTime = System.currentTimeMillis() - buildStartTime;
log.info("【性能】构建充电枪请求体耗时: {} ms", buildTime);
// 发送请求
log.info("调用 JCPP 充电枪同步接口: {}, 数量: {}", url, gunDTOs.size());
long httpStartTime = System.currentTimeMillis();
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
long httpTime = System.currentTimeMillis() - httpStartTime;
log.info("【性能】JCPP 充电枪接口 HTTP 请求耗时: {} ms (网络+JCPP处理)", httpTime);
// 打印响应状态和内容
log.info("JCPP 充电枪同步接口响应 - 状态码: {}, 响应体: {}",
response.getStatusCode(), response.getBody());
// log.info("JCPP 充电枪同步接口响应 - 状态码: {}, 响应体: {}", response.getStatusCode(), response.getBody());
if (response.getStatusCode() == HttpStatus.OK) {
// 解析响应
long parseStartTime = System.currentTimeMillis();
JSONObject responseBody = JSON.parseObject(response.getBody());
// JCPP 响应格式:{ "success": true, "data": { "results": [...] } }
@@ -662,11 +685,14 @@ public class JcppPileSyncServiceImpl implements IJcppPileSyncService {
log.warn("JCPP 充电枪同步响应中没有 data 字段");
results = new ArrayList<>();
}
long parseTime = System.currentTimeMillis() - parseStartTime;
log.info("【性能】解析充电枪响应耗时: {} ms", parseTime);
// 统计结果
long successCount = results != null ? results.stream().filter(JcppSyncResult::isSuccess).count() : 0;
long failCount = results != null ? results.size() - successCount : 0;
log.info("JCPP 充电枪同步结果 - 成功: {}, 失败: {}", successCount, failCount);
long totalTime = System.currentTimeMillis() - methodStartTime;
log.info("JCPP 充电枪同步结果 - 成功: {}, 失败: {}, 方法总耗时: {} ms", successCount, failCount, totalTime);
return results != null ? results : new ArrayList<>();
} else if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {