xiaoyong931011
2023-04-11 b6d1603cb25dfca500e4257b446e454c3e694669
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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.GreenScoreEnum;
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
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;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IApiMallMemberService;
import cc.mrbird.febs.mall.service.IApiMallMemberWalletService;
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.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
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.math.BigDecimal;
 
/**
 * @author wzy
 * @date 2022-05-05
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MallMemberWithdrawServiceImpl extends ServiceImpl<MallMemberWithdrawMapper, MallMemberWithdraw> implements IMallMemberWithdrawService {
 
    private final IApiMallMemberService mallMemberService;
    private final MallMoneyFlowMapper mallMoneyFlowMapper;
    private final MallMemberWalletMapper mallMemberWalletMapper;
    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("支付密码错误");
        }
 
        CashOutSettingVo cashOutSettingVo = new CashOutSettingVo();
        DataDictionaryCustom dic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.CASHOUT_SETTING.getType(), DataDictionaryEnum.CASHOUT_SETTING.getCode());
        if (dic != null) {
            cashOutSettingVo = JSONObject.parseObject(dic.getValue(), CashOutSettingVo.class);
        }
 
        if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) {
            throw new FebsException("最小提现金额为"+cashOutSettingVo.getMinCashOut().setScale(2,BigDecimal.ROUND_DOWN));
        }
 
        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());
        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 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;
            }
        }
 
 
        walletService.reduce(withdrawalDto.getAmount(), memberId, "balance");
        String orderNo = MallUtils.getOrderNum("W");
 
        MallMemberWithdraw withdraw = new MallMemberWithdraw();
        withdraw.setWithdrawNo(orderNo);
        withdraw.setMemberId(memberId);
        withdraw.setAmount(withdrawalDto.getAmount());
        withdraw.setStatus(1);
        withdraw.setAmountFee(serviceFee);
        withdraw.setRemark(remark);
        withdraw.setWtihdrawTypeId(mallMemberBank.getId());
        this.baseMapper.insert(withdraw);
 
        mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), orderNo, null, null, null, 1, FlowTypeEnum.BALANCE.getValue());
    }
}