TransferFuncServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.cyksj.service.pay.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.cyksj.common.EnvCommonService;
  6. import com.cyksj.common.constant.Constant;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.common.i18n.I18nMessageUtil;
  9. import com.cyksj.common.snowflake.Sequence;
  10. import com.cyksj.common.util.SmsUtil;
  11. import com.cyksj.common.util.StringUtil;
  12. import com.cyksj.dto.Result;
  13. import com.cyksj.enums.GatewayResponse;
  14. import com.cyksj.mapper.UserMapper;
  15. import com.cyksj.mapper.manage.distribute.DistributeMoneyApplyMapper;
  16. import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper;
  17. import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
  18. import com.cyksj.model.entity.DistributeMoneyApply;
  19. import com.cyksj.model.entity.DistributePointsExchangeMoney;
  20. import com.cyksj.model.entity.User;
  21. import com.cyksj.model.request.ApplyMoneyReq;
  22. import com.cyksj.redis.RedisService;
  23. import com.cyksj.service.distribute.UserDistributeReturnMoneyDetailService;
  24. import com.cyksj.service.pay.TransferFuncService;
  25. import com.cyksj.service.sys.SysConfigService;
  26. import com.cyksj.service.user.UserBenefitsService;
  27. import lombok.RequiredArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.stereotype.Service;
  30. import java.math.BigDecimal;
  31. import java.math.RoundingMode;
  32. import java.util.Optional;
  33. /*
  34. *项目名: yhlxj
  35. *文件名: TransferFuncServiceImpl
  36. *创建者: JavaZou
  37. *创建时间:2023/9/20 18:55
  38. */
  39. @Service
  40. @RequiredArgsConstructor
  41. @Slf4j
  42. public class TransferFuncServiceImpl implements TransferFuncService {
  43. private static final Sequence SEQUENCE = new Sequence(0);
  44. private final UserMapper userMapper;
  45. private final UserBenefitsService userBenefitsService;
  46. private final EnvCommonService envCommonService;
  47. private final UserDistributeMapper userDistributeMapper;
  48. private final DistributePointsExchangeMoneyMapper distributePointsExchangeMoneyMapper;
  49. private final DistributeMoneyApplyMapper distributeMoneyApplyMapper;
  50. private final RedisService redisService;
  51. private final SysConfigService sysConfigService;
  52. private final UserDistributeReturnMoneyDetailService userDistributeReturnMoneyDetailService;
  53. @Override
  54. public Result distributeApply(ApplyMoneyReq applyMoneyReq) throws Exception {
  55. applyMoneyReq.setMType(DistributeMoneyApply.MType.ds);
  56. String key = String.format(RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getName(), DateTime.now().toDateStr(), applyMoneyReq.getMType(), applyMoneyReq.getUserId());
  57. if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) {
  58. throw BusinessRuntimeException.getInstance("每天仅可提现一次");
  59. }
  60. try {
  61. Result result = commonApplyMoney(applyMoneyReq);
  62. if (result.isFlag()) {
  63. redisService.set(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout());
  64. } else {
  65. redisService.del(key);
  66. }
  67. return result;
  68. } catch (Exception e) {
  69. redisService.del(key);
  70. throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e));
  71. }
  72. }
  73. public Result commonApplyMoney(ApplyMoneyReq applyMoneyReq) throws Exception {
  74. Long userId = applyMoneyReq.getUserId();
  75. User user = userMapper.selectById(userId);
  76. if (user == null) {
  77. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
  78. }
  79. if (user.getIsBlack()) throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("user_black_error"));
  80. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  81. DistributeMoneyApply.Type type = applyMoneyReq.getType();
  82. if (type == null) type = DistributeMoneyApply.Type.usdt;
  83. if (type == DistributeMoneyApply.Type.usdt) {
  84. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  85. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_usdt_address_error"));
  86. }
  87. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  88. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_usdt_address_error"));
  89. }
  90. isTakeAll = true;
  91. }
  92. if (type == DistributeMoneyApply.Type.bank) {
  93. if (StrUtil.isEmpty(applyMoneyReq.getRealName())) {
  94. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg(I18nMessageUtil.getI18nMsg("reaL_name_error")));
  95. }
  96. if (StrUtil.isEmpty(applyMoneyReq.getBank())) {
  97. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("enter_bank_no_error"));
  98. }
  99. isTakeAll = true;
  100. }
  101. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  102. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_remark_length_error"));
  103. }
  104. DistributeMoneyApply.MType mType = applyMoneyReq.getMType();
  105. DistributeMoneyApply distributeMoneyApply = null;
  106. // //今日是否已提现
  107. // DistributeMoneyApply apply = distributeMoneyApplyMapper.selectOne(Wrappers.lambdaQuery(DistributeMoneyApply.class).eq(DistributeMoneyApply::getUserId, userId).between(DistributeMoneyApply::getCreatedTime, DateUtil.beginOfDay(DateTime.now()), DateUtil.endOfDay(DateTime.now())).last("limit 1"));
  108. // if (apply != null) {
  109. // throw BusinessRuntimeException.getInstance("今日已提现,请明日再进行操作");
  110. // }
  111. BigDecimal userPoints = null;
  112. BigDecimal needSubPoints = null;
  113. BigDecimal needApplyMoney = null;
  114. BigDecimal fourHundred = null;
  115. if (mType == DistributeMoneyApply.MType.ds) {
  116. userPoints = user.getPoints();
  117. DistributePointsExchangeMoney distributePointsExchangeMoney = distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).orderByDesc(DistributePointsExchangeMoney::getPoints).last("limit 1"));
  118. BigDecimal minPoints = BigDecimal.valueOf(distributePointsExchangeMoney.getMinPoints());
  119. BigDecimal ratePoints = BigDecimal.valueOf(distributePointsExchangeMoney.getPoints());
  120. BigDecimal minMoney = distributePointsExchangeMoney.getMoney();
  121. if (userPoints.compareTo(minPoints) < 0) {
  122. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_thresholds_error"));
  123. }
  124. BigDecimal num = userPoints.divide(ratePoints, 0, RoundingMode.DOWN);
  125. needApplyMoney = num.multiply(minMoney);
  126. needSubPoints = num.multiply(ratePoints);
  127. fourHundred = BigDecimal.valueOf(4 * 100 * 100);
  128. if (isTakeAll == null || !isTakeAll) {
  129. if (needApplyMoney.compareTo(fourHundred) > 0) {
  130. needApplyMoney = fourHundred;
  131. needSubPoints = fourHundred.divide(minMoney).multiply(ratePoints);
  132. }
  133. }
  134. }
  135. Integer update = 0;
  136. if (mType == DistributeMoneyApply.MType.ds) {
  137. update = userMapper.subPoints(user.getId(), needSubPoints);
  138. }
  139. if (update == 1) {
  140. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new DistributeMoneyApply());
  141. distributeMoneyApply.setUserId(userId);
  142. distributeMoneyApply.setPoints(needSubPoints);
  143. distributeMoneyApply.setMoney(needApplyMoney);
  144. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  145. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  146. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  147. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  148. distributeMoneyApply.setType(applyMoneyReq.getType());
  149. distributeMoneyApply.setMType(applyMoneyReq.getMType());
  150. distributeMoneyApply.setBank(applyMoneyReq.getBank());
  151. if (isTakeAll != null && isTakeAll) {
  152. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.verifying);
  153. } else {
  154. //支付宝提现去掉
  155. }
  156. try {
  157. distributeMoneyApplyMapper.insert(distributeMoneyApply);
  158. //大额提现 超过400
  159. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == DistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  160. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "银河海外推广有大额提现申请【银河录像局】");
  161. }
  162. userDistributeReturnMoneyDetailService.recordDistributeReturnMoney(distributeMoneyApply.getUserId(), distributeMoneyApply.getMoney().negate(), "提现");
  163. } catch (Exception e) {
  164. userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints());
  165. return GatewayResponse.FAIL.newBuilder().setMsg(I18nMessageUtil.getI18nMsg("sys_error")).toResult();
  166. }
  167. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  168. }
  169. throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error"));
  170. }
  171. }