package com.tanpu.community.manager;

import com.alibaba.fastjson.JSON;
import com.tanpu.community.api.beans.vo.KafkaDurationUptMsg;
import com.tanpu.community.service.VisitSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class KafkaManager {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    private VisitSummaryService visitSummaryService;


//    public void sendMessage(String message) {
//        System.out.println("#### send " + message);
//        this.kafkaTemplate.send("users", message);
//    }



    // todo topic
    @KafkaListener(topics = "newCommunityVisitor")
    public void consumeVisitorHis(String message) {
        System.out.println("#### receive " + message);
    }

    // todo topic
    @KafkaListener(topics = "newCommunityVisitor")
    public void consumeDurationUpdate(String message) {
        KafkaDurationUptMsg msg = JSON.parseObject(message, KafkaDurationUptMsg.class);
        visitSummaryService.updateDurByIdent(msg.getIdent(), msg.getDurMillsInc().intValue());
    }
}