Blame view

src/main/java/com/huaheng/pc/tool/gen/util/GenUtils.java 8.04 KB
tangying authored
1
2
package com.huaheng.pc.tool.gen.util;
mahuandong authored
3
4
5
6
import java.time.LocalDateTime;
import java.util.Arrays;

import com.huaheng.common.constant.GenConstants;
tangying authored
7
8
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.config.GenConfig;
mahuandong authored
9
10
11
12
import com.huaheng.pc.tool.gen.domain.GenTable;
import com.huaheng.pc.tool.gen.domain.GenTableColumn;
import org.apache.commons.lang3.RegExUtils;
tangying authored
13
14
15

/**
 * 代码生成器 工具类
mahuandong authored
16
17
 *
 * @author ruoyi
tangying authored
18
 */
mahuandong authored
19
20
21
22
23
24
25
26
27
28
29
30
31
public class GenUtils {
    /**
     * 初始化表信息
     */
    public static void initTable(GenTable genTable, String operName) {
        genTable.setClassName(convertClassName(genTable.getTableName()));
        genTable.setPackageName(GenConfig.getPackageName());
        genTable.setModuleName(getModuleName(GenConfig.getPackageName()));
        genTable.setBusinessName(getBusinessName(genTable.getTableName()));
        genTable.setFunctionName(replaceText(genTable.getTableComment()));
        genTable.setFunctionAuthor(GenConfig.getAuthor());
        genTable.setCreateBy(operName);
    }
tangying authored
32
mahuandong authored
33
34
35
36
37
38
39
40
41
42
    /**
     * 初始化列属性字段
     */
    public static void initColumnField(GenTableColumn column, GenTable table) {
        String dataType = getDbType(column.getColumnType());
        String columnName = column.getColumnName();
        column.setTableId(table.getTableId());
        column.setCreateBy(table.getCreateBy());
        // 设置java字段名
        column.setJavaField(StringUtils.toCamelCase(columnName));
43
mahuandong authored
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
        if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType)) {
            column.setJavaType(GenConstants.TYPE_STRING);
            // 字符串长度超过500设置为文本域
            Integer columnLength = getColumnLength(column.getColumnType());
            String htmlType = columnLength >= 500 ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT;
            column.setHtmlType(htmlType);
        } else if (arraysContains(GenConstants.COLUMNTYPE_DATETIME, dataType)) {
            column.setJavaType(GenConstants.TYPE_DATETIME);
            column.setHtmlType(GenConstants.HTML_DATETIME);
        }else if (arraysContains(GenConstants.COLUMNTYPE_DATE, dataType)) {
            column.setJavaType(GenConstants.TYPE_DATE);
            column.setHtmlType(GenConstants.HTML_DATETIME);
        }else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) {
            column.setJavaType(GenConstants.TYPE_TIME);
            column.setHtmlType(GenConstants.HTML_DATETIME);
        }else if (arraysContains(GenConstants.COLUMNTYPE_BIT, dataType)) {
            column.setJavaType(GenConstants.TYPE_INTEGER);
            column.setHtmlType(GenConstants.HTML_SELECT);
        } else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) {
            column.setHtmlType(GenConstants.HTML_INPUT);

            // 如果是浮点型
            String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ",");
            if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
                column.setJavaType(GenConstants.TYPE_BIGDECIMAL);
            }
            // 如果是整形
            else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
                column.setJavaType(GenConstants.TYPE_INTEGER);
            }
            // 长整形
            else {
                column.setJavaType(GenConstants.TYPE_LONG);
            }
        }
tangying authored
79
mahuandong authored
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
        // 插入字段(默认所有字段都需要插入)
        column.setIsInsert(GenConstants.REQUIRE);

        // 编辑字段
        if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) {
            column.setIsEdit(GenConstants.REQUIRE);
        }
        // 列表字段
        if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) {
            column.setIsList(GenConstants.REQUIRE);
        }
        // 查询字段
        if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) {
            column.setIsQuery(GenConstants.REQUIRE);
        }

        // 查询字段类型
        if (StringUtils.endsWithIgnoreCase(columnName, "name")) {
            column.setQueryType(GenConstants.QUERY_LIKE);
        }
        // 状态字段设置单选框
        if (StringUtils.endsWithIgnoreCase(columnName, "status")) {
            column.setHtmlType(GenConstants.HTML_RADIO);
        }
        // 类型&性别字段设置下拉框
        else if (StringUtils.endsWithIgnoreCase(columnName, "type")
                || StringUtils.endsWithIgnoreCase(columnName, "sex")) {
            column.setHtmlType(GenConstants.HTML_SELECT);
        }
    }
