luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
package com.zy.asrs.framework.generators.utils;
 
import com.zy.asrs.framework.common.Cools;
 
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import static java.sql.Types.*;
 
/**
 * Created by vincent on 2019-06-18
 */
public class GeneratorUtils {
 
    public static final List<String> FILTER_FIELD = new ArrayList<String>(){{
        add("deleted");
        add("tenantId");
        add("createBy");
        add("createTime");
        add("updateBy");
        add("updateTime");
    }};
 
    /**
     * 下划线 ===>> 驼峰命名
     * @param smallHump 小驼峰命名
     */
    public static String _convert(String str, boolean smallHump){
        String[] split = str.split("_");
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<split.length;i++){
            sb.append(i==0&&smallHump?split[i]:split[i].substring(0, 1).toUpperCase()+split[i].substring(1));
        }
        return sb.toString();
    }
 
    public static String _convert(String str){
        return _convert(str, true);
    }
 
    // sql类型 ===>> java类型
    public static String getType(int type){
        switch (type){
            case BIT:
                return "Boolean";
            case TINYINT:
                return "Short";
            case SMALLINT:
                return "Short";
            case INTEGER:
                return "Integer";
            case BIGINT:
                return "Long";
            case DOUBLE:
                return "Double";
            case DECIMAL:
                return "Double";
            case NCHAR:
                return "String";
            case NVARCHAR:
                return "String";
            case CHAR:
                return "String";
            case VARCHAR:
                return "String";
            case DATE:
                return "Date";
            case TIMESTAMP:
                return "Date";
            case BLOB:
                return "String";
            case LONGVARCHAR:
                return "String";
            default:
                return null;
        }
    }
 
    // sql表名 ===>> 去前缀 大驼峰
    public static String getNameSpace(String tableName){
        String[] strings = tableName.split("_");
        StringBuilder sb = new StringBuilder();
        for (int i=1;i<strings.length;i++){
            if (i!=1){
                sb.append("_");
            }
            sb.append(strings[i]);
        }
        return _convert(sb.toString(), false);
    }
 
    // htmlDetail 字符适配
    public static String supportHtmlName(String comment){
        if (Cools.isEmpty(comment)){
            return "";
        }
        if (comment.length() == 2){
            return comment.charAt(0) + "  " + comment.charAt(1);
        } else if (comment.length() == 3){
            return comment.charAt(0) + " " + comment.charAt(1) + " " +comment.charAt(2);
        }
        return comment;
    }
 
    /**
     * 获取mysql表字段长度
     */
    public static Integer getColumnLength(String typeMsg){
        if (Cools.isEmpty(typeMsg)){
            return null;
        }
        Pattern pattern = Pattern.compile("(?<=\\()(.+?)(?=\\))");
        Matcher matcher = pattern.matcher(typeMsg);
        if (matcher.find()){
            String group = matcher.group();
            if (group.contains(",")) {
                group = group.split(",")[0];
            }
            return Integer.parseInt(group);
        }
        return null;
    }
 
    /**
     * 字符串首字母大小写转换
     * @param str 字符串
     * @param low true:小写   /   false:大写
     * @return the result
     */
    public static String firstCharConvert(String str, boolean low){
        if (Cools.isEmpty(str)){
            return "";
        }
        String firstChar = str.substring(0, 1);
        if (low){
            firstChar = firstChar.toLowerCase();
        } else {
            firstChar = firstChar.toUpperCase();
        }
        return firstChar + str.substring(1);
    }
 
    public static String firstCharConvert(String str){
        return firstCharConvert(str, true);
    }
 
    /** 驼峰转下划线 */
    public static String humpToLine(String str) {
        Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
 
}