| | |
| | | package cc.mrbird.febs.mall.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.enumerates.*; |
| | | import cc.mrbird.febs.common.enumerates.FlowTypeEnum; |
| | | import cc.mrbird.febs.common.enumerates.RunVipDataDictionaryEnum; |
| | | import cc.mrbird.febs.common.enumerates.RunVipMoneyFlowTypeEnum; |
| | | import cc.mrbird.febs.common.enumerates.YesOrNoEnum; |
| | | import cc.mrbird.febs.common.exception.FebsException; |
| | | import cc.mrbird.febs.common.utils.AppContants; |
| | | import cc.mrbird.febs.common.utils.LoginUserUtil; |
| | | import cc.mrbird.febs.common.utils.MallUtils; |
| | | import cc.mrbird.febs.common.utils.RedisUtils; |
| | | import cc.mrbird.febs.mall.conversion.RunVipConversion; |
| | | import cc.mrbird.febs.mall.dto.*; |
| | | import cc.mrbird.febs.mall.entity.*; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.ThreadLocalRandom; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.IntStream; |
| | | |
| | |
| | | private final IApiMallMemberWalletService walletService; |
| | | private final IMallMoneyFlowService mallMoneyFlowService; |
| | | private final RunVipGrowMapper runVipGrowMapper; |
| | | |
| | | private final RedisUtils redisUtils; |
| | | |
| | | @Override |
| | | public List<ApiRunVipVo> vipInfo() { |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | |
| | | mallCharge.setType(mallMemberPayment.getBankNo()); |
| | | mallCharge.setAddress(mallMemberPayment.getBank()); |
| | | mallCharge.setAmount(presentAmount); |
| | | |
| | | BigDecimal amountReal = processAmount(presentAmount,memberId); |
| | | mallCharge.setAmountReal(amountReal); |
| | | |
| | | mallCharge.setFailTime(failTime); |
| | | mallCharge.setSysAddress(sysAddress); |
| | | mallCharge.setVipCode(runVipNext.getVipCode()); |
| | |
| | | ApiGoChargeVo apiGoChargeVo = new ApiGoChargeVo(); |
| | | apiGoChargeVo.setFailTime(mallCharge.getFailTime()); |
| | | apiGoChargeVo.setAddress(mallCharge.getAddress()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmount()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmountReal()); |
| | | apiGoChargeVo.setSysAddress(mallCharge.getSysAddress()); |
| | | apiGoChargeVo.setSysAddressType(mallCharge.getType()); |
| | | |
| | |
| | | } |
| | | apiGoChargeVo.setFailTime(mallCharge.getFailTime()); |
| | | apiGoChargeVo.setAddress(mallCharge.getAddress()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmount()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmountReal()); |
| | | apiGoChargeVo.setSysAddress(mallCharge.getSysAddress()); |
| | | apiGoChargeVo.setSysAddressType(mallCharge.getType()); |
| | | return new FebsResponse().success().data(apiGoChargeVo); |
| | |
| | | mallCharge.setSysAddress(sysAddress); |
| | | |
| | | mallCharge.setAmount(amount); |
| | | |
| | | BigDecimal amountReal = processAmount(amount,memberId); |
| | | mallCharge.setAmountReal(amountReal); |
| | | |
| | | mallCharge.setVipCnt(1); |
| | | mallCharge.setVipName(runVip.getVipName()+"权益"); |
| | | mallCharge.setVipCode(runVip.getVipCode()); |
| | |
| | | |
| | | apiGoChargeVo.setFailTime(mallCharge.getFailTime()); |
| | | apiGoChargeVo.setAddress(mallCharge.getAddress()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmount()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmountReal()); |
| | | apiGoChargeVo.setSysAddress(mallCharge.getSysAddress()); |
| | | apiGoChargeVo.setSysAddressType(mallCharge.getType()); |
| | | |
| | |
| | | |
| | | mallCharge.setAmount(amount); |
| | | mallCharge.setVipName(RunVipMoneyFlowTypeEnum.COMMISSION_PAY_CHARGE.getTypeDec()); |
| | | |
| | | BigDecimal amountReal = processAmount(amount,memberId); |
| | | mallCharge.setAmountReal(amountReal); |
| | | |
| | | mallChargeMapper.insert(mallCharge); |
| | | |
| | | apiGoChargeVo.setFailTime(mallCharge.getFailTime()); |
| | | apiGoChargeVo.setAddress(mallCharge.getAddress()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmount()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmountReal()); |
| | | apiGoChargeVo.setSysAddress(mallCharge.getSysAddress()); |
| | | apiGoChargeVo.setSysAddressType(mallCharge.getType()); |
| | | |
| | |
| | | } |
| | | apiGoChargeVo.setFailTime(mallCharge.getFailTime()); |
| | | apiGoChargeVo.setAddress(mallCharge.getAddress()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmount()); |
| | | apiGoChargeVo.setAmount(mallCharge.getAmountReal()); |
| | | apiGoChargeVo.setSysAddress(mallCharge.getSysAddress()); |
| | | apiGoChargeVo.setSysAddressType(mallCharge.getType()); |
| | | return new FebsResponse().success().data(apiGoChargeVo); |
| | | } |
| | | |
| | | /** |
| | | * 处理金额并生成唯一编号 |
| | | * 此方法用于根据给定的金额和会员ID生成一个唯一的数字 |
| | | * |
| | | * @param amount 金额,必须大于0 |
| | | * @param memberId 会员ID,不能为空 |
| | | * @return 生成的唯一数字 |
| | | * @throws NullPointerException 如果金额或会员ID为null |
| | | */ |
| | | public BigDecimal processAmount(BigDecimal amount, Long memberId) { |
| | | Objects.requireNonNull(amount, "金额不能为空"); |
| | | Objects.requireNonNull(memberId, "会员ID不能为空"); |
| | | |
| | | return generateUniqueNumber(amount,memberId); |
| | | } |
| | | |
| | | /** |
| | | * 生成唯一数字 |
| | | * 通过随机数和给定的金额计算生成一个唯一数字,并确保这个数字在Redis中是唯一的 |
| | | * |
| | | * @param amount 金额 |
| | | * @param memberId 会员ID |
| | | * @return 生成的唯一数字 |
| | | * @throws RuntimeException 如果在指定次数内无法生成唯一数字 |
| | | */ |
| | | private BigDecimal generateUniqueNumber(BigDecimal amount,Long memberId) { |
| | | int maxAttempts = 50; |
| | | int attempts = 0; |
| | | int num; |
| | | BigDecimal bigDecimal; |
| | | |
| | | do { |
| | | if (attempts++ >= maxAttempts) { |
| | | throw new RuntimeException("无法生成唯一数字"); |
| | | } |
| | | num = ThreadLocalRandom.current().nextInt(1, 51); |
| | | |
| | | bigDecimal = calculateAmount(amount, num); |
| | | |
| | | } while (!setRedisValue(bigDecimal, memberId)); // 原子操作检查并写入 |
| | | |
| | | return bigDecimal; |
| | | } |
| | | |
| | | /** |
| | | * 计算调整后的金额 |
| | | * 根据给定的金额和一个随机数计算新的金额,用于生成唯一数字 |
| | | * |
| | | * @param amount 原始金额 |
| | | * @param num 随机生成的数字 |
| | | * @return 调整后的金额 |
| | | */ |
| | | private BigDecimal calculateAmount(BigDecimal amount, int num) { |
| | | BigDecimal multiplier = new BigDecimal("0.01"); |
| | | BigDecimal increment = new BigDecimal(num).multiply(multiplier); |
| | | return amount.add(increment).setScale(2, RoundingMode.DOWN); |
| | | } |
| | | |
| | | /** |
| | | * 将生成的数字设置到Redis中 |
| | | * 此方法确保生成的数字是唯一的,通过在Redis中设置值并使用NX参数来实现 |
| | | * |
| | | * @param amountReal 生成的唯一数字 |
| | | * @param memberId 会员ID |
| | | * @return 如果设置成功则返回true,否则返回false |
| | | */ |
| | | private boolean setRedisValue(BigDecimal amountReal, Long memberId) { |
| | | String failMinutes = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getType(), |
| | | RunVipDataDictionaryEnum.CHARGE_SYS_FAIL_TIME.getCode() |
| | | ).getValue(); |
| | | String key = AppContants.CHARGE_AMOUNT_PROFIX + amountReal; // 添加前缀避免键冲突 |
| | | return redisUtils.setNotExist(key, memberId, 60L * Integer.parseInt(failMinutes)); |
| | | } |
| | | |
| | | } |