package com.docus.server.collect.web.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("defRedis")
public class RedisMq {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送消息
     *
     * @param message
     */
    public void push(String messageKey, String message) {
        String Key = messageKey;
        stringRedisTemplate.opsForList().leftPush(Key, message);
    }

    public void deleteKey(String Key) {
        try {
            redisTemplate.delete(Key);
        } catch (Exception e) {
            return;
        }
    }

    public void deleteAllKey(List<String> ids) {
        try {
            redisTemplate.delete(ids);
        } catch (Exception e) {
            return;
        }
    }

    public void pushAll(String messageKey, List<String> messages) {
        String Key = messageKey;
        stringRedisTemplate.opsForList().leftPushAll(Key, messages);
    }


    /**
     * 获取消息,可以对消息进行监听，没有超过监听事件，则返回消息为null
     * rightPop：1.key,2.超时时间，3.超时时间类型
     *
     * @return
     */
    public String pop(String messageKey) {
        String Key = messageKey;
        try {
            return stringRedisTemplate.opsForList().rightPop(Key);
        } catch (Exception e) {
            return null;
        }

    }


    /**
     * 是否存在key
     *
     * @param messageKey
     * @return
     */
    public boolean IsExist(String messageKey) {
        return stringRedisTemplate.hasKey(messageKey);
    }

    //向通道发送消息的方法
    public void sendChannelMess(String channel, String message) {
        redisTemplate.convertAndSend(channel, message);
    }
}
