duhuizhe
2024-04-17 c389984b5d3e5523e1b22de1fc045dc415261330
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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();
    }
}