package com.bohuikeji.delivery.backstage.listener.news;

import com.bohuikeji.delivery.backstage.entity.TNews;
import com.bohuikeji.delivery.backstage.service.ITNewsService;
import com.bohuikeji.frame.module.admin.base.MqListener;
import com.bohuikeji.frame.module.admin.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author sjh
 * @date 2022/2/22 18:43
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class PushNewsListener extends MqListener<String> {

    final ITNewsService newsService;
    /**
     * 接收到消息会调用此方法
     *
     * @param msg 消息体
     * @return 【保留返回值】暂无实际用途，后续可能用于支持消息失败重试
     */
    @Override
    public boolean onReceive(String msg) {
        TNews news= JsonUtil.strToObj(msg,TNews.class);
        news.setNewsRead(false);
        news.setNewsPublish(false);
        news.setCreateTime(LocalDateTime.now());
        newsService.save(news);
        return true;
    }

    /**
     * 当消费者业务代码出现异常时会调用该方法
     *
     * @param msg 消息体
     * @param e   异常信息
     */
    @Override
    public void onError(String msg, Exception e) {

    }
}
