|
|
|
@ -1,22 +1,22 @@ |
|
|
|
package com.qniao.dam.application.service.virtualaccount; |
|
|
|
|
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.request.UserConsumeVirtualAccountDto; |
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.request.UserCostChannelGiftDto; |
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.request.UserCostIMGiftDto; |
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.response.CostIMGiftVo; |
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.response.CostGiftVo; |
|
|
|
import com.qniao.dam.api.command.virtualaccount.user.response.UserConsumeVirtualAccountVo; |
|
|
|
import com.qniao.dam.domain.aggregate.order.entity.Order; |
|
|
|
import com.qniao.dam.domain.aggregate.order.entity.OrderItem; |
|
|
|
import com.qniao.dam.domain.aggregate.product.entity.Product; |
|
|
|
import com.qniao.dam.domain.aggregate.productspec.entity.ProductSpec; |
|
|
|
import com.qniao.dam.domain.aggregate.revenuereward.entity.RevenueReward; |
|
|
|
import com.qniao.dam.domain.aggregate.revenuereward.valueobj.RevenueRewardAssociateOrderRecord; |
|
|
|
import com.qniao.dam.domain.aggregate.revenuereward.valueobj.RevenueRewardRecord; |
|
|
|
import com.qniao.dam.domain.aggregate.virtualaccount.VirtualAccountAggregate; |
|
|
|
import com.qniao.dam.domain.aggregate.virtualaccount.entity.VirtualAccount; |
|
|
|
import com.qniao.dam.domain.aggregate.walletaccount.entity.WalletAccount; |
|
|
|
import com.qniao.dam.domain.aggregate.walletaccount.valueobj.WalletAccountRecord; |
|
|
|
import com.qniao.dam.domain.service.virtualaccount.ConsumeVirtualAccountDomainService; |
|
|
|
import com.qniao.dam.domain.service.virtualaccount.CostIMGiftRewardDomainService; |
|
|
|
import com.qniao.dam.domain.service.virtualaccount.CostGiftRewardDomainService; |
|
|
|
import com.qniao.dam.domian.aggregate.order.constant.OrderBelongingEnum; |
|
|
|
import com.qniao.dam.domian.aggregate.order.constant.OrderStatusEnum; |
|
|
|
import com.qniao.dam.domian.aggregate.order.constant.OrderTypeEnum; |
|
|
|
@ -35,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.math.RoundingMode; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Objects; |
|
|
|
@ -59,7 +60,7 @@ public class VirtualAccountApplicationService { |
|
|
|
@Resource |
|
|
|
private ConsumeVirtualAccountDomainService consumeVirtualAccountDomainService; |
|
|
|
@Resource |
|
|
|
private CostIMGiftRewardDomainService costIMGiftRewardDomainService; |
|
|
|
private CostGiftRewardDomainService costGiftRewardDomainService; |
|
|
|
|
|
|
|
public void create(VirtualAccount virtualAccount) { |
|
|
|
virtualAccountAggregate.create(virtualAccount); |
|
|
|
@ -130,9 +131,9 @@ public class VirtualAccountApplicationService { |
|
|
|
* @param dto |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public CostIMGiftVo costIMGift(UserCostIMGiftDto dto) { |
|
|
|
public CostGiftVo costIMGift(UserCostIMGiftDto dto) { |
|
|
|
//判断虚拟账户余额 |
|
|
|
CostIMGiftVo vo = new CostIMGiftVo(); |
|
|
|
CostGiftVo vo = new CostGiftVo(); |
|
|
|
synchronized (dto.getFromUserId().toString().intern()) { |
|
|
|
VirtualAccount virtualAccount = virtualAccountQueryService.queryBy(dto.getFromUserId()); |
|
|
|
//找到对应产品 |
|
|
|
@ -146,13 +147,13 @@ public class VirtualAccountApplicationService { |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public CostIMGiftVo handleIMGiftReward(VirtualAccount virtualAccount, |
|
|
|
Long fromUserId, |
|
|
|
Long toUserId, |
|
|
|
ProductSpec productSpec, |
|
|
|
Integer costBalance, |
|
|
|
Integer quantity) { |
|
|
|
CostIMGiftVo vo = new CostIMGiftVo(); |
|
|
|
public CostGiftVo handleIMGiftReward(VirtualAccount virtualAccount, |
|
|
|
Long fromUserId, |
|
|
|
Long toUserId, |
|
|
|
ProductSpec productSpec, |
|
|
|
Integer costBalance, |
|
|
|
Integer quantity) { |
|
|
|
CostGiftVo vo = new CostGiftVo(); |
|
|
|
//余额充足 创建消费订单+扣除余额 |
|
|
|
Order order = handleOrder(fromUserId, productSpec, quantity); |
|
|
|
//虚拟账户处理 |
|
|
|
@ -164,7 +165,7 @@ public class VirtualAccountApplicationService { |
|
|
|
revenueReward = RevenueReward.initUser(toUserId); |
|
|
|
} |
|
|
|
//营收额 |
|
|
|
BigDecimal revenue = BigDecimal.valueOf(costBalance).multiply(BigDecimal.valueOf(0.1428)).multiply(BigDecimal.valueOf(0.4)); |
|
|
|
BigDecimal revenue = BigDecimal.valueOf(costBalance).multiply(BigDecimal.valueOf(0.1428)).multiply(BigDecimal.valueOf(0.4)).setScale(2, RoundingMode.HALF_UP); |
|
|
|
|
|
|
|
RevenueRewardRecord revenueRewardRecord = RevenueRewardRecord.build(TradeTypeEnum.GIFT_REWARD, TradeSceneEnum.ONLINE, |
|
|
|
revenue, BigDecimal.valueOf(100), BigDecimal.ZERO, revenue, BigDecimal.valueOf(costBalance)); |
|
|
|
@ -180,9 +181,132 @@ public class VirtualAccountApplicationService { |
|
|
|
WalletAccountRecord walletAccountRecord = WalletAccountRecord.build(TradeTypeEnum.GIFT_REWARD, revenue, true, originalBalance, walletAccount.getTotalBalance()); |
|
|
|
walletAccount.setRecordList(Collections.singletonList(walletAccountRecord)); |
|
|
|
|
|
|
|
costIMGiftRewardDomainService.handle(order, virtualAccount, revenueReward, walletAccount); |
|
|
|
costGiftRewardDomainService.handle(order, virtualAccount, revenueReward, walletAccount); |
|
|
|
vo.setSuccess(true); |
|
|
|
return vo; |
|
|
|
} |
|
|
|
|
|
|
|
public CostGiftVo costChannelGift(UserCostChannelGiftDto dto) { |
|
|
|
CostGiftVo vo = new CostGiftVo(); |
|
|
|
|
|
|
|
VirtualAccount virtualAccount = virtualAccountQueryService.queryBy(dto.getFromUserId()); |
|
|
|
//找到对应产品 |
|
|
|
ProductSpec productSpec = productSpecDao.selectById(dto.getProductSpecId()); |
|
|
|
Integer costBalance = productSpec.getUnitSellingPrice().intValue() * dto.getQuantity(); |
|
|
|
if (virtualAccount != null && virtualAccount.getBalance() >= costBalance) { |
|
|
|
if (dto.getDirection() == 1) { |
|
|
|
//直接送主播 |
|
|
|
vo = handleChannelAnchorGiftReward(virtualAccount, dto.getFromUserId(), dto.getChannelCreatorId(), productSpec, costBalance, dto.getQuantity()); |
|
|
|
} else { |
|
|
|
//直接送嘉宾 |
|
|
|
vo = handleChannelUserGiftReward(virtualAccount, dto.getFromUserId(), dto.getToUserId(), dto.getChannelCreatorId(), productSpec, costBalance, dto.getQuantity()); |
|
|
|
} |
|
|
|
} |
|
|
|
return vo; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 赠送嘉宾播礼物 |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public CostGiftVo handleChannelUserGiftReward(VirtualAccount virtualAccount, |
|
|
|
Long fromUserId, |
|
|
|
Long toUserId, |
|
|
|
Long anchorId, |
|
|
|
ProductSpec productSpec, |
|
|
|
Integer costBalance, |
|
|
|
Integer quantity) { |
|
|
|
CostGiftVo vo = new CostGiftVo(); |
|
|
|
//余额充足 创建消费订单+扣除余额 |
|
|
|
Order order = handleOrder(fromUserId, productSpec, quantity); |
|
|
|
//虚拟账户处理 |
|
|
|
virtualAccountAggregate.handleConsumeData(virtualAccount, order.getSettlementAmount().intValue(), VirtualAccountTradeTypeEnum.GIFT); |
|
|
|
|
|
|
|
//收益信息 |
|
|
|
RevenueReward toRevenueReward = revenueRewardQueryService.queryByUserId(toUserId); |
|
|
|
if (Objects.isNull(toRevenueReward)) { |
|
|
|
toRevenueReward = RevenueReward.initUser(toUserId); |
|
|
|
} |
|
|
|
//营收额 todo |
|
|
|
BigDecimal toRevenue = BigDecimal.valueOf(costBalance).multiply(BigDecimal.valueOf(0.1428)).multiply(BigDecimal.valueOf(0.1)).setScale(2, RoundingMode.HALF_UP); |
|
|
|
RevenueRewardRecord toRevenueRewardRecord = RevenueRewardRecord.build(TradeTypeEnum.GIFT_REWARD, TradeSceneEnum.ONLINE, |
|
|
|
toRevenue, BigDecimal.valueOf(100), BigDecimal.ZERO, toRevenue, BigDecimal.valueOf(costBalance)); |
|
|
|
//todo |
|
|
|
toRevenueRewardRecord.setContent("直播间礼物"); |
|
|
|
toRevenueReward.setRecordList(Collections.singletonList(toRevenueRewardRecord)); |
|
|
|
//钱包 |
|
|
|
WalletAccount toWalletAccount = walletAccountQueryService.queryByType(toUserId, IdentityTypeEnum.INDIVIDUAL); |
|
|
|
BigDecimal toOriginalBalance = toWalletAccount.getTotalBalance(); |
|
|
|
toWalletAccount.setAvailableBalance(toWalletAccount.getAvailableBalance().add(toRevenue)); |
|
|
|
toWalletAccount.setTotalBalance(toWalletAccount.getAvailableBalance().add(toWalletAccount.getFrozenBalance())); |
|
|
|
WalletAccountRecord toWalletAccountRecord = WalletAccountRecord.build(TradeTypeEnum.GIFT_REWARD, toRevenue, true, toOriginalBalance, toWalletAccount.getTotalBalance()); |
|
|
|
toWalletAccount.setRecordList(Collections.singletonList(toWalletAccountRecord)); |
|
|
|
|
|
|
|
//收益信息 |
|
|
|
RevenueReward anchorRevenueReward = revenueRewardQueryService.queryByUserId(anchorId); |
|
|
|
if (Objects.isNull(anchorRevenueReward)) { |
|
|
|
anchorRevenueReward = RevenueReward.initUser(anchorId); |
|
|
|
} |
|
|
|
//营收额 todo |
|
|
|
BigDecimal anchorRevenue = BigDecimal.valueOf(costBalance).multiply(BigDecimal.valueOf(0.1428)).multiply(BigDecimal.valueOf(0.3)).setScale(2, RoundingMode.HALF_UP); |
|
|
|
RevenueRewardRecord anchorRevenueRewardRecord = RevenueRewardRecord.build(TradeTypeEnum.GIFT_REWARD, TradeSceneEnum.ONLINE, |
|
|
|
anchorRevenue, BigDecimal.valueOf(100), BigDecimal.ZERO, anchorRevenue, BigDecimal.valueOf(costBalance)); |
|
|
|
//todo |
|
|
|
anchorRevenueRewardRecord.setContent("直播间礼物"); |
|
|
|
anchorRevenueReward.setRecordList(Collections.singletonList(anchorRevenueRewardRecord)); |
|
|
|
//钱包 |
|
|
|
WalletAccount anchorWalletAccount = walletAccountQueryService.queryByType(anchorId, IdentityTypeEnum.INDIVIDUAL); |
|
|
|
BigDecimal anchorOriginalBalance = anchorWalletAccount.getTotalBalance(); |
|
|
|
anchorWalletAccount.setAvailableBalance(anchorWalletAccount.getAvailableBalance().add(anchorRevenue)); |
|
|
|
anchorWalletAccount.setTotalBalance(anchorWalletAccount.getAvailableBalance().add(anchorWalletAccount.getFrozenBalance())); |
|
|
|
WalletAccountRecord anchorWalletAccountRecord = WalletAccountRecord.build(TradeTypeEnum.GIFT_REWARD, anchorRevenue, true, anchorOriginalBalance, anchorWalletAccount.getTotalBalance()); |
|
|
|
anchorWalletAccount.setRecordList(Collections.singletonList(anchorWalletAccountRecord)); |
|
|
|
|
|
|
|
costGiftRewardDomainService.handle(order, virtualAccount, toRevenueReward, toWalletAccount, anchorRevenueReward, anchorWalletAccount); |
|
|
|
vo.setSuccess(true); |
|
|
|
return vo; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 赠送主播礼物 |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public CostGiftVo handleChannelAnchorGiftReward(VirtualAccount virtualAccount, |
|
|
|
Long fromUserId, |
|
|
|
Long toUserId, |
|
|
|
ProductSpec productSpec, |
|
|
|
Integer costBalance, |
|
|
|
Integer quantity) { |
|
|
|
CostGiftVo vo = new CostGiftVo(); |
|
|
|
//余额充足 创建消费订单+扣除余额 |
|
|
|
Order order = handleOrder(fromUserId, productSpec, quantity); |
|
|
|
//虚拟账户处理 |
|
|
|
virtualAccountAggregate.handleConsumeData(virtualAccount, order.getSettlementAmount().intValue(), VirtualAccountTradeTypeEnum.GIFT); |
|
|
|
|
|
|
|
//收益信息 |
|
|
|
RevenueReward revenueReward = revenueRewardQueryService.queryByUserId(toUserId); |
|
|
|
if (Objects.isNull(revenueReward)) { |
|
|
|
revenueReward = RevenueReward.initUser(toUserId); |
|
|
|
} |
|
|
|
//营收额 todo |
|
|
|
BigDecimal revenue = BigDecimal.valueOf(costBalance).multiply(BigDecimal.valueOf(0.1428)).multiply(BigDecimal.valueOf(0.4)).setScale(2, RoundingMode.HALF_UP); |
|
|
|
|
|
|
|
RevenueRewardRecord revenueRewardRecord = RevenueRewardRecord.build(TradeTypeEnum.GIFT_REWARD, TradeSceneEnum.ONLINE, |
|
|
|
revenue, BigDecimal.valueOf(100), BigDecimal.ZERO, revenue, BigDecimal.valueOf(costBalance)); |
|
|
|
//todo |
|
|
|
revenueRewardRecord.setContent("直播间礼物"); |
|
|
|
|
|
|
|
revenueReward.setRecordList(Collections.singletonList(revenueRewardRecord)); |
|
|
|
//钱包 |
|
|
|
WalletAccount walletAccount = walletAccountQueryService.queryByType(toUserId, IdentityTypeEnum.INDIVIDUAL); |
|
|
|
BigDecimal originalBalance = walletAccount.getTotalBalance(); |
|
|
|
walletAccount.setAvailableBalance(walletAccount.getAvailableBalance().add(revenue)); |
|
|
|
walletAccount.setTotalBalance(walletAccount.getAvailableBalance().add(walletAccount.getFrozenBalance())); |
|
|
|
WalletAccountRecord walletAccountRecord = WalletAccountRecord.build(TradeTypeEnum.GIFT_REWARD, revenue, true, originalBalance, walletAccount.getTotalBalance()); |
|
|
|
walletAccount.setRecordList(Collections.singletonList(walletAccountRecord)); |
|
|
|
|
|
|
|
costGiftRewardDomainService.handle(order, virtualAccount, revenueReward, walletAccount); |
|
|
|
vo.setSuccess(true); |
|
|
|
return vo; |
|
|
|
} |
|
|
|
} |