KKSU
2024-08-22 7a372033d44ba48383d9d1bc97e5d583b26a266f
src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
@@ -8,6 +8,7 @@
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.common.utils.MallUtils;
import cc.mrbird.febs.mall.dto.WithdrawalBalanceDto;
import cc.mrbird.febs.mall.dto.WithdrawalDto;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
@@ -16,6 +17,7 @@
import cc.mrbird.febs.mall.service.IMallMemberWithdrawService;
import cc.mrbird.febs.mall.service.MallMemberService;
import cc.mrbird.febs.mall.vo.CashOutSettingVo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
@@ -27,6 +29,8 @@
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * @author wzy
@@ -44,12 +48,19 @@
    private final MallMemberPaymentMapper mallMemberPaymentMapper;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallMemberBankMapper mallMemberBankMapper;
    private final MallMemberAmountMapper mallMemberAmountMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withdrawal(WithdrawalDto withdrawalDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = mallMemberService.getById(memberId);
        Integer outsideWith = mallMember.getOutsideWith() == null ? 2 :mallMember.getOutsideWith();
        if (1 != outsideWith) {
            throw new FebsException("功能升级中");
        }
        if (StrUtil.isBlank(mallMember.getTradePassword())) {
            throw new FebsException("未设置支付密码");
        }
@@ -64,6 +75,20 @@
            cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class);
        }
        //每日提现额度上限
        BigDecimal allCashOut = cashOutSettingVo.getAllCashOut();
        if(withdrawalDto.getAmount().compareTo(allCashOut) > 0){
            throw new FebsException("每日限额为"+cashOutSettingVo.getAllCashOut().setScale(2,BigDecimal.ROUND_DOWN));
        }
        List<MallMemberWithdraw> mallMemberWithdrawListDone = this.baseMapper.selectListByMemberIdAndDate(memberId,new Date());
        if(CollUtil.isNotEmpty(mallMemberWithdrawListDone)){
            BigDecimal amountTCAll = mallMemberWithdrawListDone.stream().map(MallMemberWithdraw::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            amountTCAll = amountTCAll.add(withdrawalDto.getAmount());
            if (amountTCAll.compareTo(allCashOut) > 0) {
                throw new FebsException("每日限额为"+cashOutSettingVo.getAllCashOut().setScale(2,BigDecimal.ROUND_DOWN));
            }
        }
        if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) {
            throw new FebsException("最小提现金额为"+cashOutSettingVo.getMinCashOut().setScale(2,BigDecimal.ROUND_DOWN));
        }
@@ -72,6 +97,15 @@
        if(ObjectUtil.isEmpty(mallMemberBank)){
            throw new FebsException("未找到银行卡信息");
        }
        if(ObjectUtil.isEmpty(mallMemberBank.getBankName())){
            throw new FebsException("请填写所属银行");
        }
        if(ObjectUtil.isEmpty(mallMemberBank.getSubbranchName())){
            throw new FebsException("请填写所属支行");
        }
//        if(ObjectUtil.isEmpty(mallMemberBank.getDigitalNo())){
//            throw new FebsException("请填写数字货币账号");
//        }
        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        // 可提现
