package com.xhkjedu.slog.mq;

import com.alibaba.fastjson.JSONObject;
import com.xhkjedu.model.slog.THandleLog;
import com.xhkjedu.slog.service.log.HandleLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description：操作日志
 * @Author: WN
 * @Date: 2023/11/15 14:29:12
 **/
@Component
@Slf4j
public class HandleLogMq {

    @Resource
    private HandleLogService handleLogService;

    /*
     * @Description 监听消息：操作日志
     * @Date 2023/11/15 14:37:14
     * @Author WN
     * @Param [handleLog, message]
     * @Return void
     **/
    @RabbitListener(queues = "${rabbitmq.handleLogQueue}")
    public void receiveHandleLogQueue(Message message) {

        try {
            String msg = new String(message.getBody(),"UTF-8");
            THandleLog handleLog = JSONObject.parseObject(msg, THandleLog.class);

            // log.info("开始监听：操作日志");
            handleLogService.add(handleLog);
        } catch (Exception e) {
            log.error("保存操作日志出错：" + e.getMessage());
        }
    }
}
