package com.njcn.system.handler;

import com.github.tocrhz.mqtt.annotation.MqttSubscribe;
import com.github.tocrhz.mqtt.annotation.Payload;
import com.njcn.common.pojo.dto.LogInfoDTO;
import com.njcn.common.utils.PubUtils;
import com.njcn.system.service.IUserLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年07月13日 13:35
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MqttMessageHandler {

    private final IUserLogService userLogService;

    /**
     * 订阅审计日志的记录，并进行入库操作
     */
    @MqttSubscribe(value = "/userLog")
    public void subUserLog(String topic, MqttMessage message, @Payload String payload) {
        LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
        userLogService.addUserLog(logInfoDTO);
    }
}
