新增动态表单
This commit is contained in:
parent
833a0e47d2
commit
427832f81e
@ -83,6 +83,4 @@ public class DatabaseController extends AbstractController {
|
||||
return databaseService.removeConnection(params);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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<String, Object> params) throws SaveException;
|
||||
|
||||
/**
|
||||
* 更新动态表单
|
||||
*
|
||||
* @param params
|
||||
* @throws UpdateException
|
||||
*/
|
||||
void updateDynamicForm(Map<String, Object> params) throws UpdateException;
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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 = "长度")
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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<DynamicFormFieldVO> dynamicFormFields;
|
||||
|
||||
public String getTableName() {
|
||||
return tableName == null ? "" : tableName.trim();
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public List<DynamicFormFieldVO> getDynamicFormFields() {
|
||||
return dynamicFormFields;
|
||||
}
|
||||
|
||||
public void setDynamicFormFields(List<DynamicFormFieldVO> 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();
|
||||
}
|
||||
}
|
@ -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<String, Object> params) throws SaveException, RemoveException, SearchException;
|
||||
|
||||
/**
|
||||
* 打开数据库连接
|
||||
*
|
||||
* @param params
|
||||
* @throws SaveException
|
||||
* @throws RemoveException
|
||||
* @throws SearchException
|
||||
*/
|
||||
void openConnection(Map<String, Object> params) throws SaveException, RemoveException, SearchException;
|
||||
|
||||
/**
|
||||
* 删除数据库链接
|
||||
*
|
||||
|
@ -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<String, Object> params) throws SaveException, RemoveException, SearchException {
|
||||
openConnection(params);
|
||||
return new SuccessResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openConnection(Map<String, Object> 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
|
||||
|
@ -36,6 +36,14 @@ public interface ITableService {
|
||||
*/
|
||||
SuccessResult saveTable(TableVO tableVO) throws SearchException, SaveException;
|
||||
|
||||
/**
|
||||
* 新增表
|
||||
*
|
||||
* @param tableVO
|
||||
* @throws SaveException
|
||||
*/
|
||||
void createTable(TableVO tableVO) throws SaveException;
|
||||
|
||||
/**
|
||||
* 删表
|
||||
*
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
@ -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<String, Object> params;
|
||||
List<TableColumnVO> 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<DynamicFormFieldVO> dynamicFormFields = dynamicFormVO.getDynamicFormFields();
|
||||
Map<String, Object> params = getHashMap(1);
|
||||
params.put("tableName", tableName);
|
||||
List<TableColumnDTO> tableColumnDTOs = tableService.listTableColumns(params);
|
||||
List<DynamicFormFieldVO> saveDynamicFormField = new ArrayList<>(0);
|
||||
List<DynamicFormFieldVO> 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<TableColumnDTO> 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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
<?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">
|
||||
|
||||
<!-- 保存动态表单 -->
|
||||
<insert id="saveDynamicForm" parameterType="map">
|
||||
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}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<!-- 更新动态表单 -->
|
||||
<update id="updateDynamicForm" parameterType="map">
|
||||
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}
|
||||
</update>
|
||||
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user