fix
Hentua
2023-06-15 8bf923b44bbe9b45b73ddcc25df75c9c59f54e07
src/main/java/cc/mrbird/febs/mall/service/impl/MallMemberWithdrawServiceImpl.java
@@ -16,10 +16,13 @@
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.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -27,6 +30,8 @@
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * @author wzy
@@ -64,6 +69,13 @@
            cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class);
        }
        if (cashOutSettingVo.getWorkingDays() != 2) {
            int i = DateUtil.dayOfWeek(new Date());
            if (i == 1 || i == 7) {
                throw new FebsException("仅工作日可提现");
            }
        }
        if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) {
            throw new FebsException("最小提现金额为"+cashOutSettingVo.getMinCashOut().setScale(2,BigDecimal.ROUND_DOWN));
        }
@@ -72,58 +84,43 @@
        if(ObjectUtil.isEmpty(mallMemberBank)){
            throw new FebsException("未找到银行卡信息");
        }
        if(ObjectUtil.isEmpty(mallMemberBank.getBankName())){
            throw new FebsException("请填写所属银行");
        }
        if(ObjectUtil.isEmpty(mallMemberBank.getSubbranchName())){
            throw new FebsException("请填写所属支行");
        }
        LambdaQueryWrapper<MallMemberWithdraw> query = new LambdaQueryWrapper<>();
        query.ge(MallMemberWithdraw::getCreatedTime, DateUtil.beginOfDay(new Date()))
                .le(MallMemberWithdraw::getCreatedTime, DateUtil.endOfDay(new Date()))
                .ne(MallMemberWithdraw::getStatus, 3)
                .eq(MallMemberWithdraw::getMemberId, memberId);
        List<MallMemberWithdraw> hasWithdraw = this.baseMapper.selectList(query);
        if (CollUtil.isNotEmpty(hasWithdraw) && hasWithdraw.size() >= cashOutSettingVo.getDailyWithdrawlCnt()) {
            throw new FebsException("达到最大提现次数");
        }
        double totalAmount = hasWithdraw.stream().mapToDouble(item -> item.getAmount().doubleValue()).sum();
        if (cashOutSettingVo.getAllCashOut().subtract(withdrawalDto.getAmount()).doubleValue() < totalAmount) {
            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());
        String remark = AppContants.MEMBER_WITHDRAW_NORMAL;
        DataDictionaryCustom voucherOnOffDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(
                GreenScoreEnum.VOUCHER_ON_OFF.getType(),
                GreenScoreEnum.VOUCHER_ON_OFF.getCode()
        );
        if(ObjectUtil.isNotEmpty(voucherOnOffDic)){
            String voucherOnOff = voucherOnOffDic.getValue();
            /**
             * 绿色凭证提现开关 1:开启 2:关闭
             *      验证账户凭证数量
             *      不需要手续费
             *      减少用户的凭证数量
             */
            if("1".equals(voucherOnOff)){
                BigDecimal voucherCntDto = withdrawalDto.getVoucherCnt();
                if(voucherCntDto.compareTo(BigDecimal.ZERO) <= 0){
                    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()).multiply(new BigDecimal(0.01));
                BigDecimal voucherPercentNeed = withdrawalDto.getAmount().multiply(voucherPercent).setScale(2,BigDecimal.ROUND_DOWN);
        // 可提现
        BigDecimal canMoney = wallet.getBalance();
//                BigDecimal voucherCnt = wallet.getVoucherCnt();
                BigDecimal voucherFireCnt = wallet.getVoucherFireCnt();
                if(voucherFireCnt.compareTo(voucherPercentNeed) < 0){
                    throw new FebsException("红豆不足");
                }
//                walletService.reduce(voucherCntDto, memberId, "voucherCnt");
                walletService.reduce(voucherCntDto, memberId, "voucherFireCnt");
                serviceFee = voucherCntDto;
                remark = AppContants.MEMBER_WITHDRAW_VOUCHER;
            }
        if(withdrawalDto.getAmount().add(serviceFee).compareTo(canMoney) > 0) {
            throw new FebsException("提现金额不足");
        }
        walletService.reduce(withdrawalDto.getAmount(), memberId, "balance");
        int index = walletService.reduce(withdrawalDto.getAmount().add(serviceFee), memberId, "balance");
        if (index == 2) {
            throw new FebsException("请刷新页面");
        }
        String orderNo = MallUtils.getOrderNum("W");
        MallMemberWithdraw withdraw = new MallMemberWithdraw();
@@ -132,10 +129,10 @@
        withdraw.setAmount(withdrawalDto.getAmount());
        withdraw.setStatus(1);
        withdraw.setAmountFee(serviceFee);
        withdraw.setRemark(remark);
        withdraw.setWtihdrawTypeId(mallMemberBank.getId());
        withdraw.setRemark(AppContants.MEMBER_WITHDRAW_NORMAL);
        this.baseMapper.insert(withdraw);
        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.BALANCE.getValue());
        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().add(serviceFee).negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.BALANCE.getValue());
    }
}