TransferFuncServiceImpl.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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(), applyMoneyReq.getUserId().toString(), applyMoneyReq.getMType(), applyMoneyReq.getUserId());
  85. if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) {
  86. throw BusinessRuntimeException.getInstance("每2天可提现一次");
  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.TRANS_ACCOUNT_NO_PWD.getProductCode());
  283. model.setBizScene(TransferConstant.DIRECT_TRANSFER);
  284. model.setOrderTitle(remark);
  285. Participant payeeInfo = new Participant();
  286. payeeInfo.setIdentity(account);
  287. payeeInfo.setIdentityType(TransferConstant.ALIPAY_LOGON_ID);
  288. payeeInfo.setName(name);
  289. model.setPayeeInfo(payeeInfo);
  290. model.setRemark("分销提现转账");
  291. transUniTransferRequest.setBizModel(model);
  292. return transUniTransferRequest;
  293. }
  294. 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 {
  295. TransferAccountsRecord record = new TransferAccountsRecord();
  296. JSONObject re = Jsons.parseObject(body, JSONObject.class);
  297. JSONObject response = Jsons.parseObject(re.get("alipay_fund_trans_uni_transfer_response"), JSONObject.class);
  298. String transactionId = response.getStr("order_id");
  299. String transDate = response.getStr("trans_date");
  300. record.setUserId(userId);
  301. record.setAccount(account);
  302. record.setName(name);
  303. record.setMoney(money);
  304. record.setType(type);
  305. record.setScope(scope);
  306. Optional.ofNullable(error).ifPresent(msg -> {
  307. record.setErrorMsg(msg);
  308. record.setStatus(0);
  309. });
  310. record.setPoints(points);
  311. record.setTransferOutNo(donNo);
  312. record.setTransactionId(transactionId);
  313. Optional.ofNullable(transDate).ifPresent(transferTime -> record.setTransferTime(DateTime.of(transferTime, "yyyy-MM-dd HH:mm:ss")));
  314. transferAccountsRecordMapper.insert(record);
  315. }
  316. public Result commonApplyMoney(ApplyMoneyReq applyMoneyReq) throws Exception {
  317. Long userId = applyMoneyReq.getUserId();
  318. User user = userMapper.selectById(userId);
  319. if (user == null) {
  320. throw BusinessRuntimeException.getInstance("请先授权登录");
  321. }
  322. if (user.getIsBlack()) throw BusinessRuntimeException.getInstance("系统检测操作异常,请稍后再试");
  323. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  324. DistributeMoneyApply.Type type = applyMoneyReq.getType();
  325. if (type == null) type = DistributeMoneyApply.Type.zfb;
  326. if (type == DistributeMoneyApply.Type.zfb) {
  327. if (ObjectUtil.hasEmpty(applyMoneyReq.getAccount(), applyMoneyReq.getRealName())) {
  328. throw BusinessRuntimeException.getInstance("必填项不能为空");
  329. }
  330. if (applyMoneyReq.getAccount().length() > 255 || applyMoneyReq.getRealName().length() > 32) {
  331. throw BusinessRuntimeException.getInstance("请输入正确的账户信息");
  332. }
  333. }
  334. if (type == DistributeMoneyApply.Type.usdt) {
  335. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  336. throw BusinessRuntimeException.getInstance("USDT提现钱包地址不能为空");
  337. }
  338. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  339. throw BusinessRuntimeException.getInstance("请输入正确的钱包地址");
  340. }
  341. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  342. throw BusinessRuntimeException.getInstance("备注过多");
  343. }
  344. isTakeAll = true;
  345. }
  346. DistributeMoneyApply.MType mType = applyMoneyReq.getMType();
  347. Assert.notNull(mType, "系统异常..");
  348. DistributeMoneyApply distributeMoneyApply = null;
  349. if (type == DistributeMoneyApply.Type.pwdRed) {
  350. distributeMoneyApply = userDistributeMapper.selectRelationBusiness(userId);
  351. isTakeAll = true;
  352. }
  353. // //今日是否已提现
  354. // 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"));
  355. // if (apply != null) {
  356. // throw BusinessRuntimeException.getInstance("今日已提现,请明日再进行操作");
  357. // }
  358. BigDecimal userPoints = null;
  359. BigDecimal needSubPoints = null;
  360. BigDecimal needApplyMoney = null;
  361. BigDecimal fourHundred = null;
  362. if (mType == DistributeMoneyApply.MType.ds) {
  363. userPoints = user.getPoints();
  364. DistributePointsExchangeMoney distributePointsExchangeMoney = distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).orderByDesc(DistributePointsExchangeMoney::getPoints).last("limit 1"));
  365. BigDecimal minPoints = BigDecimal.valueOf(distributePointsExchangeMoney.getMinPoints());
  366. BigDecimal ratePoints = BigDecimal.valueOf(distributePointsExchangeMoney.getPoints());
  367. BigDecimal minMoney = distributePointsExchangeMoney.getMoney();
  368. if (userPoints.compareTo(minPoints) < 0) {
  369. throw BusinessRuntimeException.getInstance("您当前积分未达到积分门槛,无法提现");
  370. }
  371. BigDecimal num = userPoints.divide(ratePoints, 0, RoundingMode.DOWN);
  372. needApplyMoney = num.multiply(minMoney);
  373. needSubPoints = num.multiply(ratePoints);
  374. fourHundred = BigDecimal.valueOf(4 * 100 * 100);
  375. if (isTakeAll == null || !isTakeAll) {
  376. if (needApplyMoney.compareTo(fourHundred) > 0) {
  377. needApplyMoney = fourHundred;
  378. needSubPoints = fourHundred.divide(minMoney).multiply(ratePoints);
  379. }
  380. }
  381. }
  382. if (mType == DistributeMoneyApply.MType.sub) {
  383. needApplyMoney = user.getSubApplyMoney();
  384. if (needApplyMoney.compareTo(BigDecimal.ZERO) <= 0) {
  385. throw BusinessRuntimeException.getInstance("您暂无可提现金额..");
  386. }
  387. }
  388. Integer update = 0;
  389. String zfbRemark = StrUtil.EMPTY;
  390. if (mType == DistributeMoneyApply.MType.ds) {
  391. update = userMapper.subPoints(user.getId(), needSubPoints);
  392. zfbRemark = Constant.APPLY_MONEY_DS;
  393. }
  394. if (mType == DistributeMoneyApply.MType.sub) {
  395. update = userMapper.suUserSubApplyMoney(user.getId(), needApplyMoney);
  396. zfbRemark = Constant.APPLY_MONEY_SUB;
  397. }
  398. if (update == 1) {
  399. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new DistributeMoneyApply());
  400. distributeMoneyApply.setUserId(userId);
  401. distributeMoneyApply.setPoints(needSubPoints);
  402. distributeMoneyApply.setMoney(needApplyMoney);
  403. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  404. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  405. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  406. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  407. distributeMoneyApply.setType(applyMoneyReq.getType());
  408. distributeMoneyApply.setMType(applyMoneyReq.getMType());
  409. if (isTakeAll != null && isTakeAll) {
  410. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.verifying);
  411. } else {
  412. TransferAccountsReq req = new TransferAccountsReq();
  413. req.setMoney(needApplyMoney);
  414. req.setAccount(applyMoneyReq.getAccount());
  415. req.setName(applyMoneyReq.getRealName());
  416. req.setPoints(needSubPoints);
  417. req.setUserId(userId);
  418. req.setChannelType(1);
  419. req.setRemark(zfbRemark);
  420. AlipayResponse alipayResponse = transferAccounts(req);
  421. if (alipayResponse.isSuccess()) {
  422. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.success);
  423. } else {
  424. distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.fail);
  425. distributeMoneyApply.setReason(alipayResponse.getSubMsg());
  426. if (mType == DistributeMoneyApply.MType.ds){
  427. userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints());
  428. }
  429. if (mType == DistributeMoneyApply.MType.sub) {
  430. userBenefitsService.addDistributeSubMoney(userId, needApplyMoney);
  431. }
  432. if (StrUtil.equals(alipayResponse.getSubCode(), "SECURITY_CHECK_FAILED")) {
  433. return GatewayResponse.FAIL.newBuilder().setMsg("当前支付宝提现人数较多,请稍后再试").toResult();
  434. }
  435. if (alipayResponse.getSubMsg().contains("冻结")) {
  436. return GatewayResponse.FAIL.newBuilder().setMsg("目前财务清账中,48小时后可提现").toResult();
  437. }
  438. if (alipayResponse.getSubMsg().contains("余额不足")) {
  439. return GatewayResponse.FAIL.newBuilder().setMsg("转账失败,请联系客服").toResult();
  440. }
  441. return GatewayResponse.FAIL.newBuilder().setMsg(alipayResponse.getSubMsg()).toResult();
  442. }
  443. }
  444. try {
  445. distributeMoneyApplyMapper.insert(distributeMoneyApply);
  446. //大额提现 超过400
  447. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == DistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  448. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "推广有大额提现申请【崇宇信息】");
  449. }
  450. userDistributeReturnMoneyDetailService.recordDistributeReturnMoney(distributeMoneyApply.getUserId(), distributeMoneyApply.getMoney().negate(), "提现");
  451. } catch (Exception e) {
  452. userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints());
  453. return GatewayResponse.FAIL.newBuilder().setMsg("网络错误,请重试").toResult();
  454. }
  455. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  456. }
  457. throw BusinessRuntimeException.getInstance("系统异常,请重新提交提现申请");
  458. }
  459. public Result yhShopApplyMoney(YhShopApplyMoneyReq applyMoneyReq) throws Exception {
  460. Long userId = applyMoneyReq.getUserId();
  461. User user = userMapper.selectById(userId);
  462. if (user == null) {
  463. throw BusinessRuntimeException.getInstance("请先授权登录");
  464. }
  465. Boolean isTakeAll = applyMoneyReq.getIsTakeAll();
  466. YhShopDistributeMoneyApply.Type type = applyMoneyReq.getType();
  467. if (type == null) type = YhShopDistributeMoneyApply.Type.zfb;
  468. if (type == YhShopDistributeMoneyApply.Type.zfb) {
  469. if (ObjectUtil.hasEmpty(applyMoneyReq.getAccount(), applyMoneyReq.getRealName())) {
  470. throw BusinessRuntimeException.getInstance("必填项不能为空");
  471. }
  472. if (applyMoneyReq.getAccount().length() > 255 || applyMoneyReq.getRealName().length() > 32) {
  473. throw BusinessRuntimeException.getInstance("请输入正确的账户信息");
  474. }
  475. }
  476. if (type == YhShopDistributeMoneyApply.Type.usdt) {
  477. if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) {
  478. throw BusinessRuntimeException.getInstance("USDT提现钱包地址不能为空");
  479. }
  480. if (applyMoneyReq.getWalletAddr().length() > 1000) {
  481. throw BusinessRuntimeException.getInstance("请输入正确的钱包地址");
  482. }
  483. if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) {
  484. throw BusinessRuntimeException.getInstance("备注过多");
  485. }
  486. isTakeAll = true;
  487. }
  488. YhShopDistributeMoneyApply distributeMoneyApply = null;
  489. if (type == YhShopDistributeMoneyApply.Type.pwdRed) {
  490. distributeMoneyApply = yhShopMapper.selectRelationBusiness(userId);
  491. isTakeAll = true;
  492. }
  493. BigDecimal needApplyMoney = user.getYhsMoney();
  494. BigDecimal fourHundred = BigDecimal.valueOf(4 * 10000);
  495. if (isTakeAll == null || !isTakeAll) {
  496. if (needApplyMoney.compareTo(fourHundred) > 0) {
  497. needApplyMoney = fourHundred;
  498. }
  499. }
  500. Integer update = userMapper.subYhsMoney(user.getId(), needApplyMoney);
  501. if (update == 1) {
  502. distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new YhShopDistributeMoneyApply());
  503. distributeMoneyApply.setUserId(userId);
  504. distributeMoneyApply.setMoney(needApplyMoney);
  505. distributeMoneyApply.setAccount(applyMoneyReq.getAccount());
  506. distributeMoneyApply.setRealName(applyMoneyReq.getRealName());
  507. distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr());
  508. distributeMoneyApply.setRemark(applyMoneyReq.getRemark());
  509. distributeMoneyApply.setType(applyMoneyReq.getType());
  510. if (isTakeAll != null && isTakeAll) {
  511. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.verifying);
  512. } else {
  513. TransferAccountsReq req = new TransferAccountsReq();
  514. req.setMoney(needApplyMoney);
  515. req.setAccount(applyMoneyReq.getAccount());
  516. req.setName(applyMoneyReq.getRealName());
  517. req.setChannelType(2);
  518. req.setUserId(userId);
  519. req.setRemark("小店提现");
  520. AlipayResponse alipayResponse = transferAccounts(req);
  521. if (alipayResponse.isSuccess()) {
  522. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.success);
  523. } else {
  524. distributeMoneyApply.setVerifyStatus(YhShopDistributeMoneyApply.Status.fail);
  525. distributeMoneyApply.setReason(alipayResponse.getSubMsg());
  526. userBenefitsService.addYhsMoney(userId, distributeMoneyApply.getMoney());
  527. if (StrUtil.equals(alipayResponse.getSubCode(), "SECURITY_CHECK_FAILED")) {
  528. return GatewayResponse.FAIL.newBuilder().setMsg("当前支付宝提现人数较多,请稍后再试").toResult();
  529. }
  530. if (alipayResponse.getSubMsg().contains("冻结")) {
  531. return GatewayResponse.FAIL.newBuilder().setMsg("目前财务清账中,48小时后可提现").toResult();
  532. }
  533. if (alipayResponse.getSubMsg().contains("余额不足")) {
  534. return GatewayResponse.FAIL.newBuilder().setMsg("转账失败,请联系客服").toResult();
  535. }
  536. return GatewayResponse.FAIL.newBuilder().setMsg(alipayResponse.getSubMsg()).toResult();
  537. }
  538. }
  539. try {
  540. yhShopDistributeMoneyApplyMapper.insert(distributeMoneyApply);
  541. //大额提现 超过400
  542. if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == YhShopDistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) {
  543. SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "小店店铺有大额提现申请【崇宇信息】");
  544. }
  545. } catch (Exception e) {
  546. userBenefitsService.addYhsMoney(userId, distributeMoneyApply.getMoney());
  547. return GatewayResponse.FAIL.newBuilder().setMsg("网络错误,请重试").toResult();
  548. }
  549. return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply);
  550. }
  551. throw BusinessRuntimeException.getInstance("系统异常,请重新提交提现申请");
  552. }
  553. }