Commit dcc902b4 by zhangxingmin

push

parent 3dd03e29
...@@ -43,9 +43,11 @@ import org.springframework.beans.BeanUtils; ...@@ -43,9 +43,11 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Slf4j @Slf4j
@Service @Service
...@@ -193,7 +195,8 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -193,7 +195,8 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
// 首先收集所有需要转换的请求 // 首先收集所有需要转换的请求
List<ApiExchangeRateConvertRequest> convertRequests = new ArrayList<>(); List<ApiExchangeRateConvertRequest> convertRequests = new ArrayList<>();
Map<String, ConvertInfo> convertInfoMap = new HashMap<>(); // 改为使用列表保存转换信息,按顺序匹配
List<ConvertInfo> convertInfoList = new ArrayList<>();
for (Map.Entry<String, List<ApiPremiumRemittanceDto>> entry : reconciliationRemittanceMap.entrySet()) { for (Map.Entry<String, List<ApiPremiumRemittanceDto>> entry : reconciliationRemittanceMap.entrySet()) {
String reconciliationBizId = entry.getKey(); String reconciliationBizId = entry.getKey();
...@@ -216,6 +219,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -216,6 +219,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
// 初始化总金额 // 初始化总金额
BigDecimal totalAmount = BigDecimal.ZERO; BigDecimal totalAmount = BigDecimal.ZERO;
String paymentCurrency = policyCurrency;
// 首先处理相同币种的汇款记录 // 首先处理相同币种的汇款记录
int sameCurrencyCount = 0; int sameCurrencyCount = 0;
...@@ -263,7 +267,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -263,7 +267,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
} }
} }
// 生成唯一请求ID // 生成唯一请求ID - 但汇率服务可能不返回,所以我们主要依赖顺序匹配
String requestId = reconciliationBizId + "_" + String requestId = reconciliationBizId + "_" +
(StringUtils.isNotBlank(remittanceDto.getPremiumRemittanceBizId()) ? (StringUtils.isNotBlank(remittanceDto.getPremiumRemittanceBizId()) ?
remittanceDto.getPremiumRemittanceBizId() : UUID.randomUUID().toString()); remittanceDto.getPremiumRemittanceBizId() : UUID.randomUUID().toString());
...@@ -271,13 +275,14 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -271,13 +275,14 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
convertRequests.add(convertRequest); convertRequests.add(convertRequest);
// 保存转换信息 // 保存转换信息到列表(按顺序)
ConvertInfo info = new ConvertInfo(); ConvertInfo info = new ConvertInfo();
info.reconciliationBizId = reconciliationBizId; info.reconciliationBizId = reconciliationBizId;
info.originalAmount = amount; info.originalAmount = amount;
info.originalCurrency = remittanceCurrency; info.originalCurrency = remittanceCurrency;
info.targetCurrency = policyCurrency; info.targetCurrency = policyCurrency;
convertInfoMap.put(requestId, info); info.requestId = requestId; // 保存requestId,如果汇率服务返回则使用
convertInfoList.add(info);
} }
log.debug("需要转换的汇款记录数: {}", convertCount); log.debug("需要转换的汇款记录数: {}", convertCount);
...@@ -285,7 +290,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -285,7 +290,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
// 先保存相同币种的累计金额 // 先保存相同币种的累计金额
totalAmount = totalAmount.setScale(2, RoundingMode.HALF_EVEN); totalAmount = totalAmount.setScale(2, RoundingMode.HALF_EVEN);
totalPaymentAmounts.put(reconciliationBizId, totalAmount); totalPaymentAmounts.put(reconciliationBizId, totalAmount);
paymentCurrencies.put(reconciliationBizId, policyCurrency); paymentCurrencies.put(reconciliationBizId, paymentCurrency);
log.info("对账记录 {} 初始总金额: {} {}", reconciliationBizId, totalAmount, policyCurrency); log.info("对账记录 {} 初始总金额: {} {}", reconciliationBizId, totalAmount, policyCurrency);
} }
...@@ -300,6 +305,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -300,6 +305,7 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
for (int i = 0; i < convertRequests.size(); i += batchSize) { for (int i = 0; i < convertRequests.size(); i += batchSize) {
int end = Math.min(convertRequests.size(), i + batchSize); int end = Math.min(convertRequests.size(), i + batchSize);
List<ApiExchangeRateConvertRequest> batch = convertRequests.subList(i, end); List<ApiExchangeRateConvertRequest> batch = convertRequests.subList(i, end);
List<ConvertInfo> batchConvertInfos = convertInfoList.subList(i, end);
log.info("发送第 {} 批汇率转换请求,共 {} 条", i/batchSize + 1, batch.size()); log.info("发送第 {} 批汇率转换请求,共 {} 条", i/batchSize + 1, batch.size());
...@@ -311,10 +317,35 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -311,10 +317,35 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
log.info("收到第 {} 批汇率转换响应,共 {} 条", i/batchSize + 1, batchResult.getData().size()); log.info("收到第 {} 批汇率转换响应,共 {} 条", i/batchSize + 1, batchResult.getData().size());
// 处理转换结果 // 处理转换结果 - 使用顺序匹配
for (ApiExchangeRateConvertResponse convertResponse : batchResult.getData()) { List<ApiExchangeRateConvertResponse> responses = batchResult.getData();
String requestId = convertResponse.getRequestId();
ConvertInfo info = convertInfoMap.get(requestId); // 确保响应数量与请求数量一致
if (responses.size() != batch.size()) {
log.warn("汇率转换响应数量({})与请求数量({})不一致,可能无法正确匹配",
responses.size(), batch.size());
}
// 处理每个转换结果
for (int j = 0; j < Math.min(responses.size(), batch.size()); j++) {
ApiExchangeRateConvertResponse convertResponse = responses.get(j);
ConvertInfo info = null;
// 首先尝试通过requestId匹配
if (StringUtils.isNotBlank(convertResponse.getRequestId())) {
// 如果汇率服务返回了requestId,则从整个列表中查找
for (ConvertInfo ci : convertInfoList) {
if (convertResponse.getRequestId().equals(ci.requestId)) {
info = ci;
break;
}
}
}
// 如果通过requestId没有找到,则使用顺序匹配
if (info == null && j < batchConvertInfos.size()) {
info = batchConvertInfos.get(j);
}
if (info != null && convertResponse.getConvertedAmount() != null) { if (info != null && convertResponse.getConvertedAmount() != null) {
BigDecimal currentTotal = totalPaymentAmounts.getOrDefault(info.reconciliationBizId, BigDecimal.ZERO); BigDecimal currentTotal = totalPaymentAmounts.getOrDefault(info.reconciliationBizId, BigDecimal.ZERO);
...@@ -332,8 +363,32 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -332,8 +363,32 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
log.debug("对账记录 {} 累加后总金额: {} (之前: {} + 转换: {})", log.debug("对账记录 {} 累加后总金额: {} (之前: {} + 转换: {})",
info.reconciliationBizId, newTotal, currentTotal, convertedAmount); info.reconciliationBizId, newTotal, currentTotal, convertedAmount);
} else { } else {
log.warn("转换结果缺少必要信息: requestId={}, info={}, convertedAmount={}", log.warn("转换结果缺少必要信息或无法匹配: requestId={}, info={}, convertedAmount={}",
requestId, info, convertResponse.getConvertedAmount()); convertResponse.getRequestId(), info, convertResponse.getConvertedAmount());
// 尝试通过其他方式匹配
if (convertResponse.getOriginalAmount() != null &&
StringUtils.isNotBlank(convertResponse.getOriginalCurrency()) &&
StringUtils.isNotBlank(convertResponse.getTargetCurrency())) {
// 通过金额、原币种和目标币种匹配
for (ConvertInfo ci : convertInfoList) {
if (convertResponse.getOriginalAmount().compareTo(ci.originalAmount) == 0 &&
convertResponse.getOriginalCurrency().equalsIgnoreCase(ci.originalCurrency) &&
convertResponse.getTargetCurrency().equalsIgnoreCase(ci.targetCurrency)) {
info = ci;
BigDecimal convertedAmount = convertResponse.getConvertedAmount();
if (convertedAmount != null) {
BigDecimal currentTotal = totalPaymentAmounts.getOrDefault(info.reconciliationBizId, BigDecimal.ZERO);
convertedAmount = convertedAmount.setScale(2, RoundingMode.HALF_EVEN);
BigDecimal newTotal = currentTotal.add(convertedAmount);
totalPaymentAmounts.put(info.reconciliationBizId, newTotal);
log.info("通过金额和币种匹配成功: 对账记录={}, 累加金额={}",
info.reconciliationBizId, convertedAmount);
}
break;
}
}
}
} }
} }
} else { } else {
...@@ -356,9 +411,9 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -356,9 +411,9 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
BigDecimal totalAmount = totalPaymentAmounts.get(reconciliationBizId); BigDecimal totalAmount = totalPaymentAmounts.get(reconciliationBizId);
String paymentCurrency = paymentCurrencies.get(reconciliationBizId); String paymentCurrency = paymentCurrencies.get(reconciliationBizId);
log.info("更新对账记录 {}: 付款金额={} {}, 原paymentAmount={} {}", log.info("更新对账记录 {}: 付款金额={} {}, 原paymentAmount={} {}, 原paymentCurrency={}",
reconciliationBizId, totalAmount, paymentCurrency, reconciliationBizId, totalAmount, paymentCurrency,
item.getPaymentAmount(), item.getPaymentCurrency()); item.getPaymentAmount(), item.getPaymentCurrency(), item.getPaymentCurrency());
item.setPaymentAmount(totalAmount); item.setPaymentAmount(totalAmount);
...@@ -383,57 +438,9 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -383,57 +438,9 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
BigDecimal originalAmount; BigDecimal originalAmount;
String originalCurrency; String originalCurrency;
String targetCurrency; String targetCurrency;
String requestId; // 添加requestId字段
} }
// 内部类,用于保存转换请求信息
private static class ConvertRequestInfo {
private String reconciliationBizId;
private String remittanceBizId;
private BigDecimal originalAmount;
private String originalCurrency;
private String targetCurrency;
// getters and setters
public String getReconciliationBizId() {
return reconciliationBizId;
}
public void setReconciliationBizId(String reconciliationBizId) {
this.reconciliationBizId = reconciliationBizId;
}
public String getRemittanceBizId() {
return remittanceBizId;
}
public void setRemittanceBizId(String remittanceBizId) {
this.remittanceBizId = remittanceBizId;
}
public BigDecimal getOriginalAmount() {
return originalAmount;
}
public void setOriginalAmount(BigDecimal originalAmount) {
this.originalAmount = originalAmount;
}
public String getOriginalCurrency() {
return originalCurrency;
}
public void setOriginalCurrency(String originalCurrency) {
this.originalCurrency = originalCurrency;
}
public String getTargetCurrency() {
return targetCurrency;
}
public void setTargetCurrency(String targetCurrency) {
this.targetCurrency = targetCurrency;
}
}
/** /**
* 获取缴费方式字典数据 * 获取缴费方式字典数据
*/ */
...@@ -941,4 +948,4 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia ...@@ -941,4 +948,4 @@ public class ApiPremiumReconciliationServiceImpl implements ApiPremiumReconcilia
return Result.success(premiumReconciliation); return Result.success(premiumReconciliation);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment