新增动态表单
This commit is contained in:
parent
427832f81e
commit
3ea7791fda
@ -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;
|
@ -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 {
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
@ -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;
|
||||
}
|
@ -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;
|
@ -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<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException;
|
||||
|
||||
}
|
@ -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<DynamicFormFieldVO> dynamicFormFields = dynamicFormVO.getDynamicFormFields();
|
||||
Map<String, Object> params = getHashMap(1);
|
||||
params.put("tableName", tableName);
|
||||
List<TableColumnDTO> tableColumnDTOs = tableService.listTableColumns(params);
|
||||
List<TableColumnDTO> tableColumnDTOs = dynamicTableService.listTableColumns(params);
|
||||
List<DynamicFormFieldVO> saveDynamicFormField = new ArrayList<>(0);
|
||||
List<DynamicFormFieldVO> 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) {
|
@ -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<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取建表SQL语句
|
||||
*
|
||||
* @param tableName
|
||||
* @param tableColumns
|
||||
* @return
|
||||
*/
|
||||
private String getSaveTableSQL(String tableName, List<TableColumnVO> 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();
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cm.common.plugin.dao.dynamicform.IDynamicFormDao">
|
||||
<mapper namespace="com.cm.common.plugin.dao.dynamic.IDynamicFormDao">
|
||||
|
||||
<!-- 保存动态表单 -->
|
||||
<insert id="saveDynamicForm" parameterType="map">
|
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.cm.common.plugin.dao.dynamic.IDynamicTableDao">
|
||||
|
||||
<resultMap id="tableDTO" type="com.cm.common.plugin.pojo.dtos.database.table.TableDTO">
|
||||
<result column="tableSchema" />
|
||||
<result column="tableName"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 建表 -->
|
||||
<update id="createTable" parameterType="string">
|
||||
${_parameter}
|
||||
</update>
|
||||
|
||||
<!-- 通过表名获取表 -->
|
||||
<select id="getTableByTableName" parameterType="string" resultMap="tableDTO">
|
||||
SELECT
|
||||
TABLE_SCHEMA tableSchema,
|
||||
TABLE_NAME tableName
|
||||
FROM
|
||||
`information_schema`.`TABLES`
|
||||
WHERE
|
||||
`TABLE_SCHEMA` = (SELECT database())
|
||||
AND
|
||||
`TABLE_NAME` = #{_parameter}
|
||||
</select>
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user