| | |
| | | package com.ruoyi.generator.util; |
| | | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.constant.GenConstants; |
| | | import com.ruoyi.common.core.constant.GenConstants; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.generator.config.GenConfig; |
| | | import com.ruoyi.generator.domain.GenTable; |
| | | import com.ruoyi.generator.domain.GenTableColumn; |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.apache.commons.lang3.RegExUtils; |
| | | |
| | | import java.util.Arrays; |
| | |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class GenUtils |
| | | { |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class GenUtils { |
| | | |
| | | /** |
| | | * 初始化表信息 |
| | | */ |
| | | public static void initTable(GenTable genTable, String operName) |
| | | { |
| | | public static void initTable(GenTable genTable, String operName) { |
| | | genTable.setClassName(convertClassName(genTable.getTableName())); |
| | | genTable.setPackageName(GenConfig.getPackageName()); |
| | | genTable.setModuleName(getModuleName(GenConfig.getPackageName())); |
| | |
| | | column.setJavaField(StringUtils.toCamelCase(columnName)); |
| | | // 设置默认类型 |
| | | column.setJavaType(GenConstants.TYPE_STRING); |
| | | column.setQueryType(GenConstants.QUERY_EQ); |
| | | |
| | | if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) { |
| | | // 字符串长度超过500设置为文本域 |
| | |
| | | } |
| | | } |
| | | |
| | | // 插入字段(默认所有字段都需要插入) |
| | | // 主键不需要添加 |
| | | // BO对象 默认插入勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_ADD, columnName) && !column.isPk()) { |
| | | column.setIsInsert(GenConstants.REQUIRE); |
| | | } |
| | | // 编辑字段 |
| | | // 编辑需要主键 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) |
| | | { |
| | | // BO对象 默认编辑勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) { |
| | | column.setIsEdit(GenConstants.REQUIRE); |
| | | } |
| | | // 编辑需要的设置必选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) |
| | | { |
| | | // BO对象 默认是否必填勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) { |
| | | column.setIsRequired(GenConstants.REQUIRE); |
| | | } |
| | | // 列表字段 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) |
| | | { |
| | | // VO对象 默认返回勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName)) { |
| | | column.setIsList(GenConstants.REQUIRE); |
| | | } |
| | | // 查询字段 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) |
| | | { |
| | | // BO对象 默认查询勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) { |
| | | column.setIsQuery(GenConstants.REQUIRE); |
| | | } |
| | | |
| | | // 查询字段类型 |
| | | if (StringUtils.endsWithIgnoreCase(columnName, "name")) |
| | | { |
| | | if (StringUtils.endsWithIgnoreCase(columnName, "name")) { |
| | | column.setQueryType(GenConstants.QUERY_LIKE); |
| | | } |
| | | // 状态字段设置单选框 |
| | | if (StringUtils.endsWithIgnoreCase(columnName, "status")) |
| | | { |
| | | if (StringUtils.endsWithIgnoreCase(columnName, "status")) { |
| | | column.setHtmlType(GenConstants.HTML_RADIO); |
| | | } |
| | | // 类型&性别字段设置下拉框 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "type") |
| | | || StringUtils.endsWithIgnoreCase(columnName, "sex")) |
| | | { |
| | | || StringUtils.endsWithIgnoreCase(columnName, "sex")) { |
| | | column.setHtmlType(GenConstants.HTML_SELECT); |
| | | } |
| | | // 图片字段设置图片上传控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "image")) |
| | | { |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "image")) { |
| | | column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD); |
| | | } |
| | | // 文件字段设置文件上传控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "file")) |
| | | { |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "file")) { |
| | | column.setHtmlType(GenConstants.HTML_FILE_UPLOAD); |
| | | } |
| | | // 内容字段设置富文本控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "content")) |
| | | { |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "content")) { |
| | | column.setHtmlType(GenConstants.HTML_EDITOR); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 校验数组是否包含指定值 |
| | | * |
| | | * @param arr 数组 |
| | | * @param arr 数组 |
| | | * @param targetValue 值 |
| | | * @return 是否包含 |
| | | */ |
| | | public static boolean arraysContains(String[] arr, String targetValue) |
| | | { |
| | | public static boolean arraysContains(String[] arr, String targetValue) { |
| | | return Arrays.asList(arr).contains(targetValue); |
| | | } |
| | | |
| | |
| | | * @param packageName 包名 |
| | | * @return 模块名 |
| | | */ |
| | | public static String getModuleName(String packageName) |
| | | { |
| | | public static String getModuleName(String packageName) { |
| | | int lastIndex = packageName.lastIndexOf("."); |
| | | int nameLength = packageName.length(); |
| | | String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength); |
| | | return moduleName; |
| | | return StringUtils.substring(packageName, lastIndex + 1, nameLength); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param tableName 表名 |
| | | * @return 业务名 |
| | | */ |
| | | public static String getBusinessName(String tableName) |
| | | { |
| | | public static String getBusinessName(String tableName) { |
| | | int firstIndex = tableName.indexOf("_"); |
| | | int nameLength = tableName.length(); |
| | | String businessName = StringUtils.substring(tableName, firstIndex + 1, nameLength); |
| | | businessName = StringUtils.toCamelCase(businessName); |
| | | businessName = StringUtils.toCamelCase(businessName); |
| | | return businessName; |
| | | } |
| | | |
| | |
| | | * @param tableName 表名称 |
| | | * @return 类名 |
| | | */ |
| | | public static String convertClassName(String tableName) |
| | | { |
| | | public static String convertClassName(String tableName) { |
| | | boolean autoRemovePre = GenConfig.getAutoRemovePre(); |
| | | String tablePrefix = GenConfig.getTablePrefix(); |
| | | if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) |
| | | { |
| | | if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) { |
| | | String[] searchList = StringUtils.split(tablePrefix, ","); |
| | | tableName = replaceFirst(tableName, searchList); |
| | | } |
| | |
| | | * 批量替换前缀 |
| | | * |
| | | * @param replacementm 替换值 |
| | | * @param searchList 替换列表 |
| | | * @param searchList 替换列表 |
| | | * @return |
| | | */ |
| | | public static String replaceFirst(String replacementm, String[] searchList) |
| | | { |
| | | public static String replaceFirst(String replacementm, String[] searchList) { |
| | | String text = replacementm; |
| | | for (String searchString : searchList) |
| | | { |
| | | if (replacementm.startsWith(searchString)) |
| | | { |
| | | for (String searchString : searchList) { |
| | | if (replacementm.startsWith(searchString)) { |
| | | text = replacementm.replaceFirst(searchString, ""); |
| | | break; |
| | | } |
| | |
| | | * @param text 需要被替换的名字 |
| | | * @return 替换后的名字 |
| | | */ |
| | | public static String replaceText(String text) |
| | | { |
| | | public static String replaceText(String text) { |
| | | return RegExUtils.replaceAll(text, "(?:表|若依)", ""); |
| | | } |
| | | |
| | |
| | | * @param columnType 列类型 |
| | | * @return 截取后的列类型 |
| | | */ |
| | | public static String getDbType(String columnType) |
| | | { |
| | | if (StringUtils.indexOf(columnType, '(') > 0) |
| | | { |
| | | public static String getDbType(String columnType) { |
| | | if (StringUtils.indexOf(columnType, '(') > 0) { |
| | | return StringUtils.substringBefore(columnType, "("); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | return columnType; |
| | | } |
| | | } |
| | |
| | | * @param columnType 列类型 |
| | | * @return 截取后的列类型 |
| | | */ |
| | | public static Integer getColumnLength(String columnType) |
| | | { |
| | | if (StringUtils.indexOf(columnType, '(') > 0) |
| | | { |
| | | public static Integer getColumnLength(String columnType) { |
| | | if (StringUtils.indexOf(columnType, '(') > 0) { |
| | | String length = StringUtils.substringBetween(columnType, "(", ")"); |
| | | return Integer.valueOf(length); |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | return 0; |
| | | } |
| | | } |