package com.trs.web2frame.tool.tyjyh.wcmhy03rs_yb;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
	
	private static String LOCALLOG = "D:\\data\\logs";
	 public static SimpleDateFormat fmtAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
    public static String getNowTime() {
        return fmtAll.format(new Date());
    }
	
    /**
     * 防止空指针
     * @param obj
     * @return
     */
    public static String toStr(Object obj) {

        if (obj == null) {
            return "";
        } else {
            return obj.toString().trim();
        }
    }
	/**
     * LOG输出xml
     * @param in
     * @param name
     * @param type 1:打印控制台
     */
    public static void log(Object in,String name, int type){
        if(name.equals("")){
            name = "debug";
        }
        String content = Util.toStr(in);
        if(type == 1) {
            System.out.println(content);
        }
        String path=LOCALLOG;
        path = path.replace("\\", "/");
        if(!path.endsWith("/")){
            path += "/";
        }
        File folder = new File(path);
        if(!folder.exists() && !folder.isDirectory())
        {
            folder.mkdirs();
        }
        File file=null;
        String date = Util.getNowTime().substring(0, 10);
        file = new File(path + date + "-" + name + ".log");
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
            writer.write( "\r\n" + content + "\r\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String URLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

/**

 * 根据String型时间，获取long型时间，单位毫秒

 * @param inVal 时间字符串

 * @return long型时间

 */

public static long fromDateStringToLong(String inVal) {

    Date date = null;

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    try {

        date = inputFormat.parse(inVal);

    } catch (Exception e) {

        e.printStackTrace();

    }

    return date.getTime();

}


}
