Commit 2b680f95 authored by zhangfeng's avatar zhangfeng

兑换礼品修改

parent 605cfe64
...@@ -11,9 +11,50 @@ import java.util.List; ...@@ -11,9 +11,50 @@ import java.util.List;
*/ */
public interface RedeemRewardApi { public interface RedeemRewardApi {
/**
* 兑换礼品
*
* @param redeemRewardReqVO
* @return
*/
Boolean redeemReward(RedeemRewardReqVO redeemRewardReqVO); Boolean redeemReward(RedeemRewardReqVO redeemRewardReqVO);
/**
* 执行兑换事务
*
* @param redeemRewardReqVO
* @param pointsRequire
* @param rewardId
*/
void executeRedeem(RedeemRewardReqVO redeemRewardReqVO, Integer pointsRequire, Long rewardId);
/**
* 批量兑换礼品
*
* @param redeemRewardReqVOList
* @return
*/
List<RedeemRewardRespDTO> redeemRewards(List<RedeemRewardReqVO> redeemRewardReqVOList); List<RedeemRewardRespDTO> redeemRewards(List<RedeemRewardReqVO> redeemRewardReqVOList);
/**
* 批量执行兑换事务
*
* @param redeemRewardReqVOList
* @param pointsRequire
* @param rewardId
*/
void batchExecuteRedeem(List<RedeemRewardReqVO> redeemRewardReqVOList, Integer pointsRequire, Long rewardId);
/**
* 客户端获取礼品详情
* @param redeemId
* @return
*/
RedeemDetailForUserDTO getRedeemDetailForUser(Long redeemId); RedeemDetailForUserDTO getRedeemDetailForUser(Long redeemId);
/**
* 兑换礼品发送验证码
* @param memberId
*/
void sendSmsCode(Long memberId); void sendSmsCode(Long memberId);
} }
...@@ -30,6 +30,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -30,6 +30,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -70,7 +71,6 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -70,7 +71,6 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
private SmsCodeApi smsCodeApi; private SmsCodeApi smsCodeApi;
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Boolean redeemReward(RedeemRewardReqVO redeemRewardReqVO) { public Boolean redeemReward(RedeemRewardReqVO redeemRewardReqVO) {
UserRespDTO memberUser = memberUserApi.getUser(redeemRewardReqVO.getMemberId()); UserRespDTO memberUser = memberUserApi.getUser(redeemRewardReqVO.getMemberId());
if (memberUser == null) { if (memberUser == null) {
...@@ -98,12 +98,8 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -98,12 +98,8 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
if (!lock.tryLock(2, 10, TimeUnit.SECONDS)) { if (!lock.tryLock(2, 10, TimeUnit.SECONDS)) {
throw exception(GET_LOCK_FAILED); throw exception(GET_LOCK_FAILED);
} }
// 添加兑换记录 RedeemRewardApi currentProxy = (RedeemRewardApi) AopContext.currentProxy();
Long redeemId = addRedeemRecord(redeemRewardReqVO, rewardDO); currentProxy.executeRedeem(redeemRewardReqVO, rewardDO.getPointsRequire(), rewardDO.getId());
// 更新会员积分
updateMemberScore(redeemRewardReqVO, rewardDO, redeemId);
// 更新礼品
redeemReward(rewardDO, redeemRewardReqVO.getRewardCount());
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw exception(GET_LOCK_FAILED); throw exception(GET_LOCK_FAILED);
} finally { } finally {
...@@ -112,6 +108,17 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -112,6 +108,17 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
return true; return true;
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void executeRedeem(RedeemRewardReqVO redeemRewardReqVO, Integer pointsRequire, Long rewardId) {
// 添加兑换记录
Long redeemId = addRedeemRecord(redeemRewardReqVO, pointsRequire);
// 更新会员积分
updateMemberScore(redeemRewardReqVO, pointsRequire, redeemId);
// 更新礼品
redeemReward(rewardId, redeemRewardReqVO.getRewardCount());
}
private RewardDO getAndVerifyRewardDO(RedeemRewardReqVO redeemRewardReqVO) { private RewardDO getAndVerifyRewardDO(RedeemRewardReqVO redeemRewardReqVO) {
// 查询礼品 // 查询礼品
RewardDO rewardDO = rewardMapper.selectById(redeemRewardReqVO.getRewardId()); RewardDO rewardDO = rewardMapper.selectById(redeemRewardReqVO.getRewardId());
...@@ -150,41 +157,42 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -150,41 +157,42 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
} }
} }
private void redeemReward(RewardDO reward, Integer rewardCount) { private void redeemReward(Long rewardId, Integer rewardCount) {
// 更新时校验礼品数量是否足够 // 更新时校验礼品数量是否足够
RewardDO rewardDO = rewardMapper.selectById(reward.getId()); RewardDO rewardDO = rewardMapper.selectById(rewardId);
if (rewardDO.getQuantityRemain() < rewardCount) { if (rewardDO.getQuantityRemain() < rewardCount) {
throw exception(REWARD_COUNT_NOT_ENOUGH); throw exception(REWARD_COUNT_NOT_ENOUGH);
} }
reward.setExchangeCount(rewardDO.getExchangeCount() + 1); RewardDO upReward = new RewardDO();
reward.setQuantityRemain(rewardDO.getQuantityRemain() - rewardCount); upReward.setId(rewardId);
rewardMapper.updateById(reward); upReward.setExchangeCount(rewardDO.getExchangeCount() + 1);
upReward.setQuantityRemain(rewardDO.getQuantityRemain() - rewardCount);
rewardMapper.updateById(upReward);
} }
private void updateMemberScore(RedeemRewardReqVO redeemRewardReqVO, RewardDO rewardDO, Long redeemId) { private void updateMemberScore(RedeemRewardReqVO redeemRewardReqVO, Integer pointsRequire, Long redeemId) {
Map<String, Object> extParam = new HashMap<>(); Map<String, Object> extParam = new HashMap<>();
extParam.put("redeemId", redeemId); extParam.put("redeemId", redeemId);
memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder() memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder()
.memberId(redeemRewardReqVO.getMemberId()) .memberId(redeemRewardReqVO.getMemberId())
.sourceType(ScoreSourceTypeEnum.EXCHANGE_REWARD) .sourceType(ScoreSourceTypeEnum.EXCHANGE_REWARD)
.scoreCount(redeemRewardReqVO.getRewardCount() * rewardDO.getPointsRequire()) .scoreCount(redeemRewardReqVO.getRewardCount() * pointsRequire)
.releationId(String.valueOf(redeemId)) .releationId(String.valueOf(redeemId))
.uniqueId(String.valueOf(redeemId)) .uniqueId(String.valueOf(redeemId))
.extParam(extParam) .extParam(extParam)
.build()); .build());
} }
private Long addRedeemRecord(RedeemRewardReqVO redeemRewardReqVO, RewardDO rewardDO) { private Long addRedeemRecord(RedeemRewardReqVO redeemRewardReqVO, Integer pointsRequire) {
RewardRedeemDO rewardRedeemDO = BeanUtil.copyProperties(redeemRewardReqVO, RewardRedeemDO.class); RewardRedeemDO rewardRedeemDO = BeanUtil.copyProperties(redeemRewardReqVO, RewardRedeemDO.class);
rewardRedeemDO.setId(snowflakeGenerator.next()); rewardRedeemDO.setId(snowflakeGenerator.next());
rewardRedeemDO.setStatus(RewardRedeemStatusEnum.REDEEMING.getValue()); rewardRedeemDO.setStatus(RewardRedeemStatusEnum.REDEEMING.getValue());
rewardRedeemDO.setScoreCount(redeemRewardReqVO.getRewardCount() * rewardDO.getPointsRequire()); rewardRedeemDO.setScoreCount(redeemRewardReqVO.getRewardCount() * pointsRequire);
rewardRedeemMapper.insert(rewardRedeemDO); rewardRedeemMapper.insert(rewardRedeemDO);
return rewardRedeemDO.getId(); return rewardRedeemDO.getId();
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public List<RedeemRewardRespDTO> redeemRewards(List<RedeemRewardReqVO> redeemRewardReqVOList) { public List<RedeemRewardRespDTO> redeemRewards(List<RedeemRewardReqVO> redeemRewardReqVOList) {
// 批量兑换每次最多十条 // 批量兑换每次最多十条
if (redeemRewardReqVOList.size() > 10) { if (redeemRewardReqVOList.size() > 10) {
...@@ -230,14 +238,8 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -230,14 +238,8 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
if (!lock.tryLock(2, 10, TimeUnit.SECONDS)) { if (!lock.tryLock(2, 10, TimeUnit.SECONDS)) {
throw exception(GET_LOCK_FAILED); throw exception(GET_LOCK_FAILED);
} }
for (RedeemRewardReqVO redeemRewardReqVO : redeemRewardReqVOList) { RedeemRewardApi currentProxy = (RedeemRewardApi) AopContext.currentProxy();
// 添加兑换记录 currentProxy.batchExecuteRedeem(redeemRewardReqVOList, rewardDO.getPointsRequire(), rewardDO.getId());
Long redeemId = addRedeemRecord(redeemRewardReqVO, rewardDO);
// 更新会员积分
updateMemberScore(redeemRewardReqVO, rewardDO, redeemId);
// 更新礼品
redeemReward(rewardDO, redeemRewardReqVO.getRewardCount());
}
} catch (ServiceException e) { } catch (ServiceException e) {
log.error("batch redeem rewards exception:{}", e.getMessage()); log.error("batch redeem rewards exception:{}", e.getMessage());
throw e; throw e;
...@@ -249,6 +251,14 @@ public class RedeemRewardApiImpl implements RedeemRewardApi { ...@@ -249,6 +251,14 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
return null; return null;
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void batchExecuteRedeem(List<RedeemRewardReqVO> redeemRewardReqVOList, Integer pointsRequire, Long rewardId) {
for (RedeemRewardReqVO redeemRewardReqVO : redeemRewardReqVOList) {
executeRedeem(redeemRewardReqVO, pointsRequire, rewardId);
}
}
@Override @Override
public RedeemDetailForUserDTO getRedeemDetailForUser(Long redeemId) { public RedeemDetailForUserDTO getRedeemDetailForUser(Long redeemId) {
RewardRedeemDO rewardRedeemDO = rewardRedeemMapper.selectById(redeemId); RewardRedeemDO rewardRedeemDO = rewardRedeemMapper.selectById(redeemId);
......
...@@ -3,10 +3,12 @@ package cn.iocoder.yudao.server; ...@@ -3,10 +3,12 @@ package cn.iocoder.yudao.server;
import cn.jasonone.ueditor.EnableUeditor; import cn.jasonone.ueditor.EnableUeditor;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SuppressWarnings("SpringComponentScan") // 忽略 IDEA 无法识别 ${yudao.info.base-package} @SuppressWarnings("SpringComponentScan") // 忽略 IDEA 无法识别 ${yudao.info.base-package}
//@EnableUeditor //@EnableUeditor
@SpringBootApplication(scanBasePackages = {"${yudao.info.base-package}.server", "${yudao.info.base-package}.module"}) @SpringBootApplication(scanBasePackages = {"${yudao.info.base-package}.server", "${yudao.info.base-package}.module"})
@EnableAspectJAutoProxy(exposeProxy = true)
public class YudaoServerApplication { public class YudaoServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment