| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.izouma.tcg.service.customizeStore;
- import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
- import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
- import com.github.binarywang.wxpay.exception.WxPayException;
- import com.github.binarywang.wxpay.service.WxPayService;
- import com.izouma.tcg.domain.MemberInfo;
- import com.izouma.tcg.domain.User;
- import com.izouma.tcg.domain.customizeStore.Store;
- import com.izouma.tcg.domain.customizeStore.WithdrawApply;
- import com.izouma.tcg.domain.orderInfo.TransactionRecord;
- import com.izouma.tcg.dto.PageQuery;
- import com.izouma.tcg.enums.TransactionRecordType;
- import com.izouma.tcg.enums.WithdrawStatus;
- import com.izouma.tcg.exception.BusinessException;
- import com.izouma.tcg.repo.MemberInfoRepo;
- import com.izouma.tcg.repo.UserRepo;
- import com.izouma.tcg.repo.customizeStore.StoreRepo;
- import com.izouma.tcg.repo.customizeStore.WithdrawApplyRepo;
- import com.izouma.tcg.repo.orderInfo.TransactionRecordRepo;
- import com.izouma.tcg.utils.JpaUtils;
- import jodd.util.StringUtil;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.RandomStringUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.LocalTime;
- import java.util.List;
- @Service
- @Slf4j
- @AllArgsConstructor
- public class WithdrawApplyService {
- private final WithdrawApplyRepo withdrawApplyRepo;
- private final StoreRepo storeRepo;
- private final MemberInfoRepo memberInfoRepo;
- private final TransactionRecordRepo transactionRecordRepo;
- private final WxPayService wxPayService;
- private final UserRepo userRepo;
- public Page<WithdrawApply> all(PageQuery pageQuery) {
- return withdrawApplyRepo
- .findAll(JpaUtils.toSpecification(pageQuery, WithdrawApply.class), JpaUtils.toPageRequest(pageQuery));
- }
- public WithdrawApply withdraw(Long userId, BigDecimal money) {
- List<WithdrawApply> withdrawApplies = withdrawApplyRepo.findAllByUserIdAndStatus(userId, WithdrawStatus.UNDO);
- if (withdrawApplies.size() > 0) {
- throw new BusinessException("已发起提现,请等待审核处理再发起新提现");
- }
- BigDecimal todayWithdraw = withdrawApplyRepo
- .findAllByUserIdAndStatusAndPassTimeBetween(userId, WithdrawStatus.PASS, LocalDate
- .now().atStartOfDay(), LocalDate.now().atTime(LocalTime.MAX)).stream()
- .map(WithdrawApply::getMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
- BigDecimal total = todayWithdraw.add(money);
- if (total.compareTo(BigDecimal.valueOf(20000)) > 0) {
- throw new BusinessException("今日提现超过20000元,已达今日上限.");
- }
- if (money.compareTo(BigDecimal.valueOf(1)) < 0) {
- throw new BusinessException("最低不可以小于1元");
- }
- Store store = storeRepo.findFirstByUserId(userId);
- MemberInfo memberInfo = memberInfoRepo.findAllByUserId(userId);
- BigDecimal rest = memberInfo.getMoney().subtract(money);
- if (rest.compareTo(BigDecimal.ZERO) < 0) {
- throw new BusinessException("余额不足,无法提现");
- }
- WithdrawApply record = WithdrawApply.builder().
- storeId(store.getId())
- .storeName(store.getStoreName())
- .status(WithdrawStatus.UNDO)
- .userId(userId)
- .money(money)
- .username(memberInfo.getUserName()).build();
- WithdrawApply withdrawApply = withdrawApplyRepo.save(record);
- // memberInfo.setMoney(memberInfo.getMoney().subtract(withdrawApply.getMoney()));
- // memberInfoRepo.save(memberInfo);
- TransactionRecord transactionRecord = new TransactionRecord();
- transactionRecord.setStoreId(store.getId());
- transactionRecord.setRemark("");
- transactionRecord.setUserId(userId);
- transactionRecord.setTitle("提现");
- transactionRecord.setType(TransactionRecordType.WITHDRAW);
- transactionRecord.setAmount(money);
- transactionRecord.setSettleTime(LocalDateTime.now());
- transactionRecord.setAttach(withdrawApply.getId().toString());
- transactionRecordRepo.save(transactionRecord);
- return withdrawApply;
- }
- public WithdrawApply pass(Long applyId) {
- WithdrawApply withdrawApply = withdrawApplyRepo.findById(applyId).orElseThrow(new BusinessException("未找到申请"));
- MemberInfo memberInfo = memberInfoRepo.findAllByUserId(withdrawApply.getUserId());
- User user = userRepo.findById(withdrawApply.getUserId()).orElseThrow(new BusinessException("无当前用户信息"));
- if (StringUtil.isEmpty(withdrawApply.getTradeNo())) {
- withdrawApply.setTradeNo(RandomStringUtils.randomAlphanumeric(32));
- withdrawApplyRepo.saveAndFlush(withdrawApply);
- }
- if (memberInfo.getMoney().compareTo(withdrawApply.getMoney()) < 0) {
- throw new BusinessException("余额不足,无法提现");
- }
- BigDecimal origMoney = withdrawApply.getMoney();
- BigDecimal deposit = origMoney.multiply(BigDecimal.valueOf(0.006)).setScale(2, BigDecimal.ROUND_HALF_UP);
- BigDecimal money = origMoney.subtract(deposit).setScale(2, BigDecimal.ROUND_HALF_UP);
- try {
- EntPayRequest request = new EntPayRequest();
- request.setPartnerTradeNo(withdrawApply.getTradeNo());
- request.setAmount(money.multiply(BigDecimal.valueOf(100)).intValue());
- request.setOpenid(user.getOpenId());
- request.setCheckName("NO_CHECK");
- request.setDescription(
- withdrawApply.getStoreName()
- + "店铺余额提现");
- request.setSpbillCreateIp("192.168.31.12");
- EntPayResult result = wxPayService.getEntPayService().entPay(request);
- result.checkResult(wxPayService, "MD5", true);
- } catch (WxPayException e) {
- log.error("余额提现失败", e);
- throw new BusinessException(e.getMessage());
- }
- withdrawApply.setStatus(WithdrawStatus.PASS);
- withdrawApplyRepo.save(withdrawApply);
- memberInfo.setMoney(memberInfo.getMoney().subtract(withdrawApply.getMoney()));
- memberInfoRepo.save(memberInfo);
- return withdrawApply;
- }
- public WithdrawApply deny(Long applyId) {
- WithdrawApply withdrawApply = withdrawApplyRepo.findById(applyId).orElseThrow(new BusinessException("未找到申请"));
- withdrawApply.setStatus(WithdrawStatus.DENY);
- withdrawApplyRepo.save(withdrawApply);
- return withdrawApply;
- }
- public boolean check(Long userId) {
- int count = withdrawApplyRepo.findAllByUserIdAndStatus(userId, WithdrawStatus.UNDO).size();
- return count > 0;
- }
- }
|