Commit b32084eb authored by zhangfeng's avatar zhangfeng

积分规则触发逻辑

parent d7e8488e
package cn.iocoder.yudao.module.member.enums;
import java.util.stream.Stream;
public enum TransportTypeEnum {
OCEAN_LCL(1, "海运拼柜"),
SPECIAL_LINE_AIR_FREIGHT(3, "专线空运");
......@@ -20,4 +22,7 @@ public enum TransportTypeEnum {
public String getName() {
return name;
}
public static TransportTypeEnum parseByValue(int value) {
return Stream.of(values()).filter(e -> e.getValue() == value).findAny().orElse(null);
}
}
package cn.iocoder.yudao.module.member.dal.redis;
/**
* 占位,后续有类后,可以删除,避免 package 无法提交到 Git 上
* Redis Key 枚举类
*/
package cn.iocoder.yudao.module.member.dal.redis;
public interface RedisKeyConstant {
String MEMBER_USER_SCORE_RULE = "member:user:score:rule:";
}
package cn.iocoder.yudao.module.member.dal.redis.scoreRule;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.redis.RedisKeyConstant;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.TransportTypeEnum;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
/**
* 会员积分规则 Redis DAO
*/
@Repository
public class ScoreRuleRedisDao {
@Resource
private StringRedisTemplate stringRedisTemplate;
public ScoreRuleDO getEnableScoreRule(ScoreRuleTypeEnum type, TransportTypeEnum transportType) {
// 如果transportType不为空则是订单V值类型
if (transportType == null) {
String redisKey = formatKey(type.getValue(), null);
String scoreRuleJson = stringRedisTemplate.opsForValue().get(redisKey);
if (scoreRuleJson == null) {
return null;
}
return JsonUtils.parseObject(scoreRuleJson, ScoreRuleDO.class);
} else {
String redisKey = formatKey(type.getValue(), transportType.getValue());
String scoreRuleJson = stringRedisTemplate.opsForValue().get(redisKey);
if (scoreRuleJson == null) {
return null;
}
return JsonUtils.parseObject(scoreRuleJson, ScoreRuleDO.class);
}
}
public ScoreRuleDO getEnableScoreRule(ScoreRuleTypeEnum type) {
return getEnableScoreRule(type, null);
}
public void setEnableScoreRule(ScoreRuleDO scoreRuleDO) {
// 多余字段置空
scoreRuleDO.setCreator(null);
scoreRuleDO.setUpdater(null);
scoreRuleDO.setUpdateTime(null);
scoreRuleDO.setCreateTime(null);
scoreRuleDO.setDeleted(null);
scoreRuleDO.setCoverImageEn(null);
scoreRuleDO.setCoverImageZh(null);
scoreRuleDO.setDescEn(null);
scoreRuleDO.setDescZh(null);
if (scoreRuleDO.getType() == ScoreRuleTypeEnum.ORDER_V.getValue()) {
ScoreRuleOrderVExtraVO scoreRuleOrderVExtraVO = JsonUtils.parseObject(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
stringRedisTemplate.opsForValue().set(formatKey(scoreRuleDO.getType(), scoreRuleOrderVExtraVO.getTransportType()), JsonUtils.toJsonString(scoreRuleDO));
}
stringRedisTemplate.opsForValue().set(formatKey(scoreRuleDO.getType(), null), JsonUtils.toJsonString(scoreRuleDO));
}
public void deleteEnableScoreRule(Integer type) {
if (ScoreRuleTypeEnum.ORDER_V.getValue() == type) {
stringRedisTemplate.delete(formatKey(type, 1));
stringRedisTemplate.delete(formatKey(type, 3));
}
String redisKey = formatKey(type, null);
stringRedisTemplate.delete(redisKey);
}
private static String formatKey(Integer type, Integer transportType) {
if (transportType == null) {
return RedisKeyConstant.MEMBER_USER_SCORE_RULE + type;
}
return RedisKeyConstant.MEMBER_USER_SCORE_RULE + type + ":" + transportType;
}
}
......@@ -2,23 +2,20 @@ package cn.iocoder.yudao.module.member.mq.consumer.score.core;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.module.member.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.redis.scoreRule.ScoreRuleRedisDao;
import cn.iocoder.yudao.module.member.enums.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.TransportTypeEnum;
import cn.iocoder.yudao.module.member.service.scoreRule.ScoreRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.Date;
/**
* 半成品
* @author zhangfeng
*/
public abstract class AbstractScoreRuleStrategy implements ScoreRuleStrategy {
private static ScoreRuleDO orderVShipScoreRule = null;
private static ScoreRuleDO orderVFlyScoreRule = null;
private static ScoreRuleDO registerScoreRule = null;
private static ScoreRuleDO recommendScoreRule = null;
protected ScoreRuleService scoreRuleService;
......@@ -27,63 +24,57 @@ public abstract class AbstractScoreRuleStrategy implements ScoreRuleStrategy {
this.scoreRuleService = scoreRuleService;
}
@Resource
ScoreRuleRedisDao scoreRuleRedisDao;
public ScoreRuleDO getEnableScoreRuleByType(ScoreRuleTypeEnum scoreRuleType) {
ScoreRuleDO scoreRuleDO = scoreRuleService.selectOne(new LambdaQuery<ScoreRuleDO>()
.eq(ScoreRuleDO::getType, scoreRuleType.getValue())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue()));
if (scoreRuleDO != null) {
ScoreRuleDO enableScoreRule;
enableScoreRule = scoreRuleRedisDao.getEnableScoreRule(scoreRuleType);
if (enableScoreRule == null) {
enableScoreRule = scoreRuleService.selectOne(new LambdaQuery<ScoreRuleDO>()
.eq(ScoreRuleDO::getType, scoreRuleType.getValue())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue()));
if (enableScoreRule == null) {
return null;
}
// 校验一下活动开始结束时间
if (!scoreRuleDO.getEndTime().after(new Date()) || !scoreRuleDO.getStartTime().before(new Date())) {
if (!enableScoreRule.getEndTime().after(new Date()) || !enableScoreRule.getStartTime().before(new Date())) {
return null;
}
scoreRuleRedisDao.setEnableScoreRule(enableScoreRule);
} else {
if (!enableScoreRule.getEndTime().after(new Date()) || !enableScoreRule.getStartTime().before(new Date())) {
return null;
} else {
recommendScoreRule = scoreRuleDO;
return recommendScoreRule;
}
}
return null;
return enableScoreRule;
}
public ScoreRuleDO getEnableScoreRuleByType(ScoreRuleTypeEnum scoreRuleType, TransportTypeEnum transportType) {
ScoreRuleDO scoreRuleDO = null;
ScoreRuleDO enableScoreRule;
switch (scoreRuleType) {
case ORDER_V:
scoreRuleDO = scoreRuleService.getEnabledOrderVScoreRuleByTransportType(transportType.getValue());
if (scoreRuleDO != null) {
if (!scoreRuleDO.getEndTime().after(new Date()) || !scoreRuleDO.getStartTime().before(new Date())) {
enableScoreRule = scoreRuleRedisDao.getEnableScoreRule(scoreRuleType, transportType);
if (enableScoreRule == null) {
enableScoreRule = scoreRuleService.getEnabledOrderVScoreRuleByTransportType(transportType.getValue());
if (enableScoreRule == null) {
return null;
}
if (transportType == TransportTypeEnum.OCEAN_LCL) {
orderVShipScoreRule = scoreRuleDO;
return orderVShipScoreRule;
if (!enableScoreRule.getEndTime().after(new Date()) || !enableScoreRule.getStartTime().before(new Date())) {
return null;
}
if (transportType == TransportTypeEnum.SPECIAL_LINE_AIR_FREIGHT) {
orderVFlyScoreRule = scoreRuleDO;
return orderVFlyScoreRule;
scoreRuleRedisDao.setEnableScoreRule(enableScoreRule);
} else {
if (!enableScoreRule.getEndTime().after(new Date()) || !enableScoreRule.getStartTime().before(new Date())) {
return null;
}
}
break;
return enableScoreRule;
case RECOMMEND:
case REGISTER:
return getEnableScoreRuleByType(scoreRuleType);
}
return null;
}
@Override
public void refreshScoreRule(ScoreRuleTypeEnum scoreRuleType) {
switch (scoreRuleType) {
case ORDER_V:
orderVShipScoreRule = null;
orderVFlyScoreRule = null;
break;
case RECOMMEND:
recommendScoreRule = null;
break;
case REGISTER:
registerScoreRule = null;
break;
}
}
}
......@@ -27,7 +27,6 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
......@@ -72,16 +71,11 @@ public class OderVStrategy extends AbstractScoreRuleStrategy {
}
// 判断海运空运
Integer transportId = order.getTransportId();
ScoreRuleDO scoreRuleDO = scoreRuleService.getEnabledOrderVScoreRuleByTransportType(transportId);
ScoreRuleDO scoreRuleDO = getEnableScoreRuleByType(ScoreRuleTypeEnum.ORDER_V, TransportTypeEnum.parseByValue(transportId));
if (Objects.isNull(scoreRuleDO)) {
log.info("Order in shipping listener: No score rule match,orderID:{}", orderId);
return;
}
// 是否过期,或者活动还没开始
if (!scoreRuleDO.getEndTime().after(new Date()) || !scoreRuleDO.getStartTime().before(new Date())) {
log.info("Order in shipping listener: The score rule has expired,scoreRuleID:{}", scoreRuleDO.getId());
return;
}
// 目的地信息
OrderObjectiveApiDO orderObjectiveDO = order.getOrderObjectiveDO();
ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
......@@ -177,7 +171,6 @@ public class OderVStrategy extends AbstractScoreRuleStrategy {
try {
HashMap<String, Object> map = new HashMap<>();
map.put("orderId", orderId);
// TODO:增加订单号和唯一键
memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder()
.memberId(userId)
.scoreCount(scoreCount)
......
package cn.iocoder.yudao.module.member.mq.consumer.score.core;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
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.dal.dataobject.scoreLog.MemberUserScoreLogDO;
import cn.iocoder.yudao.module.member.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.enums.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.ScoreSourceTypeEnum;
import cn.iocoder.yudao.module.member.mq.message.ScoreMessage;
......@@ -19,7 +17,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
......@@ -44,18 +41,11 @@ public class RecommendStrategy extends AbstractScoreRuleStrategy {
public void addScore(ScoreMessage message) {
Long userId = message.getUserId();
log.info("Received Recommend message,userID:{}", userId);
ScoreRuleDO scoreRuleDO = scoreRuleService.selectOne(new LambdaQuery<ScoreRuleDO>()
.eq(ScoreRuleDO::getType, ScoreRuleTypeEnum.RECOMMEND.getValue())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue()));
ScoreRuleDO scoreRuleDO = getEnableScoreRuleByType(ScoreRuleTypeEnum.RECOMMEND);
if (scoreRuleDO == null) {
log.info("Recommend rule is not exist,userID:{}", userId);
return;
}
// 是否过期,或者活动还没开始
if (!scoreRuleDO.getEndTime().after(new Date()) || !scoreRuleDO.getStartTime().before(new Date())) {
log.info("Recommend listener: The score rule has expired,scoreRuleID:{}", scoreRuleDO.getId());
return;
}
MemberUserDO memberUserDO = memberUserService.getUser(userId);
if (memberUserDO == null) {
log.info("Recommend listener: The user does not exist,userID:{}", userId);
......
package cn.iocoder.yudao.module.member.mq.consumer.score.core;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
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.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.enums.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.ScoreSourceTypeEnum;
import cn.iocoder.yudao.module.member.mq.message.ScoreMessage;
......@@ -15,7 +13,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
/**
......@@ -37,18 +34,11 @@ public class RegisterStrategy extends AbstractScoreRuleStrategy {
public void addScore(ScoreMessage message) {
Long userId = message.getUserId();
log.info("Received register message,userID:{}", userId);
ScoreRuleDO scoreRuleDO = scoreRuleService.selectOne(new LambdaQuery<ScoreRuleDO>()
.eq(ScoreRuleDO::getType, ScoreRuleTypeEnum.REGISTER.getValue())
.eq(ScoreRuleDO::getStatus, ScoreRuleStatusEnum.ENABLED.getValue()));
ScoreRuleDO scoreRuleDO = getEnableScoreRuleByType(ScoreRuleTypeEnum.REGISTER);
if (scoreRuleDO == null) {
log.info("Register rule is not exist,userID:{}", userId);
return;
}
// 是否过期,或者活动还没开始
if (!scoreRuleDO.getEndTime().after(new Date()) || !scoreRuleDO.getStartTime().before(new Date())) {
log.info("Register listener: The score rule has expired,scoreRuleID:{}", scoreRuleDO.getId());
return;
}
MemberUserDO memberUserDO = memberUserService.getUser(userId);
if (memberUserDO == null) {
log.info("Register listener: The user does not exist,userID:{}", userId);
......
......@@ -8,5 +8,4 @@ import cn.iocoder.yudao.module.member.mq.message.ScoreMessage;
public interface ScoreRuleStrategy {
void addScore(ScoreMessage message);
ScoreRuleTypeEnum getStrategyScoreRuleType();
void refreshScoreRule(ScoreRuleTypeEnum scoreRuleType);
}
......@@ -12,6 +12,7 @@ import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.module.member.convert.scoreRule.ScoreRuleConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scoreRule.ScoreRuleDO;
import cn.iocoder.yudao.module.member.dal.mysql.scoreRule.ScoreRuleMapper;
import cn.iocoder.yudao.module.member.dal.redis.scoreRule.ScoreRuleRedisDao;
import cn.iocoder.yudao.module.member.enums.ScoreRuleStatusEnum;
import cn.iocoder.yudao.module.member.enums.ScoreRuleTypeEnum;
import cn.iocoder.yudao.module.member.enums.TransportTypeEnum;
......@@ -41,6 +42,8 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
@Resource
private ScoreRuleMapper scoreRuleMapper;
@Resource
private ScoreRuleRedisDao scoreRuleRedisDao;
@Override
public Long createScoreRule(ScoreRuleCreateReqVO createReqVO) {
......@@ -52,6 +55,7 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
Integer scoreRuleType = scoreRule.getType();
verifyAndSetExtraDO(createReqVO, scoreRuleType, scoreRule);
scoreRuleMapper.insert(scoreRule);
scoreRuleRedisDao.deleteEnableScoreRule(scoreRule.getType());
// 返回
return scoreRule.getId();
}
......@@ -117,6 +121,7 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
verifyAndSetExtraDO(updateReqVO, updateReqVO.getType(), scoreRule);
// 更新
scoreRuleMapper.updateById(scoreRule);
scoreRuleRedisDao.deleteEnableScoreRule(scoreRule.getType());
}
/**
......@@ -289,6 +294,7 @@ public class ScoreRuleServiceImpl extends AbstractService<ScoreRuleMapper, Score
throw exception(SCORE_RULE_UPDATE_ERROR);
}
scoreRuleMapper.updateById(upScoreRuleDO);
scoreRuleRedisDao.deleteEnableScoreRule(scoreRuleDO.getType());
}
@Override
......
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