| | |
| | | package com.ruoyi.generator.util; |
| | | |
| | | import java.util.Arrays; |
| | | import org.apache.commons.lang3.RegExUtils; |
| | | import com.ruoyi.common.constant.GenConstants; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.constant.GenConstants; |
| | | import com.ruoyi.generator.config.GenConfig; |
| | | import com.ruoyi.generator.domain.GenTable; |
| | | import com.ruoyi.generator.domain.GenTableColumn; |
| | | import org.apache.commons.lang3.RegExUtils; |
| | | |
| | | import java.util.Arrays; |
| | | |
| | | /** |
| | | * 代码生成器 工具类 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class GenUtils |
| | |
| | | /** |
| | | * 初始化列属性字段 |
| | | */ |
| | | public static void initColumnField(GenTableColumn column, GenTable table) |
| | | { |
| | | 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)); |
| | | // 设置默认类型 |
| | | column.setJavaType(GenConstants.TYPE_STRING); |
| | | |
| | | if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType)) |
| | | { |
| | | column.setJavaType(GenConstants.TYPE_STRING); |
| | | if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) { |
| | | // 字符串长度超过500设置为文本域 |
| | | Integer columnLength = getColumnLength(column.getColumnType()); |
| | | String htmlType = columnLength >= 500 ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT; |
| | | String htmlType = columnLength >= 500 || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType) ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT; |
| | | column.setHtmlType(htmlType); |
| | | } |
| | | else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) |
| | | { |
| | | } else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) { |
| | | column.setJavaType(GenConstants.TYPE_DATE); |
| | | column.setHtmlType(GenConstants.HTML_DATETIME); |
| | | } |
| | | else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) |
| | | { |
| | | } else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) { |
| | | column.setHtmlType(GenConstants.HTML_INPUT); |
| | | |
| | | // 如果是浮点型 统一用BigDecimal |
| | | String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ","); |
| | | if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) |
| | | { |
| | | 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) |
| | | { |
| | | else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) { |
| | | column.setJavaType(GenConstants.TYPE_INTEGER); |
| | | } |
| | | // 长整形 |
| | | else |
| | | { |
| | | else { |
| | | column.setJavaType(GenConstants.TYPE_LONG); |
| | | } |
| | | } |
| | | |
| | | // 插入字段(默认所有字段都需要插入) |
| | | column.setIsInsert(GenConstants.REQUIRE); |
| | | |
| | | // 编辑字段 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) |
| | | // BO对象 默认插入勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_ADD, columnName) && !column.isPk()) { |
| | | column.setIsInsert(GenConstants.REQUIRE); |
| | | } |
| | | // BO对象 默认编辑勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) |
| | | { |
| | | column.setIsEdit(GenConstants.REQUIRE); |
| | | } |
| | | // 列表字段 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) |
| | | // BO对象 默认是否必填勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName)) |
| | | { |
| | | column.setIsRequired(GenConstants.REQUIRE); |
| | | } |
| | | // VO对象 默认返回勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName)) |
| | | { |
| | | column.setIsList(GenConstants.REQUIRE); |
| | | } |
| | | // 查询字段 |
| | | // BO对象 默认查询勾选 |
| | | if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) |
| | | { |
| | | column.setIsQuery(GenConstants.REQUIRE); |
| | |
| | | { |
| | | column.setHtmlType(GenConstants.HTML_SELECT); |
| | | } |
| | | // 图片字段设置图片上传控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "image")) |
| | | { |
| | | column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD); |
| | | } |
| | | // 文件字段设置文件上传控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "file")) |
| | | { |
| | | column.setHtmlType(GenConstants.HTML_FILE_UPLOAD); |
| | | } |
| | | // 内容字段设置富文本控件 |
| | | else if (StringUtils.endsWithIgnoreCase(columnName, "content")) |
| | | { |
| | | column.setHtmlType(GenConstants.HTML_EDITOR); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验数组是否包含指定值 |
| | | * |
| | | * |
| | | * @param arr 数组 |
| | | * @param targetValue 值 |
| | | * @return 是否包含 |
| | |
| | | |
| | | /** |
| | | * 获取模块名 |
| | | * |
| | | * |
| | | * @param packageName 包名 |
| | | * @return 模块名 |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 获取业务名 |
| | | * |
| | | * |
| | | * @param tableName 表名 |
| | | * @return 业务名 |
| | | */ |
| | | public static String getBusinessName(String tableName) |
| | | { |
| | | int lastIndex = tableName.indexOf("_"); |
| | | int firstIndex = tableName.indexOf("_"); |
| | | int nameLength = tableName.length(); |
| | | String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength); |
| | | return StringUtils.toCamelCase(businessName); |
| | | String businessName = StringUtils.substring(tableName, firstIndex + 1, nameLength); |
| | | businessName = StringUtils.toCamelCase(businessName); |
| | | return businessName; |
| | | } |
| | | |
| | | /** |
| | | * 表名转换成Java类名 |
| | | * |
| | | * |
| | | * @param tableName 表名称 |
| | | * @return 类名 |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 批量替换前缀 |
| | | * |
| | | * |
| | | * @param replacementm 替换值 |
| | | * @param searchList 替换列表 |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 关键字替换 |
| | | * |
| | | * @param name 需要被替换的名字 |
| | | * |
| | | * @param text 需要被替换的名字 |
| | | * @return 替换后的名字 |
| | | */ |
| | | public static String replaceText(String text) |
| | |
| | | |
| | | /** |
| | | * 获取数据库类型字段 |
| | | * |
| | | * |
| | | * @param columnType 列类型 |
| | | * @return 截取后的列类型 |
| | | */ |
| | | public static String getDbType(String columnType) |
| | | { |
| | | if (StringUtils.indexOf(columnType, "(") > 0) |
| | | if (StringUtils.indexOf(columnType, '(') > 0) |
| | | { |
| | | return StringUtils.substringBefore(columnType, "("); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 获取字段长度 |
| | | * |
| | | * |
| | | * @param columnType 列类型 |
| | | * @return 截取后的列类型 |
| | | */ |
| | | public static Integer getColumnLength(String columnType) |
| | | { |
| | | if (StringUtils.indexOf(columnType, "(") > 0) |
| | | if (StringUtils.indexOf(columnType, '(') > 0) |
| | | { |
| | | String length = StringUtils.substringBetween(columnType, "(", ")"); |
| | | return Integer.valueOf(length); |