Commit 82c6c4ca authored by zhaobiyan's avatar zhaobiyan

兑换礼品流程增加积分更新,兑换记录插入

parent b5756901
......@@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.math.BigDecimal;
import java.util.Date;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
......@@ -19,7 +20,7 @@ public class RedeemRewardReqVO {
private Long rewardId;
@ApiModelProperty(value = "兑换数量")
private Integer count;
private Integer rewardCount;
@ApiModelProperty(value = "兑换方式,同领取方式(1上门领取,2包邮到家,3邮寄到付)")
private Integer redeemType;
......@@ -28,7 +29,7 @@ public class RedeemRewardReqVO {
private Integer entrance;
@ApiModelProperty(value = "费用数字(两位小数)")
private Integer expenses;
private BigDecimal expenses;
@ApiModelProperty(value = "费用币种(字典配置)")
private Integer currency;
......
package cn.iocoder.yudao.module.reward.enums;
public enum RewardRedeemStatusEnum {
REDEEMING(1, "兑换中"),
REDEEMED(2, "已兑换"),
CANCELED(3, "已取消")
;
private final int value;
private final String name;
RewardRedeemStatusEnum(int value, String name) {
this.value = value;
this.name = name;
}
public int getValue() {
return value;
}
public String getName() {
return name;
}
}
package cn.iocoder.yudao.module.reward.api.reward;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.iocoder.yudao.framework.redis.helper.RedisDistributedLock;
import cn.iocoder.yudao.module.member.api.score.MemberUserScoreApi;
import cn.iocoder.yudao.module.member.api.score.dto.MemberUserScoreOperateReqDTO;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.UserRespDTO;
import cn.iocoder.yudao.module.member.enums.ScoreSourceTypeEnum;
import cn.iocoder.yudao.module.reward.api.reward.dto.RedeemRewardReqVO;
import cn.iocoder.yudao.module.reward.api.reward.dto.RedeemRewardRespDTO;
import cn.iocoder.yudao.module.reward.dal.dataobject.redeem.RewardRedeemDO;
import cn.iocoder.yudao.module.reward.dal.dataobject.reward.RewardDO;
import cn.iocoder.yudao.module.reward.dal.mysql.redeem.RewardRedeemMapper;
import cn.iocoder.yudao.module.reward.dal.mysql.reward.RewardMapper;
import cn.iocoder.yudao.module.reward.enums.RewardRedeemStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.GET_LOCK_FAILED;
......@@ -27,9 +33,15 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
@Resource
private RewardMapper rewardMapper;
@Resource
private RewardRedeemMapper rewardRedeemMapper;
@Resource
private MemberUserApi memberUserApi;
@Resource
private RedisDistributedLock redisDistributedLock;
@Resource
private MemberUserScoreApi memberUserScoreApi;
@Resource
private SnowflakeGenerator snowflakeGenerator;
@Override
public RedeemRewardRespDTO redeemReward(RedeemRewardReqVO redeemRewardReqVO) {
......@@ -56,11 +68,11 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
throw exception(REWARD_NOT_ENABLE);
}
// 会员积分不够
if (holdScore < rewardDO.getPointsRequire() * redeemRewardReqVO.getCount()) {
if (holdScore < rewardDO.getPointsRequire() * redeemRewardReqVO.getRewardCount()) {
throw exception(REWARD_SCORE_NOT_ENOUGH);
}
// 礼品数量不够
if (rewardDO.getQuantityRemain() < redeemRewardReqVO.getCount()) {
if (rewardDO.getQuantityRemain() < redeemRewardReqVO.getRewardCount()) {
throw exception(REWARD_COUNT_NOT_ENOUGH);
}
// 兑换方式不匹配
......@@ -73,9 +85,9 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
rewardMapper.updateById(rewardDO);
// 添加兑换记录
Long redeemId = addRedeemRecord(redeemRewardReqVO);
// 更新会员积分
updateMemberScore(redeemRewardReqVO, rewardDO, redeemId);
} catch (Exception e) {
throw new RuntimeException(e);
......@@ -85,6 +97,25 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
return null;
}
private void updateMemberScore(RedeemRewardReqVO redeemRewardReqVO, RewardDO rewardDO, 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())
.extParam(extParam)
.build());
}
private Long addRedeemRecord(RedeemRewardReqVO redeemRewardReqVO) {
RewardRedeemDO rewardRedeemDO = BeanUtil.copyProperties(redeemRewardReqVO, RewardRedeemDO.class);
rewardRedeemDO.setId(snowflakeGenerator.next());
rewardRedeemDO.setStatus(RewardRedeemStatusEnum.REDEEMING.getValue());
rewardRedeemMapper.insert(rewardRedeemDO);
return rewardRedeemDO.getId();
}
@Override
public List<RedeemRewardRespDTO> redeemRewards(List<RedeemRewardReqVO> redeemRewardReqVOList) {
Long rewardId = redeemRewardReqVOList.get(0).getRewardId();
......@@ -115,7 +146,7 @@ public class RedeemRewardApiImpl implements RedeemRewardApi {
memberIds.add(redeemRewardReqVO.getMemberId());
members.add(memberUserApi.getUser(redeemRewardReqVO.getMemberId()));
// 记录兑换总数
totalCount+=redeemRewardReqVO.getCount();
totalCount+=redeemRewardReqVO.getRewardCount();
}
// 查询会员列表,获取积分列表
......
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