/*
*
* Copyright (c) 2001-2018 泛微软件.
* 泛微协同商务系统,版权所有.
* 
*/
package com.cloudstore.eccom.core;
/**
 * 公共函数基类 1.0
 * @author AndyZhang  2018-7-18
 *
 */

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class WeaCommon {

	public static String createGuid() {
		UUID uuid = UUID.randomUUID();  
		return uuid.toString();
	}
	
	public static String getCurrentTimeString() {
        String timestrformart = "yyyy'-'MM'-'dd' 'HH:mm:ss" ;
        SimpleDateFormat SDF = new SimpleDateFormat(timestrformart) ;
        Calendar calendar = Calendar.getInstance() ;
        
        return SDF.format(calendar.getTime()) ;
    }
    

    public static String getCurrentDateString() {
        String timestrformart = "yyyy'-'MM'-'dd" ;
        SimpleDateFormat SDF = new SimpleDateFormat(timestrformart) ;
        Calendar calendar = Calendar.getInstance() ;
        
        return SDF.format(calendar.getTime()) ;
    }


    public static String getOnlyCurrentTimeString() {
        String timestrformart = "HH:mm:ss" ;
        SimpleDateFormat SDF = new SimpleDateFormat(timestrformart) ;
        Calendar calendar = Calendar.getInstance() ;
        return SDF.format(calendar.getTime()) ;
    }

    public static String getTimeString(Date date) {
        String timestrformart = "yyyy'-'MM'-'dd' 'HH:mm:ss" ;
        SimpleDateFormat SDF = new SimpleDateFormat(timestrformart) ;
        return SDF.format(date) ;
    }
	
	public static String getDateStr() {
		String currentDateString = getCurrentTimeString();
		String dateStr = currentDateString.substring(0,10);
		return dateStr;
	}
	
	public static String getTimeStr() {
		String currentDateString = getCurrentTimeString();
		String timeStr = currentDateString.substring(11);
		return timeStr;
	}
	
	public static int getIntValue(String v) {
        return getIntValue(v, -1);
    }

    public static int getIntValue(String v, int def) {
        try {
            return Integer.parseInt(v);
        } catch (Exception ex) {
            return def;
        }
    }

    public static float getFloatValue(String v) {
        return getFloatValue(v, -1);
    }

    public static float getFloatValue(String v, float def) {
        try {
            return Float.parseFloat(v);
        } catch (Exception ex) {
            return def;
        }
    }

    public static double getDoubleValue(String v) {
        return getDoubleValue(v, -1);
    }

    public static double getDoubleValue(String v, double def) {
        try {
            return Double.parseDouble(v);
        } catch (Exception ex) {
            return def;
        }
    }

	public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
	
	 public static boolean isEmpty(CharSequence var0) {
	        return var0 == null || var0.length() == 0;
	}

    public static boolean isNotEmpty(CharSequence var0) {
        return !isEmpty(var0);
    }

    public static boolean isBlank(CharSequence var0) {
        int var1;
        if (var0 != null && (var1 = var0.length()) != 0) {
            for(int var2 = 0; var2 < var1; ++var2) {
                if (!Character.isWhitespace(var0.charAt(var2))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(CharSequence var0) {
        return !isBlank(var0);
    }

    public static String trim(String var0) {
        return var0 == null ? null : var0.trim();
    }
    
    public static String decode(String value) throws UnsupportedEncodingException {
    	return URLDecoder.decode(value, "utf-8");
    }
    
    public static String encode(String value) throws UnsupportedEncodingException {
    	return URLDecoder.decode(value, "utf-8");
    }

    public static String createWhereInSQL(String ids,boolean isInt) {
    	String whereSql="";
    	if (isNotBlank(ids)) {
            String[] str=ids.split(",");
            for(String item:str) {
            	if (isInt)
            		whereSql=isNotBlank(whereSql)?whereSql+","+item:item;
            	else
            		whereSql=isNotBlank(whereSql)?whereSql+",'"+item+"'":"'"+item+"'";
            }
        }
    	return whereSql;
    }

    public static String createWhereInSQL(List<String> ids,boolean isInt) {
        String whereSql="";

        for(String item:ids) {
            if (isInt)
                whereSql=isNotBlank(whereSql)?whereSql+","+item:item;
            else
                whereSql=isNotBlank(whereSql)?whereSql+",'"+item+"'":"'"+item+"'";
        }

        return " ("+whereSql+") ";
    }

    public static String createWhereInSQL(String ids){
        String[] list=ids.split(",");
        String whereSql="";

        for(String item:list)
            if (StringUtils.isNotBlank(item)) {
                whereSql+= "'"+item+"',";
            }
        whereSql=whereSql.substring(0,whereSql.length()-1);
        String sql=" ("+whereSql+") ";
        return sql;
    }

    public static String createWhereInSQL(int[] ids){
        String whereSql="";

        for(int item:ids) {
            whereSql+= item+",";
        }

        whereSql=whereSql.substring(0,whereSql.length()-1);
        String sql=" ("+whereSql+") ";
        return sql;
    }

    public static String createWhereInSQL(List<String> ids,List params) {

        String whereSql="";

        for(String item:ids)
            if (StringUtils.isNotBlank(item)) {
                whereSql+= "?,";
                params.add(item);
            }
        whereSql=whereSql.substring(0,whereSql.length()-1);
        String sql=" ("+whereSql+") ";
        return sql;
    }

    public static String createWhereInSQL(String ids,List params) {
        String[] list=ids.split(",");
        String whereSql="";

        for(String item:list)
            if (StringUtils.isNotBlank(ids)) {
                whereSql+= "?,";
                params.add(item);
            }
        whereSql=whereSql.substring(0,whereSql.length()-1);
        String sql=" ("+whereSql+") ";
        return sql;
    }

    public static String createDeleteSQLByIds(String table,String key,String ids,List params) {
        String whereSql=createWhereInSQL(ids,params);
        return "delete from "+table+" where "+key+" in "+whereSql;
    }

    public static String createSelectSQLByIds(String table,String key,String ids,List params) {
        String whereSql=createWhereInSQL(ids,params);
        return "select * from "+table+" where "+key+" in "+whereSql;
    }

    public static String getFileSizeToString(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        String ret = "";
        DecimalFormat df = new DecimalFormat("0.00");
        if(size >= gb){
            ret = df.format(size/(gb*1.0)) + " GB";
        }else if(size >= mb){
            ret = df.format(size/(mb*1.0)) + " MB";
        }else if(size >= kb){
            ret = df.format(size/(kb*1.0)) + " KB";
        }else if(size > 0){
            ret = df.format(size/(1.0)) + " Byte";
        }
        return ret;
    }
    
}
