pull/1121/head
梁宇奇 2025-08-21 15:01:56 +08:00
parent fba3c5953e
commit d3925e729d
3 changed files with 58 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import com.ruoyi.xkt.enums.EEntryStatus;
import com.ruoyi.xkt.enums.EFinBillType;
import com.ruoyi.xkt.enums.ELoanDirection;
import java.util.Collection;
import java.util.List;
/**
@ -26,6 +27,14 @@ public interface IInternalAccountService {
*/
InternalAccount getById(Long id);
/**
* ids
*
* @param ids
* @return
*/
List<InternalAccount> listByIdsForUpdate(Collection<Long> ids);
/**
*
*
@ -56,6 +65,18 @@ public interface IInternalAccountService {
int addTransDetail(Long internalAccountId, TransInfo transInfo, ELoanDirection loanDirection,
EEntryStatus entryStatus);
/**
*
*
* @param internalAccount
* @param transInfo
* @param loanDirection
* @param entryStatus
* @return
*/
int addTransDetail(InternalAccount internalAccount, TransInfo transInfo, ELoanDirection loanDirection,
EEntryStatus entryStatus);
/**
*
*

View File

@ -6,6 +6,7 @@ import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -125,7 +126,6 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
@Transactional(rollbackFor = Exception.class)
@Override
public FinanceBillExt createOrderCompletedTransferBill(StoreOrderExt orderExt) {
//TODO 需要先批量获取内部账户避免出现死锁?
Assert.notNull(orderExt);
FinanceBill bill = new FinanceBill();
bill.setBillNo(generateBillNo(EFinBillType.TRANSFER));
@ -172,9 +172,11 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
.remark("订单完成")
.build();
//内部账户
internalAccountService.addTransDetail(Constants.PLATFORM_INTERNAL_ACCOUNT_ID, transInfo,
Map<Long, InternalAccount> accountMap = getInternalAccountMapForUpdate(Constants.PLATFORM_INTERNAL_ACCOUNT_ID,
inputInternalAccountId);
internalAccountService.addTransDetail(accountMap.get(Constants.PLATFORM_INTERNAL_ACCOUNT_ID), transInfo,
ELoanDirection.CREDIT, EEntryStatus.FINISH);
internalAccountService.addTransDetail(inputInternalAccountId, transInfo,
internalAccountService.addTransDetail(accountMap.get(inputInternalAccountId), transInfo,
ELoanDirection.DEBIT, EEntryStatus.FINISH);
return new FinanceBillExt(bill, billDetails);
}
@ -183,7 +185,6 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
@Override
public FinanceBillExt createOrderCompletedTransferBill(StoreOrderExt orderExt,
List<StoreOrderExt> afterSaleOrderExts) {
//TODO 需要先批量获取内部账户避免出现死锁?
Assert.notNull(orderExt);
FinanceBill bill = new FinanceBill();
bill.setBillNo(generateBillNo(EFinBillType.TRANSFER));
@ -294,9 +295,11 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
.remark("订单完成")
.build();
//内部账户
internalAccountService.addTransDetail(Constants.PLATFORM_INTERNAL_ACCOUNT_ID, transInfo,
Map<Long, InternalAccount> accountMap = getInternalAccountMapForUpdate(Constants.PLATFORM_INTERNAL_ACCOUNT_ID,
inputInternalAccountId);
internalAccountService.addTransDetail(accountMap.get(Constants.PLATFORM_INTERNAL_ACCOUNT_ID), transInfo,
ELoanDirection.CREDIT, EEntryStatus.FINISH);
internalAccountService.addTransDetail(inputInternalAccountId, transInfo,
internalAccountService.addTransDetail(accountMap.get(inputInternalAccountId), transInfo,
ELoanDirection.DEBIT, EEntryStatus.FINISH);
return new FinanceBillExt(bill, billDetails);
}
@ -541,7 +544,6 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
public FinanceBillExt createInternalTransferBill(Long inputAccountId, Long outputAccountId, BigDecimal amount,
Integer srcType, Long srcId, Integer relType, Long relId,
String remark) {
//TODO 需要先批量获取内部账户避免出现死锁?
Assert.notNull(inputAccountId);
Assert.notNull(outputAccountId);
Assert.notNull(amount);
@ -574,9 +576,11 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
.remark(remark)
.build();
//内部账户
internalAccountService.addTransDetail(outputAccountId, transInfo,
Map<Long, InternalAccount> accountMap = getInternalAccountMapForUpdate(outputAccountId,
inputAccountId);
internalAccountService.addTransDetail(accountMap.get(outputAccountId), transInfo,
ELoanDirection.CREDIT, EEntryStatus.FINISH);
internalAccountService.addTransDetail(inputAccountId, transInfo,
internalAccountService.addTransDetail(accountMap.get(inputAccountId), transInfo,
ELoanDirection.DEBIT, EEntryStatus.FINISH);
return new FinanceBillExt(bill, ListUtil.empty());
}
@ -607,6 +611,15 @@ public class FinanceBillServiceImpl implements IFinanceBillService {
return IdUtil.simpleUUID();
}
private Map<Long, InternalAccount> getInternalAccountMapForUpdate(Long... ids) {
if (ArrayUtil.isEmpty(ids)) {
return MapUtil.empty();
}
return internalAccountService.listByIdsForUpdate(CollUtil.toList(ids))
.stream()
.collect(Collectors.toMap(SimpleEntity::getId, Function.identity()));
}
private <T extends SimpleEntity> T prepareInsert(T obj) {
String username = SecurityUtils.getUsernameSafe();
obj.setCreateBy(username);

View File

@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -50,6 +51,13 @@ public class InternalAccountServiceImpl implements IInternalAccountService {
return internalAccountMapper.selectById(id);
}
@Transactional(rollbackFor = Exception.class)
@Override
public List<InternalAccount> listByIdsForUpdate(Collection<Long> ids) {
Assert.notEmpty(ids);
return internalAccountMapper.listForUpdate(ids);
}
@Override
public InternalAccount getAccount(Long ownerId, EAccountOwnerType ownerType) {
Assert.notNull(ownerId);
@ -80,6 +88,13 @@ public class InternalAccountServiceImpl implements IInternalAccountService {
EEntryStatus entryStatus) {
Assert.notNull(internalAccountId);
InternalAccount internalAccount = internalAccountMapper.getForUpdate(internalAccountId);
return addTransDetail(internalAccount, transInfo, loanDirection, entryStatus);
}
@Transactional(rollbackFor = Exception.class)
@Override
public int addTransDetail(InternalAccount internalAccount, TransInfo transInfo, ELoanDirection loanDirection,
EEntryStatus entryStatus) {
checkAccountStatus(internalAccount);
InternalAccountTransDetail transDetail = new InternalAccountTransDetail();
transDetail.setInternalAccountId(internalAccount.getId());