package com.cyksj.service.pay.impl; import cn.hutool.core.date.DateTime; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.cyksj.common.EnvCommonService; import com.cyksj.common.constant.Constant; import com.cyksj.common.exception.BusinessRuntimeException; import com.cyksj.common.i18n.I18nMessageUtil; import com.cyksj.common.snowflake.Sequence; import com.cyksj.common.util.SmsUtil; import com.cyksj.common.util.StringUtil; import com.cyksj.dto.Result; import com.cyksj.enums.GatewayResponse; import com.cyksj.mapper.UserMapper; import com.cyksj.mapper.manage.distribute.DistributeMoneyApplyMapper; import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper; import com.cyksj.mapper.manage.distribute.UserDistributeMapper; import com.cyksj.model.entity.DistributeMoneyApply; import com.cyksj.model.entity.DistributePointsExchangeMoney; import com.cyksj.model.entity.User; import com.cyksj.model.request.ApplyMoneyReq; import com.cyksj.redis.RedisService; import com.cyksj.service.distribute.UserDistributeReturnMoneyDetailService; import com.cyksj.service.pay.TransferFuncService; import com.cyksj.service.sys.SysConfigService; import com.cyksj.service.user.UserBenefitsService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Optional; /* *项目名: yhlxj *文件名: TransferFuncServiceImpl *创建者: JavaZou *创建时间:2023/9/20 18:55 */ @Service @RequiredArgsConstructor @Slf4j public class TransferFuncServiceImpl implements TransferFuncService { private static final Sequence SEQUENCE = new Sequence(0); private final UserMapper userMapper; private final UserBenefitsService userBenefitsService; private final EnvCommonService envCommonService; private final UserDistributeMapper userDistributeMapper; private final DistributePointsExchangeMoneyMapper distributePointsExchangeMoneyMapper; private final DistributeMoneyApplyMapper distributeMoneyApplyMapper; private final RedisService redisService; private final SysConfigService sysConfigService; private final UserDistributeReturnMoneyDetailService userDistributeReturnMoneyDetailService; @Override public Result distributeApply(ApplyMoneyReq applyMoneyReq) throws Exception { applyMoneyReq.setMType(DistributeMoneyApply.MType.ds); String key = String.format(RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getName(), DateTime.now().toDateStr(), applyMoneyReq.getMType(), applyMoneyReq.getUserId()); if (!redisService.setNx(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout())) { throw BusinessRuntimeException.getInstance("每天仅可提现一次"); } try { Result result = commonApplyMoney(applyMoneyReq); if (result.isFlag()) { redisService.set(key, applyMoneyReq.getUserId(), RedisService.key.APPLY_MONEY_DAY_LIMIT_KEY.getTimeout()); } else { redisService.del(key); } return result; } catch (Exception e) { redisService.del(key); throw BusinessRuntimeException.getInstance(StringUtil.getErrorMsg(e)); } } public Result commonApplyMoney(ApplyMoneyReq applyMoneyReq) throws Exception { Long userId = applyMoneyReq.getUserId(); User user = userMapper.selectById(userId); if (user == null) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error")); } if (user.getIsBlack()) throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("user_black_error")); Boolean isTakeAll = applyMoneyReq.getIsTakeAll(); DistributeMoneyApply.Type type = applyMoneyReq.getType(); if (type == null) type = DistributeMoneyApply.Type.usdt; if (type == DistributeMoneyApply.Type.usdt) { if (StrUtil.isEmpty(applyMoneyReq.getWalletAddr())) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_usdt_address_error")); } if (applyMoneyReq.getWalletAddr().length() > 1000) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_usdt_address_error")); } isTakeAll = true; } if (type == DistributeMoneyApply.Type.bank) { if (StrUtil.isEmpty(applyMoneyReq.getRealName())) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg(I18nMessageUtil.getI18nMsg("reaL_name_error"))); } if (StrUtil.isEmpty(applyMoneyReq.getBank())) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("enter_bank_no_error")); } isTakeAll = true; } if (StrUtil.isNotBlank(applyMoneyReq.getRemark()) && applyMoneyReq.getRemark().length() > 512) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_remark_length_error")); } DistributeMoneyApply.MType mType = applyMoneyReq.getMType(); DistributeMoneyApply distributeMoneyApply = null; // //今日是否已提现 // 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")); // if (apply != null) { // throw BusinessRuntimeException.getInstance("今日已提现,请明日再进行操作"); // } BigDecimal userPoints = null; BigDecimal needSubPoints = null; BigDecimal needApplyMoney = null; BigDecimal fourHundred = null; if (mType == DistributeMoneyApply.MType.ds) { userPoints = user.getPoints(); DistributePointsExchangeMoney distributePointsExchangeMoney = distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).orderByDesc(DistributePointsExchangeMoney::getPoints).last("limit 1")); BigDecimal minPoints = BigDecimal.valueOf(distributePointsExchangeMoney.getMinPoints()); BigDecimal ratePoints = BigDecimal.valueOf(distributePointsExchangeMoney.getPoints()); BigDecimal minMoney = distributePointsExchangeMoney.getMoney(); if (userPoints.compareTo(minPoints) < 0) { throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("distribute_apply_money_thresholds_error")); } BigDecimal num = userPoints.divide(ratePoints, 0, RoundingMode.DOWN); needApplyMoney = num.multiply(minMoney); needSubPoints = num.multiply(ratePoints); fourHundred = BigDecimal.valueOf(4 * 100 * 100); if (isTakeAll == null || !isTakeAll) { if (needApplyMoney.compareTo(fourHundred) > 0) { needApplyMoney = fourHundred; needSubPoints = fourHundred.divide(minMoney).multiply(ratePoints); } } } Integer update = 0; if (mType == DistributeMoneyApply.MType.ds) { update = userMapper.subPoints(user.getId(), needSubPoints); } if (update == 1) { distributeMoneyApply = Optional.ofNullable(distributeMoneyApply).orElse(new DistributeMoneyApply()); distributeMoneyApply.setUserId(userId); distributeMoneyApply.setPoints(needSubPoints); distributeMoneyApply.setMoney(needApplyMoney); distributeMoneyApply.setAccount(applyMoneyReq.getAccount()); distributeMoneyApply.setRealName(applyMoneyReq.getRealName()); distributeMoneyApply.setWalletAddr(applyMoneyReq.getWalletAddr()); distributeMoneyApply.setRemark(applyMoneyReq.getRemark()); distributeMoneyApply.setType(applyMoneyReq.getType()); distributeMoneyApply.setMType(applyMoneyReq.getMType()); distributeMoneyApply.setBank(applyMoneyReq.getBank()); if (isTakeAll != null && isTakeAll) { distributeMoneyApply.setVerifyStatus(DistributeMoneyApply.Status.verifying); } else { //支付宝提现去掉 } try { distributeMoneyApplyMapper.insert(distributeMoneyApply); //大额提现 超过400 if (envCommonService.isPrdEnv() && distributeMoneyApply.getVerifyStatus() == DistributeMoneyApply.Status.verifying && distributeMoneyApply.getMoney().compareTo(fourHundred) > 0) { SmsUtil.sendLuoKey2Msg(Constant.ZK_MOBILE, "银河海外推广有大额提现申请【银河录像局】"); } userDistributeReturnMoneyDetailService.recordDistributeReturnMoney(distributeMoneyApply.getUserId(), distributeMoneyApply.getMoney().negate(), "提现"); } catch (Exception e) { userBenefitsService.addDistributePoints(userId, distributeMoneyApply.getPoints()); return GatewayResponse.FAIL.newBuilder().setMsg(I18nMessageUtil.getI18nMsg("sys_error")).toResult(); } return GatewayResponse.SUCCESS.newBuilder().toResult(distributeMoneyApply); } throw BusinessRuntimeException.getInstance(I18nMessageUtil.getI18nMsg("sys_error")); } }