
use crate::common::define::{
    Order, CMD_RESTART ,REQ_DURATION, TOPIC_ALL, TOPIC_RESTART,  API_REQ
};
use super::dyn_send::{Trans,Pass,Nat};
use crate::common::myfs::get_dot_env;
lazy_static! {
    pub static ref NATS_URL:String =get_dot_env("NATS_URL");
    pub static ref NATS_ACCOUNT:String =get_dot_env("NATS_ACCOUNT");
    pub static ref NATS_PWD:String =get_dot_env("NATS_PWD");
    pub static ref SLAVE_ID:String=get_dot_env("SLAVE_ID");
}

// 用match 不可以，只能if,必须明确引入结构体，必须&self
fn select_trans() -> Box<dyn Trans> {

    let method = get_dot_env("TRANS");
    if method=="pass".to_string(){
        return Box::new(Pass{});
    }
    Box::new(Nat{})
}


pub fn send_order(ord: &Order) -> Order {
    let t=select_trans();
    let res = t._send_order(ord);
    match res {
        Ok(o) => o,
        Err(e) => {
            let mut res = ord.clone();
            res.resp = format!("发送失败:{}", e);
            res
        }
    }
}

pub fn pub_restart(slave: &str) {
    let t=select_trans();
    let res = t._pub_restart(slave);
    match res {
        Ok(()) => {
            println!("发送成功");
        }
        Err(e) => {
            let res = format!("发送失败:{}", e);
            println!("{}", res);
        }
    }
}



