|
|
@@ -7,10 +7,13 @@ import com.fs.comm.domain.CommGatewayApiLog;
|
|
|
import com.fs.comm.dto.CommCallSendRequest;
|
|
|
import com.fs.comm.dto.CommSmsSendRequest;
|
|
|
import com.fs.comm.model.CommGatewayBillingQuote;
|
|
|
+import com.fs.comm.support.CommTenantDataSourceHelper;
|
|
|
import com.fs.common.exception.ServiceException;
|
|
|
import com.fs.common.utils.ServletUtils;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.common.utils.ip.IpUtils;
|
|
|
+import com.fs.company.domain.CompanyCommGatewayLog;
|
|
|
+import com.fs.company.service.ICompanyCommGatewayLogService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -20,7 +23,7 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- * 通讯网关 API 调用日志记录(主库)
|
|
|
+ * 通讯网关 API 调用日志记录(主库 + 租户库)
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -29,9 +32,15 @@ public class CommGatewayApiLogRecorder {
|
|
|
@Autowired
|
|
|
private ICommGatewayApiLogService commGatewayApiLogService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ICompanyCommGatewayLogService companyCommGatewayLogService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private CommGatewayBillingService commGatewayBillingService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private CommTenantDataSourceHelper commTenantDataSourceHelper;
|
|
|
+
|
|
|
public void recordCallAttempt(Long companyId, Long tenantId, CommCallSendRequest request,
|
|
|
CommApiRecordResult result, String calleePhone, String callerPhone,
|
|
|
Long gatewayId, long startMs) {
|
|
|
@@ -58,7 +67,7 @@ public class CommGatewayApiLogRecorder {
|
|
|
logEntity.setCallerAccount(session.getAccount());
|
|
|
logEntity.setAuthScope(session.getScope());
|
|
|
}
|
|
|
- fillCompanyUserIdFromRequest(logEntity, request);
|
|
|
+ fillIdentityFromRequest(logEntity, request);
|
|
|
logEntity.setApiType(apiType);
|
|
|
logEntity.setApiPath(apiPath);
|
|
|
logEntity.setRequestBody(JSON.toJSONString(request));
|
|
|
@@ -111,14 +120,23 @@ public class CommGatewayApiLogRecorder {
|
|
|
logEntity.setBillingAmount(quote.getBillingAmount());
|
|
|
}
|
|
|
|
|
|
- private void fillCompanyUserIdFromRequest(CommGatewayApiLog logEntity, Object request) {
|
|
|
- if (logEntity.getCompanyUserId() != null || request == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ private void fillIdentityFromRequest(CommGatewayApiLog logEntity, Object request) {
|
|
|
if (request instanceof CommCallSendRequest) {
|
|
|
- logEntity.setCompanyUserId(((CommCallSendRequest) request).getCompanyUserId());
|
|
|
+ CommCallSendRequest callRequest = (CommCallSendRequest) request;
|
|
|
+ if (callRequest.getCompanyUserId() != null) {
|
|
|
+ logEntity.setCompanyUserId(callRequest.getCompanyUserId());
|
|
|
+ }
|
|
|
} else if (request instanceof CommSmsSendRequest) {
|
|
|
- logEntity.setCompanyUserId(((CommSmsSendRequest) request).getCompanyUserId());
|
|
|
+ CommSmsSendRequest smsRequest = (CommSmsSendRequest) request;
|
|
|
+ if (smsRequest.getCompanyId() != null) {
|
|
|
+ logEntity.setCompanyId(smsRequest.getCompanyId());
|
|
|
+ }
|
|
|
+ if (smsRequest.getCompanyUserId() != null) {
|
|
|
+ logEntity.setCompanyUserId(smsRequest.getCompanyUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (logEntity.getCompanyUserId() == null) {
|
|
|
+ logEntity.setCompanyUserId(CommAuthContext.getCompanyUserId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -135,6 +153,7 @@ public class CommGatewayApiLogRecorder {
|
|
|
logEntity.setCostPrice(BigDecimal.ZERO);
|
|
|
logEntity.setCalcPrice(BigDecimal.ZERO);
|
|
|
logEntity.setBillingQuantity(0);
|
|
|
+ logEntity.setBillingUnit(logEntity.getApiType());
|
|
|
Map<String, Object> body = new HashMap<>();
|
|
|
body.put("code", 500);
|
|
|
body.put("msg", message);
|
|
|
@@ -142,11 +161,66 @@ public class CommGatewayApiLogRecorder {
|
|
|
}
|
|
|
|
|
|
private void record(CommGatewayApiLog logEntity) {
|
|
|
+ Long masterLogId = null;
|
|
|
try {
|
|
|
commGatewayApiLogService.saveLog(logEntity);
|
|
|
+ masterLogId = logEntity.getLogId();
|
|
|
} catch (Exception ex) {
|
|
|
- log.error("写入通讯网关调用日志失败", ex);
|
|
|
+ log.error("写入主库通讯网关调用日志失败", ex);
|
|
|
+ }
|
|
|
+ saveTenantLog(logEntity, masterLogId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveTenantLog(CommGatewayApiLog logEntity, Long masterLogId) {
|
|
|
+ if (logEntity == null || logEntity.getTenantId() == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ commTenantDataSourceHelper.ensureTenant(logEntity.getTenantId());
|
|
|
+ companyCommGatewayLogService.saveLog(toTenantLog(logEntity, masterLogId));
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("写入租户通讯网关调用日志失败 tenantId={}, companyId={}",
|
|
|
+ logEntity.getTenantId(), logEntity.getCompanyId(), ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private CompanyCommGatewayLog toTenantLog(CommGatewayApiLog source, Long masterLogId) {
|
|
|
+ CompanyCommGatewayLog target = new CompanyCommGatewayLog();
|
|
|
+ target.setMasterLogId(masterLogId);
|
|
|
+ target.setCompanyId(source.getCompanyId());
|
|
|
+ target.setCompanyUserId(source.getCompanyUserId());
|
|
|
+ target.setCallerAccount(source.getCallerAccount());
|
|
|
+ target.setApiType(source.getApiType());
|
|
|
+ target.setApiPath(source.getApiPath());
|
|
|
+ target.setRequestBody(source.getRequestBody());
|
|
|
+ target.setResponseBody(source.getResponseBody());
|
|
|
+ target.setResultCode(source.getResultCode());
|
|
|
+ target.setResultMsg(source.getResultMsg());
|
|
|
+ target.setSuccess(source.getSuccess());
|
|
|
+ target.setLimitHit(source.getLimitHit());
|
|
|
+ target.setLimitReason(source.getLimitReason());
|
|
|
+ target.setCalleePhone(source.getCalleePhone());
|
|
|
+ target.setCallerPhone(source.getCallerPhone());
|
|
|
+ target.setGatewayId(source.getGatewayId());
|
|
|
+ target.setBillingAmount(source.getBillingAmount());
|
|
|
+ target.setCostPrice(source.getCostPrice());
|
|
|
+ target.setCalcPrice(source.getCalcPrice());
|
|
|
+ target.setBillingQuantity(source.getBillingQuantity());
|
|
|
+ target.setBillingUnit(source.getBillingUnit());
|
|
|
+ target.setClientIp(source.getClientIp());
|
|
|
+ target.setAuthScope(source.getAuthScope());
|
|
|
+ target.setDurationMs(source.getDurationMs());
|
|
|
+ target.setCreateTime(source.getCreateTime());
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 是否为频率/QPS 类限制失败 */
|
|
|
+ public static boolean isLimitFailure(ServiceException ex) {
|
|
|
+ if (ex == null || StringUtils.isBlank(ex.getMessage())) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+ String message = ex.getMessage();
|
|
|
+ return message.contains("限制") || message.contains("超限") || message.contains("频率");
|
|
|
}
|
|
|
|
|
|
public CommApiRecordResult buildLimitFailure(ServiceException ex, String calleePhone, String callerPhone, Long gatewayId) {
|