Commit 78b37103 authored by zhangfeng's avatar zhangfeng

订单V值触发加锁

parent 308e8702
...@@ -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,10 +91,24 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy { ...@@ -78,10 +91,24 @@ 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) {
lockKey1 = "score:rule:order:lock:" + order.getUserId();
} else {
lockKey1 = "score:rule:order:lock:" + order.getOrderConsignorDO().getCustomerId();
}
String lockKey2 = "score:rule:order:lock:" + order.getOrderConsigneeDO().getCustomerId();
RLock lock1 = redissonClient.getLock(lockKey1);
RLock lock2 = redissonClient.getLock(lockKey2);
RedissonMultiLock multiLock = new RedissonMultiLock(lock1, lock2);
try {
boolean tryLock = multiLock.tryLock(2, 10, TimeUnit.SECONDS);
if (!tryLock) {
log.error("Order in shipping listener: get lock error, orderId:{}", orderId);
}
// 获取需要添加积分的用户 // 获取需要添加积分的用户
List<Long> userIds = getAddScoreUser(order, extraInfo, scoreRuleDO); List<Long> userIds = getAddScoreUser(order, extraInfo, scoreRuleDO);
if (CollectionUtil.isEmpty(userIds)) { if (CollectionUtil.isEmpty(userIds)) {
...@@ -115,6 +142,11 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy { ...@@ -115,6 +142,11 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
log.error("Order in shipping listener: operateScore error", 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