@@ -99,22 +133,30 @@
            if("1".equals(voucherOnOff)){
                BigDecimal voucherCntDto = withdrawalDto.getVoucherCnt();
                if(voucherCntDto.compareTo(BigDecimal.ZERO) <= 0){
                    throw new FebsException("绿色积分不足");
                    throw new FebsException("能量不足");
                }
                //提现需要的凭证数量 = 提现金额 * 余额提现需要绿色凭证比例
                DataDictionaryCustom voucherPercentDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                        GreenScoreEnum.VOUCHER_PERCENT.getType(),
                        GreenScoreEnum.VOUCHER_PERCENT.getCode()
                );
                BigDecimal voucherPercent = new BigDecimal(voucherPercentDic.getValue() == null ? "0" : voucherPercentDic.getValue());
                BigDecimal voucherPercent = new BigDecimal(voucherPercentDic.getValue() == null ? "0" : voucherPercentDic.getValue()).multiply(new BigDecimal(0.01));
                BigDecimal voucherPercentNeed = withdrawalDto.getAmount().multiply(voucherPercent).setScale(2,BigDecimal.ROUND_DOWN);
                BigDecimal voucherCnt = wallet.getVoucherCnt();
                if(voucherCnt.compareTo(voucherPercentNeed) < 0){
                    throw new FebsException("绿色积分不足");
                DataDictionaryCustom scorePriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                        GreenScoreEnum.SCORE_PRICE.getType(),
                        GreenScoreEnum.SCORE_PRICE.getCode());
                BigDecimal scorePrice = new BigDecimal(scorePriceDic.getValue()).setScale(2, BigDecimal.ROUND_DOWN);
                voucherPercentNeed = voucherPercentNeed.divide(scorePrice,2,BigDecimal.ROUND_DOWN);
//                BigDecimal voucherCnt = wallet.getVoucherCnt();
                BigDecimal voucherFireCnt = wallet.getVoucherFireCnt();
                if(voucherFireCnt.compareTo(voucherPercentNeed) < 0){
                    throw new FebsException("能量不足");
                }
                walletService.reduce(voucherCntDto, memberId, "voucher_cnt");
//                walletService.reduce(voucherCntDto, memberId, "voucherCnt");
                walletService.reduce(voucherCntDto, memberId, "voucherFireCnt");
                serviceFee = voucherCntDto;
                remark = AppContants.MEMBER_WITHDRAW_VOUCHER;
            }
@@ -136,4 +178,51 @@
        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.BALANCE.getValue());
    }
    @Override
    public void withdrawalBalance(WithdrawalBalanceDto withdrawalBalanceDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = mallMemberService.getById(memberId);
        if(ObjectUtil.isEmpty(mallMember.getBindPhone())){
            throw new FebsException("请绑定充值提取地址");
        }
        DataDictionaryCustom withdrawAmountDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.WITHDRAW_AMOUNT.getType(),
                DataDictionaryEnum.WITHDRAW_AMOUNT.getCode()
        );
        BigDecimal amount = withdrawalBalanceDto.getAmount();
        BigDecimal withdrawAmount = new BigDecimal(withdrawAmountDic.getValue()).setScale(2, BigDecimal.ROUND_DOWN);
        if(withdrawAmount.compareTo(amount) > 0){
            throw new FebsException("最小金额:"+withdrawAmount);
        }
        MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(mallMember.getId());
        BigDecimal balance = mallMemberWallet.getBalance();
        if(amount.compareTo(balance) > 0){
            throw new FebsException("账户余额不足");
        }
        DataDictionaryCustom feeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                DataDictionaryEnum.WITHDRAW_AMOUNT_FEE.getType(),
                DataDictionaryEnum.WITHDRAW_AMOUNT_FEE.getCode()
        );
        BigDecimal fee = new BigDecimal(feeDic.getValue())
                .multiply(new BigDecimal("0.01"))
                .multiply(amount)
                .setScale(2, BigDecimal.ROUND_DOWN);
        mallMemberWallet.setBalance(mallMemberWallet.getBalance().subtract(amount));
        mallMemberWalletMapper.updateBalanceWithVersion(mallMemberWallet);
        String orderNo = MallUtils.getOrderNum("W");
        MallMemberWithdraw withdraw = new MallMemberWithdraw();
        withdraw.setWithdrawNo(orderNo);
        withdraw.setMemberId(memberId);
        withdraw.setAmount(amount.negate());
        withdraw.setStatus(1);
        withdraw.setAmountFee(fee);
        withdraw.setRemark(mallMember.getBindPhone());
        this.baseMapper.insert(withdraw);
    }
}