package net.northking.cctp.executePlan.consumer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import net.northking.cctp.executePlan.constants.PlanConstant;
import net.northking.cctp.executePlan.constants.RabbitConstant;
import net.northking.cctp.executePlan.db.entity.AtuPlanSceneCaseTask;
import net.northking.cctp.executePlan.db.entity.AtuPlanTask;
import net.northking.cctp.executePlan.db.service.AtuPlanSceneCaseTaskService;
import net.northking.cctp.executePlan.db.service.AtuPlanTaskService;
import net.northking.cctp.executePlan.dto.planTask.AtuTaskExecDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

public class TaskInputParamConsumer {

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

    @Autowired
    private AtuPlanSceneCaseTaskService planSceneCaseTaskService;

    @Autowired
    private AtuPlanTaskService planTaskService;

    @Autowired
    private AmqpAdmin amqpAdmin;

    @PostConstruct
    public void init(){
        // 初始化队列并绑定到交换机
        Exchange exchange = new DirectExchange(RabbitConstant.PROJECT_EXCHANGE_KEY);
        amqpAdmin.declareExchange(exchange);
        Queue queue = new Queue(RabbitConstant.TASK_INPUT_PARAM_UPDATE);
        amqpAdmin.declareQueue(queue);
        Binding binding = BindingBuilder.bind(queue).to(exchange)
                .with(RabbitConstant.TASK_INPUT_PARAM_UPDATE).noargs();
        amqpAdmin.declareBinding(binding);
    }

    /**
     * 接收任务输入参数并更新入库
     *
     * @param message 对象消息
     */
    @Transactional(rollbackFor = Exception.class)
    @RabbitListener(queues = RabbitConstant.TASK_INPUT_PARAM_UPDATE)
    public void receiveTaskInputParam(Message message) {
        logger.info("接收信息 ==> " + new String(message.getBody()));
        try {
            JSONObject event = JSONUtil.parseObj(new String(message.getBody()));
            AtuTaskExecDto receiveTask = JSONUtil.toBean(event, AtuTaskExecDto.class);
            if (PlanConstant.SCRIPT_TYPE_SCENE.equals(receiveTask.getCaseType())) {
                // 更新到场景任务表
                AtuPlanSceneCaseTask sceneTask = new AtuPlanSceneCaseTask();
                sceneTask.setId(receiveTask.getTaskId());
                sceneTask.setNodeParams(JSONUtil.toJsonStr(receiveTask.getCaseParams()));
                planSceneCaseTaskService.updateByPrimaryKey(sceneTask);
            } else {
                // 更新到任务表
                AtuPlanTask updateTask = new AtuPlanTask();
                updateTask.setId(receiveTask.getTaskId());
                updateTask.setCaseParam(JSONUtil.toJsonStr(receiveTask.getCaseParams()));
                planTaskService.updateByPrimaryKey(updateTask);
            }
        } catch (Exception e) {
            logger.error("更新任务的输入参数失败", e);
        }
    }
}
