package com.steerinfo.util;



import com.steerinfo.meterwork.enums.PrimaryKeyEnum;
import com.steerinfo.meterwork.interfaces.IGetNextId;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName GeneratedNumberUtil
 * @Description: 自动生成主键(长度[20])
 * @Author lishuzhen
 * @Date 2020/09/23
 * @Version V1.0
 */
public class GeneratedNumberUtil {
    public final static SimpleDateFormat sdfall = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    public final static SimpleDateFormat sdfdd = new SimpleDateFormat("yyyy-MM-dd");

    public final static SimpleDateFormat sdfm = new SimpleDateFormat("HH24:mm:ss");
    private final static DecimalFormat df = new DecimalFormat("000000");
    private final static DecimalFormat df5 = new DecimalFormat("00000");

    private final static int noMaxLength = 32;

    private final static int pkMaxLength = 20;

    private final static int randomNumberLength = 6;

    /**尽量一天之内不要重复*/
    private final static Set<String> containerSet = new HashSet<>();
    /******使用线程安全的map**/
    public volatile static Map<PrimaryKeyEnum, String> mapPk = new ConcurrentHashMap<>();
    /**质数可以极低重复几率*/
    private final static Integer setSize = 97;//100内最大的

    /**
     * @MethodName:getPk
     * @Description: 生成主键的方法（注意配合自己的 getNewID(方法的使用)）
     * @Author: yaoxin.
     * @Date: 2020/9/12 16:30
     */
    public static String getPk(PrimaryKeyEnum pke, IGetNextId next){
            if (mapPk.get(pke) == null) {
                mapPk.put(pke, next.getNewID());
            }
            return  getPk(pke);
    }

    public static String getPk(PrimaryKeyEnum pke){
        synchronized (pke) {
            mapPk.put(pke, mapPk.get(pke) + 1);
            return pke.getCode() + sdf.format(new Date()) + df.format(mapPk.get(pke));
        }
    }

    public static String getPk2(PrimaryKeyEnum pke){
        String date = sdf.format(new Date());
        String pk = pke.getCode() + "-" + date + getContainerSet() + getContainerSet();
        if(pk.length()<pkMaxLength) pk = pk+getContainerSet();
        return pk.substring(0, pkMaxLength);
    }

    public static String getNo(PrimaryKeyEnum pke){
        String date = sdfall.format(new Date());
        String pk = pke.getCode() + "-" + date + getContainerSet() + getContainerSet()+ getContainerSet()+ getContainerSet();
        if(pk.length()<noMaxLength) pk = pk+getContainerSet();
        return pk.substring(0, noMaxLength);
    }

    public static String getShipmentsBillNo(String billNo){
        //XG2020-0708-01
        return "XG"+sdfdd.format(new Date())+"-"+billNo;
    }

    public static String getNo(PrimaryKeyEnum pke, int num){
        String date = sdf.format(new Date());
        String strNum = df.format(num);
        String pk = pke.getCode() + "-" + date + getContainerSet() + strNum;
        if(pk.length()>noMaxLength) pk.substring(0, noMaxLength);
        return  pk;
    }

    private static String getContainerSet(){
        if(containerSet.size()==0) creation();
        String str = containerSet.iterator().next();
        containerSet.remove(str);
        creation();
        return str;
    }

    private static void creation(){
        String ran = getRandomNumberString(randomNumberLength);
        containerSet.add(ran);
        if(containerSet.size() < setSize) creation();
    }


    /**
     * @MethodName:getRandomString
     * @Description: 字母与数字
     * @Author: yaoxin.
     * @Date: 2020/5/18 16:24
     */
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
    /**
     * @MethodName:getRandomLetterString
     * @Description: 只有字母
     * @Author: yaoxin.
     * @Date: 2020/5/18 16:24
     */
    public static String getRandomLetterString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * @MethodName:getRandomNumberString
     * @Description: 只有数字
     * @Author: yaoxin.
     * @Date: 2020/5/18 16:24
     */
    public static String getRandomNumberString(int length){
        String str="0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

}
