package com.bweb.config.msg;

import com.hai.entity.HighGasOilPriceTask;
import com.hai.msg.entity.MsgTopic;
import com.hai.service.HighGasOilPriceTaskService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hurui
 * @version 1.0
 * @ClassName RedisKeyExpirationListener
 * @description: TODO
 * @date 2021/8/19 10:00
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    private static Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class);

    @Resource
    private HighGasOilPriceTaskService gasOilPriceTaskService;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    public void onMessage(Message message, byte[] pattern) {
        try {
            if (message != null && StringUtils.isNotBlank(message.toString())) {
                // 加油站价格任务
                if (message.toString().contains(MsgTopic.oilPriceTask.getName())) {
                    // 截取任务id
                    Long taskId = Long.parseLong(StringUtils.substringAfterLast(message.toString(), MsgTopic.oilPriceTask.getName() + "-"));
                    if (taskId != null) {
                        // 查询任务
                        HighGasOilPriceTask gasOilPriceTask = gasOilPriceTaskService.getDetailById(taskId);
                        if (gasOilPriceTask != null) {
                            // 任务处理
                            gasOilPriceTaskService.businessHandle(gasOilPriceTask);
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("redis过期事件异常：", e);
        }
    }
}
