Преглед изворни кода

消息队列实现取消订单

zhh пре 6 година
родитељ
комит
f50739ac20

+ 11 - 0
mall-portal/pom.xml

@@ -81,6 +81,17 @@
             <artifactId>druid-spring-boot-starter</artifactId>
             <version>1.1.10</version>
         </dependency>
+        <!--集成消息队列-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-amqp</artifactId>
+        </dependency>
+        <!--lombok依赖-->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 
     <build>

+ 26 - 0
mall-portal/src/main/java/com/macro/mall/portal/component/CancelOrderReceiver.java

@@ -0,0 +1,26 @@
+package com.macro.mall.portal.component;
+
+import com.macro.mall.portal.service.OmsPortalOrderService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 取消订单消息的处理者
+ * Created by macro on 2018/9/14.
+ */
+@Component
+@RabbitListener(queues = "${rabbitmq.queue.name.cancelOrder}")
+public class CancelOrderReceiver {
+    private static Logger LOGGER =LoggerFactory.getLogger(CancelOrderReceiver.class);
+    @Autowired
+    private OmsPortalOrderService portalOrderService;
+    @RabbitHandler
+    public void process(Long orderId){
+        portalOrderService.cancelOrder(orderId);
+        LOGGER.info("process orderId:{}",orderId);
+    }
+}

+ 26 - 0
mall-portal/src/main/java/com/macro/mall/portal/component/CancelOrderSender.java

@@ -0,0 +1,26 @@
+package com.macro.mall.portal.component;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.amqp.core.AmqpTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+/**
+ * 取消订单消息的发出者
+ * Created by macro on 2018/9/14.
+ */
+@Component
+public class CancelOrderSender {
+    private static Logger LOGGER =LoggerFactory.getLogger(CancelOrderSender.class);
+    @Value("${rabbitmq.queue.name.cancelOrder}")
+    private String QUEUE_NAME_CANCEL_ORDER;
+    @Autowired
+    private AmqpTemplate amqpTemplate;
+
+    public void send(Long orderId){
+        amqpTemplate.convertAndSend(QUEUE_NAME_CANCEL_ORDER,orderId);
+        LOGGER.info("send orderId:{}",orderId);
+    }
+}

+ 23 - 0
mall-portal/src/main/java/com/macro/mall/portal/config/RabbitMqConfig.java

@@ -0,0 +1,23 @@
+package com.macro.mall.portal.config;
+
+import org.springframework.amqp.core.Queue;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 消息队列配置
+ * Created by macro on 2018/9/14.
+ */
+@Configuration
+public class RabbitMqConfig {
+    @Value("${rabbitmq.queue.name.cancelOrder}")
+    private String QUEUE_NAME_CANCEL_ORDER;
+    /**
+     * 超时取消订单的消息
+     */
+    @Bean
+    public Queue cancelOrderQueue(){
+        return new Queue(QUEUE_NAME_CANCEL_ORDER);
+    }
+}

+ 11 - 0
mall-portal/src/main/java/com/macro/mall/portal/controller/OmsPortalOrderController.java

@@ -1,5 +1,6 @@
 package com.macro.mall.portal.controller;
 
+import com.macro.mall.portal.component.CancelOrderSender;
 import com.macro.mall.portal.domain.CommonResult;
 import com.macro.mall.portal.domain.ConfirmOrderResult;
 import com.macro.mall.portal.domain.OrderParam;
