From b55f243f74f75a294e8c7d2244eeb56d2c36aee1 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Wed, 4 Dec 2019 18:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamic/form/DynamicDataController.java | 40 ++++ .../dynamic/form/DynamicFormController.java | 7 +- .../plugin/dao/dynamic/IDynamicDataDao.java | 20 ++ .../plugin/dao/dynamic/IDynamicFormDao.java | 12 ++ .../plugin/dao/dynamic/IDynamicTableDao.java | 27 +++ .../enums/dynamic/FieldRequireTypeEnum.java | 33 ++++ .../FieldTypeEnum.java | 4 +- .../pojo/dtos/dynamic/DynamicFormDTO.java | 171 ++++++++++++++++++ .../service/dynamic/IDynamicDataService.java | 30 +++ .../service/dynamic/IDynamicFormService.java | 16 +- .../service/dynamic/IDynamicTableService.java | 12 +- .../dynamic/impl/DynamicDataServiceImpl.java | 102 +++++++++++ .../dynamic/impl/DynamicFormServiceImpl.java | 58 +++--- .../dynamic/impl/DynamicTableServiceImpl.java | 19 +- .../mapper/dynamic/dynamicform-mapper.xml | 25 +++ .../mapper/dynamic/dynamictable-mapper.xml | 52 +++++- .../java/com/cm/common/utils/RegexUtil.java | 100 ++++++++++ 17 files changed, 677 insertions(+), 51 deletions(-) create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicDataController.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicDataDao.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamic/FieldRequireTypeEnum.java rename cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/{dynamicform => dynamic}/FieldTypeEnum.java (88%) create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/dynamic/DynamicFormDTO.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicDataService.java create mode 100644 cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicDataServiceImpl.java create mode 100644 cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicDataController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicDataController.java new file mode 100644 index 0000000..d74430f --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicDataController.java @@ -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 params) { + return dynamicDataService.saveDynamicData(tableName, params); + } + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java index 2351196..7a6067b 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/apis/dynamic/form/DynamicFormController.java @@ -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 diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicDataDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicDataDao.java new file mode 100644 index 0000000..9a0554a --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicDataDao.java @@ -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 { + + + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java index fea4e7c..af397c0 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicFormDao.java @@ -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 params) throws UpdateException; + + /** + * 动态表单详情 + * + * @param tableName + * @return + * @throws SearchException + */ + List listDynamicForm(String tableName) throws SearchException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java index 528a44b..05cf66e 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/dao/dynamic/IDynamicTableDao.java @@ -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 listTableColumns(String tableName) throws SearchException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamic/FieldRequireTypeEnum.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamic/FieldRequireTypeEnum.java new file mode 100644 index 0000000..b08ef2b --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamic/FieldRequireTypeEnum.java @@ -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; + } +} 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/dynamic/FieldTypeEnum.java similarity index 88% rename from cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamicform/FieldTypeEnum.java rename to cloud-common-plugin/src/main/java/com/cm/common/plugin/enums/dynamic/FieldTypeEnum.java index 6b22901..cadcc46 100644 --- 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/dynamic/FieldTypeEnum.java @@ -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 diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/dynamic/DynamicFormDTO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/dynamic/DynamicFormDTO.java new file mode 100644 index 0000000..42d4331 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/dtos/dynamic/DynamicFormDTO.java @@ -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(); + } +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicDataService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicDataService.java new file mode 100644 index 0000000..af8d046 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicDataService.java @@ -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 params) throws SearchException, SaveException; +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java index 856a530..492daf2 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicFormService.java @@ -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 listDynamicForm(String tableName) throws SearchException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java index 83814b6..b226c93 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/IDynamicTableService.java @@ -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 listTableColumns(Map params) throws SearchException; + List listTableColumns(String tableName) throws SearchException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicDataServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicDataServiceImpl.java new file mode 100644 index 0000000..bf76da5 --- /dev/null +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicDataServiceImpl.java @@ -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 params) throws SearchException, SaveException { + List dynamicFormDTOs = dynamicFormService.listDynamicForm(tableName); + requireData(params, dynamicFormDTOs); + return null; + } + + /** + * 验证数据 + * + * @param params + * @param dynamicFormDTOs + */ + private void requireData(Map params, List dynamicFormDTOs) { + for (Map.Entry 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; + } + } + } + } + +} diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java index 54dc604..f0e1c46 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicFormServiceImpl.java @@ -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 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 dynamicFormFields = dynamicFormVO.getDynamicFormFields(); - Map params = getHashMap(1); - params.put("tableName", tableName); - List tableColumnDTOs = dynamicTableService.listTableColumns(params); + List tableColumnDTOs = dynamicTableService.listTableColumns(tableName); List saveDynamicFormField = new ArrayList<>(0); List 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 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 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; - } - /** * 获取建表的列 * diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java index 5edb70d..1b60be6 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java @@ -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 listTableColumns(Map params) throws SearchException { - return null; + public List 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 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())) { diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml index b610262..fd31b92 100644 --- a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicform-mapper.xml @@ -2,6 +2,21 @@ + + + + + + + + + + + + + + + INSERT INTO dynamic_form( @@ -48,4 +63,14 @@ field_name = #{fieldName} + + + \ No newline at end of file diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml index 131ff4c..8661ccc 100644 --- a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamictable-mapper.xml @@ -3,8 +3,23 @@ - - + + + + + + + + + + + + + + + + + @@ -12,6 +27,16 @@ ${_parameter} + + + ${_parameter} + + + + + ${_parameter} + + + + + \ No newline at end of file diff --git a/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java new file mode 100644 index 0000000..720d54f --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java @@ -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(); + } + +}