package com.ruoyi.business.utils;

import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 订单编码码生成器，生成32位数字编码，
 *
 * @author tony.wu
 * @生成规则 1位单号类型+17位时间戳+14位(用户id加密&随机数)
 *
 */
public class OrderCodeFactory {
    /**
     * 订单类别头
     */
    //private static final String ORDER_CODE = "1";

    /**
     * 随即编码
     */
    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /**
     * 用户id和随机数总长度
     */
    private static final int maxLength = 10;

    /**
     * 更具id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(String id) {
        if(StringUtils.isBlank(id)){
            id = "10000";
        }
        StringBuilder idsbs = new StringBuilder();
        for (int i = id.length() - 1; i >= 0; i--) {
            idsbs.append(r[id.charAt(i) - '0']);
        }
        return idsbs.append(getRandom(maxLength - id.length())).toString();
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 生成固定长度随机码
     *
     * @param n 长度
     */
    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        return (((long) (new SecureRandom().nextDouble() * (max - min)))) + min;
    }

    /**
     * 生成不带类别标头的编码
     *
     * @param userId
     */
    private static synchronized String getCode(String userId) {
        return getDateTime() + toCode(userId);
    }

    public static void main(String[] args) {
        System.out.println(getOrderCode("CK",null));
    }

    /**
     * 生成订单单号编码
     *
     * @param userId
     */
    public static String getOrderCode(String order_code, String userId) {
        if(StringUtils.isNotBlank(order_code)) {
            return order_code + getCode(userId);
        }else {
            return  getCode(userId);
        }
    }

    /**
     * 生成批次号
     * 物料代码(4位)+年(2位)+月(2位)+天(2位)  +序列号(3位，按物料编号、月份来累计)，共13位
     * @param
     */
    public static synchronized String getBarcode(String goodsId,String serialNum) {

        if(StringUtils.isBlank(goodsId) || StringUtils.isBlank(serialNum)){
            throw new NullPointerException("物料或者序列号为空异常");
        }
        if(!StringUtils.isNumeric(serialNum)){
           throw new NumberFormatException("序列号非数字异常");
        }

        return getGoodsId(goodsId)+getYmd()+ getSerialNum(serialNum);
    }

    public static String getGoodsId(String goodsId) {
        int length = goodsId.length();
        String result = StringUtils.EMPTY;
        if(length<4) {
            result = goodsId;
            for (int i = 0; i < 4 - length; i++) {
                result = "0" + result;
            }
        }else {
            result = goodsId.substring(length-4,length);
        }
        return result;
    }

    private static String getYmd(){
        Date date = new Date();
       return new SimpleDateFormat("yyMMdd").format(date);
    }
    private static String getSerialNum(String serialNum){
        String serialNUmString = String.valueOf(Integer.parseInt(serialNum)+1);
        int length = serialNUmString.length();
        String result = serialNUmString;
        for (int i = 0; i < 3 - length; i++) {
            result = "0" + result;
        }
        return result;
    }


    /**
     * 生成退货单号编码
     *
     * @param userId
     */
 /*   public static String getReturnCode(String return_order,String userId) {
        return return_order + getCode(userId);
    }*/

    /**
     * 生成退款单号编码
     *
     * @param userId
     */
  /*  public static String getRefundCode(String refund_order,String userId) {
        return refund_order + getCode(userId);
    }*/

    /**
     * 未付款重新支付
     *
     * @param userId
     */
  /*  public static String getAgainCode(String again_order, String userId) {
        return again_order + getCode(userId);
    }*/

}