package com.xhkjedu.sapi.listener;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.sapi.mapper.research.ResearchMapper;
import com.xhkjedu.sapi.model.research.TResearch;
import com.xhkjedu.sapi.model.subjectbook.TResource;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 消息到达消费者监听类
 */
@Component
@Slf4j
public class MessageConsumer {

    @Resource
    private ResearchMapper researchMapper;

    //教研转换完处理教研文件
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.researchHandleQueue}")
    public void researchHandleQueue(Message message) {
        try {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println(LocalDateTime.now()+" 教研处理接收消息内容："+msg);
            TResource resource = JSON.parseObject(msg, TResource.class);
            String resourceid = resource.getResourceid();
            String resourceurl = resource.getResourceurl();
            String mp4code = resource.getMp4code();
            TResearch research = new TResearch();
            research.setResearchid(N_Utils.str2Int(resourceid));
            research.setResearchpath(resourceurl);
            research.setMp4code(mp4code);
            researchMapper.updateResourceMp4code(research);
        } catch (Exception e) {
            log.error("教研转换失败："+e.getMessage());
        }
    }
}
