package com.bootdo.datasend.dianxin.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
public class ProducerController {
    private static final Logger logger = LoggerFactory.getLogger(ProducerController.class);

    private final RabbitTemplate rabbitTemplate;

    public ProducerController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @PostMapping("/send")
    public String sendMessage(@RequestBody String message) {
        rabbitTemplate.convertAndSend("warnrecord", "routing.key.warnrecord", message);
        return "Message sent!";
    }


//    @PostConstruct
    public String sendMessagetest() {
        try {
            JSONObject json = new JSONObject();
            json.put("12aaa3", "2aaa34");
            json.put("2aaa34", "45aaa6");
            rabbitTemplate.convertAndSend("warnrecord", "routing.key.warnrecord", json);
            logger.info("Message sent!");
            return "Message sent!";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
