feat(orders.manager):新增监听支付成功消息更新支付状态的功能

This commit is contained in:
JIAN
2024-09-10 11:29:43 +08:00
parent 7e0b8840a4
commit 31c36abeaa
4 changed files with 99 additions and 19 deletions

View File

@@ -13,6 +13,8 @@ import com.jzo2o.orders.base.model.dto.OrderUpdateStatusDTO;
* @since 2023-08-02
*/
public interface IOrdersCommonService extends IService<Orders> {
Integer updateStatus(OrderUpdateStatusDTO orderUpdateStatusReqDTO);
}
/**
* 更新指定id订单的状态
*/
Boolean updateStatus(OrderUpdateStatusDTO orderUpdateStatusReqDTO);
}

View File

@@ -1,8 +1,6 @@
package com.jzo2o.orders.base.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.orders.base.mapper.OrdersMapper;
import com.jzo2o.orders.base.model.domain.Orders;
@@ -14,27 +12,25 @@ import org.springframework.stereotype.Service;
* <p>
* 订单表 服务实现类
* </p>
*
* @author itcast
* @since 2023-08-02
*/
@Service
public class OrdersCommonServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersCommonService {
@Override
public Integer updateStatus(OrderUpdateStatusDTO orderUpdateStatusReqDTO) {
LambdaUpdateWrapper<Orders> updateWrapper = Wrappers.<Orders>lambdaUpdate()
public Boolean updateStatus(OrderUpdateStatusDTO orderUpdateStatusReqDTO) {
return lambdaUpdate()
.eq(Orders::getId, orderUpdateStatusReqDTO.getId())
.gt(Orders::getUserId, 0)
.eq(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getOriginStatus()),Orders::getOrdersStatus,orderUpdateStatusReqDTO.getOriginStatus())
.eq(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getOriginStatus()), Orders::getOrdersStatus, orderUpdateStatusReqDTO.getOriginStatus())
.set(Orders::getOrdersStatus, orderUpdateStatusReqDTO.getTargetStatus())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getPayStatus()),Orders::getPayStatus,orderUpdateStatusReqDTO.getPayStatus())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getPayTime()),Orders::getPayTime,orderUpdateStatusReqDTO.getPayTime())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getEvaluationTime()),Orders::getEvaluationTime,orderUpdateStatusReqDTO.getEvaluationTime())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTradingOrderNo()),Orders::getTradingOrderNo,orderUpdateStatusReqDTO.getTradingOrderNo())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTransactionId()),Orders::getTransactionId,orderUpdateStatusReqDTO.getTransactionId())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTradingChannel()),Orders::getTradingChannel,orderUpdateStatusReqDTO.getTradingChannel())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getRefundStatus()),Orders::getRefundStatus,orderUpdateStatusReqDTO.getRefundStatus());
boolean update = super.update(updateWrapper);
return update?1:0;
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getPayStatus()), Orders::getPayStatus, orderUpdateStatusReqDTO.getPayStatus())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getPayTime()), Orders::getPayTime, orderUpdateStatusReqDTO.getPayTime())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getEvaluationTime()), Orders::getEvaluationTime, orderUpdateStatusReqDTO.getEvaluationTime())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTradingOrderNo()), Orders::getTradingOrderNo, orderUpdateStatusReqDTO.getTradingOrderNo())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTransactionId()), Orders::getTransactionId, orderUpdateStatusReqDTO.getTransactionId())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getTradingChannel()), Orders::getTradingChannel, orderUpdateStatusReqDTO.getTradingChannel())
.set(ObjectUtil.isNotNull(orderUpdateStatusReqDTO.getRefundStatus()), Orders::getRefundStatus, orderUpdateStatusReqDTO.getRefundStatus())
.update();
}
}
}

View File

@@ -0,0 +1,77 @@
package com.jzo2o.orders.manager.listener;
import com.alibaba.fastjson.JSON;
import com.jzo2o.common.constants.MqConstants;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.msg.TradeStatusMsg;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.base.enums.OrderPayStatusEnum;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.dto.OrderUpdateStatusDTO;
import com.jzo2o.orders.base.service.IOrdersCommonService;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
/**
* 监听支付订单状态更新的消息
* @author JIAN
*/
@Slf4j
@Component
public class TradeStatusListener {
@Resource
private IOrdersCreateService ordersCreateService;
@Resource
private IOrdersCommonService ordersCommonService;
@Resource
private TransactionTemplate transactionTemplate;
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = MqConstants.Queues.ORDERS_TRADE_UPDATE_STATUS),
exchange = @Exchange(name = MqConstants.Exchanges.TRADE, type = ExchangeTypes.TOPIC),
key = MqConstants.RoutingKeys.TRADE_UPDATE_STATUS))
public void onStatusUpdateMsg(String msg) {
log.info("接收到支付结果状态的消息 ({}) -> {}", MqConstants.Queues.ORDERS_TRADE_UPDATE_STATUS, msg);
List<TradeStatusMsg> msgList = JSON.parseArray(msg, TradeStatusMsg.class);
if (CollUtils.isEmpty(msgList)) {
return;
}
// 处理当前微服务并且支付成功的订单
List<TradeStatusMsg> tradeStatusMsgList = msgList.stream()
.filter(statusMsg -> ordersCreateService.PRODUCT_APP_ID.equals(statusMsg.getProductAppId())
&& statusMsg.getStatusCode() == OrderPayStatusEnum.PAY_SUCCESS.getStatus())
.collect(Collectors.toList());
transactionTemplate.execute(status -> {
for (TradeStatusMsg tradeStatusMsg : tradeStatusMsgList) {
if (!ordersCommonService.updateStatus(OrderUpdateStatusDTO.builder()
.id(tradeStatusMsg.getProductOrderNo())
.originStatus(OrderStatusEnum.NO_PAY.getStatus())
.targetStatus(OrderStatusEnum.DISPATCHING.getStatus())
.payStatus(OrderPayStatusEnum.PAY_SUCCESS.getStatus())
.tradingOrderNo(tradeStatusMsg.getTradingOrderNo())
.transactionId(tradeStatusMsg.getTransactionId())
.tradingChannel(tradeStatusMsg.getTradingChannel())
.payTime(LocalDateTime.now()).build())) {
throw new DBException("更新订单状态失败");
}
}
return true;
});
}
}

View File

@@ -15,6 +15,11 @@ import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
* @since 2023-07-10
*/
public interface IOrdersCreateService extends IService<Orders> {
/**
* 微服务标识
*/
String PRODUCT_APP_ID = "jzo2o.orders";
/**
* 客户端下单接口
*/