干燥机配套车间生产管理系统/云平台服务端
baoshiwei
2023-03-10 58d42ccf875b120f40fddce63752298e916e0b0b
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.jeecg.common.util;
 
import io.netty.util.internal.StringUtil;
 
/**
 * 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
 * A001
 * A001A002
 * @Author zhangdaihao
 *
 */
public class YouBianCodeUtil {
 
    // 数字位数(默认生成3位的数字)
 
    /**代表数字位数*/
    private static final int NUM_LENGTH = 2;
 
    public static final int ZHANWEI_LENGTH = 1+ NUM_LENGTH;
 
    public static final char LETTER= 'Z';
 
    /**
     * 根据前一个code,获取同级下一个code
     * 例如:当前最大code为D01A04,下一个code为:D01A05
     * 
     * @param code
     * @return
     */
    public static synchronized String getNextYouBianCode(String code) {
        String newcode = "";
        if (oConvertUtils.isEmpty(code)) {
            String zimu = "A";
            String num = getStrNum(1);
            newcode = zimu + num;
        } else {
            String beforeCode = code.substring(0, code.length() - 1- NUM_LENGTH);
            String afterCode = code.substring(code.length() - 1 - NUM_LENGTH,code.length());
            char afterCodeZimu = afterCode.substring(0, 1).charAt(0);
            Integer afterCodeNum = Integer.parseInt(afterCode.substring(1));
//            org.jeecgframework.core.util.LogUtil.info(after_code);
//            org.jeecgframework.core.util.LogUtil.info(after_code_zimu);
//            org.jeecgframework.core.util.LogUtil.info(after_code_num);
 
            String nextNum = "";
            char nextZimu = 'A';
            // 先判断数字等于999*,则计数从1重新开始,递增
            if (afterCodeNum == getMaxNumByLength(NUM_LENGTH)) {
                nextNum = getNextStrNum(0);
            } else {
                nextNum = getNextStrNum(afterCodeNum);
            }
            // 先判断数字等于999*,则字母从A重新开始,递增
            if(afterCodeNum == getMaxNumByLength(NUM_LENGTH)) {
                nextZimu = getNextZiMu(afterCodeZimu);
            }else{
                nextZimu = afterCodeZimu;
            }
 
            // 例如Z99,下一个code就是Z99A01
            if (LETTER == afterCodeZimu && getMaxNumByLength(NUM_LENGTH) == afterCodeNum) {
                newcode = code + (nextZimu + nextNum);
            } else {
                newcode = beforeCode + (nextZimu + nextNum);
            }
        }
        return newcode;
 
    }
 
    /**
     * 根据父亲code,获取下级的下一个code
     * 
     * 例如:父亲CODE:A01
     *       当前CODE:A01B03
     *       获取的code:A01B04
     *       
     * @param parentCode   上级code
     * @param localCode    同级code
     * @return
     */
    public static synchronized String getSubYouBianCode(String parentCode,String localCode) {
        if(localCode!=null && localCode!=""){
 
//            return parentCode + getNextYouBianCode(localCode);
            return getNextYouBianCode(localCode);
 
        }else{
            parentCode = parentCode + "A"+ getNextStrNum(0);
        }
        return parentCode;
    }
 
    
 
    /**
     * 将数字前面位数补零
     * 
     * @param num
     * @return
     */
    private static String getNextStrNum(int num) {
        return getStrNum(getNextNum(num));
    }
 
    /**
     * 将数字前面位数补零
     * 
     * @param num
     * @return
     */
    private static String getStrNum(int num) {
        String s = String.format("%0" + NUM_LENGTH + "d", num);
        return s;
    }
 
    /**
     * 递增获取下个数字
     * 
     * @param num
     * @return
     */
    private static int getNextNum(int num) {
        num++;
        return num;
    }
 
    /**
     * 递增获取下个字母
     * 
     * @param num
     * @return
     */
    private static char getNextZiMu(char zimu) {
        if (zimu == LETTER) {
            return 'A';
        }
        zimu++;
        return zimu;
    }
    
    /**
     * 根据数字位数获取最大值
     * @param length
     * @return
     */
    private static int getMaxNumByLength(int length){
        if(length==0){
            return 0;
        }
        StringBuilder maxNum = new StringBuilder();
        for (int i=0;i<length;i++){
            maxNum.append("9");
        }
        return Integer.parseInt(maxNum.toString());
    }
    public static String[] cutYouBianCode(String code){
        if(code==null || StringUtil.isNullOrEmpty(code)){
            return null;
        }else{
            //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
            int c = code.length()/(NUM_LENGTH +1);
            String[] cutcode = new String[c];
            for(int i =0 ; i <c;i++){
                cutcode[i] = code.substring(0,(i+1)*(NUM_LENGTH +1));
            }
            return cutcode;
        }
        
    }
//    public static void main(String[] args) {
//        // org.jeecgframework.core.util.LogUtil.info(getNextZiMu('C'));
//        // org.jeecgframework.core.util.LogUtil.info(getNextNum(8));
//        // org.jeecgframework.core.util.LogUtil.info(cutYouBianCode("C99A01B01")[2]);
//    }
}