package com.qmth.qrzk.repository.utils;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Random;

public class StringUtils {
	
	
	private final static String EMPTY_SYMBOL = "-";
	
	
	public static String getRandomComplexString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }
	    return sb.toString();   
	 }
	
	public static String getRandomSimpleString(int length) { //length表示生成字符串的长度
	    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	 }

	/**
	 * @param psStr
	 * @return
	 */
	public static boolean isEmpty(String psStr){
		if(psStr == null || "".equals(psStr)){
			return true;
		}
		
		return false;
	}
	
	/**
	 * @param psStr
	 * @return
	 */
	public static boolean isNotEmpty(String psStr){
		if(psStr != null && !"".equals(psStr)){
			return true;
		}
		
		return false;
	}
	
	/**
	 * @param piLength
	 * @return
	 */
	public static String getRandom(Integer piLength){
		String random = "";
		Random rand = new Random();
		for (int i = 0; i < piLength; i++) {
			random = random + rand.nextInt(10);
		}
		return random;
	}
	
	/**
	 * 格式化str
	 * @param psStr
	 * @return
	 */
	public static String escapeNull(String psStr){
		return psStr == null || psStr.equals("")?EMPTY_SYMBOL:psStr;
	}
	
	/**
	 * 格式化str
	 * @param psStr
	 * @return
	 */
	public static String escapeNull(Integer piInt){
		return piInt == null ?EMPTY_SYMBOL:piInt.toString();
	}
	
	public static String array2String(String [] pasStr,String psSplit){
		if(pasStr == null || pasStr.length == 0){
			return "";
		}
		
		StringBuffer sb = new StringBuffer();
		for(String str: pasStr){
			sb.append(str);
			sb.append(psSplit);
		}
		return sb.substring(0,sb.length()-1);
	}
	
	public static String join(String[] strs,char flag){
		String result = null;
		StringBuilder str = new StringBuilder();
		if(strs != null && strs.length > 0){
			for(int i = 0 ; i < strs.length ; i++){
				str.append(strs[i]+flag);
			}
			result = str.toString().substring(0,str.length()-1);
		}
		return result;
	}
	
	public static String join(Collection<String> strs,char flag){
		String result = null;
		StringBuilder str = new StringBuilder();
		if(strs != null && strs.size() > 0){
			for(String s : strs){
				str.append(s+flag);
			}
			result = str.toString().substring(0,str.length()-1);
		}
		return result;
	}
	
	/**
	 * 判断字符串是否是数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){
		if (str==null||str.length()==0) {
			return false;
		}
		for(int i=str.length();--i>=0;){
		int chr=str.charAt(i);
		if(chr<48 || chr>57)
			return false;
		}
			return true;
		}
	/**
     * 获得主键
     * 
     * @return String
     */
    public static String getPrimaryKey() {
        Date now = new Date();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyMMddHHmmssSS");
        return dateformat.format(now) + getRandomChar(1);
    }

    /**
     * 获得0-9,a-z,A-Z范围的随机数
     * 
     * @param length
     *            随机数长度
     * @return String
     */

    public static String getRandomChar(int length) {
        char[] chr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
                'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z' };

        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(62)]);
        }
        return buffer.toString();
    }

    /**
     * 获得10位随机字符
     * 
     * @return String
     */
    public static String getRandomChar() {
        return getRandomChar(10);
    }
    
    /**
     * 获得主键
     * 
     * @return String
     */
    public static String getStringByDate(Date data) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateformat.format(data);
    }
}
