package net.northking.cctp.scriptcase.bus;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
import net.northking.cctp.common.dto.CpLogAddDto;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.feign.MonsterLogServer;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.scriptcase.enums.ScriptCaseAppError;
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;
    @Autowired
    private EntityNameCache entityNameCache;
    /**
     * 头部数据KEY：租户
     */
    private static final String Key_SaaS_Tenant_ID = "SaaS_Tenant_ID";
    /**
     * 头部数据KEY：用户ID
     */
    private static final String Key_CCTP_User_ID = "CCTP_User_ID";

    /**
     * 发送高危通知日志记录
     */
    public static final String Q_Platform_Log_Insert_REQ = "Platform.Log.Insert.REQ";

    @Override
    public void deployHighRiskLog(String type,String delName,List<String>names,String projectId) {
        byte[] json;
        String projectName = null;
        if(projectId!=null){
            projectName = entityNameCache.translateById(EntityNameCatalog.MONSTER_SYSTEM,projectId);
        }
        try {
            CpLogAddDto dto = new CpLogAddDto();
            String[] tool = getModuleByType(type,delName,projectName);
            dto.setContent(tool[1]);
            dto.setModule(tool[0]);
            dto.setType("3");//类型3 删除类日志
            dto.setContentDetails(names);
            dto.setRemoteAddr(request.getRemoteAddr());
            //记录平台日志
            MonsterLogServer.sendLogToMonster(dto);
            json = objectMapper.writeValueAsBytes(dto);
        } catch (JsonProcessingException e) {
            throw new PlatformRuntimeException(ScriptCaseAppError.HIGH_RISK_SEND);
        }
        MessageBuilder mb = MessageBuilder.withBody(json);
        mb.setContentType(MediaType.APPLICATION_JSON_VALUE);
        mb.setHeader(Key_SaaS_Tenant_ID, NKSecurityContext.getTenantId());
        mb.setHeader(Key_CCTP_User_ID, NKSecurityContext.getUserId());
        rabbitTemplate.send(Q_Platform_Log_Insert_REQ, mb.build());
    }

    private String[] getModuleByType(String type,String delName,String projectName){
        String[] result = new String[2];
        switch(type){
            case "busCom":
                result[0]="业务组件";
                result[1]="【删除业务组件】系统："+projectName+"，业务组件:"+delName;
                break;
            case "busComBatch":
                result[0]="业务组件";
                result[1]="【批量删除业务组件】系统："+projectName+"，共:"+delName+"条";
                break;
            case "libBusCom":
                result[0]="业务组件库";
                result[1]="【删除业务组件库】系统："+projectName+"，组件："+delName;
                break;
            case "libBusComBatch":
                result[0]="业务组件库";
                result[1]="【批量删除业务组件库组件】共:"+delName+"条";
        }
        return result;
    }
}
