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.exception.FebsException;
|
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 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;
|
|
@Override
|
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);
|
}
|
DataDictionaryCustom withdrawalCnt = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.MEMBER_WITHDRAWAL_CNT.getType(), DataDictionaryEnum.MEMBER_WITHDRAWAL_CNT.getCode());
|
if (withdrawalDto.getAmount().compareTo(cashOutSettingVo.getMinCashOut()) < 0) {
|
throw new FebsException("最小提现金额为"+withdrawalCnt.getValue()+"元");
|
}
|
|
String openId = mallMember.getOpenId();
|
if(StrUtil.isEmpty(openId)){
|
throw new FebsException("没有绑定微信");
|
}
|
|
MallMemberWallet wallet = mallMemberWalletMapper.selectWalletByMemberId(memberId);
|
if (ObjectUtil.isNotEmpty(wallet)) {
|
// 可提现
|
BigDecimal canMoney = wallet.getBalance();
|
if(withdrawalDto.getAmount().compareTo(canMoney) > 0) {
|
throw new FebsException("提现金额不足");
|
}
|
}
|
|
walletService.reduce(withdrawalDto.getAmount(), memberId, "balance");
|
String withdrawalNo = "TX_"+MallUtils.getOrderNum();
|
MallMemberWithdraw withdraw = new MallMemberWithdraw();
|
withdraw.setWithdrawNo(withdrawalNo);
|
withdraw.setMemberId(memberId);
|
withdraw.setAmount(withdrawalDto.getAmount());
|
withdraw.setStatus(1);
|
this.baseMapper.insert(withdraw);
|
|
mallMemberService.addMoneyFlow(memberId, withdrawalDto.getAmount().negate(), MoneyFlowTypeEnum.WITHDRAWAL.getValue(), withdrawalNo, null, null, null, 1, FlowTypeEnum.BALANCE.getValue());
|
}
|
}
|