/**
 * 
 */
package com.huatek.busi.common.util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;

/**
 * 计算
 * 
 * @author aaly_lei
 *
 */
public class ComputeUtil {
	
	/**
	 * 加载js引擎
	 * @param reader js文件
	 * @return
	 * @throws ScriptException 
	 * @throws Exception
	 */
	public static ScriptEngine loadJsScriptEngine(FileReader reader) throws ScriptException{
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		// 执行指定脚本
		engine.eval(reader);
		return engine;
	}
	/**
	 * 公式计算
	 * @param engine js引擎
	 * @param functionName js方法名称
	 * @param params 参数
	 * @return
	 * @throws ScriptException
	 * @throws NoSuchMethodException
	 * @throws IOException
	 */
	public static String eval(ScriptEngine engine, String functionName, Object... params) throws NoSuchMethodException, ScriptException {
		String result = "";
		if (engine instanceof Invocable) {
			Invocable invoke = (Invocable) engine;
			result = invoke.invokeFunction(functionName, params).toString();
		}
		if("NaN".equals(result) || "Infinity".equals(result) || "-Infinity".equals(result)) {
			result = "0";
		}
		return result;
	}
	/**
	 * 获取js文件
	 * @param jsFilePath 文件路径
	 * @return
	 * @throws FileNotFoundException 
	 * @throws Exception
	 */
	public static FileReader readerJsFile(String jsFilePath) throws FileNotFoundException {
		// 读取js文件
		FileReader reader = new FileReader(jsFilePath);
		// 执行指定脚本
//		reader.close();
		return reader;
	}
	/**
	 * 关闭reader流
	 * @param fileReader 文件流
	 * @throws IOException
	 */
	public static void close(FileReader fileReader) throws IOException {
		fileReader.close();
	}

	
	/**
	 * 获取项目部署跟路径
	 * @param request
	 * @return
	 */
	public static String getRealPath(HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("/");
		return path;
	}
	
	/**
	 * 获取变压器默认js文件路径
	 * @param request
	 * @return
	 */
	public static String getDefJsPath(HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("/")+"static/js/formula.js";
		return path;
	}
	/**
	 * 获取GIS默认js文件路径
	 * @param request
	 * @return
	 */
	public static String getDISDefJsPath(HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("/")+"static/js/formula_gis.js";
		return path;
	}
	
	public static void main(String[] args) throws Exception {
		FileReader fileReader = readerJsFile("E:\\huatek_project\\04ShuJuJianCe\\ShuJuJianCe\\src\\main\\webapp\\static\\js\\formula.js");
		ScriptEngine engine = loadJsScriptEngine(fileReader);
		ComputeUtil.eval(engine,"expression", new Integer(11));
	}
	
    /**
     * 生成唯一编码编号
    * @Title: getOrderNo 
    * @Description: TODO 
    * @createDate: Jul 4, 2018 5:08:09 PM
    * @return
     */
    public static synchronized String getUniqueNo() { 
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        System.out.println(newDate+result);
        return newDate+result;
    }
}