package com.youchain.modules.quartz.utils;

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

public class TimeNumberUtils {
    private static int sequence = 0;
    private static int length = 5;

    public static synchronized String getCKCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "CK"+datetime +addLeftZero(s, length);
    }

    public static synchronized String getRKCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "RK"+datetime +addLeftZero(s, length);
    }

    public static synchronized String getRCCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "RC"+datetime +addLeftZero(s, length);
    }

    public static synchronized String getLXTaskCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "LX"+datetime +addLeftZero(s, length);
    }

    public static synchronized String getGTTaskCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "GT"+datetime +addLeftZero(s, length);
    }

    public static synchronized String getKLXTaskCode() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = new SimpleDateFormat("yyMMddHHmmss")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "KLX"+datetime +addLeftZero(s, length);
    }
    public static synchronized String getKmResCode() {
        String datetime = new SimpleDateFormat("yyMMdd")
                .format(new Date());
        String s = Integer.toString(sequence);
        return "KmRes"+datetime;
    }
    /**
     * 左填0
     * @author shijing
     * 2018年6月29日下午1:24:32
     * @param s
     * @param length
     * @return
     */
    public static String addLeftZero(String s, int length) {
        // StringBuilder sb=new StringBuilder();
        int old = s.length();
        if (length > old) {
            char[] c = new char[length];
            char[] x = s.toCharArray();
            if (x.length > length) {
                throw new IllegalArgumentException(
                        "Numeric value is larger than intended length: " + s
                                + " LEN " + length);
            }
            int lim = c.length - x.length;
            for (int i = 0; i < lim; i++) {
                c[i] = '0';
            }
            System.arraycopy(x, 0, c, lim, x.length);
            return new String(c);
        }
        return s.substring(0, length);
    }
}
