package com.pj.current;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * springboot启动之后，打印版本信息 
 * @author kong
 *
 */
@Component
public class SaPlusStartup implements CommandLineRunner {

	@Value("${spring.application.name:sa-plus}")
    private String applicationName;
	
	@Value("${server.port:8080}")
    private String port;

    @Value("${server.servlet.context-path:}")
    private String path;

    @Value("${spring.profiles.active:}")
    private String active;
   
    public static final String version = "v1.6.0";
    
    @Override
    public void run(String... args) throws Exception {
    	 String ip = InetAddress.getLocalHost().getHostAddress();
         String str = "\n------------- " + applicationName + " [" + version + "] (" + active + ") 启动成功 --by " + getNow() + " -------------\n" + 
                 "\t- Local:   http://localhost:" + port + path + "\n" +
                 "\t- Local2:  http://127.0.0.1:" + port + path + ""
                 ;
         System.out.println(str);
         // 
         try {
        	 System.out.println("\t- Network: http://" + ip + ":" + port + path + "\n");
         } catch (Exception e) {
        	 System.out.println("\t- Network: 未能成功获取\n");
        	 System.err.println("异常：" + e.getMessage());
         }
    }

	/**
	 * 返回系统当前时间的YYYY-MM-dd hh:mm:ss 字符串格式
	 */
	private static String getNow(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
    
}

