TransferFuncServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. package com.cyksj.service.pay.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.alipay.api.AlipayClient;
  7. import com.alipay.api.AlipayRequest;
  8. import com.alipay.api.AlipayResponse;
  9. import com.alipay.api.domain.AlipayFundTransUniTransferModel;
  10. import com.alipay.api.domain.Participant;
  11. import com.alipay.api.request.AlipayFundTransUniTransferRequest;
  12. import com.baomidou.mybatisplus.core.toolkit.Assert;
  13. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  14. import com.cyksj.common.EnvCommonService;
  15. import com.cyksj.common.constant.Constant;
  16. import com.cyksj.common.constant.TransferConstant;
  17. import com.cyksj.common.exception.BusinessRuntimeException;
  18. import com.cyksj.common.snowflake.Sequence;
  19. import com.cyksj.common.util.Jsons;
  20. import com.cyksj.common.util.SmsUtil;
  21. import com.cyksj.common.util.StringUtil;
  22. import com.cyksj.config.zfb.AliPayClientFactory;
  23. import com.cyksj.config.zfb.ZfbProductCode;
  24. import com.cyksj.dto.Result;
  25. import com.cyksj.enums.GatewayResponse;
  26. import com.cyksj.mapper.TransferAccountsRecordMapper;
  27. import com.cyksj.mapper.UserMapper;
  28. import com.cyksj.mapper.manage.distribute.DistributeMoneyApplyMapper;
  29. import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper;
  30. import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
  31. import com.cyksj.mapper.mirrorshop.UserMirrorShopDistributeMoneyApplyMapper;
  32. import com.cyksj.mapper.shop.YhShopDistributeMoneyApplyMapper;
  33. import com.cyksj.mapper.shop.YhShopMapper;
  34. import com.cyksj.model.entity.*;
  35. import com.cyksj.model.request.ApplyMoneyReq;
  36. import com.cyksj.model.request.TransferAccountsReq;
  37. import com.cyksj.model.request.UserMirrorShopApplyMoneyReq;
  38. import com.cyksj.model.request.YhShopApplyMoneyReq;
  39. import com.cyksj.redis.RedisService;
  40. import com.cyksj.service.distribute.UserDistributeReturnMoneyDetailService;
  41. import com.cyksj.service.pay.TransferFuncService;
  42. import com.cyksj.service.sys.SysConfigService;
  43. import com.cyksj.service.user.UserBenefitsService;
  44. import lombok.RequiredArgsConstructor;
  45. import lombok.extern.slf4j.Slf4j;
  46. import org.springframework.stereotype.Service;
  47. import java.math.BigDecimal;
  48. import java.math.RoundingMode;
  49. import java.util.Optional;
  50. /*
  51. *项目名: yhlxj
  52. *文件名: TransferFuncServiceImpl
  53. *创建者: JavaZou
  54. *创建时间:2023/9/20 18:55
  55. */
  56. @Service
  57. @RequiredArgsConstructor
  58. @Slf4j
  59. public class TransferFuncServiceImpl implements TransferFuncService {
  60. private static final Sequence SEQUENCE = new Sequence(0);
  61. private final TransferAccountsRecordMapper transferAccountsRecordMapper;
  62. private final YhShopMapper yhShopMapper;
  63. private final UserMapper userMapper;
  64. private final UserBenefitsService userBenefitsService;
  65. private final YhShopDistributeMoneyApplyMapper yhShopDistributeMoneyApplyMapper;
  66. private final EnvCommonService envCommonService;
  67. private final UserDistributeMapper userDistributeMapper;
  68. private final DistributePointsExchangeMoneyMapper distributePointsExchangeMoneyMapper;
  69. private final DistributeMoneyApplyMapper distributeMoneyApplyMapper;
  70. private final RedisService redisService;
  71. private final SysConfigService sysConfigService;
  72. private final UserDistributeReturnMoneyDetailService userDistributeReturnMoneyDetailService;
  73. private final UserMirrorShopDistributeMoneyApplyMapper userMirrorShopDistributeMoneyApplyMapper;
  74. @Override
  75. public Result distributeApply(ApplyMoneyReq applyMoneyReq) throws Exception {
  76. applyMoneyReq.setMType(DistributeMoneyApply.MType.ds);
  77. SysConfig black = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  78. .eq(SysConfig::getSysKey, Constant.APPLY_MONEY_BLACK).last("limit 1"));
  79. if (black != null && StrUtil.isNotBlank(black.getSysValue())) {
  80. if (Jsons.parseList(black.getSysValue(), Long.class).contains(applyMoneyReq.getUserId())) {
  81. throw BusinessRuntimeException.getInstance("提现异常");
  82. }
  83. }
  84. String key = String.format(RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getName(), DateTime.now().toDateStr(), applyMoneyReq.getMType(), applyMoneyReq.getUserId());
  85. if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) {
  86. throw BusinessRuntimeException.getInstance("每天仅可提现一次");
  87. }
  88. try {
  89. Result result = commonApplyMoney(applyMoneyReq);
  90. if (result.isFlag()) {
  91. redisService.set(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout());
  92. } else {
  93. redisService.del(key);
  94. }
  95. return result;
  96. } catch (Exception e) {
  97. redisService.del(key);
  98. throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e));
  99. }
  100. }
  101. @Override
  102. public Result yhShopDistributeApply(YhShopApplyMoneyReq applyMoneyReq) throws Exception {
  103. String type = "shop";
  104. String key = String.format(RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getName(), DateTime.now().toDateStr(), type, applyMoneyReq.getUserId());
  105. if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) {
  106. throw BusinessRuntimeException.getInstance("每天仅可提现一次");
  107. }
  108. try {
  109. Result result = yhShopApplyMoney(applyMoneyReq);
  110. if (result.isFlag()) {
  111. redisService.set(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout());
  112. } else {
  113. redisService.del(key);
  114. }
  115. return result;
  116. } catch (Exception e) {
  117. redisService.del(key);
  118. throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e));
  119. }
  120. }
  121. @Override
  122. public AlipayResponse transferAccounts(TransferAccountsReq transferAccountsReq) throws Exception {
  123. Long userId = transferAccountsReq.getUserId();
  124. String account = transferAccountsReq.getAccount();
  125. String name = transferAccountsReq.getName();
  126. BigDecimal money = transferAccountsReq.getMoney();
  127. String type = transferAccountsReq.getType();
  128. BigDecimal points = transferAccountsReq.getPoints();
  129. Integer channelType = transferAccountsReq.getChannelType();
  130. String remark = transferAccountsReq.getRemark();
  131. /* 生成商户转账订单号 */
  132. String donNo = SEQUENCE.nextId().toString();
  133. if (StrUtil.isEmpty(type)) {
  134. type = TransferConstant.ALIPAY;
  135. }
  136. if (!envCommonService.isPrdEnv()) {
  137. name += "xx";
  138. }
  139. Integer scope = transferAccountsReq.getScope();
  140. SysConfig config = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  141. .eq(SysConfig::getSysKey, Constant.TAKE_MONEY_DEFAULT_ZFB)
  142. .last("limit 1"));
  143. String takeMoneyDefaultZfb = ShopConfig.DISTRIBUTE_ZFB_DEFAULT_APP_ID;
  144. if (config != null && StrUtil.isNotEmpty(config.getSysValue())) {
  145. takeMoneyDefaultZfb = config.getSysValue();
  146. }
  147. AlipayClient alipayClient = AliPayClientFactory.getAlipayClient(takeMoneyDefaultZfb);
  148. AlipayRequest alipayRequest = aliPayTransferAccountsRequest(donNo, account, name, money, remark);
  149. AlipayResponse response = alipayClient.certificateExecute(alipayRequest);
  150. if (response.isSuccess()) {
  151. log.info("支付宝转账金额:{}至用户:{}成功", money.divide(BigDecimal.valueOf(100)), userId);
  152. saveTransferRecord(userId, donNo, account, name, money, points, type, scope, response.getBody(), null, channelType);
  153. return response;
  154. }
  155. log.info("支付宝转账金额:{}至用户:{}失败", money.divide(BigDecimal.valueOf(100)), userId);
  156. saveTransferRecord(userId, donNo, account, name, money, points, type, scope, response.getBody(), response.getSubMsg(), channelType);
  157. return response;
  158. }
  159. @Override
  160. public Result<String> subApplyMoney(ApplyMoneyReq applyMoneyReq) throws Exception {
  161. applyMoneyReq.setMType(DistributeMoneyApply.MType.sub);
  162. return commonApplyMoney(applyMoneyReq);
  163. }
  164. @Override
  165. public Result<String> userMirrorShopDistributeApply(UserMirrorShopApplyMoneyReq applyMoneyReq) {
  166. String type = "mirrorShop";
  167. String key = String.format(RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getName(), DateTime.now().toDateStr(), type, applyMoneyReq.getUserId());
  168. if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) {
  169. throw BusinessRuntimeException.getInstance("每天仅可提现一次");
  170. }
  171. try {
  172. Result result = userMirrorShopApplyMoney(applyMoneyReq);
  173. if (result.isFlag()) {
  174. redisService.set(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout());
  175. } else {
  176. redisService.del(key);
  177. }
  178. return result;
  179. } catch (Exception e) {
  180. redisService.del(key);
  181. throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e));
  182. }
  183. }
  184. private Result userMirrorShopApplyMoney(UserMirrorShopApplyMoneyReq applyMoneyReq) throws Exception {
  185. Long userId = applyMoneyReq.getUserId();
  186. User user = userMapper.selectById(userId);
  187. if (user == null) {
  188. throw BusinessRuntimeException.getInstance("请先授权登录");
  189. }
  190. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  191. UserMirrorShopDistributeMoneyApply.Type type = applyMoneyReq.getType();
  192. if (type == null) type = UserMirrorShopDistributeMoneyApply.Type.zfb;
  193. if (type == UserMirrorShopDistributeMoneyApply.Type.zfb) {
  194. if (ObjectUtil.hasEmpty(applyMoneyReq.getAccount(), applyMoneyReq.getRealName())) {
  195. throw BusinessRuntimeException.getInstance("必填项不能为空");
  196. }
  197. if (applyMoneyReq.getAccount().length() > 255 || applyMoneyReq.getRealName().length() > 32) {
  198. throw BusinessRuntimeException.getInstance("请输入正确的账户信息");
  199. }
  200. }
  201. if (type == UserMirrorShopDistributeMoneyApply.Type.usdt) {
  202. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  203. throw BusinessRuntimeException.getInstance("USDT提现钱包地址不能为空");
  204. }
  205. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  206. throw BusinessRuntimeException.getInstance("请输入正确的钱包地址");
  207. }
  208. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  209. throw BusinessRuntimeException.getInstance("备注过多");
  210. }
  211. isTakeAll = true;
  212. }
  213. UserMirrorShopDistributeMoneyApply distributeMoneyApply = null;
  214. BigDecimal needApplyMoney = user.getMpMoney();
  215. BigDecimal fourHundred = BigDecimal.valueOf(4 * 10000);
  216. if (isTakeAll == null || !isTakeAll) {
  217. if (needApplyMoney.compareTo(fourHundred) > 0) {
  218. needApplyMoney = fourHundred;
  219. }
  220. }
  221. Integer update = userMapper.subUserMirrorShopMoney(user.getId(), user.getMpMoney(), needApplyMoney);
  222. if (update == 1) {
  223. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new UserMirrorShopDistributeMoneyApply());
  224. distributeMoneyApply.setUserId(userId);
  225. distributeMoneyApply.setMoney(needApplyMoney);
  226. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  227. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  228. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  229. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  230. distributeMoneyApply.setType(applyMoneyReq.getType());
  231. if (isTakeAll != null && isTakeAll) {
  232. distributeMoneyApply.setVerifyStatus(UserMirrorShopDistributeMoneyApply.Status.verifying);
  233. } else {
  234. TransferAccountsReq req = new TransferAccountsReq();
  235. req.setMoney(needApplyMoney);
  236. req.setAccount(applyMoneyReq.getAccount());
  237. req.setName(applyMoneyReq.getRealName());
  238. req.setChannelType(2);
  239. req.setUserId(userId);
  240. req.setRemark("镜像小店提现");
  241. AlipayResponse alipayResponse = transferAccounts(req);
  242. if (alipayResponse.isSuccess()) {
  243. distributeMoneyApply.setVerifyStatus(UserMirrorShopDistributeMoneyApply.Status.success);
  244. } else {
  245. distributeMoneyApply.setVerifyStatus(UserMirrorShopDistributeMoneyApply.Status.fail);
  246. distributeMoneyApply.setReason(alipayResponse.getSubMsg());
  247. userBenefitsService.addMpMoney(userId, distributeMoneyApply.getMoney());
  248. if (StrUtil.equals(alipayResponse.getSubCode(), "SECURITY_CHECK_FAILED")) {
  249. return GatewayResponse.FAIL.newBuilder().setMsg("当前支付宝提现人数较多,请稍后再试").toResult();
  250. }
  251. if (alipayResponse.getSubMsg().contains("冻结")) {
  252. return GatewayResponse.FAIL.newBuilder().setMsg("目前财务清账中,48小时后可提现").toResult();
  253. }
  254. if (alipayResponse.getSubMsg().contains("余额不足")) {
  255. return GatewayResponse.FAIL.newBuilder().setMsg("转账失败,请联系客服").toResult();
  256. }
  257. return GatewayResponse.FAIL.newBuilder().setMsg(alipayResponse.getSubMsg()).toResult();
  258. }
  259. }
  260. try {
  261. userMirrorShopDistributeMoneyApplyMapper.insert(distributeMoneyApply);
  262. //大额提现 超过400
  263. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == UserMirrorShopDistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  264. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "镜像小店有大额提现申请【崇宇信息】");
  265. }
  266. } catch (Exception e) {
  267. userBenefitsService.addMpMoney(userId, distributeMoneyApply.getMoney());
  268. return GatewayResponse.FAIL.newBuilder().setMsg("网络错误,请重试").toResult();
  269. }
  270. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  271. }
  272. throw BusinessRuntimeException.getInstance("系统异常,请重新提交提现申请");
  273. }
  274. private AlipayRequest aliPayTransferAccountsRequest(String donNo, String account, String name, BigDecimal money, String remark) {
  275. //支付宝转账
  276. AlipayFundTransUniTransferRequest transUniTransferRequest = new AlipayFundTransUniTransferRequest();
  277. //转账参数
  278. AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
  279. model.setOutBizNo(donNo);
  280. //单位元
  281. model.setTransAmount(money.divide(BigDecimal.valueOf(100)).toString());
  282. model.setProductCode(ZfbProductCode.STD_RED_PACKET.getProductCode());
  283. model.setBizScene(TransferConstant.DIRECT_TRANSFER);
  284. model.setOrderTitle(remark);
  285. //红包业务必传,取值REDPACKET
  286. JSONObject businessParams = new JSONObject();
  287. businessParams.putOpt(TransferConstant.SUB_BIZ_SCENE, TransferConstant.RED_PACKET);
  288. model.setBusinessParams(businessParams.toString());
  289. Participant payeeInfo = new Participant();
  290. payeeInfo.setIdentity(account);
  291. payeeInfo.setIdentityType(TransferConstant.ALIPAY_LOGON_ID);
  292. payeeInfo.setName(name);
  293. model.setPayeeInfo(payeeInfo);
  294. model.setRemark("分销提现转账");
  295. transUniTransferRequest.setBizModel(model);
  296. return transUniTransferRequest;
  297. }
  298. public void saveTransferRecord(Long userId, String donNo, String account, String name, BigDecimal money, BigDecimal points, String type, Integer scope, String body, String error, Integer channelType) throws Exception {
  299. TransferAccountsRecord record = new TransferAccountsRecord();
  300. JSONObject re = Jsons.parseObject(body, JSONObject.class);
  301. JSONObject response = Jsons.parseObject(re.get("alipay_fund_trans_uni_transfer_response"), JSONObject.class);
  302. String transactionId = response.getStr("order_id");
  303. String transDate = response.getStr("trans_date");
  304. record.setUserId(userId);
  305. record.setAccount(account);
  306. record.setName(name);
  307. record.setMoney(money);
  308. record.setType(type);
  309. record.setScope(scope);
  310. Optional.ofNullable(error).ifPresent(msg -> {
  311. record.setErrorMsg(msg);
  312. record.setStatus(0);
  313. });
  314. record.setPoints(points);
  315. record.setTransferOutNo(donNo);
  316. record.setTransactionId(transactionId);
  317. Optional.ofNullable(transDate).ifPresent(transferTime -> record.setTransferTime(DateTime.of(transferTime, "yyyy-MM-dd HH:mm:ss")));
  318. transferAccountsRecordMapper.insert(record);
  319. }
  320. public Result commonApplyMoney(ApplyMoneyReq applyMoneyReq) throws Exception {
  321. Long userId = applyMoneyReq.getUserId();
  322. User user = userMapper.selectById(userId);
  323. if (user == null) {
  324. throw BusinessRuntimeException.getInstance("请先授权登录");
  325. }
  326. if (user.getIsBlack()) throw BusinessRuntimeException.getInstance("系统检测操作异常,请稍后再试");
  327. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  328. DistributeMoneyApply.Type type = applyMoneyReq.getType();
  329. if (type == null) type = DistributeMoneyApply.Type.zfb;
  330. if (type == DistributeMoneyApply.Type.zfb) {
  331. if (ObjectUtil.hasEmpty(applyMoneyReq.getAccount(), applyMoneyReq.getRealName())) {
  332. throw BusinessRuntimeException.getInstance("必填项不能为空");
  333. }
  334. if (applyMoneyReq.getAccount().length() > 255 || applyMoneyReq.getRealName().length() > 32) {
  335. throw BusinessRuntimeException.getInstance("请输入正确的账户信息");
  336. }
  337. }
  338. if (type == DistributeMoneyApply.Type.usdt) {
  339. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  340. throw BusinessRuntimeException.getInstance("USDT提现钱包地址不能为空");
  341. }
  342. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  343. throw BusinessRuntimeException.getInstance("请输入正确的钱包地址");
  344. }
  345. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  346. throw BusinessRuntimeException.getInstance("备注过多");
  347. }
  348. isTakeAll = true;
  349. }
  350. DistributeMoneyApply.MType mType = applyMoneyReq.getMType();
  351. Assert.notNull(mType, "系统异常..");
  352. DistributeMoneyApply distributeMoneyApply = null;
  353. if (type == DistributeMoneyApply.Type.pwdRed) {
  354. distributeMoneyApply = userDistributeMapper.selectRelationBusiness(userId);
  355. isTakeAll = true;
  356. }
  357. // //今日是否已提现
  358. // 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"));
  359. // if (apply != null) {
  360. // throw BusinessRuntimeException.getInstance("今日已提现,请明日再进行操作");
  361. // }
  362. BigDecimal userPoints = null;
  363. BigDecimal needSubPoints = null;
  364. BigDecimal needApplyMoney = null;
  365. BigDecimal fourHundred = null;
  366. if (mType == DistributeMoneyApply.MType.ds) {
  367. userPoints = user.getPoints();
  368. DistributePointsExchangeMoney distributePointsExchangeMoney = distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).orderByDesc(DistributePointsExchangeMoney::getPoints).last("limit 1"));
  369. BigDecimal minPoints = BigDecimal.valueOf(distributePointsExchangeMoney.getMinPoints());
  370. BigDecimal ratePoints = BigDecimal.valueOf(distributePointsExchangeMoney.getPoints());
  371. BigDecimal minMoney = distributePointsExchangeMoney.getMoney();
  372. if (userPoints.compareTo(minPoints) < 0) {
  373. throw BusinessRuntimeException.getInstance("您当前积分未达到积分门槛,无法提现");
  374. }
  375. BigDecimal num = userPoints.divide(ratePoints, 0, RoundingMode.DOWN);
  376. needApplyMoney = num.multiply(minMoney);
  377. needSubPoints = num.multiply(ratePoints);
  378. fourHundred = BigDecimal.valueOf(4 * 100 * 100);
  379. if (isTakeAll == null || !isTakeAll) {
  380. if (needApplyMoney.compareTo(fourHundred) > 0) {
  381. needApplyMoney = fourHundred;
  382. needSubPoints = fourHundred.divide(minMoney).multiply(ratePoints);
  383. }
  384. }
  385. }
  386. if (mType == DistributeMoneyApply.MType.sub) {
  387. needApplyMoney = user.getSubApplyMoney();
  388. if (needApplyMoney.compareTo(BigDecimal.ZERO) <= 0) {
  389. throw BusinessRuntimeException.getInstance("您暂无可提现金额..");
  390. }
  391. }
  392. Integer update = 0;
  393. String zfbRemark = StrUtil.EMPTY;
  394. if (mType == DistributeMoneyApply.MType.ds) {
  395. update = userMapper.subPoints(user.getId(), needSubPoints);
  396. zfbRemark = Constant.APPLY_MONEY_DS;
  397. }
  398. if (mType == DistributeMoneyApply.MType.sub) {
  399. update = userMapper.suUserSubApplyMoney(user.getId(), needApplyMoney);
  400. zfbRemark = Constant.APPLY_MONEY_SUB;
  401. }
  402. if (update == 1) {
  403. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new DistributeMoneyApply());
  404. distributeMoneyApply.setUserId(userId);
  405. distributeMoneyApply.setPoints(needSubPoints);
  406. distributeMoneyApply.setMoney(needApplyMoney);
  407. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  408. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  409. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  410. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  411. distributeMoneyApply.setType(applyMoneyReq.getType());
  412. distributeMoneyApply.setMType(applyMoneyReq.getMType());
  413. if (isTakeAll != null && isTakeAll) {
  414. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.verifying);
  415. } else {
  416. TransferAccountsReq req = new TransferAccountsReq();
  417. req.setMoney(needApplyMoney);
  418. req.setAccount(applyMoneyReq.getAccount());
  419. req.setName(applyMoneyReq.getRealName());
  420. req.setPoints(needSubPoints);
  421. req.setUserId(userId);
  422. req.setChannelType(1);
  423. req.setRemark(zfbRemark);
  424. AlipayResponse alipayResponse = transferAccounts(req);
  425. if (alipayResponse.isSuccess()) {
  426. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.success);
  427. } else {
  428. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.fail);
  429. distributeMoneyApply.setReason(alipayResponse.getSubMsg());
  430. if (mType == DistributeMoneyApply.MType.ds){
  431. userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints());
  432. }
  433. if (mType == DistributeMoneyApply.MType.sub) {
  434. userBenefitsService.addDistributeSubMoney(userId, needApplyMoney);
  435. }
  436. if (StrUtil.equals(alipayResponse.getSubCode(), "SECURITY_CHECK_FAILED")) {
  437. return GatewayResponse.FAIL.newBuilder().setMsg("当前支付宝提现人数较多,请稍后再试").toResult();
  438. }
  439. if (alipayResponse.getSubMsg().contains("冻结")) {
  440. return GatewayResponse.FAIL.newBuilder().setMsg("目前财务清账中,48小时后可提现").toResult();
  441. }
  442. if (alipayResponse.getSubMsg().contains("余额不足")) {
  443. return GatewayResponse.FAIL.newBuilder().setMsg("转账失败,请联系客服").toResult();
  444. }
  445. return GatewayResponse.FAIL.newBuilder().setMsg(alipayResponse.getSubMsg()).toResult();
  446. }
  447. }
  448. try {
  449. distributeMoneyApplyMapper.insert(distributeMoneyApply);
  450. //大额提现 超过400
  451. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == DistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  452. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "推广有大额提现申请【崇宇信息】");
  453. }
  454. userDistributeReturnMoneyDetailService.recordDistributeReturnMoney(distributeMoneyApply.getUserId(), distributeMoneyApply.getMoney().negate(), "提现");
  455. } catch (Exception e) {
  456. userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints());
  457. return GatewayResponse.FAIL.newBuilder().setMsg("网络错误,请重试").toResult();
  458. }
  459. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  460. }
  461. throw BusinessRuntimeException.getInstance("系统异常,请重新提交提现申请");
  462. }
  463. public Result yhShopApplyMoney(YhShopApplyMoneyReq applyMoneyReq) throws Exception {
  464. Long userId = applyMoneyReq.getUserId();
  465. User user = userMapper.selectById(userId);
  466. if (user == null) {
  467. throw BusinessRuntimeException.getInstance("请先授权登录");
  468. }
  469. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  470. YhShopDistributeMoneyApply.Type type = applyMoneyReq.getType();
  471. if (type == null) type = YhShopDistributeMoneyApply.Type.zfb;
  472. if (type == YhShopDistributeMoneyApply.Type.zfb) {
  473. if (ObjectUtil.hasEmpty(applyMoneyReq.getAccount(), applyMoneyReq.getRealName())) {
  474. throw BusinessRuntimeException.getInstance("必填项不能为空");
  475. }
  476. if (applyMoneyReq.getAccount().length() > 255 || applyMoneyReq.getRealName().length() > 32) {
  477. throw BusinessRuntimeException.getInstance("请输入正确的账户信息");
  478. }
  479. }
  480. if (type == YhShopDistributeMoneyApply.Type.usdt) {
  481. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  482. throw BusinessRuntimeException.getInstance("USDT提现钱包地址不能为空");
  483. }
  484. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  485. throw BusinessRuntimeException.getInstance("请输入正确的钱包地址");
  486. }
  487. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  488. throw BusinessRuntimeException.getInstance("备注过多");
  489. }
  490. isTakeAll = true;
  491. }
  492. YhShopDistributeMoneyApply distributeMoneyApply = null;
  493. if (type == YhShopDistributeMoneyApply.Type.pwdRed) {
  494. distributeMoneyApply = yhShopMapper.selectRelationBusiness(userId);
  495. isTakeAll = true;
  496. }
  497. BigDecimal needApplyMoney = user.getYhsMoney();
  498. BigDecimal fourHundred = BigDecimal.valueOf(4 * 10000);
  499. if (isTakeAll == null || !isTakeAll) {
  500. if (needApplyMoney.compareTo(fourHundred) > 0) {
  501. needApplyMoney = fourHundred;
  502. }
  503. }
  504. Integer update = userMapper.subYhsMoney(user.getId(), needApplyMoney);
  505. if (update == 1) {
  506. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new YhShopDistributeMoneyApply());
  507. distributeMoneyApply.setUserId(userId);
  508. distributeMoneyApply.setMoney(needApplyMoney);
  509. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  510. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  511. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  512. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  513. distributeMoneyApply.setType(applyMoneyReq.getType());
  514. if (isTakeAll != null && isTakeAll) {
  515. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.verifying);
  516. } else {
  517. TransferAccountsReq req = new TransferAccountsReq();
  518. req.setMoney(needApplyMoney);
  519. req.setAccount(applyMoneyReq.getAccount());
  520. req.setName(applyMoneyReq.getRealName());
  521. req.setChannelType(2);
  522. req.setUserId(userId);
  523. req.setRemark("小店提现");
  524. AlipayResponse alipayResponse = transferAccounts(req);
  525. if (alipayResponse.isSuccess()) {
  526. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.success);
  527. } else {
  528. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.fail);
  529. distributeMoneyApply.setReason(alipayResponse.getSubMsg());
  530. userBenefitsService.addYhsMoney(userId, distributeMoneyApply.getMoney());
  531. if (StrUtil.equals(alipayResponse.getSubCode(), "SECURITY_CHECK_FAILED")) {
  532. return GatewayResponse.FAIL.newBuilder().setMsg("当前支付宝提现人数较多,请稍后再试").toResult();
  533. }
  534. if (alipayResponse.getSubMsg().contains("冻结")) {
  535. return GatewayResponse.FAIL.newBuilder().setMsg("目前财务清账中,48小时后可提现").toResult();
  536. }
  537. if (alipayResponse.getSubMsg().contains("余额不足")) {
  538. return GatewayResponse.FAIL.newBuilder().setMsg("转账失败,请联系客服").toResult();
  539. }
  540. return GatewayResponse.FAIL.newBuilder().setMsg(alipayResponse.getSubMsg()).toResult();
  541. }
  542. }
  543. try {
  544. yhShopDistributeMoneyApplyMapper.insert(distributeMoneyApply);
  545. //大额提现 超过400
  546. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == YhShopDistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  547. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "小店店铺有大额提现申请【崇宇信息】");
  548. }
  549. } catch (Exception e) {
  550. userBenefitsService.addYhsMoney(userId, distributeMoneyApply.getMoney());
  551. return GatewayResponse.FAIL.newBuilder().setMsg("网络错误,请重试").toResult();
  552. }
  553. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  554. }
  555. throw BusinessRuntimeException.getInstance("系统异常,请重新提交提现申请");
  556. }
  557. }