@@ -20,6 +21,8 @@ import org.springframework.web.bind.annotation.*;
 public class OmsPortalOrderController {
     @Autowired
     private OmsPortalOrderService portalOrderService;
+    @Autowired
+    private CancelOrderSender cancelOrderSender;
     @ApiOperation("根据购物车信息生成确认单信息")
     @RequestMapping(value = "/generateConfirmOrder",method = RequestMethod.POST)
     @ResponseBody
@@ -47,4 +50,12 @@ public class OmsPortalOrderController {
     public Object cancelTimeOutOrder(){
         return portalOrderService.cancelTimeOutOrder();
     }
+
+    @ApiOperation("取消单个超时订单")
+    @RequestMapping(value = "/cancelOrder",method = RequestMethod.POST)
+    @ResponseBody
+    public Object cancelOrder(Long orderId){
+        cancelOrderSender.send(orderId);
+        return new CommonResult().success(null);
+    }
 }

+ 38 - 0
mall-portal/src/main/java/com/macro/mall/portal/domain/QueueEnum.java

@@ -0,0 +1,38 @@
+package com.macro.mall.portal.domain;
+
+import lombok.Getter;
+
+/**
+ * 消息队列枚举配置
+ * Created by macro on 2018/9/14.
+ */
+@Getter
+public enum QueueEnum {
+    /**
+     * 消息通知队列
+     */
+    MESSAGE_QUEUE_ORDER("mall.order.direct", "mall.order.cancel", "mall.order.cancel"),
+    /**
+     * 消息通知ttl队列
+     */
+    MESSAGE_TTL_QUEUE_ORDER("mall.order.topic.ttl", "mall.order.cancel.ttl", "mall.order.cancel.ttl");
+
+    /**
+     * 交换名称
+     */
+    private String exchange;
+    /**
+     * 队列名称
+     */
+    private String name;
+    /**
+     * 路由键
+     */
+    private String routeKey;
+
+    QueueEnum(String exchange, String name, String routeKey) {
+        this.exchange = exchange;
+        this.name = name;
+        this.routeKey = routeKey;
+    }
+}

+ 6 - 0
mall-portal/src/main/java/com/macro/mall/portal/service/OmsPortalOrderService.java

@@ -32,4 +32,10 @@ public interface OmsPortalOrderService {
      */
     @Transactional
     CommonResult cancelTimeOutOrder();
+
+    /**
+     * 取消单个超时订单
+     */
+    @Transactional
+    void cancelOrder(Long orderId);
 }

+ 33 - 0
mall-portal/src/main/java/com/macro/mall/portal/service/impl/OmsPortalOrderServiceImpl.java

@@ -2,6 +2,7 @@ package com.macro.mall.portal.service.impl;
 
 import com.macro.mall.mapper.*;
 import com.macro.mall.model.*;
+import com.macro.mall.portal.component.CancelOrderSender;
 import com.macro.mall.portal.dao.PortalOrderDao;
 import com.macro.mall.portal.dao.PortalOrderItemDao;
 import com.macro.mall.portal.dao.SmsCouponHistoryDao;
@@ -51,6 +52,9 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
     private PortalOrderDao portalOrderDao;
     @Autowired
     private OmsOrderSettingMapper orderSettingMapper;
+    @Autowired
+    private OmsOrderItemMapper orderItemMapper;
+
     @Override
     public ConfirmOrderResult generateConfirmOrder() {
         ConfirmOrderResult result = new ConfirmOrderResult();
@@ -259,6 +263,35 @@ public class OmsPortalOrderServiceImpl implements OmsPortalOrderService {
         return new CommonResult().success(null);
     }
 
+    @Override
+    public void cancelOrder(Long orderId) {
+        //查询为付款的取消订单
+        OmsOrderExample example = new OmsOrderExample();
+        example.createCriteria().andIdEqualTo(orderId).andStatusEqualTo(0).andDeleteStatusEqualTo(0);
+        List<OmsOrder> cancelOrderList = orderMapper.selectByExample(example);
+        if(CollectionUtils.isEmpty(cancelOrderList)){
+            return;
+        }
+        OmsOrder cancelOrder = cancelOrderList.get(0);
+        if(cancelOrder!=null){
+            //修改订单状态为取消
+            cancelOrder.setStatus(4);
+            orderMapper.updateByPrimaryKeySelective(cancelOrder);
+            OmsOrderItemExample orderItemExample=new OmsOrderItemExample();
+            orderItemExample.createCriteria().andOrderIdEqualTo(orderId);
+            List<OmsOrderItem> orderItemList = orderItemMapper.selectByExample(orderItemExample);
+            //解除订单商品库存锁定
+            portalOrderDao.releaseSkuStockLock(orderItemList);
+            //修改优惠券使用状态
+            updateCouponStatus(cancelOrder.getCouponId(),cancelOrder.getMemberId(),0);
+            //返还使用积分
+            if(cancelOrder.getUseIntegration()!=null){
+                UmsMember member = memberService.getById(cancelOrder.getMemberId());
+                memberService.updateIntegration(cancelOrder.getMemberId(),member.getIntegration()+cancelOrder.getUseIntegration());
+            }
+        }
+    }
+
     /**
      * 生成18位订单编号:8位日期+2位平台号码+2位支付方式+6位以上自增id
      */

+ 8 - 0
mall-portal/src/main/resources/application-dev.properties

@@ -60,3 +60,11 @@ spring.redis.pool.min-idle=0
 spring.redis.timeout=0
 #===redis end===
 
+#===rabbitMq start===
+spring.rabbitmq.host=localhost
+spring.rabbitmq.port=5672
+spring.rabbitmq.virtual-host=/mall
+spring.rabbitmq.username=mall
+spring.rabbitmq.password=mall
+#===rabbitMq end===
+

+ 6 - 0
mall-portal/src/main/resources/application.properties

@@ -15,3 +15,9 @@ redis.key.prefix.orderId=portal:orderId:
 authCode.expire.seconds=90
 #===redis custom key end===
 
+#===rabbitmq queue name start===
+rabbitmq.queue.name.cancelOrder="cancelOrderQueue"
+#===rabbitmq queue name end===
+
+
+