Commit 906e0a3f by jianan

前端对接问题修复69

parent 34843699
......@@ -202,10 +202,9 @@ public class ApiFortuneController {
if (CollectionUtils.isEmpty(fortuneDownloadRequest.getFortuneBizIdList())) {
throw new BusinessException(ResultCode.PARAMS_ERROR.getCode(), "请选择要出账的发佣数据");
}
try {
fortuneService.downloadAccount(fortuneDownloadRequest, response);
return Result.success(true);
} catch (Exception e) {
// // 设置响应类型为 JSON,而不是文件流
// response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
// response.setContentType(MediaType.APPLICATION_JSON_VALUE);
......@@ -218,11 +217,10 @@ public class ApiFortuneController {
//
// response.getWriter().write(new ObjectMapper().writeValueAsString(errorResult));
log.error("生成出账清单失败", e);
return Result.success(false);
}
return Result.success(true);
}
/**
* 分页获取出账列表
*
......
package com.yd.csf.service.service.impl;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Date;
......@@ -247,77 +248,97 @@ public class FortuneAccountServiceImpl extends ServiceImpl<FortuneAccountMapper,
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean completeFortuneAccount(CompleteFortuneAccountRequest completeFortuneAccountRequest) {
List<String> fortuneAccountBizIdList = completeFortuneAccountRequest.getFortuneAccountBizIdList();
List<FortuneAccount> fortuneAccountList = this.list(new QueryWrapper<FortuneAccount>().in("fortune_account_biz_id", fortuneAccountBizIdList));
if (CollectionUtils.isEmpty(fortuneAccountList)) {
throw new BusinessException(ResultCode.NULL_ERROR.getCode(), ResultCode.NULL_ERROR.getMessage());
public Boolean completeFortuneAccount(CompleteFortuneAccountRequest req) {
/* 1. 参数校验 & 查询 --------------------------------------------------*/
List<String> accountBizIds = req.getFortuneAccountBizIdList();
if (CollectionUtils.isEmpty(accountBizIds)) {
throw new BusinessException("出账清单不能为空");
}
// 当前登录用户
AuthUserDto currentLoginUser = SecurityUtil.getCurrentLoginUser();
Long loginUserId = currentLoginUser.getId();
// 更新 FortuneAccount 状态为已出账
for (FortuneAccount fortuneAccount : fortuneAccountList) {
fortuneAccount.setStatus(FortuneAccountStatusEnum.SENT.getItemValue());
fortuneAccount.setFortuneAccountDate(new Date());
fortuneAccount.setUpdaterId(loginUserId.toString());
fortuneAccount.setUpdateTime(new Date());
List<FortuneAccount> accountList = this.lambdaQuery()
.in(FortuneAccount::getFortuneAccountBizId, accountBizIds)
.list();
Long loginUserId = SecurityUtil.getCurrentLoginUser().getId();
/* 2. 更新 FortuneAccount 状态为已出账 ----------------------------------*/
accountList.forEach(a -> {
a.setStatus(FortuneAccountStatusEnum.SENT.getItemValue());
a.setFortuneAccountDate(new Date());
a.setUpdaterId(loginUserId.toString());
a.setUpdateTime(new Date());
});
this.updateBatchById(accountList);
/* 3. 取出关联的所有 fortune 并一次性更新为已出账 ------------------------*/
List<String> accountBizIdList = accountList.stream()
.map(FortuneAccount::getFortuneAccountBizId).collect(Collectors.toList());
List<Fortune> fortunes = fortuneService.lambdaQuery()
.in(Fortune::getFortuneAccountBizId, accountBizIdList)
.eq(Fortune::getStatus, FortuneStatusEnum.CHECKED.getItemValue())
.list();
if (CollectionUtils.isNotEmpty(fortunes)) {
fortunes.forEach(f -> {
f.setStatus(FortuneStatusEnum.SENT.getItemValue());
f.setUpdaterId(loginUserId.toString());
f.setUpdateTime(new Date());
});
fortuneService.updateBatchById(fortunes);
}
this.updateBatchById(fortuneAccountList);
// 更新 Fortune 状态为已出账
List<String> accountBizIdList = fortuneAccountList.stream().map(FortuneAccount::getFortuneAccountBizId).collect(Collectors.toList());
List<Fortune> updateFortuneList = fortuneService.list(new QueryWrapper<Fortune>().in("fortune_account_biz_id", accountBizIdList));
if (CollectionUtils.isNotEmpty(updateFortuneList)) {
for (Fortune item : updateFortuneList) {
item.setStatus(FortuneStatusEnum.SENT.getItemValue());
item.setUpdaterId(loginUserId.toString());
item.setUpdateTime(new Date());
}
fortuneService.updateBatchById(updateFortuneList);
/* 4. 按 expected_fortune 维度重新汇总已出账金额 --------------------------*/
// 4.1 收集所有涉及的 expectedFortuneBizId
Set<String> expectedIds = fortunes.stream()
.map(Fortune::getExpectedFortuneBizId)
.collect(Collectors.toSet());
if (CollectionUtils.isEmpty(expectedIds)) {
return true; // 没有对应预计出账,直接结束
}
// 更新预计出账记录
List<String> expectedFortuneBizIdList = updateFortuneList.stream().map(Fortune::getExpectedFortuneBizId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(expectedFortuneBizIdList)) {
List<ExpectedFortune> expectedFortuneList = expectedFortuneService.list(new QueryWrapper<ExpectedFortune>().in("expected_fortune_biz_id", expectedFortuneBizIdList));
// 预计出账映射
Map<String, ExpectedFortune> expectedFortuneMap = expectedFortuneList.stream()
.collect(Collectors.toMap(ExpectedFortune::getExpectedFortuneBizId, Function.identity()));
// 遍历本次出账记录,更新预计出账记录
for (Fortune item : updateFortuneList) {
ExpectedFortune expectedFortune = expectedFortuneMap.get(item.getExpectedFortuneBizId());
if (Objects.nonNull(expectedFortune)) {
BigDecimal currentPaymentAmount = item.getCurrentPaymentAmount();// 本次出账金额
BigDecimal paidAmount = expectedFortune.getPaidAmount();// 已出账金额
if (expectedFortune.getAmount().compareTo(paidAmount.add(currentPaymentAmount)) == 0) {
// 已出账金额等于预计出账金额,更新状态为完成出账
expectedFortune.setPaidAmount(paidAmount.add(currentPaymentAmount));
expectedFortune.setUnpaidAmount(BigDecimal.ZERO);
expectedFortune.setStatus(FortuneStatusEnum.SENT.getItemValue());
// 4.2 先锁再算
List<ExpectedFortune> expectedList = expectedFortuneService.lambdaQuery()
.in(ExpectedFortune::getExpectedFortuneBizId, expectedIds)
.last("FOR UPDATE")
.list();
// 4.3 全量汇总:把同一 expected_fortune 下所有 fortune 的 current_payment_amount 求和
Map<String, BigDecimal> paidMap = fortuneService.lambdaQuery()
.in(Fortune::getExpectedFortuneBizId, expectedIds)
.eq(Fortune::getStatus, FortuneStatusEnum.SENT.getItemValue())
.list()
.stream()
.collect(Collectors.groupingBy(Fortune::getExpectedFortuneBizId,
Collectors.mapping(Fortune::getCurrentPaymentAmount,
Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
// 4.4 反写 expected_fortune
expectedList.forEach(ef -> {
BigDecimal totalPaid = paidMap.getOrDefault(ef.getExpectedFortuneBizId(), BigDecimal.ZERO);
BigDecimal totalAmount = ef.getAmount();
BigDecimal unpaid = totalAmount.subtract(totalPaid);
String newStatus;
if (unpaid.compareTo(BigDecimal.ZERO) == 0) {
newStatus = FortuneStatusEnum.SENT.getItemValue(); // 全部完成
} else if (totalPaid.compareTo(BigDecimal.ZERO) > 0) {
newStatus = FortuneStatusEnum.PARTIAL_SENT.getItemValue(); // 部分完成
} else {
// 已出账金额小于预计出账金额,更新已出账金额
expectedFortune.setPaidAmount(paidAmount.add(currentPaymentAmount));
expectedFortune.setUnpaidAmount(expectedFortune.getAmount().subtract(paidAmount.add(currentPaymentAmount)));
expectedFortune.setStatus(FortuneStatusEnum.PARTIAL_SENT.getItemValue());
expectedFortune.setUpdaterId(loginUserId.toString());
expectedFortune.setUpdateTime(LocalDateTime.now());
newStatus = ef.getStatus(); // 无变化
}
// 更新预计出账记录
expectedFortuneService.lambdaUpdate()
.set(ExpectedFortune::getPaidAmount, expectedFortune.getPaidAmount())
.set(ExpectedFortune::getUnpaidAmount, expectedFortune.getUnpaidAmount())
.set(ExpectedFortune::getStatus, expectedFortune.getStatus())
.set(ExpectedFortune::getUpdaterId, expectedFortune.getUpdaterId())
.set(ExpectedFortune::getUpdateTime, expectedFortune.getUpdateTime())
.eq(ExpectedFortune::getId, expectedFortune.getId())
.set(ExpectedFortune::getPaidAmount, totalPaid)
.set(ExpectedFortune::getUnpaidAmount, unpaid)
.set(ExpectedFortune::getPaidRatio,
totalPaid.divide(totalAmount, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))
.set(ExpectedFortune::getUnpaidRatio,
unpaid.divide(totalAmount, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)))
.set(ExpectedFortune::getStatus, newStatus)
.set(ExpectedFortune::getUpdaterId, loginUserId.toString())
.set(ExpectedFortune::getUpdateTime, LocalDateTime.now())
.eq(ExpectedFortune::getId, ef.getId())
.update();
}
}
}
});
return true;
}
......
......@@ -187,6 +187,9 @@ public class FortuneServiceImpl extends ServiceImpl<FortuneMapper, Fortune>
if (currentPaymentAmount.compareTo(expectedFortune.getAmount()) > 0) {
throw new BusinessException(ResultCode.PARAM_CHECK_ERROR.getCode(), "出账金额不能大于应付款金额");
}
if (StringUtils.isBlank(fortuneUpdateRequest.getCurrency())) {
throw new BusinessException(ResultCode.PARAM_CHECK_ERROR.getCode(), "出账币种不能为空");
}
}
// 获取当前登录用户
......@@ -319,6 +322,9 @@ public class FortuneServiceImpl extends ServiceImpl<FortuneMapper, Fortune>
if (!StringUtils.equals(fortune.getStatus(), FortuneStatusEnum.CHECKED.getItemValue())) {
validateMsg.append(fortune.getPolicyNo()).append("-").append(fortune.getBroker()).append("未检核,不能生成出账记录; ");
}
if (StringUtils.equals(fortune.getStatus(), FortuneStatusEnum.SENT.getItemValue())) {
validateMsg.append(fortune.getPolicyNo()).append("-").append(fortune.getBroker()).append("已完成出账,不能生成出账记录; ");
}
}
if (StringUtils.isNotBlank(validateMsg.toString())) {
throw new BusinessException(ResultCode.FAIL.getCode(), validateMsg.toString());
......
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