diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamicform/DynamicFormController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java similarity index 90% rename from cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamicform/DynamicFormController.java rename to cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java index e2a14d8..2351196 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamicform/DynamicFormController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java @@ -1,17 +1,16 @@ -package com.cm.common.plugin.controller.apis.dynamicform; +package com.cm.common.plugin.controller.apis.dynamic.form; import com.cm.common.annotation.CheckRequestBodyAnnotation; import com.cm.common.base.AbstractController; import com.cm.common.constants.ISystemConstant; import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormVO; -import com.cm.common.plugin.service.dynamicform.IDynamicFormService; +import com.cm.common.plugin.service.dynamic.IDynamicFormService; import com.cm.common.result.ErrorResult; import com.cm.common.result.SuccessResult; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/form/FormController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/form/FormController.java deleted file mode 100644 index fc0d471..0000000 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/form/FormController.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cm.common.plugin.controller.apis.form; - -import com.cm.common.base.AbstractController; -import com.cm.common.constants.ISystemConstant; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @ClassName: FormController - * @Description: 表单 - * @Author: WangGeng - * @Date: 2019-07-14 18:15 - * @Version: 1.0 - **/ -@RestController -@RequestMapping(ISystemConstant.API_PREFIX + "/form") -public class FormController extends AbstractController { - - - -} \ No newline at end of file diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamicform/IDynamicFormDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java similarity index 94% rename from cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamicform/IDynamicFormDao.java rename to cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java index b67f9f4..fea4e7c 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamicform/IDynamicFormDao.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java @@ -1,4 +1,4 @@ -package com.cm.common.plugin.dao.dynamicform; +package com.cm.common.plugin.dao.dynamic; import com.cm.common.exception.SaveException; import com.cm.common.exception.UpdateException; diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java new file mode 100644 index 0000000..528a44b --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java @@ -0,0 +1,38 @@ +package com.cm.common.plugin.dao.dynamic; + +import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; +import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import org.springframework.stereotype.Repository; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDynamicTableDao + * @Description: 动态表格 + * @Author: WangGeng + * @Date: 2019/12/3 11:01 下午 + * @Version: 1.0 + **/ +@Repository +public interface IDynamicTableDao { + /** + * 建表 + * + * @param createTableSQL + * @throws UpdateException + */ + void createTable(String createTableSQL) throws UpdateException; + + /** + * 通过表名获取表 + * + * @param tableName + * @return + * @throws SearchException + */ + TableDTO getTableByTableName(String tableName) throws SearchException; +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/IDynamicFormService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java similarity index 93% rename from cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/IDynamicFormService.java rename to cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java index 5ef60a1..856a530 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/IDynamicFormService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java @@ -1,4 +1,4 @@ -package com.cm.common.plugin.service.dynamicform; +package com.cm.common.plugin.service.dynamic; import com.cm.common.exception.SaveException; import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormVO; diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java new file mode 100644 index 0000000..83814b6 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java @@ -0,0 +1,68 @@ +package com.cm.common.plugin.service.dynamic; + +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; +import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; +import com.cm.common.plugin.pojo.vos.database.table.TableVO; + +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDynamicTableService + * @Description: 动态表格 + * @Author: WangGeng + * @Date: 2019/12/3 10:54 下午 + * @Version: 1.0 + **/ +public interface IDynamicTableService { + + /** + * 建表 + * + * @param tableVO + * @throws SaveException + */ + void createTable(TableVO tableVO) throws SaveException; + + /** + * 加列 + * + * @param tableName + * @param tableColumnVO + * @throws SaveException + */ + void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException; + + /** + * 修改表列类型 + * + * @param tableName + * @param tableColumnVO + */ + void updateTableColumnType(String tableName, TableColumnVO tableColumnVO); + + /** + * 通过表名获取表 + * + * @param tableName + * @return + * @throws SearchException + */ + TableDTO getTableByTableName(String tableName) throws SearchException; + + /** + * 表详情 + * + * @param params + * @return + * @throws SearchException + */ + List listTableColumns(Map params) throws SearchException; + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/impl/DynamicFormServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java similarity index 92% rename from cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/impl/DynamicFormServiceImpl.java rename to cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java index f194e08..54dc604 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/impl/DynamicFormServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java @@ -1,7 +1,7 @@ -package com.cm.common.plugin.service.dynamicform.impl; +package com.cm.common.plugin.service.dynamic.impl; import com.cm.common.base.AbstractService; -import com.cm.common.plugin.dao.dynamicform.IDynamicFormDao; +import com.cm.common.plugin.dao.dynamic.IDynamicFormDao; import com.cm.common.plugin.enums.database.table.ColumnDataTypeEnum; import com.cm.common.plugin.enums.dynamicform.FieldTypeEnum; import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; @@ -11,9 +11,8 @@ import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; import com.cm.common.plugin.pojo.vos.database.table.TableVO; import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormFieldVO; import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormVO; -import com.cm.common.plugin.service.database.IDatabaseService; -import com.cm.common.plugin.service.database.table.ITableService; -import com.cm.common.plugin.service.dynamicform.IDynamicFormService; +import com.cm.common.plugin.service.dynamic.IDynamicFormService; +import com.cm.common.plugin.service.dynamic.IDynamicTableService; import com.cm.common.result.SuccessResult; import com.cm.common.utils.HashMapUtil; import com.cm.common.utils.UUIDUtil; @@ -42,15 +41,12 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF @Autowired private IDynamicFormDao dynamicFormDao; @Autowired - private IDatabaseService databaseService; - @Autowired - private ITableService tableService; + private IDynamicTableService dynamicTableService; @Override public SuccessResult saveDynamicForm(DynamicFormVO dynamicFormVO) throws Exception { LOG.debug("添加字段"); - databaseService.openConnection(HashMapUtil.beanToMap(getDataBase())); - TableDTO tableDTO = tableService.getTableByTableName(dynamicFormVO.getTableName()); + TableDTO tableDTO = dynamicTableService.getTableByTableName(dynamicFormVO.getTableName()); if (tableDTO == null) { LOG.debug("表不存在,新增表和字段信息"); saveDynamicFormInfo(dynamicFormVO); @@ -58,7 +54,6 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF LOG.debug("表存在,更新表字段信息"); updateDynamicFormInfo(tableDTO.getTableName(), dynamicFormVO); } - databaseService.removeConnection(null); return new SuccessResult(); } @@ -79,7 +74,7 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF TableVO tableVO = new TableVO(); tableVO.setTableName(dynamicFormVO.getTableName()); tableVO.setTableColumns(tableColumns); - tableService.createTable(tableVO); + dynamicTableService.createTable(tableVO); LOG.debug("新增字段信息"); for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormVO.getDynamicFormFields()) { params = HashMapUtil.beanToMap(dynamicFormFieldVO); @@ -96,7 +91,7 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF List dynamicFormFields = dynamicFormVO.getDynamicFormFields(); Map params = getHashMap(1); params.put("tableName", tableName); - List tableColumnDTOs = tableService.listTableColumns(params); + List tableColumnDTOs = dynamicTableService.listTableColumns(params); List saveDynamicFormField = new ArrayList<>(0); List updateDynamicFormField = new ArrayList<>(0); for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormFields) { @@ -110,12 +105,12 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF LOG.debug("新增表字段"); for (DynamicFormFieldVO dynamicFormFieldVO : saveDynamicFormField) { TableColumnVO tableColumnVO = getTableColumn(dynamicFormFieldVO); - tableService.saveTableColumn(tableName, tableColumnVO); + dynamicTableService.saveTableColumn(tableName, tableColumnVO); } LOG.debug("更新表字段"); for (DynamicFormFieldVO dynamicFormFieldVO : updateDynamicFormField) { TableColumnVO tableColumnVO = getTableColumn(dynamicFormFieldVO); - tableService.updateTableColumnType(tableName, tableColumnVO); + dynamicTableService.updateTableColumnType(tableName, tableColumnVO); } LOG.debug("新增表字段信息"); for (DynamicFormFieldVO dynamicFormFieldVO : saveDynamicFormField) { diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java new file mode 100644 index 0000000..5edb70d --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java @@ -0,0 +1,128 @@ +package com.cm.common.plugin.service.dynamic.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.dao.dynamic.IDynamicTableDao; +import com.cm.common.plugin.enums.database.table.ColumnDataTypeEnum; +import com.cm.common.plugin.enums.database.table.ColumnIsNullableEnum; +import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO; +import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO; +import com.cm.common.plugin.pojo.vos.database.table.TableVO; +import com.cm.common.plugin.service.dynamic.IDynamicTableService; +import com.cm.common.utils.WStringUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicTableServiceImpl + * @Description: 动态表格 + * @Author: WangGeng + * @Date: 2019/12/3 10:55 下午 + * @Version: 1.0 + **/ +@Service +public class DynamicTableServiceImpl extends AbstractService implements IDynamicTableService { + + @Autowired + private IDynamicTableDao dynamicTableDao; + + @Override + public void createTable(TableVO tableVO) throws SaveException { + dynamicTableDao.createTable(getSaveTableSQL(tableVO.getTableName(), tableVO.getTableColumns())); + } + + @Override + public void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException { + + } + + @Override + public void updateTableColumnType(String tableName, TableColumnVO tableColumnVO) { + + } + + @Override + public TableDTO getTableByTableName(String tableName) throws SearchException { + return dynamicTableDao.getTableByTableName(tableName); + } + + @Override + public List listTableColumns(Map params) throws SearchException { + return null; + } + + /** + * 获取建表SQL语句 + * + * @param tableName + * @param tableColumns + * @return + */ + private String getSaveTableSQL(String tableName, List tableColumns) { + String underLineTableName = WStringUtil.lowerUpper2UnderLine(tableName); + StringBuilder sql = new StringBuilder("CREATE TABLE `").append(tableName).append("`("); + sql.append("`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',"); + sql.append(String.format("`%s_id` CHAR(36) NOT NULL COMMENT '主键',", underLineTableName)); + for (TableColumnVO tableColumn : tableColumns) { + sql.append(getSaveColumnSql(tableColumn)).append(","); + } + sql.append("`creator` CHAR(36) DEFAULT NULL,"); + sql.append("`gmt_create` datetime DEFAULT NULL,"); + sql.append("`modifier` CHAR(36) DEFAULT NULL,"); + sql.append("`gmt_modified` datetime DEFAULT NULL,"); + sql.append("`is_delete` int(1) DEFAULT '0',"); + sql.append(String.format("PRIMARY KEY (`id`, `%s_id`)", underLineTableName)); + sql.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + return sql.toString(); + } + + /** + * 新增列的SQL + * + * @param tableColumn + * @return + */ + private String getSaveColumnSql(TableColumnVO tableColumn) { + boolean notNull = true; + StringBuilder columnSql = new StringBuilder(); + if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.VARCHAR.getDataType())) { + columnSql.append(String.format("`%s` VARCHAR(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.INT.getDataType())) { + columnSql.append(String.format("`%s` INT(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.DOUBLE.getDataType())) { + columnSql.append(String.format("`%s` DOUBLE(%d, %d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength(), tableColumn.getColumnFloatLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.BIGINT.getDataType())) { + columnSql.append(String.format("`%s` BIGINT(%d)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.DATE.getDataType())) { + columnSql.append(String.format("`%s` DATE", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.DATETIME.getDataType())) { + columnSql.append(String.format("`%s` DATETIME", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.TEXT.getDataType())) { + columnSql.append(String.format("`%s` TEXT DEFAULT NULL", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + notNull = false; + } else if (StringUtils.equals(tableColumn.getDataType(), ColumnDataTypeEnum.LONGTEXT.getDataType())) { + columnSql.append(String.format("`%s` LONGTEXT DEFAULT NULL", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + notNull = false; + } + if (notNull && StringUtils.equals(tableColumn.getIsNullable(), ColumnIsNullableEnum.YES.getIsNullable())) { + columnSql.append(" NOT NULL"); + } + if (!StringUtils.isEmpty(tableColumn.getColumnDefault())) { + columnSql.append(String.format(" DEFAULT '%s'", tableColumn.getColumnDefault())); + } + if (!StringUtils.isEmpty(tableColumn.getColumnComment())) { + columnSql.append(String.format(" COMMENT '%s'", tableColumn.getColumnComment())); + } + return columnSql.toString(); + } +} diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml similarity index 94% rename from cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml rename to cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml index 78823a1..b610262 100644 --- a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml @@ -1,6 +1,6 @@ - + diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml new file mode 100644 index 0000000..131ff4c --- /dev/null +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + ${_parameter} + + + + + + \ No newline at end of file