| | |
| | | |
| | | import com.tourguide.common.exception.BusinessException; |
| | | import com.tourguide.modules.user.mapper.GuideUserMapper; |
| | | import com.tourguide.modules.user.mapper.GuideTeamMapper; |
| | | import com.tourguide.modules.user.entity.GuideTeam; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | |
| | | @Service |
| | | public class GuideWithdrawableBalanceService { |
| | | private final GuideUserMapper guideUserMapper; |
| | | private final GuideTeamMapper guideTeamMapper; |
| | | |
| | | public GuideWithdrawableBalanceService(GuideUserMapper guideUserMapper) { |
| | | public GuideWithdrawableBalanceService(GuideUserMapper guideUserMapper, GuideTeamMapper guideTeamMapper) { |
| | | this.guideUserMapper = guideUserMapper; |
| | | this.guideTeamMapper = guideTeamMapper; |
| | | } |
| | | |
| | | public BigDecimal getAvailableAmount(Long guideId) { |
| | |
| | | } |
| | | } |
| | | |
| | | public BigDecimal getAvailableTeamAmount(Long teamId) { |
| | | if (teamId == null) return BigDecimal.ZERO.setScale(2); |
| | | GuideTeam team = guideTeamMapper.selectById(teamId); |
| | | return money(team == null ? null : team.getBalance()); |
| | | } |
| | | |
| | | public boolean debitTeam(Long teamId, BigDecimal amount) { |
| | | BigDecimal value = money(amount); |
| | | return teamId != null && value.signum() > 0 |
| | | && guideTeamMapper.decreaseBalance(teamId, value) == 1; |
| | | } |
| | | |
| | | public void creditTeam(Long teamId, BigDecimal amount) { |
| | | BigDecimal value = money(amount); |
| | | if (value.signum() == 0) return; |
| | | if (teamId == null || value.signum() < 0 |
| | | || guideTeamMapper.increaseBalance(teamId, value) != 1) { |
| | | throw new BusinessException(500, "讲解队余额更新失败"); |
| | | } |
| | | } |
| | | |
| | | private BigDecimal money(BigDecimal value) { |
| | | return (value == null ? BigDecimal.ZERO : value).setScale(2, RoundingMode.HALF_UP); |
| | | } |