package com.zanxiang.game.data.serve.service.impl;

import com.alibaba.fastjson2.JSON;
import com.zanxiang.game.data.serve.pojo.dto.WebLogDTO;
import com.zanxiang.game.data.serve.service.IWebVisitLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * web访问日志记录
 */
@Service
@Slf4j
public class IWebVisitLogServiceImpl implements IWebVisitLogService {


    @Value("${spring.kafka.game-data.webLogTopic}")
    private String webLogTopic;

    @Value("${spring.kafka.game-data.bootstrap-servers}")
    private String gameSdkKafkaSevers;


    @Autowired
    @Qualifier("gameDataKafkaProducer")
    private Producer<String, String> kafkaProducer;

    /**
     * 将数据发到kafka
     * @param webLogDTO
     */
    @Override
    public void insertVisitlog(WebLogDTO webLogDTO) {
        System.out.println(gameSdkKafkaSevers);
        String webLogJsonString = JSON.toJSONString(webLogDTO);
        //将记录发到kafka记录
        try {
            kafkaProducer.send(new ProducerRecord<>(this.webLogTopic,webLogJsonString));
            log.error("发送web访问日志到kafka成功，topic：{},数据为：{}",this.webLogTopic,webLogJsonString);
        }catch (Exception e){
            log.error("发送web访问日志到kafka失败，topic：{},数据为：{},异常为：{}",this.webLogTopic,webLogJsonString,e.toString());
        }
    }
}
