WithdrawApplyService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.izouma.tcg.service.customizeStore;
  2. import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
  3. import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
  4. import com.github.binarywang.wxpay.exception.WxPayException;
  5. import com.github.binarywang.wxpay.service.WxPayService;
  6. import com.izouma.tcg.domain.MemberInfo;
  7. import com.izouma.tcg.domain.User;
  8. import com.izouma.tcg.domain.customizeStore.Store;
  9. import com.izouma.tcg.domain.customizeStore.WithdrawApply;
  10. import com.izouma.tcg.domain.orderInfo.TransactionRecord;
  11. import com.izouma.tcg.dto.PageQuery;
  12. import com.izouma.tcg.enums.TransactionRecordType;
  13. import com.izouma.tcg.enums.WithdrawStatus;
  14. import com.izouma.tcg.exception.BusinessException;
  15. import com.izouma.tcg.repo.MemberInfoRepo;
  16. import com.izouma.tcg.repo.UserRepo;
  17. import com.izouma.tcg.repo.customizeStore.StoreRepo;
  18. import com.izouma.tcg.repo.customizeStore.WithdrawApplyRepo;
  19. import com.izouma.tcg.repo.orderInfo.TransactionRecordRepo;
  20. import com.izouma.tcg.utils.JpaUtils;
  21. import jodd.util.StringUtil;
  22. import lombok.AllArgsConstructor;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.RandomStringUtils;
  25. import org.springframework.data.domain.Page;
  26. import org.springframework.stereotype.Service;
  27. import java.math.BigDecimal;
  28. import java.time.LocalDate;
  29. import java.time.LocalDateTime;
  30. import java.time.LocalTime;
  31. import java.util.List;
  32. @Service
  33. @Slf4j
  34. @AllArgsConstructor
  35. public class WithdrawApplyService {
  36. private final WithdrawApplyRepo withdrawApplyRepo;
  37. private final StoreRepo storeRepo;
  38. private final MemberInfoRepo memberInfoRepo;
  39. private final TransactionRecordRepo transactionRecordRepo;
  40. private final WxPayService wxPayService;
  41. private final UserRepo userRepo;
  42. public Page<WithdrawApply> all(PageQuery pageQuery) {
  43. return withdrawApplyRepo
  44. .findAll(JpaUtils.toSpecification(pageQuery, WithdrawApply.class), JpaUtils.toPageRequest(pageQuery));
  45. }
  46. public WithdrawApply withdraw(Long userId, BigDecimal money) {
  47. List<WithdrawApply> withdrawApplies = withdrawApplyRepo.findAllByUserIdAndStatus(userId, WithdrawStatus.UNDO);
  48. if (withdrawApplies.size() > 0) {
  49. throw new BusinessException("已发起提现,请等待审核处理再发起新提现");
  50. }
  51. BigDecimal todayWithdraw = withdrawApplyRepo
  52. .findAllByUserIdAndStatusAndPassTimeBetween(userId, WithdrawStatus.PASS, LocalDate
  53. .now().atStartOfDay(), LocalDate.now().atTime(LocalTime.MAX)).stream()
  54. .map(WithdrawApply::getMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
  55. BigDecimal total = todayWithdraw.add(money);
  56. if (total.compareTo(BigDecimal.valueOf(20000)) > 0) {
  57. throw new BusinessException("今日提现超过20000元,已达今日上限.");
  58. }
  59. if (money.compareTo(BigDecimal.valueOf(1)) < 0) {
  60. throw new BusinessException("最低不可以小于1元");
  61. }
  62. Store store = storeRepo.findFirstByUserId(userId);
  63. MemberInfo memberInfo = memberInfoRepo.findAllByUserId(userId);
  64. BigDecimal rest = memberInfo.getMoney().subtract(money);
  65. if (rest.compareTo(BigDecimal.ZERO) < 0) {
  66. throw new BusinessException("余额不足,无法提现");
  67. }
  68. WithdrawApply record = WithdrawApply.builder().
  69. storeId(store.getId())
  70. .storeName(store.getStoreName())
  71. .status(WithdrawStatus.UNDO)
  72. .userId(userId)
  73. .money(money)
  74. .username(memberInfo.getUserName()).build();
  75. WithdrawApply withdrawApply = withdrawApplyRepo.save(record);
  76. // memberInfo.setMoney(memberInfo.getMoney().subtract(withdrawApply.getMoney()));
  77. // memberInfoRepo.save(memberInfo);
  78. TransactionRecord transactionRecord = new TransactionRecord();
  79. transactionRecord.setStoreId(store.getId());
  80. transactionRecord.setRemark("");
  81. transactionRecord.setUserId(userId);
  82. transactionRecord.setTitle("提现");
  83. transactionRecord.setType(TransactionRecordType.WITHDRAW);
  84. transactionRecord.setAmount(money);
  85. transactionRecord.setSettleTime(LocalDateTime.now());
  86. transactionRecord.setAttach(withdrawApply.getId().toString());
  87. transactionRecordRepo.save(transactionRecord);
  88. return withdrawApply;
  89. }
  90. public WithdrawApply pass(Long applyId) {
  91. WithdrawApply withdrawApply = withdrawApplyRepo.findById(applyId).orElseThrow(new BusinessException("未找到申请"));
  92. MemberInfo memberInfo = memberInfoRepo.findAllByUserId(withdrawApply.getUserId());
  93. User user = userRepo.findById(withdrawApply.getUserId()).orElseThrow(new BusinessException("无当前用户信息"));
  94. if (StringUtil.isEmpty(withdrawApply.getTradeNo())) {
  95. withdrawApply.setTradeNo(RandomStringUtils.randomAlphanumeric(32));
  96. withdrawApplyRepo.saveAndFlush(withdrawApply);
  97. }
  98. if (memberInfo.getMoney().compareTo(withdrawApply.getMoney()) < 0) {
  99. throw new BusinessException("余额不足,无法提现");
  100. }
  101. BigDecimal origMoney = withdrawApply.getMoney();
  102. BigDecimal deposit = origMoney.multiply(BigDecimal.valueOf(0.006)).setScale(2, BigDecimal.ROUND_HALF_UP);
  103. BigDecimal money = origMoney.subtract(deposit).setScale(2, BigDecimal.ROUND_HALF_UP);
  104. try {
  105. EntPayRequest request = new EntPayRequest();
  106. request.setPartnerTradeNo(withdrawApply.getTradeNo());
  107. request.setAmount(money.multiply(BigDecimal.valueOf(100)).intValue());
  108. request.setOpenid(user.getOpenId());
  109. request.setCheckName("NO_CHECK");
  110. request.setDescription(
  111. withdrawApply.getStoreName()
  112. + "店铺余额提现");
  113. request.setSpbillCreateIp("192.168.31.12");
  114. EntPayResult result = wxPayService.getEntPayService().entPay(request);
  115. result.checkResult(wxPayService, "MD5", true);
  116. } catch (WxPayException e) {
  117. log.error("余额提现失败", e);
  118. throw new BusinessException(e.getMessage());
  119. }
  120. withdrawApply.setStatus(WithdrawStatus.PASS);
  121. withdrawApplyRepo.save(withdrawApply);
  122. memberInfo.setMoney(memberInfo.getMoney().subtract(withdrawApply.getMoney()));
  123. memberInfoRepo.save(memberInfo);
  124. return withdrawApply;
  125. }
  126. public WithdrawApply deny(Long applyId) {
  127. WithdrawApply withdrawApply = withdrawApplyRepo.findById(applyId).orElseThrow(new BusinessException("未找到申请"));
  128. withdrawApply.setStatus(WithdrawStatus.DENY);
  129. withdrawApplyRepo.save(withdrawApply);
  130. return withdrawApply;
  131. }
  132. public boolean check(Long userId) {
  133. int count = withdrawApplyRepo.findAllByUserIdAndStatus(userId, WithdrawStatus.UNDO).size();
  134. return count > 0;
  135. }
  136. }