Commit fa761e5d authored by Smile's avatar Smile Committed by wux

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

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