| | |
| | | import com.tourguide.modules.finance.entity.GuideWithdrawalAccount; |
| | | import com.tourguide.modules.finance.mapper.GuideWithdrawalMapper; |
| | | import com.tourguide.modules.user.entity.GuideUser; |
| | | import com.tourguide.modules.user.entity.GuideTeam; |
| | | import com.tourguide.modules.user.mapper.GuideTeamMapper; |
| | | import com.tourguide.modules.user.mapper.GuideUserMapper; |
| | | import com.tourguide.modules.user.service.GuideUserService; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | private final GuideWithdrawableBalanceService balanceService; |
| | | private final GuideWithdrawalStateService stateService; |
| | | private final GuideWithdrawalPayoutService payoutService; |
| | | private final GuideTeamMapper teamMapper; |
| | | private final GuideUserMapper guideUserMapper; |
| | | |
| | | public GuideWithdrawalService(GuideWithdrawalMapper withdrawalMapper, |
| | | GuideSettlementService settlementService, |
| | | GuideUserService guideUserService, |
| | | GuideWithdrawableBalanceService balanceService, |
| | | GuideWithdrawalStateService stateService, |
| | | GuideWithdrawalPayoutService payoutService) { |
| | | GuideWithdrawalPayoutService payoutService, |
| | | GuideTeamMapper teamMapper, |
| | | GuideUserMapper guideUserMapper) { |
| | | this.withdrawalMapper = withdrawalMapper; |
| | | this.settlementService = settlementService; |
| | | this.guideUserService = guideUserService; |
| | | this.balanceService = balanceService; |
| | | this.stateService = stateService; |
| | | this.payoutService = payoutService; |
| | | this.teamMapper = teamMapper; |
| | | this.guideUserMapper = guideUserMapper; |
| | | } |
| | | |
| | | public GuideWithdrawalSummaryResponse summary(Long guideId) { |
| | | try { |
| | | settlementService.requireApproved(guideId); |
| | | return new GuideWithdrawalSummaryResponse(balanceService.getAvailableAmount(guideId)); |
| | | GuideUser guide = guideUserService.findEnabledById(guideId); |
| | | GuideTeam collectionTeam = findCollectionTeam(guide); |
| | | return new GuideWithdrawalSummaryResponse(collectionTeam == null |
| | | ? balanceService.getAvailableAmount(guideId) |
| | | : balanceService.getAvailableTeamAmount(collectionTeam.getId())); |
| | | } catch (BusinessException exception) { |
| | | if (exception.getCode() == 409) { |
| | | return new GuideWithdrawalSummaryResponse(money(BigDecimal.ZERO)); |
| | |
| | | |
| | | @Transactional |
| | | public WithdrawalResponse create(Long guideId, GuideWithdrawalCreateRequest request) { |
| | | GuideUser guide = guideUserService.findEnabledById(guideId); |
| | | GuideUser guide = guideUserMapper.selectForUpdate(guideId); |
| | | if (guide == null || !Integer.valueOf(1).equals(guide.getStatus())) { |
| | | throw new BusinessException(401, "导游用户不存在或已停用"); |
| | | } |
| | | GuideWithdrawalAccount account = settlementService.requireApprovedCardForUpdate( |
| | | guideId, request.getBankCardId()); |
| | | BigDecimal amount = money(request.getAmount()); |
| | | if (amount.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new BusinessException(400, "提现金额必须大于0"); |
| | | } |
| | | if (!balanceService.debit(guideId, amount)) { |
| | | GuideTeam collectionTeam = findCollectionTeam(guide); |
| | | boolean debited = collectionTeam == null |
| | | ? balanceService.debit(guideId, amount) |
| | | : balanceService.debitTeam(collectionTeam.getId(), amount); |
| | | if (!debited) { |
| | | throw new BusinessException(409, "提现金额不能超过当前可提金额"); |
| | | } |
| | | GuideWithdrawal withdrawal = new GuideWithdrawal(); |
| | | withdrawal.setWithdrawalNo(generateWithdrawalNo()); |
| | | withdrawal.setGuideId(guideId); |
| | | withdrawal.setTeamId(collectionTeam == null ? null : collectionTeam.getId()); |
| | | withdrawal.setGuideName(StringUtils.hasText(guide.getRealName()) |
| | | ? guide.getRealName() : guide.getUsername()); |
| | | withdrawal.setGuideMobile(guide.getMobile()); |
| | |
| | | + String.format("%04d", ThreadLocalRandom.current().nextInt(10000)); |
| | | } |
| | | |
| | | private GuideTeam findCollectionTeam(GuideUser guide) { |
| | | if (guide == null || guide.getTeamId() == null) return null; |
| | | GuideTeam team = teamMapper.selectById(guide.getTeamId()); |
| | | return team != null && guide.getId().equals(team.getCollectionGuideId()) ? team : null; |
| | | } |
| | | |
| | | private String maskCard(String cardNumber) { |
| | | if (!StringUtils.hasText(cardNumber)) return ""; |
| | | String value = cardNumber.trim(); |