package com.huimv.eartag.producer;

import com.alibaba.fastjson.JSONObject;
import com.huimv.eartag.utils.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Component
@Slf4j
public class InputProducer {
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    /**
     * @Method      : saveRawdata
     * @Description : 保存原始数据到rabbitmq
     * @Params      : [askText]
     * @Return      : java.lang.String
     *
     * @Author      : ZhuoNing
     * @Date        : 2022/2/8
     * @Time        : 17:39
     */
    public String sendClientAsk(String askText){
        Map map = new HashMap();
//        System.out.println("000000000000000000 >>"+askText);
        map.put("askText",askText);
//        System.out.println("111111111111111111>>"+map.toString());
//        rabbitTemplate.convertAndSend("askExchange", "topic.askText.key", map);
        rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK, Const.ROUTING_KEY_ASK, map);
//        System.out.println("保存原始数据到rabbitMQ>>"+map.toString());
        return "ok";
    }

    //处理心跳数据
    public String sendHeartbeat(JSONObject dataJo) {
        Map map = new HashMap();
        map.put("heartbeat",dataJo.toString());
        rabbitTemplate.convertAndSend(Const.EXCHANGE_DEVICE, Const.ROUTING_KEY_DEVICE_HEARTBEAT, map);
        System.out.println("处理心跳 >>"+dataJo);
        return "ok";
    }

    //处理环境温度数据
    public String sendDeviceTemp(JSONObject dataJo) {
        Map map = new HashMap();
        map.put("deviceTemp",dataJo);
        rabbitTemplate.convertAndSend(Const.EXCHANGE_DEVICE, Const.ROUTING_KEY_DEVICE_TEMP, map);
        System.out.println("处理环境温度 >>"+dataJo);
        return "ok";
    }

    //处理耳标数据
    public String sendEartag(JSONObject dataJo) {
        Map map = new HashMap();
        map.put("eartag",dataJo);
        rabbitTemplate.convertAndSend(Const.EXCHANGE_EARTAG, Const.ROUTING_KEY_EARTAG, map);
        System.out.println("处理耳标数据 >>"+dataJo);
        return "ok";
    }
    
    /**
     * @Method      : sendClientAsk2
     * @Description : 发送耳标数据队列和设备数据队列
     * @Params      : [askText]
     * @Return      : void
     * 
     * @Author      : ZhuoNing
     * @Date        : 2022/3/12       
     * @Time        : 18:30
     */
    public String sendClientAsk2(String askText) {
        askText = askText.replaceAll("\r","").replaceAll("\n","");
        Map map = new HashMap();
        map.put("askText",askText);
        String[] dataArray = askText.split("\\+");
        String cmdHeader = dataArray[0];
        if (cmdHeader.trim().equalsIgnoreCase("hm")) {
            //发送到设备处理消息队列
            rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK_DEVICE, Const.ROUTING_KEY_ASK_DEVICE, map);
            System.out.println("## >> 发送设备处理 ...");
        } else if (cmdHeader.trim().equalsIgnoreCase("zj")) {
            if(dataArray[2].trim().equalsIgnoreCase("ffffffffffffffff") || dataArray[2].trim().equalsIgnoreCase("0000000000000000")){
                log.error("舍弃掉未配置耳标号的耳标，"+askText+"");
            }else{
                //发送到耳标处理消息队列
                rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK_EARTAG, Const.ROUTING_KEY_ASK_EARTAG, map);
                System.out.println("## >> 发送耳标处理 ...");
            }
        }
        return "ok";
    }
}
