package com.qnfhq.utils;

import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component
public class OrderUtils {

    public static synchronized String generateOrderNum() {
        Random random = new Random();
        SimpleDateFormat allTime = new SimpleDateFormat("MMddHHmmSS");
        String subjectno = allTime.format(new Date())+random.nextInt(10);
        return subjectno+random.nextInt(10);
    }

    /**
     * n位随机数
     * @param n
     * @return
     */
    public static String randomNumber(int n){
        String str="0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ";
        StringBuilder sb=new StringBuilder(n);
        for(int i=0;i<n;i++){
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    public static String generateNickname(String prefix,int n){
        String str="0123456789abcdefghiklmnopqrstuvwxyz";
        StringBuilder sb=new StringBuilder();
        sb.append(prefix);
        for(int i=0;i<n;i++){
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * 返回n位随机数
     * @param code
     * @return
     */
    public static String generateActiveCode(int n,Long code){
        StringBuilder sb=new StringBuilder(n);
        String activeCode = "";
        if(code != null){
            activeCode = code.toString();
        }
        if(activeCode.length()<n){
            int shortNum = n-activeCode.length();
            String str="0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ";
            for(int i=0;i<shortNum;i++){
                char ch=str.charAt(new Random().nextInt(str.length()));
                sb.append(ch);
            }
            sb.append(activeCode);
        }
        return sb.toString();
    }


    public static String randomNumberLower(int n){
        String str="0123456789abcdef";
        StringBuilder sb=new StringBuilder(n);
        for(int i=0;i<5;i++){
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    public static synchronized String generateAdNum() {
        Random random = new Random();
        SimpleDateFormat allTime = new SimpleDateFormat("YYYYMMddHHmmSS");
        String subjectno = allTime.format(new Date())+random.nextInt(10);
        return subjectno+random.nextInt(10);
    }

}