tangying authored
110
111

    /**
mahuandong authored
112
113
114
115
116
     * 校验数组是否包含指定值
     *
     * @param arr         数组
     * @param targetValue 
     * @return 是否包含
tangying authored
117
     */
mahuandong authored
118
119
    public static boolean arraysContains(String[] arr, String targetValue) {
        return Arrays.asList(arr).contains(targetValue);
tangying authored
120
121
122
    }

    /**
mahuandong authored
123
124
125
126
     * 获取模块名
     *
     * @param packageName 包名
     * @return 模块名
tangying authored
127
     */
mahuandong authored
128
129
130
131
132
    public static String getModuleName(String packageName) {
        int lastIndex = packageName.lastIndexOf(".");
        int nameLength = packageName.length();
        String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
        return moduleName;
tangying authored
133
134
135
    }

    /**
mahuandong authored
136
137
138
139
     * 获取业务名
     *
     * @param tableName 表名
     * @return 业务名
tangying authored
140
     */
mahuandong authored
141
142
143
144
145
    public static String getBusinessName(String tableName) {
        int lastIndex = tableName.lastIndexOf("_");
        int nameLength = tableName.length();
        String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength);
        return businessName;
tangying authored
146
147
148
149
    }

    /**
     * 表名转换成Java类名
mahuandong authored
150
151
152
     *
     * @param tableName 表名称
     * @return 类名
tangying authored
153
     */
mahuandong authored
154
155
156
157
158
159
    public static String convertClassName(String tableName) {
        boolean autoRemovePre = GenConfig.getAutoRemovePre();
        String tablePrefix = GenConfig.getTablePrefix();
        if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) {
            String[] searchList = StringUtils.split(tablePrefix, ",");
            tableName = replaceFirst(tableName, searchList);
tangying authored
160
161
162
163
164
        }
        return StringUtils.convertToCamelCase(tableName);
    }

    /**
mahuandong authored
165
166
167
168
169
     * 批量替换前缀
     *
     * @param replacementm 替换值
     * @param searchList   替换列表
     * @return
tangying authored
170
     */
mahuandong authored
171
172
173
174
175
176
177
    public static String replaceFirst(String replacementm, String[] searchList) {
        String text = replacementm;
        for (String searchString : searchList) {
            if (replacementm.startsWith(searchString)) {
                text = replacementm.replaceFirst(searchString, "");
                break;
            }
tangying authored
178
        }
mahuandong authored
179
180
        return text;
    }
tangying authored
181
mahuandong authored
182
183
184
185
186
187
188
189
    /**
     * 关键字替换
     *
     * @param text 需要被替换的名字
     * @return 替换后的名字
     */
    public static String replaceText(String text) {
        return RegExUtils.replaceAll(text, "(?:表|若依)", "");
tangying authored
190
191
192
    }

    /**
mahuandong authored
193
194
195
196
     * 获取数据库类型字段
     *
     * @param columnType 列类型
     * @return 截取后的列类型
tangying authored
197
     */
mahuandong authored
198
199
200
201
202
203
    public static String getDbType(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            return StringUtils.substringBefore(columnType, "(");
        } else {
            return columnType;
        }
tangying authored
204
205
    }
mahuandong authored
206
207
208
209
210
211
212
213
214
215
216
217
218
    /**
     * 获取字段长度
     *
     * @param columnType 列类型
     * @return 截取后的列类型
     */
    public static Integer getColumnLength(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            String length = StringUtils.substringBetween(columnType, "(", ")");
            return Integer.valueOf(length);
        } else {
            return 0;
        }
tangying authored
219
220
    }
mahuandong authored
221
222
223
224
225
226
227
228
229
230
231
232
    /**
     * 获取空数组列表
     *
     * @param length 长度
     * @return 数组信息
     */
    public static String[] emptyList(int length) {
        String[] values = new String[length];
        for (int i = 0; i < length; i++) {
            values[i] = StringUtils.EMPTY;
        }
        return values;
tangying authored
233
    }
234
}