新增自定义数据

This commit is contained in:
wenc000 2019-12-04 18:48:49 +08:00
parent 3ea7791fda
commit b55f243f74
17 changed files with 677 additions and 51 deletions

View File

@ -0,0 +1,40 @@
package com.cm.common.plugin.controller.apis.dynamic.form;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.plugin.service.dynamic.IDynamicDataService;
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.web.bind.annotation.*;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: DynamicDataController
* @Description: 动态数据
* @Author: WangGeng
* @Date: 2019/12/4 15:23
* @Version: 1.0
**/
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/dynamicdata")
public class DynamicDataController extends AbstractController {
@Autowired
private IDynamicDataService dynamicDataService;
@ApiOperation(value = "保存动态数据", notes = "保存动态数据接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PostMapping("savedynamicdata/{tableName}")
public SuccessResult saveDynamicData(@PathVariable("tableName") String tableName, @RequestBody Map<String, Object> params) {
return dynamicDataService.saveDynamicData(tableName, params);
}
}

View File

@ -11,10 +11,9 @@ 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.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;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started

View File

@ -0,0 +1,20 @@
package com.cm.common.plugin.dao.dynamic;
import org.springframework.stereotype.Repository;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: IDynamicDataDao
* @Description: 动态数据
* @Author: WangGeng
* @Date: 2019/12/4 16:38
* @Version: 1.0
**/
@Repository
public class IDynamicDataDao {
}

View File

@ -1,9 +1,12 @@
package com.cm.common.plugin.dao.dynamic;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.plugin.pojo.dtos.dynamic.DynamicFormDTO;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
@ -34,4 +37,13 @@ public interface IDynamicFormDao {
* @throws UpdateException
*/
void updateDynamicForm(Map<String, Object> params) throws UpdateException;
/**
* 动态表单详情
*
* @param tableName
* @return
* @throws SearchException
*/
List<DynamicFormDTO> listDynamicForm(String tableName) throws SearchException;
}

View File

@ -2,9 +2,11 @@ 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.TableColumnDTO;
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
@ -27,6 +29,22 @@ public interface IDynamicTableDao {
*/
void createTable(String createTableSQL) throws UpdateException;
/**
* 加列
*
* @param toString
* @throws UpdateException
*/
void saveTableColumn(String toString) throws UpdateException;
/**
* 修改列属性
*
* @param toString
* @throws UpdateException
*/
void updateTableColumnType(String toString) throws UpdateException;
/**
* 通过表名获取表
*
@ -35,4 +53,13 @@ public interface IDynamicTableDao {
* @throws SearchException
*/
TableDTO getTableByTableName(String tableName) throws SearchException;
/**
* 表详情
*
* @param tableName
* @return
* @throws SearchException
*/
List<TableColumnDTO> listTableColumns(String tableName) throws SearchException;
}

View File

@ -0,0 +1,33 @@
package com.cm.common.plugin.enums.dynamic;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: FieldRequireTypeEnum
* @Description: 字段校验类型
* @Author: WangGeng
* @Date: 2019/12/4 17:29
* @Version: 1.0
**/
public enum FieldRequireTypeEnum {
NONE("none"),
REQUIRED("required"),
PHONE("phone"),
EMAIL("email"),
URL("url"),
NUMBER("number"),
DATE("date"),
IDENTITY("identity"),
CUSTOM("custom");
private String value;
FieldRequireTypeEnum(String value) {
this.value = value;
}
public String getValue() {
return value == null ? "" : value;
}
}

View File

@ -1,6 +1,4 @@
package com.cm.common.plugin.enums.dynamicform;
import com.google.inject.internal.util.$AbstractMapEntry;
package com.cm.common.plugin.enums.dynamic;
/**
* When you feel like quitting. Think about why you started

View File

@ -0,0 +1,171 @@
package com.cm.common.plugin.pojo.dtos.dynamic;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: DynamicFormDTO
* @Description: 动态表单
* @Author: WangGeng
* @Date: 2019/12/4 16:58
* @Version: 1.0
**/
@ApiModel
public class DynamicFormDTO {
@ApiModelProperty(name = "id", value = "id")
private String id;
@ApiModelProperty(name = "tableName", value = "表名")
private String tableName;
@ApiModelProperty(name = "fieldName", value = "字段")
private String fieldName;
@ApiModelProperty(name = "fieldExplain", value = "字段说明")
private String fieldExplain;
@ApiModelProperty(name = "fieldType", value = "字段类型")
private String fieldType;
@ApiModelProperty(name = "fieldDefault", value = "字段默认")
private String fieldDefault;
@ApiModelProperty(name = "dictionaryId", value = "字典")
private String dictionaryId;
@ApiModelProperty(name = "verifyType", value = "校验类型")
private String verifyType;
@ApiModelProperty(name = "verifyRegular", value = "校验正则")
private String verifyRegular;
@ApiModelProperty(name = "fieldSort", value = "字段排序")
private Integer fieldSort;
@ApiModelProperty(name = "listShow", value = "列表显示")
private Integer listShow;
@ApiModelProperty(name = "formShow", value = "表单显示")
private Integer formShow;
public String getId() {
return id == null ? "" : id;
}
public void setId(String id) {
this.id = id;
}
public String getTableName() {
return tableName == null ? "" : tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getFieldName() {
return fieldName == null ? "" : fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldExplain() {
return fieldExplain == null ? "" : fieldExplain;
}
public void setFieldExplain(String fieldExplain) {
this.fieldExplain = fieldExplain;
}
public String getFieldType() {
return fieldType == null ? "" : fieldType;
}
public void setFieldType(String fieldType) {
this.fieldType = fieldType;
}
public String getFieldDefault() {
return fieldDefault == null ? "" : fieldDefault;
}
public void setFieldDefault(String fieldDefault) {
this.fieldDefault = fieldDefault;
}
public String getDictionaryId() {
return dictionaryId == null ? "" : dictionaryId;
}
public void setDictionaryId(String dictionaryId) {
this.dictionaryId = dictionaryId;
}
public String getVerifyType() {
return verifyType == null ? "" : verifyType;
}
public void setVerifyType(String verifyType) {
this.verifyType = verifyType;
}
public String getVerifyRegular() {
return verifyRegular == null ? "" : verifyRegular;
}
public void setVerifyRegular(String verifyRegular) {
this.verifyRegular = verifyRegular;
}
public Integer getFieldSort() {
return fieldSort;
}
public void setFieldSort(Integer fieldSort) {
this.fieldSort = fieldSort;
}
public Integer getListShow() {
return listShow;
}
public void setListShow(Integer listShow) {
this.listShow = listShow;
}
public Integer getFormShow() {
return formShow;
}
public void setFormShow(Integer formShow) {
this.formShow = formShow;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"id\":\"")
.append(id).append('\"');
sb.append(",\"tableName\":\"")
.append(tableName).append('\"');
sb.append(",\"fieldName\":\"")
.append(fieldName).append('\"');
sb.append(",\"fieldExplain\":\"")
.append(fieldExplain).append('\"');
sb.append(",\"fieldType\":\"")
.append(fieldType).append('\"');
sb.append(",\"fieldDefault\":\"")
.append(fieldDefault).append('\"');
sb.append(",\"dictionaryId\":\"")
.append(dictionaryId).append('\"');
sb.append(",\"verifyType\":\"")
.append(verifyType).append('\"');
sb.append(",\"verifyRegular\":\"")
.append(verifyRegular).append('\"');
sb.append(",\"fieldSort\":")
.append(fieldSort);
sb.append(",\"listShow\":")
.append(listShow);
sb.append(",\"formShow\":")
.append(formShow);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,30 @@
package com.cm.common.plugin.service.dynamic;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.result.SuccessResult;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: IDynamicDataService
* @Description: 动态数据
* @Author: WangGeng
* @Date: 2019/12/4 16:35
* @Version: 1.0
**/
public interface IDynamicDataService {
/**
* 保存动态数据
*
* @param tableName
* @param params
* @return
* @throws SearchException
* @throws SaveException
*/
SuccessResult saveDynamicData(String tableName, Map<String, Object> params) throws SearchException, SaveException;
}

View File

@ -1,9 +1,14 @@
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.dynamic.DynamicFormDTO;
import com.cm.common.plugin.pojo.vos.dynamicform.DynamicFormVO;
import com.cm.common.result.SuccessResult;
import java.util.List;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -20,7 +25,16 @@ public interface IDynamicFormService {
*
* @param dynamicFormVO
* @return
* @throws SaveException
* @throws Exception
*/
SuccessResult saveDynamicForm(DynamicFormVO dynamicFormVO) throws Exception;
/**
* 动态表单详情
*
* @param tableName
* @return
* @throws SearchException
*/
List<DynamicFormDTO> listDynamicForm(String tableName) throws SearchException;
}

View File

@ -2,6 +2,7 @@ package com.cm.common.plugin.service.dynamic;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
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;
@ -22,6 +23,11 @@ import java.util.Map;
**/
public interface IDynamicTableService {
/**
* 动态表前缀
*/
String DYNAMIC_TABLE_PREFIX = "dynamic_";
/**
* 建表
*
@ -37,7 +43,7 @@ public interface IDynamicTableService {
* @param tableColumnVO
* @throws SaveException
*/
void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException;
void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws UpdateException;
/**
* 修改表列类型
@ -59,10 +65,10 @@ public interface IDynamicTableService {
/**
* 表详情
*
* @param params
* @param tableName
* @return
* @throws SearchException
*/
List<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException;
List<TableColumnDTO> listTableColumns(String tableName) throws SearchException;
}

View File

@ -0,0 +1,102 @@
package com.cm.common.plugin.service.dynamic.impl;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.dao.dynamic.IDynamicDataDao;
import com.cm.common.plugin.enums.dynamic.FieldRequireTypeEnum;
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
import com.cm.common.plugin.pojo.dtos.dynamic.DynamicFormDTO;
import com.cm.common.plugin.service.dynamic.IDynamicDataService;
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.WStringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: DynamicDataServiceImpl
* @Description: 动态数据
* @Author: WangGeng
* @Date: 2019/12/4 16:35
* @Version: 1.0
**/
@Service
public class DynamicDataServiceImpl extends AbstractService implements IDynamicDataService {
@Autowired
private IDynamicDataDao dynamicDataDao;
@Autowired
private IDynamicFormService dynamicFormService;
@Override
public SuccessResult saveDynamicData(String tableName, Map<String, Object> params) throws SearchException, SaveException {
List<DynamicFormDTO> dynamicFormDTOs = dynamicFormService.listDynamicForm(tableName);
requireData(params, dynamicFormDTOs);
return null;
}
/**
* 验证数据
*
* @param params
* @param dynamicFormDTOs
*/
private void requireData(Map<String, Object> params, List<DynamicFormDTO> dynamicFormDTOs) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
for (DynamicFormDTO dynamicFormDTO : dynamicFormDTOs) {
// 表单显示并且字段相同有校验类型则校验参数
if (dynamicFormDTO.getFormShow() == 1
&& StringUtils.equals(key, dynamicFormDTO.getFieldName())
&& !StringUtils.equals(FieldRequireTypeEnum.NONE.getValue(), dynamicFormDTO.getVerifyType())) {
if (StringUtils.equals(FieldRequireTypeEnum.REQUIRED.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.PHONE.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.EMAIL.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.URL.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.NUMBER.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.DATE.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.IDENTITY.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
} else if (StringUtils.equals(FieldRequireTypeEnum.CUSTOM.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key));
}
}
break;
}
}
}
}
}

View File

@ -1,12 +1,14 @@
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.IDynamicFormDao;
import com.cm.common.plugin.enums.database.table.ColumnDataTypeEnum;
import com.cm.common.plugin.enums.dynamicform.FieldTypeEnum;
import com.cm.common.plugin.enums.dynamic.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.dtos.dynamic.DynamicFormDTO;
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;
@ -52,11 +54,16 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF
saveDynamicFormInfo(dynamicFormVO);
} else {
LOG.debug("表存在,更新表字段信息");
updateDynamicFormInfo(tableDTO.getTableName(), dynamicFormVO);
updateDynamicFormInfo(StringUtils.removeStart(tableDTO.getTableName(), IDynamicTableService.DYNAMIC_TABLE_PREFIX), dynamicFormVO);
}
return new SuccessResult();
}
@Override
public List<DynamicFormDTO> listDynamicForm(String tableName) throws SearchException {
return dynamicFormDao.listDynamicForm(tableName);
}
/**
* 保存动态表单信息
*
@ -89,9 +96,7 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF
*/
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 = dynamicTableService.listTableColumns(params);
List<TableColumnDTO> tableColumnDTOs = dynamicTableService.listTableColumns(tableName);
List<DynamicFormFieldVO> saveDynamicFormField = new ArrayList<>(0);
List<DynamicFormFieldVO> updateDynamicFormField = new ArrayList<>(0);
for (DynamicFormFieldVO dynamicFormFieldVO : dynamicFormFields) {
@ -112,6 +117,7 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF
TableColumnVO tableColumnVO = getTableColumn(dynamicFormFieldVO);
dynamicTableService.updateTableColumnType(tableName, tableColumnVO);
}
Map<String, Object> params = getHashMap(0);
LOG.debug("新增表字段信息");
for (DynamicFormFieldVO dynamicFormFieldVO : saveDynamicFormField) {
params = HashMapUtil.beanToMap(dynamicFormFieldVO);
@ -137,7 +143,7 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF
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())) {
if (isSameType(dynamicFormFieldVO.getFieldType(), tableColumnDTO.getDataType())) {
return 0;
} else {
return -1;
@ -151,48 +157,38 @@ public class DynamicFormServiceImpl extends AbstractService implements IDynamicF
* 字段类型是否相同
*
* @param fieldType
* @param columnType
* @param dataType
* @return
*/
private boolean isSameType(String fieldType, String columnType) {
if (StringUtils.equals(FieldTypeEnum.STRING.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.VARCHAR.getDataType(), columnType)) {
private boolean isSameType(String fieldType, String dataType) {
if (StringUtils.equals(FieldTypeEnum.STRING.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.VARCHAR.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.DATETIME.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATETIME.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.DATETIME.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATETIME.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.DATE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATE.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.DATE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DATE.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.NUMBER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.INT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.NUMBER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.INT.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.DOUBLE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DOUBLE.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.DOUBLE.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.DOUBLE.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.TEXTAREA.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.TEXTAREA.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.RICH_TEXT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.LONGTEXT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.RICH_TEXT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.LONGTEXT.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.SELECT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.SELECT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.CHECKBOX.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.CHECKBOX.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.RADIO.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.RADIO.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.CHAR.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.SELECT_USER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.SELECT_USER.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), dataType)) {
return true;
} else if (StringUtils.equals(FieldTypeEnum.SELECT_DEPARTMENT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), columnType)) {
} else if (StringUtils.equals(FieldTypeEnum.SELECT_DEPARTMENT.getValue(), fieldType) && StringUtils.equals(ColumnDataTypeEnum.TEXT.getDataType(), dataType)) {
return true;
}
return false;
}
/**
* 获取数据库连接
*
* @return
*/
private DatabaseVO getDataBase() {
DatabaseVO databaseVO = new DatabaseVO();
return databaseVO;
}
/**
* 获取建表的列
*

View File

@ -3,6 +3,7 @@ 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.exception.UpdateException;
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;
@ -42,23 +43,25 @@ public class DynamicTableServiceImpl extends AbstractService implements IDynamic
}
@Override
public void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws SaveException {
public void saveTableColumn(String tableName, TableColumnVO tableColumnVO) throws UpdateException {
StringBuilder saveTableColumnSQL = new StringBuilder(String.format("ALTER TABLE `%s%s` ADD COLUMN ", DYNAMIC_TABLE_PREFIX, WStringUtil.lowerUpper2UnderLine(tableName))).append(getSaveColumnSql(tableColumnVO));
dynamicTableDao.saveTableColumn(saveTableColumnSQL.toString());
}
@Override
public void updateTableColumnType(String tableName, TableColumnVO tableColumnVO) {
StringBuilder updateTableColumnTypeSQL = new StringBuilder(String.format("ALTER TABLE `%s%s` MODIFY ", DYNAMIC_TABLE_PREFIX, WStringUtil.lowerUpper2UnderLine(tableName))).append(getSaveColumnSql(tableColumnVO));
dynamicTableDao.updateTableColumnType(updateTableColumnTypeSQL.toString());
}
@Override
public TableDTO getTableByTableName(String tableName) throws SearchException {
return dynamicTableDao.getTableByTableName(tableName);
return dynamicTableDao.getTableByTableName(String.format("%s%s", DYNAMIC_TABLE_PREFIX, WStringUtil.lowerUpper2UnderLine(tableName)));
}
@Override
public List<TableColumnDTO> listTableColumns(Map<String, Object> params) throws SearchException {
return null;
public List<TableColumnDTO> listTableColumns(String tableName) throws SearchException {
return dynamicTableDao.listTableColumns(String.format("%s%s", DYNAMIC_TABLE_PREFIX, WStringUtil.lowerUpper2UnderLine(tableName)));
}
/**
@ -70,7 +73,7 @@ public class DynamicTableServiceImpl extends AbstractService implements IDynamic
*/
private String getSaveTableSQL(String tableName, List<TableColumnVO> tableColumns) {
String underLineTableName = WStringUtil.lowerUpper2UnderLine(tableName);
StringBuilder sql = new StringBuilder("CREATE TABLE `").append(tableName).append("`(");
StringBuilder sql = new StringBuilder("CREATE TABLE `").append(DYNAMIC_TABLE_PREFIX).append(WStringUtil.lowerUpper2UnderLine(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) {
@ -103,6 +106,8 @@ public class DynamicTableServiceImpl extends AbstractService implements IDynamic
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.CHAR.getDataType())) {
columnSql.append(String.format("`%s` CHAR(%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())) {

View File

@ -2,6 +2,21 @@
<!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.IDynamicFormDao">
<resultMap id="dynamicFormDTO" type="com.cm.common.plugin.pojo.dtos.dynamic.DynamicFormDTO">
<id property="id" column="id"/>
<result property="tableName" column="table_name"/>
<result property="fieldName" column="field_name"/>
<result property="fieldExplain" column="field_explain"/>
<result property="fieldType" column="field_type"/>
<result property="fieldDefault" column="field_default"/>
<result property="dictionaryId" column="dictionary_id"/>
<result property="verifyType" column="verify_type"/>
<result property="verifyRegular" column="verify_regular"/>
<result property="fieldSort" column="field_sort"/>
<result property="listShow" column="list_show"/>
<result property="formShow" column="form_show"/>
</resultMap>
<!-- 保存动态表单 -->
<insert id="saveDynamicForm" parameterType="map">
INSERT INTO dynamic_form(
@ -48,4 +63,14 @@
field_name = #{fieldName}
</update>
<!-- 动态表单详情 -->
<select id="listDynamicForm" parameterType="string" resultMap="dynamicFormDTO">
SELECT
*
FROM
dynamic_form
WHERE
table_name = #{_parameter}
</select>
</mapper>

View File

@ -3,8 +3,23 @@
<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"/>
<result column="tableSchema" property="tableSchema"/>
<result column="tableName" property="tableName"/>
</resultMap>
<resultMap id="tableColumnDTO" type="com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO">
<result column="columnName" property="columnName"/>
<result column="columnDefault" property="columnDefault"/>
<result column="isNullable" property="isNullable"/>
<result column="dataType" property="dataType"/>
<result column="characterMaximumLength" property="characterMaximumLength"/>
<result column="numericPrecision" property="numericPrecision"/>
<result column="numericScale" property="numericScale"/>
<result column="datetimePrecision" property="datetimePrecision"/>
<result column="columnType" property="columnType"/>
<result column="columnKey" property="columnKey"/>
<result column="columnComment" property="columnComment"/>
<result column="extra" property="extra"/>
</resultMap>
<!-- 建表 -->
@ -12,6 +27,16 @@
${_parameter}
</update>
<!-- 加列 -->
<update id="saveTableColumn" parameterType="string">
${_parameter}
</update>
<!-- 修改列属性 -->
<update id="updateTableColumnType" parameterType="string">
${_parameter}
</update>
<!-- 通过表名获取表 -->
<select id="getTableByTableName" parameterType="string" resultMap="tableDTO">
SELECT
@ -25,4 +50,27 @@
`TABLE_NAME` = #{_parameter}
</select>
<!-- 表详情 -->
<select id="listTableColumns" parameterType="string" resultMap="tableColumnDTO">
SELECT
COLUMN_NAME columnName,
COLUMN_DEFAULT columnDefault,
IS_NULLABLE isNullable,
DATA_TYPE dataType,
CHARACTER_MAXIMUM_LENGTH characterMaximumLength,
NUMERIC_PRECISION numericPrecision,
NUMERIC_SCALE numericScale,
DATETIME_PRECISION datetimePrecision,
COLUMN_TYPE columnType,
COLUMN_KEY columnKey,
COLUMN_COMMENT columnComment,
EXTRA extra
FROM
`information_schema`.`COLUMNS`
WHERE
`TABLE_SCHEMA`= (SELECT database())
AND
`TABLE_NAME`= #{_parameter}
</select>
</mapper>

View File

@ -0,0 +1,100 @@
package com.cm.common.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: RegexUtil
* @Description: 正则校验工具类
* @Author: WangGeng
* @Date: 2019/12/4 18:13
* @Version: 1.0
**/
public class RegexUtil {
/**
* 手机
*/
private static final Pattern PATTERN_PHONE = Pattern.compile("^1\\d{10}$");
/**
* 邮箱
*/
private static final Pattern PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
/**
* 邮箱
*/
private static final Pattern PATTERN_URL = Pattern.compile("(^#)|(^http(s*):\\/\\/[^\\s]+)");
/**
* 日期格式
*/
private static final Pattern PATTERN_DATE = Pattern.compile("^(\\d{4})[-\\/](\\d{1}|0\\d{1}|1[0-2])([-\\/](\\d{1}|0\\d{1}|[1-2][0-9]|3[0-1]))*$");
/**
* 身份证
*/
private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)");
/**
* 判断电话
*
* @param input
* @return
*/
public static boolean isPhone(String input) {
return PATTERN_PHONE.matcher(input).matches();
}
/**
* 判断邮箱
*
* @param input
* @return
*/
public static boolean isEmail(String input) {
return PATTERN_PHONE.matcher(input).matches();
}
/**
* 判断URL
*
* @param input
* @return
*/
public static boolean isUrl(String input) {
return PATTERN_PHONE.matcher(input).matches();
}
/**
* 判断日期
*
* @param input
* @return
*/
public static boolean isDate(String input) {
return PATTERN_PHONE.matcher(input).matches();
}
/**
* 判断身份证
*
* @param input
* @return
*/
public static boolean isIdentity(String input) {
return PATTERN_PHONE.matcher(input).matches();
}
/**
* 自定义判断
*
* @param customPattern
* @param input
* @return
*/
public static boolean isMatch(Pattern customPattern, String input) {
return customPattern.matcher(input).matches();
}
}