倒计时取消订单

package com.itsoft.hotel.configuration.redis;

import com.itsoft.framework.core.user.UserHelper;
import com.itsoft.hotel.configuration.mybatisplus.DynamicDataSourceContextHolder;
import com.itsoft.hotel.entity.ManagerHotelOrderVO;
import com.itsoft.hotel.service.HotelLogService;
import com.itsoft.hotel.service.ManagerHotelOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.itsoft.hotel.commons.utils.Constant.*;

/**
 * 主要作用就是:接收过期的redis消息,获取到key,key就是订单号,然后去更新订单号的状态
 */
@Transactional
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
 
  @Autowired
  private ManagerHotelOrderService managerHotelOrderService;
  @Autowired
  private HotelLogService hotelLogService;
  public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
    super(listenerContainer);
  }
 
 
  @Override
  public void onMessage(Message message, byte[] pattern) {
    String orderCode = message.toString();
    if (!StringUtils.isBlank(orderCode)) {
      if (orderCode.startsWith(ORDER_HOTEL_RETURN)) {
        try {
          List<String> list = Arrays.asList(orderCode.split("_"));
          DynamicDataSourceContextHolder
                  .setDataSourceKey(list.get(2));
          ManagerHotelOrderVO orderInfo = managerHotelOrderService.getById(list.get(3));
          if (orderInfo.getStatus() == 0) {
            //处于未支付状态
            orderInfo.setStatus(4);//取消订单
            managerHotelOrderService.updateById(orderInfo);
            //添加日志
            hotelLogService.addLog(orderInfo.getId(), ITSOFT_AUTO, CANCEL_ORDER, ITSOFT_NAME, ITSOFT_AUTO_CONTENT);
          }
        } catch (Exception ex) {
          throw ex;
        } finally {
          DynamicDataSourceContextHolder.clearDataSourceKey();
        }
      }
    }
  }
}
package com.itsoft.hotel.configuration.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
 
/**
 * 注册redisTemplate,作为消息队列的发布者
 */
@Configuration
public class RedisListenerConfig {
 
  @Bean
  public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
 
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    return container;
  }
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肉肉的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值