|  |  |  | 
|---|
|  |  |  | package cc.mrbird.febs.mall.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.FlowTypeEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum; | 
|---|
|  |  |  | import cc.mrbird.febs.common.exception.FebsException; | 
|---|
|  |  |  | import cc.mrbird.febs.common.utils.LoginUserUtil; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.dto.CommissionChangeDto; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.MallMember; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.entity.MallMemberWallet; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.mapper.MallMemberWalletMapper; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IApiMallMemberWalletService; | 
|---|
|  |  |  | import cc.mrbird.febs.mall.service.IMallMoneyFlowService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import lombok.RequiredArgsConstructor; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.lang.reflect.Field; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | 
|---|
|  |  |  | @RequiredArgsConstructor | 
|---|
|  |  |  | public class ApiMallMemberWalletServiceImpl extends ServiceImpl<MallMemberWalletMapper, MallMemberWallet> implements IApiMallMemberWalletService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private final IMallMoneyFlowService moneyFlowService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void addBalance(BigDecimal amount, Long memberId) { | 
|---|
|  |  |  | 
|---|
|  |  |  | while (flag) { | 
|---|
|  |  |  | i++; | 
|---|
|  |  |  | MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId); | 
|---|
|  |  |  | wallet.setCommission(wallet.getCommission().add(amount)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MallMemberWallet update = new MallMemberWallet(); | 
|---|
|  |  |  | update.setId(wallet.getId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | declaredField = MallMemberWallet.class.getDeclaredField(field); | 
|---|
|  |  |  | declaredField.setAccessible(true); | 
|---|
|  |  |  | declaredField.set(update, amount); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BigDecimal balance = (BigDecimal) declaredField.get(wallet); | 
|---|
|  |  |  | declaredField.set(update, balance.add(amount)); | 
|---|
|  |  |  | } catch (NoSuchFieldException | IllegalAccessException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | throw new FebsException("金额新增失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int result = this.baseMapper.updateAmountWithVersion(wallet); | 
|---|
|  |  |  | int result = this.baseMapper.updateAmountWithVersion(update); | 
|---|
|  |  |  | if (result > 0) { | 
|---|
|  |  |  | flag = false; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (amount.compareTo(balance) > 0) { | 
|---|
|  |  |  | throw new FebsException("余额不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | declaredField.set(update, wallet.getCommission().subtract(amount)); | 
|---|
|  |  |  | declaredField.set(update, balance.subtract(amount)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (NoSuchFieldException | IllegalAccessException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int result = this.baseMapper.updateAmountWithVersion(wallet); | 
|---|
|  |  |  | int result = this.baseMapper.updateAmountWithVersion(update); | 
|---|
|  |  |  | if (result > 0) { | 
|---|
|  |  |  | flag = false; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = Exception.class) | 
|---|
|  |  |  | public void commissionChange(CommissionChangeDto commissionChange) { | 
|---|
|  |  |  | MallMember member = LoginUserUtil.getLoginUser(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // TODO 判断是否开启了划转 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int type; | 
|---|
|  |  |  | int flowType; | 
|---|
|  |  |  | this.reduce(commissionChange.getAmount(), member.getId(), "commission"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 佣金转余额 | 
|---|
|  |  |  | if (commissionChange.getType() == 1) { | 
|---|
|  |  |  | type = MoneyFlowTypeEnum.COMMISSION_TO_BALANCE.getValue(); | 
|---|
|  |  |  | flowType = FlowTypeEnum.BALANCE.getValue(); | 
|---|
|  |  |  | this.add(commissionChange.getAmount(), member.getId(), "balance"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 佣金转竞猜积分 | 
|---|
|  |  |  | } else if (commissionChange.getType() == 2){ | 
|---|
|  |  |  | type = MoneyFlowTypeEnum.COMMISSION_TO_PRIZESCORE.getValue(); | 
|---|
|  |  |  | flowType = FlowTypeEnum.PRIZE_SCORE.getValue(); | 
|---|
|  |  |  | this.add(commissionChange.getAmount(), member.getId(), "prizeScore"); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new FebsException("参数错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | moneyFlowService.addMoneyFlow(member.getId(), commissionChange.getAmount().negate(), type, null,  FlowTypeEnum.COMMISSION.getValue()); | 
|---|
|  |  |  | moneyFlowService.addMoneyFlow(member.getId(), commissionChange.getAmount(), type, null,  flowType); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|