package com.fufan.mtsaas.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;

public class CodeUtil {
    private  static final int NUMBER = 6;
    private static final Random random = new Random();
    public static String generate() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
    public static String appendZero(String data){
        StringBuilder stringBuilder = new StringBuilder(data);
        Integer length = data.length();
        for (int i =0;i< 6 - length;i++){
            stringBuilder.append("0");
        }
        return stringBuilder.toString();
    }

//    public static void main(String[] args) {
//        String aa = "313131";
//        System.out.println(aa.substring(0,2));
//        System.out.println(appendZero(aa.substring(0,2)));
//    }
    public static boolean verifyDi(String di){
        try {
            if(StringUtils.isEmpty(di)){
                return false;
            }
//            if(di.length() != 14){
//                return false;
//            }
            int s1 = 0;
            int s2 = 0;
            int x = 0;
            for (int i = di.length() -1;i >=0;i--){
                int k = di.length() - i;
                if(k >= 2 && (k%2==0)){
                    s2 += (int)di.charAt(i) - (int)('0');
                }
                if(k >= 3 && (k % 2==1)){
                    s1 += (int)di.charAt(i) - (int)('0');
                }
            }
            x = (3 * s2 + s1) % 10;
            if((3 * s2 + s1) % 10 == 0){
                x = 0;
            }else {
                x = 10 - x;
            }
            return Integer.parseInt(di.charAt(di.length()-1) + "") == x;
        }catch (Exception e){
            return false;
        }
    }
    //6901234567892  6975908960038
    public static void main(String[] args) {
        System.out.println(verifyDi("16901234567899"));
    }

}
