package com.post;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;

/**
 * TODO MyBatis-Plus的雪花算法工具类
 * MyBatis-Plus也提供了一个雪花算法工具类，想用的可以使用：com.baomidou.mybatisplus.core.toolkit.IdWorker。
 */
public class MybatisPlusIdWorker {

    public static void main(String[] args) {
        // 返回值  1385106677482582018
        System.out.println(IdWorker.getId());
        long id = IdWorker.getId();
        System.out.println(id + ("\t" + "雪花ID"));
        System.out.println(id / 100000 + ("\t" + "雪花ID/100000"));
        long time = System.currentTimeMillis();
        System.out.println(time + ("\t" + "时间戳"));
        System.out.println(time - id / 100000 + ("\t" + "时间戳 - id/100000"));
//        System.out.println(System.currentTimeMillis());
        // 返回值 "1385106677482582019"
        System.out.println(IdWorker.getIdStr());


        Set<String> hasSet = new HashSet<>();
        for (int i = 0; i < 10000000; i++) {
            Long forId = IdWorker.getId();
//            System.out.println("forId = " + forId);
            String subId = forId.toString().substring(4);

            if (hasSet.add(subId)) {
                System.out.println(subId + "\t" + i);
            } else {
                System.out.println(subId + "\t" + i + "\t重复了");
            }

//            long l = Long.parseLong(IdWorker.getIdStr().substring(4));


//            Double doubleId = Double.parseDouble(forId.toString());
//            doubleId = doubleId / 199999;
//
//            DecimalFormat decimalFormat = new DecimalFormat("#");
//            String formatId = decimalFormat.format(doubleId);
//
//            System.out.println("forId(" + i + ") => " + formatId);
        }

    }
}
