package cn.net.cpzslibs.label;

import java.util.regex.Pattern;

public class LabelUtils {
	/*public static void main(String[] args) {
		String s = "http://d.cpwzs.owrg/?iwd=	2100031000001000012457";
//		String s = "2100031000001000012457";
		System.out.println(getLabelSplits(s).length());
		http://d.cpzs.org/?id=	1000020993216988
		
	}*/
	/**
	 * 区分一代二代三代的标签 <br/>
	 * 2015年5月19日16:34:17
	 */
	/*public static String getLabelSplits(String scanContent) {
		scanContent = scanContent.trim();
		String new_lable_id = "";
		String second = "http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=";
		String third = "http://d.cpzs.org/?id=";
		if (scanContent.contains(second)) {
			// 二代码//http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=1000123170957980
			String[] ids = scanContent.split("lableId=");
			new_lable_id = ids[1];
		} else if (scanContent.contains(third)) {
			// 三代标签http://d.cpzs.org/?id=1000123170957980
			new_lable_id = scanContent.split("d.cpzs.org\\/\\?id=")[1];
		} else {
			new_lable_id = scanContent.trim();
		}
		return new_lable_id;
	}*/

	
	public static String getLabelSplits(String scanContent) {

        if (scanContent == null)
            return "";
        scanContent = scanContent.trim();
        scanContent = scanContent.replaceAll("\\s", "");
//        scanContent = scanContent.replaceAll("", replacement);
        String new_lable_id = "";
        // String second =
        // "http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=";
        //http://y.cpzs.org/2100031000001000012457
        String third = "http://d.cpzs.org/?id=";
        String four = "http://y.cpzs.org/";
        String nongyao="http://ny.cpzs.org/";
        
        if(scanContent.contains(nongyao)){//农药截取
       	 	new_lable_id = (scanContent.substring(scanContent.length()-16));
       	 return new_lable_id;
       }
        
        if (scanContent.contains("lableId=")) {
            // 二代码//http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=1000123170957980
            String[] ids = scanContent.split("lableId=");
            new_lable_id = ids[1];
        } else if (scanContent.contains(third)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("d.cpzs.org\\/\\?id=")[1];
        } else if (scanContent.contains(four)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("y.cpzs.org/")[1];
        } 
        //
        if (scanContent.length() > 22) {
            //长度大于22位，截取后22位判断
            String sub22Str = scanContent.substring(scanContent.length() - 22, scanContent.length());
            if (isNumber(sub22Str)) {
                new_lable_id = sub22Str;
            } else {
                String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
                if (isNumber(sub16Str)){
                    new_lable_id = sub16Str;
                } else{
                    new_lable_id = scanContent;
                }
            }

        } else if (scanContent.length() > 16 && scanContent.length() < 22) {
            String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
            if (isNumber(sub16Str)){
                new_lable_id = sub16Str;
            } else{
                new_lable_id = scanContent;
            }
        } else {
            new_lable_id = scanContent.trim();
        }
        return new_lable_id;
	}
	
	public static String getJinLabelSplits(String scanContent) {

        if (scanContent == null)
            return "";
        scanContent = scanContent.trim();
        scanContent = scanContent.replaceAll("\\s", "");
        String new_lable_id = "";
        // String second =
        // "http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=";
        //http://y.cpzs.org/2100031000001000012457
        String third = "http://d.cpzs.org/?id=";
        String four = "http://y.cpzs.org/";
        if (scanContent.contains("lableId=")) {
            // 二代码//http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=1000123170957980
            String[] ids = scanContent.split("lableId=");
            new_lable_id = ids[1];
        } else if (scanContent.contains(third)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("d.cpzs.org\\/\\?id=")[1];
        } else if (scanContent.contains(four)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("y.cpzs.org/")[1];
        } 
        
        if (scanContent.length() > 22) {
            //长度大于22位，截取后22位判断
            String sub22Str = scanContent.substring(scanContent.length() - 22, scanContent.length());
            if (isNumber(sub22Str)) {
                new_lable_id = sub22Str;
            } else {
                String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
                if (isNumber(sub16Str)){
                    new_lable_id = sub16Str;
                } else{
                    new_lable_id = scanContent;
                }
            }

        } else if (scanContent.length() > 16 && scanContent.length() < 22) {
            String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
            if (isNumber(sub16Str)){
                new_lable_id = sub16Str;
            } else{
                new_lable_id = scanContent;
            }
        } else {
            new_lable_id = scanContent.trim();
        }
        return new_lable_id;
	}
	/**
	 * 验证消费码 2014-5-12下午9:26:32
	 * 
	 * @param inputConsCode
	 *            输入的四位消费码
	 * 
	 * @param consCode
	 *            服务器获取消费码
	 * @return
	 */
	public static boolean checkConsCode(String inputConsCode, String consCode) {

		if (inputConsCode.trim().equals(consCode.trim())) {
			return true;
		} else {
			return false;
		}
	}

	// >=16位和<19位为正确<br/>
	// 2015年4月10日15:32:50<br/>
	/**
	 * 简单的验证追溯码是否正确<br/>
	 * >=16位和<23位为正确<br/>
	 * 修正 2015年10月28日 17:50:36
	 * 
	 * @param label
	 * @return
	 */
	public static boolean checkLabelLength(String label) {
		if (label.trim().length() != 16) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是不是数字 2014-5-28下午1:56:22
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		return Pattern.compile("^\\d*$").matcher(str).matches();
	}
}
