package com.yqzx.common.util; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; /** * @Description: Code生成工具类 * @Author: liujun * @Date: 2019-08-06 10:38 **/ public class CodeUtil { /** * 自定义code++ * * @param maxCode * 本级最大code * @param parentCode * 父级code * @return */ public static String getCode(String maxCode, String parentCode) { if (StrUtil.isBlank(parentCode)) { parentCode = ""; } if (StrUtil.isBlank(maxCode)) { return parentCode + "001"; } else { maxCode = Convert.toStr(Convert.toLong(maxCode) + 1); for (int i = 0; i < maxCode.length() % 3; i++) { maxCode = "0" + maxCode; } return parentCode + maxCode; } } public static String getCode2(String maxCode, String parentCode) { if (StrUtil.isBlank(parentCode)) { parentCode = ""; } if (StrUtil.isBlank(maxCode)) { return parentCode + "001"; } else { maxCode = Convert.toStr(Convert.toLong(maxCode) + 1); return maxCode; } } /** * 自定义类目code++ * * @param maxCode * 本级最大code * @param parentCode * 父级code * @return */ public static String getCategoryCode(String maxCode, String parentCode, String name) { String startCode = "01"; if (StrUtil.isBlank(parentCode)) { parentCode = ""; startCode = "10"; } if ("其他".equals(name)) { return parentCode + "99"; } if (StrUtil.isBlank(maxCode)) { return parentCode + startCode; } else { maxCode = Convert.toStr(Convert.toInt(maxCode) + 1); for (int i = 0; i < maxCode.length() % 2; i++) { maxCode = "0" + maxCode; } return maxCode; } } /** * 生成日期格式的编号 * * @return */ public static String getTimeCode() { return Convert.toStr(System.currentTimeMillis()); } /** * @Description: 根据前缀+时间戳 * @Date: 2019-08-23 11:35 * @Author: Lx */ public static String getPrefixCode(String prefix){ return prefix + System.currentTimeMillis(); } /** * @Description: 生成短信验证码 * @Date: 2019-08-31 10:21 * @Author: Lx * @param length 需要生成的验证码长度,不传默认4位 */ public static String getSmsCheckCode(Integer length) { if (length == null) { length = 4; } String str = "0123456789"; StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { char ch = str.charAt(Integer.parseInt(RandomUtil.randomNumbers(1))); sb.append(ch); } return sb.toString(); } }