package com.gyee.wisdom.xfai.net3.controller;

import com.gyee.wisdom.xfai.net3.model.XFCommand;
import com.gyee.wisdom.xfai.net3.service.XFCommandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jms.Topic;
import java.lang.reflect.InvocationTargetException;

@Controller
//接收端
@Slf4j
public class MQConsumer {

    @Autowired
    Topic topic;

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    XFCommandService xfCommandService;


    @JmsListener(destination = "LVSP.function")
    public void listenTopic(String msg) {
        log.info("listenTopic接口的入参为"+msg);
        if(msg.startsWith("U3D:MessageToU3D:")) {
            String cmd = msg.substring(17);
            System.out.println("命令截取后的指令为"+cmd);
            XFCommand xfCommand = new XFCommand(cmd);
            if (xfCommand.validate()) {
                String result = null;
                result = xfCommandService.phraseXFCommand(xfCommand);
                System.out.println(result);
                jmsMessagingTemplate.convertAndSend(topic, "TTS:" + result);
            } else {
                String answerMsg = "sorry!";
                if (xfCommand.isBadCommand()) {
                    answerMsg = "命令无效！";
                } else if (xfCommand.isNoBusiness()) {
                    answerMsg = "不支持该查询！";
                } else if (xfCommand.isNoData()) {
                    answerMsg = "超出数据范围！";
                }
                jmsMessagingTemplate.convertAndSend(topic, "TTS:" + answerMsg);
            }
        }
    }
}
