Commit 78b37103 authored by zhangfeng's avatar zhangfeng

订单V值触发加锁

parent 308e8702
......@@ -21,13 +21,19 @@ import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.vo.scoreRule.extra.ScoreRuleOrderVExtraVO;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;
import java.math.BigDecimal;
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
......@@ -39,6 +45,8 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
private OrderApi orderApi;
@Resource
private CustomerContactsService customerContactsService;
@Resource
private RedissonClient redissonClient;
public OrderVStrategy(ScoreRuleService scoreRuleService, MemberUserScoreLogService memberUserScoreLogService, MemberUserService memberUserService, MemberUserScoreApi memberUserScoreApi) {
super(scoreRuleService, memberUserScoreLogService, memberUserService, memberUserScoreApi);
......@@ -46,6 +54,11 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
@Override
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());
Long orderId = message.getOrderId();
// 获取订单信息
......@@ -78,42 +91,61 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
String[] warehouseIds = extraInfo.getReceiveAddr().split(",");
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;
}
// 获取需要添加积分的用户
List<Long> userIds = getAddScoreUser(order, extraInfo, scoreRuleDO);
if (CollectionUtil.isEmpty(userIds)) {
log.info("Order in shipping listener: The customer is not bound to the member or have default userId, customerId:{}", order.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 lockKey1;
if (order.getUserId() > 0) {
lockKey1 = "score:rule:order:lock:" + order.getUserId();
} else {
lockKey1 = "score:rule:order:lock:" + order.getOrderConsignorDO().getCustomerId();
}
// 增加积分
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);
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);
if (CollectionUtil.isEmpty(userIds)) {
log.info("Order in shipping listener: The customer is not bound to the member or have default userId, customerId:{}", order.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;
}
// 增加积分
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