package net.northking.cctp.testbug.bus.publisher;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.testbug.bus.constants.MQConstants;
import net.northking.cctp.testbug.constants.TestBugError;
import net.northking.cctp.testbug.dto.BugStatisticsChartAddDto;
import net.northking.cctp.testbug.dto.bus.BugGradeStatisticsAddDto;
import net.northking.cctp.testbug.dto.bus.BugTendencyChartTmpAddDto;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
public class NoticeRabbitMQPublisher implements NoticePublisher {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 发送bug_grade_statistics对象到数据仓库
     *
     * @param bugGradeStatisticsAddDtoList
     */
    @Override
    public void syncBugGradeNotice(List<BugGradeStatisticsAddDto> bugGradeStatisticsAddDtoList) {
        byte[] json;
        try{
            json = objectMapper.writeValueAsBytes(bugGradeStatisticsAddDtoList);
        }catch (JsonProcessingException e){
            throw new PlatformRuntimeException(TestBugError.NOTICE_BUG_GRADE_FAILED);
        }
        MessageBuilder mb = MessageBuilder.withBody(json);
        mb.setContentType(MediaType.APPLICATION_JSON_VALUE);
        rabbitTemplate.send(MQConstants.Queue.Q_Platform_Notice_TestBugGrade_REQ,mb.build());
    }

    /**
     * 发送bug_tendency_chart_tmp对象到数据仓库
     *
     * @param bugTendencyChartTmpAddDtoList
     */
    @Override
    public void syncBugTendencyNotice(List<BugTendencyChartTmpAddDto> bugTendencyChartTmpAddDtoList) {
        byte[] json;
        try{
            json = objectMapper.writeValueAsBytes(bugTendencyChartTmpAddDtoList);
        }catch (JsonProcessingException e){
            throw new PlatformRuntimeException(TestBugError.NOTICE_BUG_GRADE_FAILED);
        }
        MessageBuilder mb = MessageBuilder.withBody(json);
        mb.setContentType(MediaType.APPLICATION_JSON_VALUE);
        rabbitTemplate.send(MQConstants.Queue.Q_Platform_Notice_TestBugGrade_REQ,mb.build());
    }

    /**
     * 发送bug_status_statistics对象到数据仓库
     *
     * @param bugStatisticsChartAddDtoList
     */
    @Override
    public void syncBugStatusNotice(List<BugStatisticsChartAddDto> bugStatisticsChartAddDtoList) {
        byte[] json;
        try{
            json = objectMapper.writeValueAsBytes(bugStatisticsChartAddDtoList);
        }catch (JsonProcessingException e){
            throw new PlatformRuntimeException(TestBugError.NOTICE_BUG_GRADE_FAILED);
        }
        MessageBuilder mb = MessageBuilder.withBody(json);
        mb.setContentType(MediaType.APPLICATION_JSON_VALUE);
        rabbitTemplate.send(MQConstants.Queue.Q_Platform_Notice_TestBugGrade_REQ,mb.build());
    }

}
