package net.northking.cctp.scriptcase.bus;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.scriptcase.enums.ErrorMessage;
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;

@Component
public class PlanRabbitMQPublisher {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public void updatePlanStatusMQ(String scriptId){
        byte[] json;
        try {

            json = objectMapper.writeValueAsBytes(scriptId);
        } catch (JsonProcessingException e) {
            throw new PlatformRuntimeException(new ErrorMessage("通知计划脚本发生变动"));
        }
        MessageBuilder mb = MessageBuilder.withBody(json);
        mb.setContentType(MediaType.APPLICATION_JSON_VALUE);
        mb.setHeader("CCTP_User_ID", NKSecurityContext.getUserId());
        mb.setHeader("SaaS_Tenant_ID", NKSecurityContext.getTenantId());
        rabbitTemplate.send("Plan.Update.Status.REQ", mb.build());
    }

}
