|
@@ -0,0 +1,175 @@
|
|
|
+package com.yonge.cooleshow.biz.dal.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.yonge.cooleshow.biz.dal.constant.SysConfigConstant;
|
|
|
+import com.yonge.cooleshow.biz.dal.dao.UserAccountDao;
|
|
|
+import com.yonge.cooleshow.biz.dal.dao.UserBankCardDao;
|
|
|
+import com.yonge.cooleshow.biz.dal.dto.UserAccountRecordDto;
|
|
|
+import com.yonge.cooleshow.biz.dal.dto.req.WithdrawalReq;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.UserAccountRecord;
|
|
|
+import com.yonge.cooleshow.biz.dal.enums.AccountBizTypeEnum;
|
|
|
+import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
|
|
|
+import com.yonge.cooleshow.biz.dal.enums.InOrOutEnum;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.SysConfigService;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.UserAccountService;
|
|
|
+import com.yonge.cooleshow.biz.dal.vo.UserAccountVo;
|
|
|
+import com.yonge.cooleshow.biz.dal.vo.UserBankCardVo;
|
|
|
+import com.yonge.cooleshow.biz.dal.vo.res.WithdrawalInfoRes;
|
|
|
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
|
|
|
+import com.yonge.cooleshow.common.exception.BizException;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.yonge.cooleshow.biz.dal.entity.UserWithdrawal;
|
|
|
+import com.yonge.cooleshow.biz.dal.vo.UserWithdrawalVo;
|
|
|
+import com.yonge.cooleshow.biz.dal.dto.search.TeacherWithdrawalSearch;
|
|
|
+import com.yonge.cooleshow.biz.dal.dao.UserWithdrawalDao;
|
|
|
+import com.yonge.cooleshow.biz.dal.service.UserWithdrawalService;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class UserWithdrawalServiceImpl extends ServiceImpl<UserWithdrawalDao, UserWithdrawal> implements UserWithdrawalService {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(UserWithdrawalServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserBankCardDao bankCardDao;
|
|
|
+ @Autowired
|
|
|
+ private UserAccountService userAccountService;
|
|
|
+ @Autowired
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserWithdrawalVo detail(Long id) {
|
|
|
+ UserWithdrawalVo detail = baseMapper.detail(id);
|
|
|
+ return detail;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<UserWithdrawalVo> selectPage(IPage<UserWithdrawalVo> page, TeacherWithdrawalSearch query) {
|
|
|
+ return page.setRecords(baseMapper.selectPage(page, query));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpResponseResult<WithdrawalInfoRes> getWithdrawalInfo(Long userId) {
|
|
|
+ WithdrawalInfoRes withdrawalInfoRes = new WithdrawalInfoRes();
|
|
|
+ UserBankCardVo defaultBank = bankCardDao.getDefaultBankByUserId(userId);
|
|
|
+ if (null == defaultBank) {
|
|
|
+ return HttpResponseResult.failed("用户未绑定银行卡");
|
|
|
+ }
|
|
|
+ withdrawalInfoRes.setUserBankCard(defaultBank);
|
|
|
+ //获取平台提现手续费
|
|
|
+ BigDecimal withdrawalServiceFee = getWithdrawalServiceFee();
|
|
|
+ withdrawalInfoRes.setWithdrawalServiceFee(withdrawalServiceFee);
|
|
|
+ //获取用户可用于提现余额
|
|
|
+ withdrawalInfoRes.setAmountWithdrawal(getAmountWithdrawal(userId));
|
|
|
+ return HttpResponseResult.succeed(withdrawalInfoRes);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public HttpResponseResult<Boolean> withdrawal(Long userId, WithdrawalReq withdrawalReq) {
|
|
|
+ BigDecimal withdrawalServiceFee = getWithdrawalServiceFee();
|
|
|
+ //校验金额
|
|
|
+ if (withdrawalReq.getAmountWithdrawal().subtract(withdrawalServiceFee).floatValue() < 0) {
|
|
|
+ return HttpResponseResult.failed("提现金额过小");
|
|
|
+ }
|
|
|
+ //获取用户提现金额
|
|
|
+ BigDecimal amountWithdrawal = getAmountWithdrawal(userId);
|
|
|
+ //比较大小
|
|
|
+ if (amountWithdrawal.compareTo(withdrawalReq.getAmountWithdrawal()) < 0) {
|
|
|
+ return HttpResponseResult.failed("账户余额不足");
|
|
|
+ }
|
|
|
+ //获取提现银行卡
|
|
|
+ UserBankCardVo bankCardVo = bankCardDao.getBankByUserIdAndCardId(userId, withdrawalReq.getBankCardId());
|
|
|
+ if (null == bankCardVo) {
|
|
|
+ return HttpResponseResult.failed("未找到用户绑卡信息");
|
|
|
+ }
|
|
|
+ //插入用户提现表
|
|
|
+ UserWithdrawal userWithdrawal = insertUserWithdrawal(userId, withdrawalReq, bankCardVo, withdrawalServiceFee);
|
|
|
+ //插入账户变更
|
|
|
+ UserAccountRecordDto accountRecordDto = new UserAccountRecordDto(
|
|
|
+ userId, withdrawalReq.getAmountWithdrawal(), InOrOutEnum.OUT, AccountBizTypeEnum.WITHDRAWAL,
|
|
|
+ userWithdrawal.getId(), "老师提现", null
|
|
|
+ );
|
|
|
+ HttpResponseResult<UserAccountRecord> accountChange = userAccountService.accountChange(accountRecordDto);
|
|
|
+ if(accountChange.getStatus()){
|
|
|
+ return HttpResponseResult.succeed(true);
|
|
|
+ }else{
|
|
|
+ throw new BizException("提现失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 入提现表
|
|
|
+ * @author liweifan
|
|
|
+ * @param: userId
|
|
|
+ * @param: withdrawalReq
|
|
|
+ * @param: bankCardVo
|
|
|
+ * @param: userAccountRecord
|
|
|
+ * @updateTime 2022/4/7 16:46
|
|
|
+ */
|
|
|
+ private UserWithdrawal insertUserWithdrawal(Long userId, WithdrawalReq withdrawalReq,
|
|
|
+ UserBankCardVo bankCardVo, BigDecimal withdrawalServiceFee) {
|
|
|
+ UserWithdrawal userWithdrawal = new UserWithdrawal();
|
|
|
+ userWithdrawal.setUserId(userId);
|
|
|
+ userWithdrawal.setAmount(withdrawalReq.getAmountWithdrawal());
|
|
|
+ userWithdrawal.setPlantformFee(withdrawalServiceFee);
|
|
|
+
|
|
|
+ BigDecimal actualAmount = withdrawalReq.getAmountWithdrawal().subtract(withdrawalServiceFee);
|
|
|
+ userWithdrawal.setActualAmount(actualAmount);
|
|
|
+ userWithdrawal.setBankCard(bankCardVo.getBankCard());
|
|
|
+ //todo 银行流水号
|
|
|
+
|
|
|
+ userWithdrawal.setCreateTime(new Date());
|
|
|
+ userWithdrawal.setUpdateTime(new Date());
|
|
|
+ userWithdrawal.setStatus(AuthStatusEnum.DOING);
|
|
|
+ baseMapper.insert(userWithdrawal);
|
|
|
+ return userWithdrawal;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取平台提现服务费
|
|
|
+ * @author liweifan
|
|
|
+ * @param: userId
|
|
|
+ * @updateTime 2022/4/7 14:59
|
|
|
+ * @return: java.math.BigDecimal
|
|
|
+ */
|
|
|
+ private BigDecimal getWithdrawalServiceFee() {
|
|
|
+ BigDecimal withdrawalServiceFee = new BigDecimal(0);
|
|
|
+ try {
|
|
|
+ String configValue = sysConfigService.findConfigValue(SysConfigConstant.WITHDRAWAL_SERVICE_FEE);
|
|
|
+ withdrawalServiceFee = new BigDecimal(configValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.info("获取平台配置失败 key={}", SysConfigConstant.WITHDRAWAL_SERVICE_FEE);
|
|
|
+ }
|
|
|
+ return withdrawalServiceFee;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取用户可提现金额
|
|
|
+ * @author liweifan
|
|
|
+ * @param: userId
|
|
|
+ * @param: withdrawalServiceFee
|
|
|
+ * @updateTime 2022/4/7 15:02
|
|
|
+ * @return: java.math.BigDecimal
|
|
|
+ */
|
|
|
+ private BigDecimal getAmountWithdrawal(Long userId) {
|
|
|
+ //查询用户账户余额
|
|
|
+ UserAccountVo detail = userAccountService.detail(userId);
|
|
|
+ BigDecimal subtract = detail.getAmountUsable();
|
|
|
+ if (subtract.intValue() < 0) {
|
|
|
+ subtract = new BigDecimal(0);
|
|
|
+ }
|
|
|
+ return subtract;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|