package com.ruoyi.web.modules.diagnosis.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.diagnosis.domain.ConsultPushPayload;

/**
 * 经 Redis 频道广播问诊新消息。
 */
@Service
public class ConsultRedisPushService implements ConsultPushService
{
    private static final Logger log = LoggerFactory.getLogger(ConsultRedisPushService.class);

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @Override
    public void publishNewMessage(ConsultPushPayload payload)
    {
        if (payload == null)
        {
            return;
        }
        if (redisTemplate == null)
        {
            log.debug("Redis unavailable, skip consult push sessionId={}", payload.getSessionId());
            return;
        }
        try
        {
            redisTemplate.convertAndSend(ConsultSessionRules.REDIS_PUSH_CHANNEL, payload);
        }
        catch (Exception ex)
        {
            log.warn("consult push failed sessionId={}", payload.getSessionId(), ex);
        }
    }
}
