Commit 7bc0cadf authored by Smile's avatar Smile Committed by wux

会员积分规则匹配,当有两个不相冲突的积分规则,只匹配一个问题修改

parent 141ac79e
......@@ -16,6 +16,8 @@ import cn.iocoder.yudao.module.customer.vo.customer.customerContacts.CustomerCon
import cn.iocoder.yudao.module.customer.vo.customer.customerContacts.CustomerContactsPageReqVO;
import cn.iocoder.yudao.module.customer.vo.customer.customerContacts.CustomerContactsUpdateReqVO;
import cn.iocoder.yudao.module.ecw.service.paramValid.ParamValidatorService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.UserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
......@@ -44,6 +46,8 @@ public class CustomerContactsServiceImpl extends AbstractService<CustomerContact
private CustomerContactsMapper customerContactsMapper;
@Resource
private ParamValidatorService paramValidatorService;
@Resource
private MemberUserApi memberUserApi;
@Override
public Long createCustomerContacts(CustomerContactsCreateReqVO createReqVO) {
......@@ -124,7 +128,14 @@ public class CustomerContactsServiceImpl extends AbstractService<CustomerContact
@Override
public List<CustomerContactsDO> getCustomerContactsListByCustomerId(Long customerId) {
return customerContactsMapper.selectList(new LambdaQueryWrapperX<CustomerContactsDO>().eqIfPresent(CustomerContactsDO::getCustomerId, customerId));
List<CustomerContactsDO> customerContactsDOS = customerContactsMapper.selectList(new LambdaQueryWrapperX<CustomerContactsDO>().eqIfPresent(CustomerContactsDO::getCustomerId, customerId));
customerContactsDOS.forEach(customerContactsDO -> {
if (customerContactsDO.getUserid()!=null) {
UserRespDTO user = memberUserApi.getUser(customerContactsDO.getUserid());
customerContactsDO.setUsername(user.getNickname());
}
});
return customerContactsDOS;
}
@Override
......
......@@ -35,6 +35,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.module.member.enums.TransportTypeEnum.SPECIAL_LINE_AIR_FREIGHT;
......@@ -97,73 +98,77 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
consigneeId = order.getOrderConsigneeDO().getCustomerId();
}
// 获取订单规则
ScoreRuleDO scoreRuleDO = this.getMatchRule(order, consigneeId);
if (Objects.isNull(scoreRuleDO)) {
List<ScoreRuleDO> scoreRuleDOList = this.getMatchRule(order, consigneeId);
if (Objects.isNull(scoreRuleDOList)) {
log.info("Order in shipping listener: No score rule match,orderID:{}", orderId);
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody.append("无匹配积分规则;")));
orderApi.createOrderOperateLog(orderOperateLogDTO);
return;
}
orderOperateLogDTO.setCreator(scoreRuleDO.getCode());
ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
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:" + consigneeId;
RLock lock1 = redissonClient.getLock(lockKey1);
RLock lock2 = consigneeId == 0 ? null : 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.getUserId(), order.getOrderConsignorDO().getCustomerId(), consigneeId, scoreRuleDO, logBody);
if (CollectionUtil.isEmpty(userIds)) {
log.info("Order in shipping listener: consignor and consignee are not bound to the member, orderId:{}", orderId);
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody));
orderApi.createOrderOperateLog(orderOperateLogDTO);
return;
}
// 计算积分
Integer scoreCount = getScoreCount(order.getTransportId(), extraInfo.getOrderVRule(), orderId);
logBody.append("根据").append(scoreRuleDO.getCode()).append("规则计算积分为:").append(scoreCount).append(";");
if (scoreCount == 0) {
log.info("Order in shipping listener: The score count is less than or equal to 0,orderId:{}", orderId);
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody));
orderApi.createOrderOperateLog(orderOperateLogDTO);
return;
for (int i = 0; i < scoreRuleDOList.size(); i++) {
ScoreRuleDO scoreRuleDO = scoreRuleDOList.get(i);
orderOperateLogDTO.setCreator(scoreRuleDO.getCode());
ScoreRuleOrderVExtraVO extraInfo = JSONUtil.toBean(scoreRuleDO.getExtra(), ScoreRuleOrderVExtraVO.class);
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<>(1);
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(), orderId, userId);
} catch (Exception e) {
log.error("Order in shipping listener: operateScore error", e);
String lockKey2 = "score:rule:order:lock:" + consigneeId;
RLock lock1 = redissonClient.getLock(lockKey1);
RLock lock2 = consigneeId == 0 ? null : 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.getUserId(), order.getOrderConsignorDO().getCustomerId(), consigneeId, scoreRuleDO, logBody);
if (CollectionUtil.isEmpty(userIds)) {
log.info("Order in shipping listener: consignor and consignee are not bound to the member, orderId:{}", orderId);
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody));
orderApi.createOrderOperateLog(orderOperateLogDTO);
return;
}
// 计算积分
Integer scoreCount = getScoreCount(order.getTransportId(), extraInfo.getOrderVRule(), orderId);
logBody.append("根据").append(scoreRuleDO.getCode()).append("规则计算积分为:").append(scoreCount).append(";");
if (scoreCount == 0) {
log.info("Order in shipping listener: The score count is less than or equal to 0,orderId:{}", orderId);
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody));
orderApi.createOrderOperateLog(orderOperateLogDTO);
return;
}
// 增加积分
for (Long userId : userIds) {
try {
HashMap<String, Object> map = new HashMap<>(1);
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(), orderId, 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();
}
} catch (InterruptedException e) {
log.error("Order in shipping listener: lock error orderId:{}", orderId, e);
} finally {
multiLock.unlock();
}
orderOperateLogDTO.setBody(JSONObject.toJSONString(logBody.append("获取积分成功")));
orderApi.createOrderOperateLog(orderOperateLogDTO);
}
......@@ -213,7 +218,7 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
boolean hasConsignor =ArrayUtil.contains(customerSideArray, String.valueOf(CustomerSideEnum.CONSIGNOR.getValue()));
// 发货人和收货人都要判断给分
boolean isBackendOrder = userId <= 0;
logNote.append("此订单为").append(isBackendOrder ? "后台" : "客户端").append("下单;");
logNote.append("匹配规则"+scoreRuleDO.getCode()+" 此订单为").append(isBackendOrder ? "后台" : "客户端").append("下单;");
// 如果是后台下单,重新获取发货人用户ID
if (isBackendOrder) {
CustomerContactsDO contactsDO = getCustomerMemberId(consignorId);
......@@ -224,7 +229,7 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
if (!hasConsignor&&customerSideArray.length!=0){
logNote.append("发货人绑定会员:").append(contactsDO.getUsername()).append(",会员ID:").append(userId).append(";");
userId = 0L;
logNote.append("积分规则客户方不包含发货人");
logNote.append("积分规则客户方不包含发货人 ");
}
if (hasConsignor||customerSideArray.length==0){
userId = contactsDO.getUserid();
......@@ -269,7 +274,7 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
log.info("Order in shipping listener: The consignee has reached the maximum score, userId:{}, scoreRuleId:{}", consigneeMemberId, scoreRuleDO.getId());
} else {
if (!hasConsignee&&customerSideArray.length!=0){
logNote.append("积分规则客户方不包含收货人");
logNote.append("积分规则客户方不包含收货人 ");
}
if (hasConsignee||customerSideArray.length==0){
users.add(consigneeMemberId);
......@@ -303,7 +308,7 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
* @param consigneeId
* @return
*/
private ScoreRuleDO getMatchRule(OrderRespDTO order, long consigneeId) {
private List<ScoreRuleDO> getMatchRule(OrderRespDTO order, long consigneeId) {
List<ScoreRuleDO> scoreRuleDOs = scoreRuleService.getEnableOrderVRule();
if (CollectionUtil.isEmpty(scoreRuleDOs)) {
return null;
......@@ -341,7 +346,7 @@ public class OrderVStrategy extends AbstractScoreRuleStrategy {
}
}
return true;
}).findFirst().orElse(null);
}).collect(Collectors.toList());
}
/**
......
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