package cn.net.cpzslibs.label;

import com.jymf.common.LableUtil;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Random;

public class JinLabelUtil {
	// 瓶盖码：日期加密4位（GetDays方法） + 产线号两位 + 顺序号6位 + 随机数两位
	private static boolean m_isInit = true;
	private static int m_index = 1000;
	private static Random m_rand = new Random();
	private static int line_code = 23;
	private static Calendar c = Calendar.getInstance();

	public static void main(String[] args) {
//		String label = "4150606001007008580200";
		String l = "415060600100700500";
//		char[] lNum = l.toCharArray();
//		String l12 = lNum[1]+""+lNum[3]+""+lNum[5]+""+lNum[7]+""+lNum[8]+""+lNum[10]+"" + lNum[11]+l.substring(13, 18);
//		System.out.println("" + l12);
////		char[] ckNums = checkCode.toCharArray();
//		
//		char[] ckNums = LableUtil.getCheckNum(l12).substring(l12.length()).toCharArray();
//		char[] cl = l.toCharArray();
//		
//		String label22 = l.substring(0, 15) + ckNums[0]+"" + cl[15]+""+ckNums[1]+"" + cl[16]+""+ ckNums[2]+"" + cl[17] +""+ ckNums[3];
////		
		System.out.println(getLabelId22(l));
		/*for (int i = 0; i < 20; i++) {
			// 瓶标
			// System.out.println(createBottleCode(85));
			// 箱标
//			System.out.println(getBoxCode(line_code));
			System.err.println(" 托盘号：" + getPalletCode(line_code));
		}*/
		// System.err.println("days:" + GetDays());
		
//		System.err.println(" 托盘号：" + getPalletCode(line_code));
		
		
		// System.out.println(" 瓶标： " + createBottleCode(85) +" "
		// +createBottleCode(85).length());
		// System.out.println("箱标：" + CreateTraceCode(85) + " "
//		9170808001007003018035
//		System.out.println("" + LabelUtils.getJinLabelSplits("9170808001007003018035"));
		
//		System.err.println("" + getDB18Label("9170808001007003018035"));
		// +CreateTraceCode(85).length());
	}
	public static String getLabelId22(String l){
		if (l.length() < 18) {
			return l;
		}
		char[] lNum = l.toCharArray();
		String l12 = lNum[1]+""+lNum[3]+""+lNum[5]+""+lNum[7]+""+lNum[8]+""+lNum[10]+"" + lNum[11]+l.substring(13, 18);
//		System.out.println(LableUtil.getCheckNum(l12)+" num:" + l12);
//		char[] ckNums = checkCode.toCharArray();
		
		char[] ckNums = LableUtil.getCheckNum(l12).substring(l12.length()).toCharArray();
//		char[] ckNums = EchoLabelUtil.getCheckNum(l12).substring(l12.length()).toCharArray();

		char[] cl = l.toCharArray();
		
		String label22 = l.substring(0, 15) + ckNums[0]+"" + cl[15]+""+ckNums[1]+"" + cl[16]+""+ ckNums[2]+"" + cl[17] +""+ ckNums[3];
		return label22;
	}
	public static BigDecimal getLabelId18(String id){
		String labelid = "";
		for(int i=1;i<=id.length();i++){
			if(i!=16&&i!=18&&i!=20&&i!=22){
				labelid += id.substring(i-1,i);
			}
		}
		return new BigDecimal(labelid);
	}
	public static String getJinLabelId( long l){
		return JPlabelUtil.buildLabId("2017", l);
	}
	
	public static String getDB18Label(String label_id){
		return label_id.substring(0, 15) + label_id.substring(16, 17) + label_id.substring(18, 19) + label_id.substring(20, 21);
	}
	public static String getBottleId(int _lineFlag) {
		String str = String.format("%04d", GetBottleDays()) + String.format("%02d", _lineFlag)
				+ String.format("%06d", m_index) + String.format("%02d", m_rand.nextInt(100));
		m_index++;
		if (m_index > 0xf423f) {
			m_index = 0;
		}
		return str;
	}

	public static int GetBottleDays() {
		int num2 = c.get(Calendar.YEAR) - 0x7da;
		num2 = num2 % 0x1a;
		return (c.get(Calendar.DAY_OF_YEAR) + (0x16e * num2));
	}

	/**
	 * 获取托盘号
	 * 
	 * @param _lineFlag
	 * @return
	 */
	public static String getPalletCode(int _lineFlag) {
		return (String.format("%04d", GetDays()) + String.format("%02d", _lineFlag)
				+ String.valueOf(c.get(Calendar.HOUR_OF_DAY)) + String.valueOf(c.get(Calendar.MINUTE))
				+ String.format("%03d", GetIndex()));
	}

	// 箱码：日期加密4位（GetDays方法）+ 产线号两位 + 时分4位 + 顺序号两位

	public static String getBoxCode(int _lineFlag) {
//		System.out.println(" "+String.valueOf(c.get(Calendar.HOUR_OF_DAY)));
//		System.out.println(" "+String.valueOf(c.get(Calendar.MINUTE)));
		
		return (String.format("%04d", GetDays()) + String.format("%02d", _lineFlag)
				+ String.valueOf(c.get(Calendar.HOUR_OF_DAY)) + String.valueOf(c.get(Calendar.MINUTE))
				+ String.format("%02d", GetIndex()));
	}

	private static int GetDays() {
		// int num2 = c.get(Calendar.YEAR) - 0x7da;
		int num2 = c.get(Calendar.YEAR) - 0x7da;

		num2 = num2 % 0x1a;
		return (c.get(Calendar.DAY_OF_YEAR) + (0x16e * num2));

	}

	private static int GetIndex() {
		m_index++;
		if (m_index > 0x63) {
			m_index = 0;
		}
		return m_index;
	}

}
