package net.northking.cctp.flowable.bus.receiver;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.flowable.api.implement.service.ImplementProcessService;
import net.northking.cctp.flowable.api.sm.service.StateProcessDefApiService;
import net.northking.cctp.flowable.bus.BusConstants;
import net.northking.cctp.flowable.bus.dto.ImFlowMQDto;
import net.northking.cctp.flowable.common.FlowableConstants;
import net.northking.cctp.flowable.sm.StateProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class PlatformMQReceiver {

    private final static Logger logger = LoggerFactory.getLogger(PlatformMQReceiver.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ImplementProcessService imService;

    @Autowired
    private StateProcessDefApiService spService;

    @RabbitListener(queuesToDeclare = {@Queue(value = BusConstants.Flow_Process_Create_REQ)})
    public void receive(Message message) {
        MessageProperties mp = message.getMessageProperties();
        Map<String, Object> headers = mp.getHeaders();
        if (headers.containsKey(BusConstants.Key_SaaS_Tenant_ID)) {
        }
        String tenantId = (String) headers.get(BusConstants.Key_SaaS_Tenant_ID);  // 租户id
        NKSecurityContext.setTenantId(tenantId);
        NKSecurityContext.setUserId(FlowableConstants.SYSTEM_CREATED);
        String type; // 流程类型
        if (headers.containsKey(BusConstants.Key_Flow_Type)) {
            type = (String) headers.get(BusConstants.Key_Flow_Type);
        } else {
            type = FlowableConstants.NO;
        }
        switch (type) {
            case FlowableConstants.IM_PROCESS:
                try {
                    ImFlowMQDto imFlow = objectMapper.readValue(message.getBody(), new TypeReference<ImFlowMQDto>() {});
                    imService.createProcess(imFlow);
                } catch (Exception e) {
                    logger.error("消息转换失败", e);
                }
                break;
            case FlowableConstants.BUG_PROCESS:
                // 创建缺陷流程
                try {
                    StateProcessDefinition dto = objectMapper.readValue(message.getBody(), new TypeReference<StateProcessDefinition>() {});
                    spService.save(dto);
                } catch (Exception e) {
                    logger.error("消息转换失败", e);
                }
                break;
            default:
                logger.info("没有需要处理消息");
        }
    }
}
