From 427832f81e4414e983b97565d357257dfdbbc0d7 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 3 Dec 2019 13:41:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A8=E6=80=81=E8=A1=A8?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apis/database/DatabaseController.java | 2 - .../dynamicform/DynamicFormController.java | 45 ++++ .../dao/database/table/impl/TableDaoImpl.java | 11 +- .../dao/dynamicform/IDynamicFormDao.java | 37 +++ .../database/table/ColumnDataTypeEnum.java | 20 +- .../enums/dynamicform/FieldTypeEnum.java | 40 +++ .../vos/database/table/TableColumnVO.java | 2 +- .../vos/dynamicform/DynamicFormFieldVO.java | 126 +++++++++ .../pojo/vos/dynamicform/DynamicFormVO.java | 55 ++++ .../service/database/IDatabaseService.java | 11 + .../database/impl/DatabaseServiceImpl.java | 8 +- .../service/database/table/ITableService.java | 8 + .../database/table/impl/TableServiceImpl.java | 12 +- .../dynamicform/IDynamicFormService.java | 26 ++ .../impl/DynamicFormServiceImpl.java | 246 ++++++++++++++++++ .../mapper/dynamicform/dynamicform-mapper.xml | 51 ++++ 16 files changed, 689 insertions(+), 11 deletions(-) create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamicform/DynamicFormController.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamicform/IDynamicFormDao.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamicform/FieldTypeEnum.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormVO.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/IDynamicFormService.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/impl/DynamicFormServiceImpl.java create mode 100644 cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/DatabaseController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/DatabaseController.java index eacd851..018b792 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/DatabaseController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/database/DatabaseController.java @@ -83,6 +83,4 @@ public class DatabaseController extends AbstractController { return databaseService.removeConnection(params); } - - } 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/dynamicform/DynamicFormController.java new file mode 100644 index 0000000..e2a14d8 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamicform/DynamicFormController.java @@ -0,0 +1,45 @@ +package com.cm.common.plugin.controller.apis.dynamicform; + +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.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; +import org.springframework.web.bind.annotation.RestController; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicFormController + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 3:54 下午 + * @Version: 1.0 + **/ +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/dynamicform") +public class DynamicFormController extends AbstractController { + + @Autowired + private IDynamicFormService dynamicFormService; + + @ApiOperation(value = "保存动态表单", notes = "保存动态表单接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("savedynamicform") + @CheckRequestBodyAnnotation + public SuccessResult saveDynamicForm(@RequestBody DynamicFormVO dynamicFormVO) throws Exception { + return dynamicFormService.saveDynamicForm(dynamicFormVO); + } + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java index 9c42595..f4ae868 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/database/table/impl/TableDaoImpl.java @@ -225,10 +225,17 @@ public class TableDaoImpl implements ITableDao { } 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)", WStringUtil.lowerUpper2UnderLine(tableColumn.getColumnName()), tableColumn.getColumnLength())); + 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.RICH_TEXT.getDataType())) { + } 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; } 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/dynamicform/IDynamicFormDao.java new file mode 100644 index 0000000..b67f9f4 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamicform/IDynamicFormDao.java @@ -0,0 +1,37 @@ +package com.cm.common.plugin.dao.dynamicform; + +import com.cm.common.exception.SaveException; +import com.cm.common.exception.UpdateException; +import org.springframework.stereotype.Repository; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDynamicFormDao + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 4:05 下午 + * @Version: 1.0 + **/ +@Repository +public interface IDynamicFormDao { + + /** + * 保存动态表单 + * + * @param params + * @throws SaveException + */ + void saveDynamicForm(Map params) throws SaveException; + + /** + * 更新动态表单 + * + * @param params + * @throws UpdateException + */ + void updateDynamicForm(Map params) throws UpdateException; +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java index aa23db7..167a670 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/database/table/ColumnDataTypeEnum.java @@ -24,14 +24,30 @@ public enum ColumnDataTypeEnum { * 双精度 */ DOUBLE("double"), + /** + * 时间戳 + */ + DATETIME("datetime"), + /** + * 日期 + */ + DATE("date"), /** * 字符串 */ VARCHAR("varchar"), /** - * 富文本 + * 字符 */ - RICH_TEXT("richText"); + CHAR("char"), + /** + * 文本 + */ + TEXT("text"), + /** + * 长文本 + */ + LONGTEXT("longtext"); private String dataType; diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamicform/FieldTypeEnum.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamicform/FieldTypeEnum.java new file mode 100644 index 0000000..6b22901 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamicform/FieldTypeEnum.java @@ -0,0 +1,40 @@ +package com.cm.common.plugin.enums.dynamicform; + +import com.google.inject.internal.util.$AbstractMapEntry; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicFormEnum + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 5:28 下午 + * @Version: 1.0 + **/ +public enum FieldTypeEnum { + + STRING("string"), + DATETIME("datetime"), + DATE("date"), + NUMBER("number"), + DOUBLE("double"), + TEXTAREA("textarea"), + RICH_TEXT("richText"), + SELECT("select"), + CHECKBOX("checkbox"), + RADIO("radio"), + SELECT_USER("selectUser"), + SELECT_DEPARTMENT("selectDepartment"); + + private String value; + + FieldTypeEnum(String value) { + this.value = value; + } + + public String getValue() { + return value == null ? "" : value.trim(); + } + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java index 8531588..b08114e 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/database/table/TableColumnVO.java @@ -34,7 +34,7 @@ public class TableColumnVO { private String isNullable; @ApiModelProperty(name = "dataType", value = "类型") - @CheckEmptyAnnotation(name = "类型", types = {"int", "double", "bigint", "varchar", "richText"}) + @CheckEmptyAnnotation(name = "类型", types = {"int", "double", "bigint", "datetime", "date", "varchar", "char", "text", "longtext"}) private String dataType; @ApiModelProperty(name = "columnLength", value = "长度") diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java new file mode 100644 index 0000000..9bcd1c3 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java @@ -0,0 +1,126 @@ +package com.cm.common.plugin.pojo.vos.dynamicform; + +import com.cm.common.annotation.CheckEmptyAnnotation; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicFormFieldVO + * @Description: 动态表单字段 + * @Author: WangGeng + * @Date: 2019/12/2 4:11 下午 + * @Version: 1.0 + **/ +@ApiModel +public class DynamicFormFieldVO { + + @ApiModelProperty(name = "fieldName", value = "字段名称") + @CheckEmptyAnnotation(name = "字段名称") + private String fieldName; + @ApiModelProperty(name = "fieldExplain", value = "字段说明") + @CheckEmptyAnnotation(name = "字段说明") + private String fieldExplain; + @ApiModelProperty(name = "fieldType", value = "字段类型") + @CheckEmptyAnnotation(name = "字段类型", types = {"string", "datetime", "date", "number", "double", "textarea", "richText", "select", "checkbox", "radio", "selectUser", "selectDepartment"}) + private String fieldType; + @ApiModelProperty(name = "fieldDefault", value = "字段默认值") + private String fieldDefault; + @ApiModelProperty(name = "dictionaryId", value = "字典ID") + private String dictionaryId; + @ApiModelProperty(name = "verifyType", value = "校验类型") + private String verifyType; + @ApiModelProperty(name = "verifyRegular", value = "校验正则") + private String verifyRegular; + @ApiModelProperty(name = "fieldSort", value = "排序") + private Integer fieldSort; + + public String getFieldName() { + return fieldName == null ? "" : fieldName.trim(); + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + public String getFieldExplain() { + return fieldExplain == null ? "" : fieldExplain.trim(); + } + + public void setFieldExplain(String fieldExplain) { + this.fieldExplain = fieldExplain; + } + + public String getFieldType() { + return fieldType == null ? "" : fieldType.trim(); + } + + public void setFieldType(String fieldType) { + this.fieldType = fieldType; + } + + public String getFieldDefault() { + return fieldDefault == null ? "" : fieldDefault.trim(); + } + + public void setFieldDefault(String fieldDefault) { + this.fieldDefault = fieldDefault; + } + + public String getDictionaryId() { + return dictionaryId == null ? "" : dictionaryId.trim(); + } + + public void setDictionaryId(String dictionaryId) { + this.dictionaryId = dictionaryId; + } + + public String getVerifyType() { + return verifyType == null ? "" : verifyType.trim(); + } + + public void setVerifyType(String verifyType) { + this.verifyType = verifyType; + } + + public String getVerifyRegular() { + return verifyRegular == null ? "" : verifyRegular.trim(); + } + + public void setVerifyRegular(String verifyRegular) { + this.verifyRegular = verifyRegular; + } + + public Integer getFieldSort() { + return fieldSort; + } + + public void setFieldSort(Integer fieldSort) { + this.fieldSort = fieldSort; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"fieldName\":") + .append("\"").append(fieldName).append("\""); + sb.append(",\"fieldExplain\":") + .append("\"").append(fieldExplain).append("\""); + sb.append(",\"fieldType\":") + .append("\"").append(fieldType).append("\""); + sb.append(",\"fieldDefault\":") + .append("\"").append(fieldDefault).append("\""); + sb.append(",\"dictionaryId\":") + .append("\"").append(dictionaryId).append("\""); + sb.append(",\"verifyType\":") + .append("\"").append(verifyType).append("\""); + sb.append(",\"verifyRegular\":") + .append("\"").append(verifyRegular).append("\""); + sb.append(",\"fieldSort\":") + .append(fieldSort); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormVO.java new file mode 100644 index 0000000..a45f1f7 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormVO.java @@ -0,0 +1,55 @@ +package com.cm.common.plugin.pojo.vos.dynamicform; + +import com.cm.common.annotation.CheckEmptyAnnotation; +import com.cm.common.annotation.CheckListAnnotation; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicFormVO + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 4:10 下午 + * @Version: 1.0 + **/ +@ApiModel +public class DynamicFormVO { + + @ApiModelProperty(name = "tableName", value = "表名") + @CheckEmptyAnnotation(name = "表名") + private String tableName; + @CheckListAnnotation(name = "字段列表") + private List dynamicFormFields; + + public String getTableName() { + return tableName == null ? "" : tableName.trim(); + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public List getDynamicFormFields() { + return dynamicFormFields; + } + + public void setDynamicFormFields(List dynamicFormFields) { + this.dynamicFormFields = dynamicFormFields; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"tableName\":") + .append("\"").append(tableName).append("\""); + sb.append(",\"dynamicFormFields\":") + .append(dynamicFormFields); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/IDatabaseService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/IDatabaseService.java index 675739b..0311f1a 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/IDatabaseService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/IDatabaseService.java @@ -3,6 +3,7 @@ package com.cm.common.plugin.service.database; import com.cm.common.exception.RemoveException; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; +import com.cm.common.plugin.pojo.vos.database.DatabaseVO; import com.cm.common.result.SuccessResult; import java.util.Map; @@ -46,6 +47,16 @@ public interface IDatabaseService { */ SuccessResult saveConnection(Map params) throws SaveException, RemoveException, SearchException; + /** + * 打开数据库连接 + * + * @param params + * @throws SaveException + * @throws RemoveException + * @throws SearchException + */ + void openConnection(Map params) throws SaveException, RemoveException, SearchException; + /** * 删除数据库链接 * diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/impl/DatabaseServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/impl/DatabaseServiceImpl.java index 7faa8a8..ec19b23 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/impl/DatabaseServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/impl/DatabaseServiceImpl.java @@ -4,6 +4,7 @@ import com.cm.common.base.AbstractService; import com.cm.common.exception.RemoveException; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; +import com.cm.common.plugin.pojo.vos.database.DatabaseVO; import com.cm.common.plugin.service.database.IDatabaseService; import com.cm.common.result.SuccessResult; import com.cm.common.result.SuccessResultData; @@ -31,6 +32,12 @@ public class DatabaseServiceImpl extends AbstractService implements IDatabaseSer @Override public SuccessResult saveConnection(Map params) throws SaveException, RemoveException, SearchException { + openConnection(params); + return new SuccessResult(); + } + + @Override + public void openConnection(Map params) throws SaveException, RemoveException, SearchException { LOG.debug("关闭原有链接"); closeConnection(getConnectionFromSession()); LOG.debug("创建新连接"); @@ -52,7 +59,6 @@ public class DatabaseServiceImpl extends AbstractService implements IDatabaseSer LOG.error(e.getMessage(), e); throw new SaveException("打开数据库连接失败"); } - return new SuccessResult(); } @Override diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java index 6907c5e..abac5d3 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/ITableService.java @@ -36,6 +36,14 @@ public interface ITableService { */ SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException; + /** + * 新增表 + * + * @param tableVO + * @throws SaveException + */ + void createTable(TableVO tableVO) throws SaveException; + /** * 删表 * diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java index 3a6983b..c215042 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/database/table/impl/TableServiceImpl.java @@ -39,17 +39,23 @@ public class TableServiceImpl extends AbstractService implements ITableService { @Override public synchronized SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException { - Connection connection = getConnectionFromSession(); - String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); String tableName = tableVO.getTableName(); TableDTO tableDTO = getTableByTableName(tableName); if (tableDTO != null) { throw new SaveException("表已存在"); } - tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns()); + createTable(tableVO); return new SuccessResult(); } + @Override + public void createTable(TableVO tableVO) throws SaveException { + Connection connection = getConnectionFromSession(); + String databaseName = getHttpSession().getAttribute(IDatabaseService.DATABASE_NAME).toString(); + String tableName = tableVO.getTableName(); + tableDao.saveTable(connection, databaseName, tableName, tableVO.getTableColumns()); + } + @Override public SuccessResult removeTable(String tableName) throws RemoveException { Connection connection = getConnectionFromSession(); 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/dynamicform/IDynamicFormService.java new file mode 100644 index 0000000..5ef60a1 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/IDynamicFormService.java @@ -0,0 +1,26 @@ +package com.cm.common.plugin.service.dynamicform; + +import com.cm.common.exception.SaveException; +import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormVO; +import com.cm.common.result.SuccessResult; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDynamicFormService + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 4:04 下午 + * @Version: 1.0 + **/ +public interface IDynamicFormService { + /** + * 保存动态表单 + * + * @param dynamicFormVO + * @return + * @throws SaveException + */ + SuccessResult saveDynamicForm(DynamicFormVO dynamicFormVO) throws Exception; +} 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/dynamicform/impl/DynamicFormServiceImpl.java new file mode 100644 index 0000000..f194e08 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamicform/impl/DynamicFormServiceImpl.java @@ -0,0 +1,246 @@ +package com.cm.common.plugin.service.dynamicform.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.plugin.dao.dynamicform.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; +import com.cm.common.plugin.pojo.dtos.database.table.TableDTO; +import com.cm.common.plugin.pojo.vos.database.DatabaseVO; +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.result.SuccessResult; +import com.cm.common.utils.HashMapUtil; +import com.cm.common.utils.UUIDUtil; +import com.cm.common.utils.WStringUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DynamicFormServiceImpl + * @Description: 动态表单 + * @Author: WangGeng + * @Date: 2019/12/2 4:05 下午 + * @Version: 1.0 + **/ +@Service +public class DynamicFormServiceImpl extends AbstractService implements IDynamicFormService { + + @Autowired + private IDynamicFormDao dynamicFormDao; + @Autowired + private IDatabaseService databaseService; + @Autowired + private ITableService tableService; + + @Override + public SuccessResult saveDynamicForm(DynamicFormVO dynamicFormVO) throws Exception { + LOG.debug("添加字段"); + databaseService.openConnection(HashMapUtil.beanToMap(getDataBase())); + TableDTO tableDTO = tableService.getTableByTableName(dynamicFormVO.getTableName()); + if (tableDTO == null) { + LOG.debug("表不存在,新增表和字段信息"); + saveDynamicFormInfo(dynamicFormVO); + } else { + LOG.debug("表存在,更新表字段信息"); + updateDynamicFormInfo(tableDTO.getTableName(), dynamicFormVO); + } + databaseService.removeConnection(null); + return new SuccessResult(); + } + + /** + * 保存动态表单信息 + * + * @param dynamicFormVO + * @throws Exception + */ + private void saveDynamicFormInfo(DynamicFormVO dynamicFormVO) throws Exception { + Map params; + List tableColumns = new ArrayList<>(); + for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormVO.getDynamicFormFields()) { + TableColumnVO tableColumn = getTableColumn(dynamicFormFieldVO); + tableColumns.add(tableColumn); + } + LOG.debug("建表"); + TableVO tableVO = new TableVO(); + tableVO.setTableName(dynamicFormVO.getTableName()); + tableVO.setTableColumns(tableColumns); + tableService.createTable(tableVO); + LOG.debug("新增字段信息"); + for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormVO.getDynamicFormFields()) { + params = HashMapUtil.beanToMap(dynamicFormFieldVO); + params.put("id", UUIDUtil.getUUID()); + params.put("tableName", dynamicFormVO.getTableName()); + dynamicFormDao.saveDynamicForm(params); + } + } + + /** + * 更新动态表单信息 + */ + private void updateDynamicFormInfo(String tableName, DynamicFormVO dynamicFormVO) throws Exception { + List dynamicFormFields = dynamicFormVO.getDynamicFormFields(); + Map params = getHashMap(1); + params.put("tableName", tableName); + List tableColumnDTOs = tableService.listTableColumns(params); + List saveDynamicFormField = new ArrayList<>(0); + List updateDynamicFormField = new ArrayList<>(0); + for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormFields) { + int checkTableColumn = checkTableColumn(dynamicFormFieldVO, tableColumnDTOs); + if (-1 == checkTableColumn) { + updateDynamicFormField.add(dynamicFormFieldVO); + } else if (1 == checkTableColumn) { + saveDynamicFormField.add(dynamicFormFieldVO); + } + } + LOG.debug("新增表字段"); + for (DynamicFormFieldVO dynamicFormFieldVO : saveDynamicFormField) { + TableColumnVO tableColumnVO = getTableColumn(dynamicFormFieldVO); + tableService.saveTableColumn(tableName, tableColumnVO); + } + LOG.debug("更新表字段"); + for (DynamicFormFieldVO dynamicFormFieldVO : updateDynamicFormField) { + TableColumnVO tableColumnVO = getTableColumn(dynamicFormFieldVO); + tableService.updateTableColumnType(tableName, tableColumnVO); + } + LOG.debug("新增表字段信息"); + for (DynamicFormFieldVO dynamicFormFieldVO : saveDynamicFormField) { + params = HashMapUtil.beanToMap(dynamicFormFieldVO); + params.put("id", UUIDUtil.getUUID()); + params.put("tableName", dynamicFormVO.getTableName()); + dynamicFormDao.saveDynamicForm(params); + } + LOG.debug("更新表字段信息"); + for (DynamicFormFieldVO dynamicFormFieldVO : updateDynamicFormField) { + params = HashMapUtil.beanToMap(dynamicFormFieldVO); + params.put("tableName", dynamicFormVO.getTableName()); + dynamicFormDao.updateDynamicForm(params); + } + } + + /** + * 检查table的列 + * + * @param dynamicFormFieldVO + * @param tableColumnDTOs + * @return -1:需要修改,0:无需修改,1:需要新增 + */ + private int checkTableColumn(DynamicFormFieldVO dynamicFormFieldVO, List tableColumnDTOs) { + for (TableColumnDTO tableColumnDTO : tableColumnDTOs) { + if (StringUtils.equals(WStringUtil.lowerUpper2UnderLine(dynamicFormFieldVO.getFieldName()), tableColumnDTO.getColumnName())) { + if (isSameType(dynamicFormFieldVO.getFieldType(), tableColumnDTO.getColumnType())) { + return 0; + } else { + return -1; + } + } + } + return 1; + } + + /** + * 字段类型是否相同 + * + * @param fieldType + * @param columnType + * @return + */ + private boolean isSameType(String fieldType, String columnType) { + if (StringUtils.equals(FieldTypeEnum.STRING.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.VARCHAR.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.DATETIME.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATETIME.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.DATE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATE.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.NUMBER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.INT.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.DOUBLE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DOUBLE.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.TEXTAREA.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.RICH_TEXT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.LONGTEXT.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.SELECT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.CHECKBOX.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.RADIO.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.SELECT_USER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) { + return true; + } else if (StringUtils.equals(FieldTypeEnum.SELECT_DEPARTMENT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) { + return true; + } + return false; + } + + /** + * 获取数据库连接 + * + * @return + */ + private DatabaseVO getDataBase() { + DatabaseVO databaseVO = new DatabaseVO(); + return databaseVO; + } + + /** + * 获取建表的列 + * + * @param dynamicFormFieldVO + * @return + */ + private TableColumnVO getTableColumn(DynamicFormFieldVO dynamicFormFieldVO) { + TableColumnVO tableColumnVO = new TableColumnVO(); + tableColumnVO.setColumnName(dynamicFormFieldVO.getFieldName()); + tableColumnVO.setColumnComment(dynamicFormFieldVO.getFieldExplain()); + tableColumnVO.setColumnDefault(dynamicFormFieldVO.getFieldDefault()); + if (StringUtils.equals(FieldTypeEnum.STRING.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.VARCHAR.getDataType()); + tableColumnVO.setColumnLength(255); + } else if (StringUtils.equals(FieldTypeEnum.DATETIME.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.DATETIME.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.DATE.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.DATE.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.NUMBER.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.INT.getDataType()); + tableColumnVO.setColumnLength(11); + } else if (StringUtils.equals(FieldTypeEnum.DOUBLE.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.DOUBLE.getDataType()); + tableColumnVO.setColumnLength(11); + tableColumnVO.setColumnFloatLength(2); + } else if (StringUtils.equals(FieldTypeEnum.TEXTAREA.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.TEXT.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.RICH_TEXT.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.LONGTEXT.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.SELECT.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.CHAR.getDataType()); + tableColumnVO.setColumnLength(36); + } else if (StringUtils.equals(FieldTypeEnum.CHECKBOX.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.TEXT.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.RADIO.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.CHAR.getDataType()); + tableColumnVO.setColumnLength(36); + } else if (StringUtils.equals(FieldTypeEnum.SELECT_USER.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.TEXT.getDataType()); + } else if (StringUtils.equals(FieldTypeEnum.SELECT_DEPARTMENT.getValue(), dynamicFormFieldVO.getFieldType())) { + tableColumnVO.setDataType(ColumnDataTypeEnum.TEXT.getDataType()); + } + return tableColumnVO; + } + +} diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml new file mode 100644 index 0000000..78823a1 --- /dev/null +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamicform/dynamicform-mapper.xml @@ -0,0 +1,51 @@ + + + + + + + INSERT INTO dynamic_form( + id, + table_name, + field_name, + field_explain, + field_type, + field_default, + dictionary_id, + verify_type, + verify_regular, + field_sort + ) VALUES( + #{id}, + #{tableName}, + #{fieldName}, + #{fieldExplain}, + #{fieldType}, + #{fieldDefault}, + #{dictionaryId}, + #{verifytype}, + #{verifyRegular}, + #{fieldSort} + ) + + + + + UPDATE + dynamic_form + SET + field_name = #{fieldName}, + field_explain = #{fieldExplain}, + field_type = #{fieldType}, + field_default = #{fieldDefault}, + dictionary_id = #{dictionaryId}, + verify_type = #{verifyType}, + verify_regular = #{verifyRegular}, + field_sort = #{fieldSort} + WHERE + table_name = #{tableName} + AND + field_name = #{fieldName} + + + \ No newline at end of file