Commit 7280a77c authored by zhangfeng's avatar zhangfeng

Merge branch 'refs/heads/feature_member_score_zhangfeng' into feature_member_score

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