Browse Source

接口触发同步salesforce

jwy
review512jwy@163.com 2 days ago
parent
commit
20000517d3
  1. 49
      data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/CommonController.java
  2. 29
      data-center-business-service/src/main/java/com/techsor/datacenter/business/service/schedule/ScheduleSyncSalesforceInfo.java

49
data-center-business-controller/src/main/java/com/techsor/datacenter/business/controller/CommonController.java

@ -2,12 +2,14 @@ package com.techsor.datacenter.business.controller;
import java.util.List;
import com.techsor.datacenter.business.common.response.ResponseCode;
import com.techsor.datacenter.business.configurator.interceptor.AccessRequired;
import com.techsor.datacenter.business.configurator.interceptor.ApiTokenRequired;
import com.techsor.datacenter.business.dto.common.RepostRoidParams;
import com.techsor.datacenter.business.dto.common.api.*;
import com.techsor.datacenter.business.dto.common.roidproblemreport.ProblemReportsSummariesSearchParams;
import com.techsor.datacenter.business.service.ApiAuthService;
import com.techsor.datacenter.business.service.schedule.ScheduleSyncSalesforceInfo;
import com.techsor.datacenter.business.util.ApiContext;
import com.techsor.datacenter.business.vo.common.ApiTokenVO;
import org.slf4j.Logger;
@ -15,13 +17,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.techsor.datacenter.business.common.response.SimpleDataResponse;
@ -66,6 +62,8 @@ public class CommonController{
private CommonService commonService;
@Autowired
private ApiAuthService apiAuthService;
@Autowired
private ScheduleSyncSalesforceInfo scheduleSyncSalesforceInfo;
@Value("${open.api.mock}")
private String openApiMock;
@ -428,4 +426,41 @@ public class CommonController{
@RequestHeader("X-API-KEY") String apiKey) {
return apiAuthService.generateToken(apiKey);
}
@Operation(summary = "Manually trigger Salesforce data synchronization for all companies")
@RequestMapping(value = "/syncSalesforce/all", method = RequestMethod.GET)
public SimpleDataResponse syncAll() {
SimpleDataResponse response = new SimpleDataResponse();
try {
logger.info("Manual sync triggered");
scheduleSyncSalesforceInfo.syncAllCompanies();
response.setCode(ResponseCode.SUCCESS);
response.setMsg("Synchronization started successfully");
response.setData(null);
} catch (Exception e) {
logger.error("Manual sync failed", e);
response.setCode(ResponseCode.SERVER_ERROR);
response.setMsg("Synchronization failed: " + e.getMessage());
}
return response;
}
@Operation(summary = "Manually trigger Salesforce data synchronization for a specific company")
@RequestMapping(value = "/syncSalesforce/{companyId}", method = RequestMethod.GET)
public SimpleDataResponse syncByCompany(@PathVariable Long companyId) {
SimpleDataResponse response = new SimpleDataResponse();
try {
logger.info("Manual sync triggered for company: {}", companyId);
scheduleSyncSalesforceInfo.syncByCompany(companyId);
response.setCode(ResponseCode.SUCCESS);
response.setMsg("Synchronization started successfully for company: " + companyId);
response.setData(null);
} catch (Exception e) {
logger.error("Manual sync failed for company: {}", companyId, e);
response.setCode(ResponseCode.SERVER_ERROR);
response.setMsg("Synchronization failed: " + e.getMessage());
}
return response;
}
}

29
data-center-business-service/src/main/java/com/techsor/datacenter/business/service/schedule/ScheduleSyncSalesforceInfo.java

@ -21,6 +21,7 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
@ -64,14 +65,38 @@ public class ScheduleSyncSalesforceInfo {
@Scheduled(cron = "0 0 3 ? * SAT")
// @Scheduled(cron = "0 * * * * ?")
public void start() {
syncAllCompanies();
}
@Async("threadPoolTaskExecutor")
public void syncAllCompanies() {
logger.info("Starting async sync for all companies");
List<Long> topCompanyIdList = dataSourceInterceptor.getTopCompanyIdList();
for (Long tempTopCompanyId:topCompanyIdList) {
logger.debug("Using dataSourceForCompany_" + tempTopCompanyId);
DataSourceContextHolder.setCurrentDataSourceKey("dataSourceForCompany_" + tempTopCompanyId);
try {
logger.debug("Using dataSourceForCompany_" + tempTopCompanyId);DataSourceContextHolder.setCurrentDataSourceKey("dataSourceForCompany_" + tempTopCompanyId);
process(tempTopCompanyId);
logger.info("Successfully synced company: {}", tempTopCompanyId);
} catch (Exception e) {
logger.error("Failed to sync company: {}", tempTopCompanyId, e);
} finally {
DataSourceContextHolder.clearCurrentDataSourceKey();
}
}
}
@Async("threadPoolTaskExecutor")
public void syncByCompany(Long companyId) {
logger.info("Starting async sync for company: {}", companyId);
try {
DataSourceContextHolder.setCurrentDataSourceKey("dataSourceForCompany_" + companyId);
process(companyId);
logger.info("Async sync completed successfully for company: {}", companyId);
} catch (Exception e) {
logger.error("Async sync failed for company: {}", companyId, e);
} finally {
DataSourceContextHolder.clearCurrentDataSourceKey();
}
}
private void process(Long tempTopCompanyId) {

Loading…
Cancel
Save