|
@@ -1,9 +1,15 @@
|
|
|
package com.qs.mp.user.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
+import com.qs.mp.common.core.redis.DistributedLocker;
|
|
|
import com.qs.mp.common.enums.CoinLogTypeEnum;
|
|
|
+import com.qs.mp.common.exception.ServiceException;
|
|
|
+import com.qs.mp.framework.redis.RedisLockKey;
|
|
|
import com.qs.mp.user.domain.UserCoin;
|
|
|
import com.qs.mp.user.domain.UserCoinLog;
|
|
|
+import com.qs.mp.user.domain.dto.CoinTransferParamDTO;
|
|
|
+import com.qs.mp.user.domain.param.CoinTransferParam;
|
|
|
import com.qs.mp.user.mapper.UserCoinMapper;
|
|
|
import com.qs.mp.user.service.IUserCoinLogService;
|
|
|
import com.qs.mp.user.service.IUserCoinService;
|
|
@@ -28,6 +34,9 @@ public class UserCoinServiceImpl extends ServiceImpl<UserCoinMapper, UserCoin> i
|
|
|
@Autowired
|
|
|
private IUserCoinLogService userCoinLogService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DistributedLocker distributedLocker;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void produce(Long userId, Integer logCoin, String bizId, CoinLogTypeEnum coinLogTypeEnum) {
|
|
@@ -76,4 +85,66 @@ public class UserCoinServiceImpl extends ServiceImpl<UserCoinMapper, UserCoin> i
|
|
|
userCoinLog.setRefId(bizId);
|
|
|
userCoinLogService.save(userCoinLog);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public boolean transfer(CoinTransferParamDTO coinTransferParamDTO) {
|
|
|
+
|
|
|
+ // 转赠限制规则
|
|
|
+ // 一天:受赠人每天受赠10次,上限2000
|
|
|
+ // 一周:受赠人每周受赠50次,上限6000
|
|
|
+ // 每月:受赠人每月受赠100次,上限15000
|
|
|
+ // 每季度:受赠人每季度受赠500次,上限30000
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String LockKey = RedisLockKey.build(RedisLockKey.USER_COIN_TRANSFER_LOCK, coinTransferParamDTO.getFromUserId());
|
|
|
+ if (!distributedLocker.tryLock(LockKey)) {
|
|
|
+ throw new ServiceException("系统繁忙,请稍后再试");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 获取转赠方拥有盲豆数量
|
|
|
+ UserCoin userCoin = getById(coinTransferParamDTO.getFromUserId());
|
|
|
+ if (userCoin.getCoin() < (coinTransferParamDTO.getRealTransferCoin() + coinTransferParamDTO.getDeductTransferCoin())) {
|
|
|
+ throw new ServiceException("您的盲豆数量不足,不能转赠");
|
|
|
+ }
|
|
|
+ // 扣除转赠方盲豆
|
|
|
+ boolean rtn1 = update(new LambdaUpdateWrapper<UserCoin>().set(UserCoin::getCoin, userCoin.getCoin() - (coinTransferParamDTO.getRealTransferCoin() + coinTransferParamDTO.getDeductTransferCoin()))
|
|
|
+ .eq(UserCoin::getUserId, coinTransferParamDTO.getFromUserId()));
|
|
|
+ Assert.isTrue(rtn1, "扣除转赠方盲豆失败, userId:" + coinTransferParamDTO.getFromUserId());
|
|
|
+ // 增加受赠方盲豆
|
|
|
+ UserCoin toUserCoin = getById(coinTransferParamDTO.getToUserId());
|
|
|
+ boolean rtn2 = update(new LambdaUpdateWrapper<UserCoin>().set(UserCoin::getCoin, toUserCoin.getCoin() + coinTransferParamDTO.getRealTransferCoin())
|
|
|
+ .eq(UserCoin::getUserId, coinTransferParamDTO.getToUserId()));
|
|
|
+ Assert.isTrue(rtn2, "增加受赠方盲豆失败,userId:" + coinTransferParamDTO.getToUserId());
|
|
|
+
|
|
|
+ // 盲豆转赠日志
|
|
|
+ UserCoinLog userCoinFromLog = new UserCoinLog();
|
|
|
+ userCoinFromLog.setUserId(coinTransferParamDTO.getFromUserId());
|
|
|
+ userCoinFromLog.setType(CoinLogTypeEnum.TRANSFER);
|
|
|
+ userCoinFromLog.setMoney(userCoin.getCoin() - (coinTransferParamDTO.getRealTransferCoin() + coinTransferParamDTO.getDeductTransferCoin()));
|
|
|
+ userCoinFromLog.setLogMoney(-(coinTransferParamDTO.getRealTransferCoin() + coinTransferParamDTO.getDeductTransferCoin()));
|
|
|
+ userCoinFromLog.setIncomeExpense(CoinLogTypeEnum.EXPENSES);
|
|
|
+ userCoinFromLog.setLogText("盲豆转赠:(" + coinTransferParamDTO.getFromPhone() + ")" + ",转赠折损:" + coinTransferParamDTO.getDeductTransferCoin() + "盲豆");
|
|
|
+ userCoinFromLog.setBizTime(new Date());
|
|
|
+ userCoinFromLog.setCreatedTime(new Date());
|
|
|
+ userCoinFromLog.setUpdatedTime(new Date());
|
|
|
+ userCoinLogService.save(userCoinFromLog);
|
|
|
+
|
|
|
+ UserCoinLog userCoinToLog = new UserCoinLog();
|
|
|
+ userCoinToLog.setUserId(coinTransferParamDTO.getToUserId());
|
|
|
+ userCoinToLog.setType(CoinLogTypeEnum.TRANSFER);
|
|
|
+ userCoinToLog.setMoney(toUserCoin.getCoin() + coinTransferParamDTO.getRealTransferCoin());
|
|
|
+ userCoinToLog.setLogMoney(coinTransferParamDTO.getRealTransferCoin());
|
|
|
+ userCoinToLog.setIncomeExpense(CoinLogTypeEnum.INCOME);
|
|
|
+ userCoinToLog.setLogText("盲豆转赠:(" + coinTransferParamDTO.getFromPhone() + ")");
|
|
|
+ userCoinToLog.setBizTime(new Date());
|
|
|
+ userCoinToLog.setCreatedTime(new Date());
|
|
|
+ userCoinToLog.setUpdatedTime(new Date());
|
|
|
+ userCoinLogService.save(userCoinToLog);
|
|
|
+ } finally {
|
|
|
+ distributedLocker.unlock(LockKey);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|