package com.storlead.framework.common.util.generate;

import cn.hutool.core.util.StrUtil;
import com.storlead.framework.common.constant.CodeGenerateInterface;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: sp-project-system
 * @description:
 * @author: chenkq
 * @create: 2021-09-11 11:13
 */
public class CodeGenerate {


    public static void main(String[] args) {
        for (int i = 0; i < 200; i++) {
            System.out.println(CodeGenerate.generate10Code(CodeGenerateInterface.ORDER_COST_CODE));
        }
    }

//    public static String Generate8Code(String moduleKey) {
//        String id = IdWorker.nextId().toString();
//        System.out.println(id);
//        String code = StrUtil.sub(id, 8, id.length());
//        System.out.println(code);
//        return moduleKey + code;
//    }
//
//    public static String Generate12Code(String moduleKey) {
//        String id = IdWorker.nextId().toString();
//        System.out.println(id);
//        String code = StrUtil.sub(id, id.length()-12, id.length());
//        System.out.println(code);
//        return moduleKey + code;
//    }

    public static String GenerateFullCode() {
        String code = IdWorker.nextId().toString();
        return code;
    }

    /**
     * 生成模块对于的Code
     *
     * @param moduleKey  模块Key
     * @return encodeStr 生成的唯一的Code
     * @throws Exception
     */
    public static String generate10Code(String moduleKey) {
        String id = IdWorker.nextId().toString();
        String code = StrUtil.sub(id, id.length() - 6, id.length());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyMM");
        String time = sdf1.format(new Date(System.currentTimeMillis()));
        String encodeStr = CodeGenerateInterface.CODE_KEY + moduleKey + time + code;
        return encodeStr;
    }

    public static String generate14Code(String moduleKey) {
        String id = IdWorker.nextId().toString();
        String code = StrUtil.sub(id, id.length() - 10, id.length());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyMM");
        String time = sdf1.format(new Date(System.currentTimeMillis()));
        String encodeStr = CodeGenerateInterface.CODE_KEY + moduleKey + time + code;
        return encodeStr;
    }

    public static String generate12Code(String moduleKey) {
        String id = IdWorker.nextId().toString();
        String code = StrUtil.sub(id, id.length() - 8, id.length());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyMM");
        String time = sdf1.format(new Date(System.currentTimeMillis()));
        String encodeStr = CodeGenerateInterface.CODE_KEY + moduleKey + time + code;
        return encodeStr;
    }
}
