package com.neusoft.smart.pos.framework.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;

public class IdGenerateUtils {

    public static final String RANDOM_FOR_NUMBER = "0123456789";
    public static final String RANDOM_FOR_LETTER_NUMBER = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String RANDOM_FOR_LETTER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static final String MCODE_PREFIX = "NEU";    //商户号生成前缀
    public static final String STATIC_SIGN_KEY = "NEUPOS";

    /**
     * 生成交易tradeId
     * 生成规则:当前时间毫秒数+5位随机数
     * @return
     */
    public static String generateTradeId() {
        String tradeId = (new Date()).getTime()+ randomNumber(5);
        return tradeId;
    }

    /**
     * 生成15位商户号ID
     * 生成规则:NEU+2位随机数+年(yy)+2位随机数+月(mm)+2位随机数+日(dd)
     * @return
     */
    public static String generateMCode() {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyMMdd");
        String localDate = LocalDate.now().format(format);
        StringBuilder mcode = new StringBuilder();
        mcode.append(MCODE_PREFIX);
        mcode.append(localDate.substring(0,2)); //年
        mcode.append(randomNumber(2));
        mcode.append(localDate.substring(2,4)); //月
        mcode.append(randomNumber(2));
        mcode.append(localDate.substring(4,6)); //日
        mcode.append(randomNumber(2));
        return mcode.toString();
    }

    /**
     * 生成15位退款订单号ID
     * 生成规则:年份(4位)+季度(1位)+本季度第几日(2位)+随机数(8位)
     * @return
     */
    public static String generateRefundOrderNumber() {
        StringBuilder orderNumber = new StringBuilder();
        orderNumber.append(LocalDateTimeUtils.getCurrentYear()); //年份(4位)
        orderNumber.append(LocalDateTimeUtils.getCurrentQuarter()); //季度(1位)
        orderNumber.append(String.format("%02d", LocalDateTimeUtils.getPassDayOfQuarter(LocalDateTimeUtils.getNow()))); //本季度第几日(2位)
        orderNumber.append(randomNumber(8));  //随机数(8位)
        return orderNumber.toString();
    }

    /**
     * 生成12位订单号ID
     * 生成规则:年份末位(1位)+季度(1位)+本季度第几日(2位)+随机数(8位)
     * @return
     */
    public static String generateOrderNumber() {
        StringBuilder orderNumber = new StringBuilder();
        orderNumber.append(LocalDateTimeUtils.getCurrentYear().toString().substring(3)); //年份末位(1位)
        orderNumber.append(LocalDateTimeUtils.getCurrentQuarter()); //季度(1位)
        orderNumber.append(String.format("%02d", LocalDateTimeUtils.getPassDayOfQuarter(LocalDateTimeUtils.getNow()))); //本季度第几日(2位)
        orderNumber.append(randomNumber(8));  //随机数(8位)
        return orderNumber.toString();
    }

    /**
     * generate member card number
     * @return
     */
    public static String generateMemberCardNum() {
        String cardNum = LocalDateTimeUtils.getDateTime("yyyyMMdd");
        // TODO: should use serial number instead of random number
        cardNum += randomNumber(8);

        return cardNum;
    }

    /**
     * 获取数字count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String randomNumber(int count){
        return random(count,RANDOM_FOR_NUMBER);
    }

    /**
     * 获取字母及数字count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String randomLetterNumber(int count){
        return random(count,RANDOM_FOR_LETTER_NUMBER);
    }

    /**
     * 获取字母count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String randomLetter(int count){
        return random(count,RANDOM_FOR_LETTER);
    }

    /**
     * 获取count个随机数
     * @param count 随机数个数
     * @return
     */
    public static String random(int count,String value){
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        for(int i=0;i<count;i++){
            int num = r.nextInt(value.length());
            sb.append(value.charAt(num));
        }
        return sb.toString();
    }
}
