package com.yida.data.school.transaction.util;


import java.util.Calendar;
import java.util.Random;

public class TransactionNumberUtil {
    public static String getTransactionNumber(Integer schoolId, Integer productCode) {
        int count = 0;
        int a = 0;

        //8位随机数
        Random random = new Random();
        String substring = String.valueOf(random.nextLong()).substring(1, 8 + 1);
        //学校id添0
        StringBuilder transactionNumber = new StringBuilder();
        transactionNumber.append("z");
        StringBuilder school = new StringBuilder();
        StringBuilder product = new StringBuilder();
        while (schoolId != 0) {
            count++;
            schoolId /= 10;
        }
        for (int i = 0; i < (count - 6); i++) {
            school.append("0");
        }
        school.append(schoolId);

        //商品编码添0
        while (productCode != 0) {
            a++;
            productCode /= 10;
        }
        for (int i = 0; i < (a - 6); i++) {
            school.append("0");
        }
        product.append(productCode);

        //购买日期
        Calendar rightNow = Calendar.getInstance();
        Integer year = rightNow.get(Calendar.YEAR);
        Integer month = rightNow.get(Calendar.MONTH) + 1; //第一个月从0开始，所以得到月份＋1
        Integer day = rightNow.get(Calendar.DAY_OF_MONTH);
        String date = year + month + day + "";
        String TransactionNumber = "Z" + school + product + date + substring;

        return TransactionNumber;
    }
}