KKSU
2024-03-15 b87c5549fe471167882192944db3f2d6b729476c
src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
@@ -1,9 +1,8 @@
package cc.mrbird.febs.mall.service.impl;
import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
import cc.mrbird.febs.common.enumerates.FlowTypeEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.enumerates.*;
import cc.mrbird.febs.common.exception.FebsException;
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.WithdrawalDto;
@@ -14,6 +13,8 @@
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;
import com.alibaba.fastjson.JSONObject;
@@ -21,8 +22,11 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * @author wzy
@@ -39,38 +43,13 @@
    private final IApiMallMemberWalletService walletService;
    private final MallMemberPaymentMapper mallMemberPaymentMapper;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallMemberBankMapper mallMemberBankMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void withdrawal(WithdrawalDto withdrawalDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = mallMemberService.getById(memberId);
        if (StrUtil.isBlank(mallMember.getTradePassword())) {
            throw new FebsException("未设置支付密码");
        }
        if (!mallMember.getTradePassword().equals(SecureUtil.md5(withdrawalDto.getTradePwd()))) {
            throw new FebsException("支付密码错误");
        }
        if (withdrawalDto.getAmount().compareTo(BigDecimal.valueOf(100)) < 0) {
            throw new FebsException("最小提现金额为100");
        }
        MallMemberPayment payment = mallMemberPaymentMapper.selectByMemberId(memberId);
        if (payment == null) {
            throw new FebsException("未设置收款方式");
        }
        BigDecimal profit = mallMoneyFlowMapper.selectProfitByDateAndMemberId(memberId);
        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        if (profit != null) {
            // 可提现
            BigDecimal canMoney = wallet.getCommission().subtract(profit);
            if(withdrawalDto.getAmount().compareTo(canMoney) > 0) {
                throw new FebsException("提现金额不足");
            }
        }
        CashOutSettingVo cashOutSettingVo = new CashOutSettingVo();
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.CASHOUT_SETTING.getType(), DataDictionaryEnum.CASHOUT_SETTING.getCode());
@@ -78,9 +57,40 @@
            cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class);
        }
        BigDecimal serviceFee = cashOutSettingVo.getServiceFee().multiply(BigDecimal.valueOf(0.01));
//        //每日提现额度上限
//        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));
//        }
        walletService.reduce(withdrawalDto.getAmount(), memberId, "commission");
        MallMemberBank mallMemberBank = mallMemberBankMapper.selectById(withdrawalDto.getBankId());
        if(ObjectUtil.isEmpty(mallMemberBank)){
            throw new FebsException("未找到银行卡信息");
        }
        MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
        // 可提现
        BigDecimal canMoney = wallet.getBalance();
        if(withdrawalDto.getAmount().compareTo(canMoney) > 0) {
            throw new FebsException("金额不足");
        }
        BigDecimal serviceFeePercent = cashOutSettingVo.getServiceFee().multiply(BigDecimal.valueOf(0.01));
        BigDecimal serviceFee = serviceFeePercent.multiply(withdrawalDto.getAmount());
        walletService.reduce(withdrawalDto.getAmount(), memberId, "balance");
        String orderNo = MallUtils.getOrderNum("W");
        MallMemberWithdraw withdraw = new MallMemberWithdraw();
@@ -88,9 +98,46 @@
        withdraw.setMemberId(memberId);
        withdraw.setAmount(withdrawalDto.getAmount());
        withdraw.setStatus(1);
        withdraw.setAmountFee(serviceFee.multiply(withdrawalDto.getAmount()));
        withdraw.setAmountFee(serviceFee);
        withdraw.setRemark("提现");
        withdraw.setWtihdrawTypeId(mallMemberBank.getId());
        this.baseMapper.insert(withdraw);
        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.COMMISSION.getValue());
        mallMemberService.addMoneyFlow(
                memberId,
                withdrawalDto.getAmount().negate(),
                MoneyFlowTypeNewEnum.RANK_BONUS.getValue(),
                orderNo,
                MoneyFlowTypeNewEnum.RANK_BONUS.getDescription(),
                null,
                null,
                1,
                FlowTypeEnum.BALANCE.getValue());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void charge(WithdrawalDto withdrawalDto) {
        Long memberId = LoginUserUtil.getLoginUser().getId();
        MallMember mallMember = mallMemberService.getById(memberId);
        MallMemberBank mallMemberBank = mallMemberBankMapper.selectById(withdrawalDto.getBankId());
        if(ObjectUtil.isEmpty(mallMemberBank)){
            throw new FebsException("未找到银行卡信息");
        }
        if(BigDecimal.ZERO.compareTo(withdrawalDto.getAmount()) >= 0) {
            throw new FebsException("金额需要大于0");
        }
        String orderNo = MallUtils.getOrderNum("C");
        MallMemberWithdraw withdraw = new MallMemberWithdraw();
        withdraw.setWithdrawNo(orderNo);
        withdraw.setMemberId(memberId);
        withdraw.setAmount(withdrawalDto.getAmount());
        withdraw.setStatus(1);
        withdraw.setRemark("充值");
        withdraw.setWtihdrawTypeId(mallMemberBank.getId());
        this.baseMapper.insert(withdraw);
    }
}