Commit d02f3825 authored by zhangfeng's avatar zhangfeng

订单V值触发加锁

parent 2caacacb
...@@ -21,13 +21,19 @@ import cn.iocoder.yudao.module.member.service.user.MemberUserService; ...@@ -21,13 +21,19 @@ import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO; import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable; import org.redisson.RedissonMultiLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/** /**
* @author zhangfeng * @author zhangfeng
...@@ -39,6 +45,8 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy { ...@@ -39,6 +45,8 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
private OrderApi orderApi; private OrderApi orderApi;
@Resource @Resource
private CustomerContactsService customerContactsService; private CustomerContactsService customerContactsService;
@Resource
private RedissonClient redissonClient;
public OrderVStrategy(ScoreRuleService scoreRuleService, MemberUserScoreLogService memberUserScoreLogService, MemberUserService memberUserService, MemberUserScoreApi memberUserScoreApi) { public OrderVStrategy(ScoreRuleService scoreRuleService, MemberUserScoreLogService memberUserScoreLogService, MemberUserService memberUserService, MemberUserScoreApi memberUserScoreApi) {
super(scoreRuleService, memberUserScoreLogService, memberUserService, memberUserScoreApi); super(scoreRuleService, memberUserScoreLogService, memberUserService, memberUserScoreApi);
...@@ -46,6 +54,11 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy { ...@@ -46,6 +54,11 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
@Override @Override
public void addScore(ScoreMessage message) { public void addScore(ScoreMessage message) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
log.error("Order in shipping listener: Thread sleep error", e);
}
log.info("Received order V message,orderID:{}", message.getOrderId()); log.info("Received order V message,orderID:{}", message.getOrderId());
Long orderId = message.getOrderId(); Long orderId = message.getOrderId();
// 获取订单信息 // 获取订单信息
...@@ -78,42 +91,61 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy { ...@@ -78,42 +91,61 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class); ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
String[] warehouseIds = extraInfo.getReceiveAddr().split(","); String[] warehouseIds = extraInfo.getReceiveAddr().split(",");
if (!ArrayUtil.contains(warehouseIds, orderObjectiveDO.getObjectiveWarehouseId().toString())) { if (!ArrayUtil.contains(warehouseIds, orderObjectiveDO.getObjectiveWarehouseId().toString())) {
log.info("Order in shipping listener: The rule does not contain the order destination warehouse,orderID:{},warehouseId:{}", orderId,orderObjectiveDO.getObjectiveWarehouseId().toString()); log.info("Order in shipping listener: The rule does not contain the order destination warehouse,orderID:{},warehouseId:{}", orderId, orderObjectiveDO.getObjectiveWarehouseId().toString());
return; return;
} }
String lockKey1;
// 获取需要添加积分的用户 if (order.getUserId() > 0) {
List<Long> userIds = getAddScoreUser(order, extraInfo, scoreRuleDO); lockKey1 = "score:rule:order:lock:" + order.getUserId();
if (CollectionUtil.isEmpty(userIds)) { } else {
log.info("Order in shipping listener: The customer is not bound to the member or have default userId, customerId:{}", order.getCustomerId()); lockKey1 = "score:rule:order:lock:" + order.getOrderConsignorDO().getCustomerId();
return;
}
// 计算积分
Integer scoreCount = getScoreCount(transportId, order.getChannelId(), extraInfo.getChannel(), extraInfo.getOrderVRule(), orderId);
if (scoreCount == 0) {
log.info("Order in shipping listener: The score count is less than or equal to 0,orderId:{}", orderId);
return;
} }
// 增加积分 String lockKey2 = "score:rule:order:lock:" + order.getOrderConsigneeDO().getCustomerId();
for (Long userId : userIds) { RLock lock1 = redissonClient.getLock(lockKey1);
try { RLock lock2 = redissonClient.getLock(lockKey2);
HashMap<String, Object> map = new HashMap<>(); RedissonMultiLock multiLock = new RedissonMultiLock(lock1, lock2);
map.put("orderId", orderId); try {
memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder() boolean tryLock = multiLock.tryLock(2, 10, TimeUnit.SECONDS);
.memberId(userId) if (!tryLock) {
.scoreCount(scoreCount) log.error("Order in shipping listener: get lock error, orderId:{}", orderId);
.sourceType(ScoreSourceTypeEnum.ORDER_V) }
.ruleId(scoreRuleDO.getId()) // 获取需要添加积分的用户
.expireDays(scoreRuleDO.getScorePeriod()) List<Long> userIds = getAddScoreUser(order, extraInfo, scoreRuleDO);
.releationId(String.valueOf(orderId)) if (CollectionUtil.isEmpty(userIds)) {
.uniqueId(orderId + "_" + userId) log.info("Order in shipping listener: The customer is not bound to the member or have default userId, customerId:{}", order.getCustomerId());
.extParam(map) return;
.build() }
); // 计算积分
log.info("Order V rule add score success,ruleID:{},orderID:{},userID:{}", scoreRuleDO.getId(), message.getOrderId(), userId); Integer scoreCount = getScoreCount(transportId, order.getChannelId(), extraInfo.getChannel(), extraInfo.getOrderVRule(), orderId);
} catch (Exception e) { if (scoreCount == 0) {
log.error("Order in shipping listener: operateScore error", e); log.info("Order in shipping listener: The score count is less than or equal to 0,orderId:{}", orderId);
return;
}
// 增加积分
for (Long userId : userIds) {
try {
HashMap<String, Object> map = new HashMap<>();
map.put("orderId", orderId);
memberUserScoreApi.operateScore(MemberUserScoreOperateReqDTO.builder()
.memberId(userId)
.scoreCount(scoreCount)
.sourceType(ScoreSourceTypeEnum.ORDER_V)
.ruleId(scoreRuleDO.getId())
.expireDays(scoreRuleDO.getScorePeriod())
.releationId(String.valueOf(orderId))
.uniqueId(orderId + "_" + userId)
.extParam(map)
.build()
);
log.info("Order V rule add score success,ruleID:{},orderID:{},userID:{}", scoreRuleDO.getId(), message.getOrderId(), userId);
} catch (Exception e) {
log.error("Order in shipping listener: operateScore error", e);
}
} }
} catch (InterruptedException e) {
log.error("Order in shipping listener: lock error orderId:{}", orderId, e);
} finally {
multiLock.unlock();
} }
} }
......
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