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 index 9a0554a..fe0efc0 100644 --- 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 @@ -1,7 +1,11 @@ package com.cm.common.plugin.dao.dynamic; +import com.cm.common.exception.SaveException; +import com.cm.common.result.SuccessResult; import org.springframework.stereotype.Repository; +import java.util.Map; + /** * When you feel like quitting. Think about why you started * 当你想要放弃的时候,想想当初你为何开始 @@ -13,8 +17,14 @@ import org.springframework.stereotype.Repository; * @Version: 1.0 **/ @Repository -public class IDynamicDataDao { - - +public interface IDynamicDataDao { + /** + * 新增动态数据 + * + * @param params + * @return + * @throws SaveException + */ + void saveDynamicData(Map params) throws SaveException; } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java index 9bcd1c3..802a090 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/pojo/vos/dynamicform/DynamicFormFieldVO.java @@ -1,6 +1,7 @@ package com.cm.common.plugin.pojo.vos.dynamicform; import com.cm.common.annotation.CheckEmptyAnnotation; +import com.cm.common.annotation.CheckNumberAnnotation; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -31,11 +32,18 @@ public class DynamicFormFieldVO { @ApiModelProperty(name = "dictionaryId", value = "字典ID") private String dictionaryId; @ApiModelProperty(name = "verifyType", value = "校验类型") + @CheckEmptyAnnotation(name = "校验类型", types = {"none", "required", "phone", "email", "url", "number", "date", "identity", "custom"}) private String verifyType; @ApiModelProperty(name = "verifyRegular", value = "校验正则") private String verifyRegular; @ApiModelProperty(name = "fieldSort", value = "排序") private Integer fieldSort; + @ApiModelProperty(name = "listShow", value = "列表显示") + @CheckNumberAnnotation(name = "列表显示", types = {"0", "1"}) + private Integer listShow; + @ApiModelProperty(name = "formShow", value = "表单显示") + @CheckNumberAnnotation(name = "表单显示", types = {"0", "1"}) + private Integer formShow; public String getFieldName() { return fieldName == null ? "" : fieldName.trim(); @@ -101,6 +109,22 @@ public class DynamicFormFieldVO { 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("{"); @@ -120,6 +144,10 @@ public class DynamicFormFieldVO { .append("\"").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/impl/DynamicDataServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicDataServiceImpl.java index bf76da5..76ec713 100644 --- 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 @@ -12,13 +12,18 @@ 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.RegexUtil; +import com.cm.common.utils.UUIDUtil; import com.cm.common.utils.WStringUtil; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** * When you feel like quitting. Think about why you started @@ -41,8 +46,25 @@ public class DynamicDataServiceImpl extends AbstractService implements IDynamicD @Override public SuccessResult saveDynamicData(String tableName, Map params) throws SearchException, SaveException { List dynamicFormDTOs = dynamicFormService.listDynamicForm(tableName); + LOG.debug("校验参数"); requireData(params, dynamicFormDTOs); - return null; + LOG.debug("获取插入列和插入值"); + List insertFieldList = new ArrayList<>(); + List insertValueList = new ArrayList<>(); + for (DynamicFormDTO dynamicFormDTO : dynamicFormDTOs) { + insertFieldList.add(WStringUtil.lowerUpper2UnderLine(dynamicFormDTO.getFieldName())); + insertValueList.add(params.get(dynamicFormDTO.getFieldName())); + } + params.clear(); + String lowerUnderLineTableName = WStringUtil.lowerUpper2UnderLine(tableName); + params.put("tableName", String.format("%s%s", IDynamicTableService.DYNAMIC_TABLE_PREFIX, lowerUnderLineTableName)); + params.put("uuidField", String.format("%s_id", lowerUnderLineTableName)); + params.put("uuidValue", UUIDUtil.getUUID()); + params.put("insertFieldList", insertFieldList); + params.put("insertValueList", insertValueList); + setSaveInfo(params); + dynamicDataDao.saveDynamicData(params); + return new SuccessResult(); } /** @@ -52,6 +74,12 @@ public class DynamicDataServiceImpl extends AbstractService implements IDynamicD * @param dynamicFormDTOs */ private void requireData(Map params, List dynamicFormDTOs) { + if (params.isEmpty()) { + throw new SaveException("新增数据为空"); + } + if (dynamicFormDTOs == null || dynamicFormDTOs.isEmpty()) { + throw new SaveException("新增表列为空"); + } for (Map.Entry entry : params.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); @@ -60,37 +88,37 @@ public class DynamicDataServiceImpl extends AbstractService implements IDynamicD 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)); + if (value == null || StringUtils.isBlank(value.toString())) { + throw new ParamsException(String.format("%s不能为空", key)); + } + if (StringUtils.equals(FieldRequireTypeEnum.PHONE.getValue(), dynamicFormDTO.getVerifyType())) { + if (RegexUtil.isPhone(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)); + if (RegexUtil.isEmail(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)); + if (RegexUtil.isEmail(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)); + if (NumberUtils.isNumber(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)); + if (RegexUtil.isDate(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)); + if (RegexUtil.isIdentity(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)); + Pattern pattern = Pattern.compile(dynamicFormDTO.getVerifyRegular()); + if (RegexUtil.isMatch(pattern, 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/DynamicTableServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/dynamic/impl/DynamicTableServiceImpl.java index 1b60be6..2ab1d0e 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 @@ -73,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(DYNAMIC_TABLE_PREFIX).append(WStringUtil.lowerUpper2UnderLine(tableName)).append("`("); + StringBuilder sql = new StringBuilder("CREATE TABLE `").append(DYNAMIC_TABLE_PREFIX).append(underLineTableName).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) { diff --git a/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicdata-mapper.xml b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicdata-mapper.xml new file mode 100644 index 0000000..a55daa5 --- /dev/null +++ b/cloud-common-plugin/src/main/resources/mybatis/mapper/dynamic/dynamicdata-mapper.xml @@ -0,0 +1,30 @@ + + + + + + + INSERT INTO ${tableName}( + ${uuidField}, + + ${insertField} + + creator, + gmt_create, + modifier, + gmt_modified, + is_delete + ) VALUES( + #{uuidValue}, + + #{insertValue} + + #{creator}, + #{gmtCreate}, + #{modifier}, + #{gmtModified}, + #{isDelete} + ) + + + \ No newline at end of file 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 fd31b92..b35df13 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 @@ -29,7 +29,9 @@ dictionary_id, verify_type, verify_regular, - field_sort + field_sort, + list_show, + form_show ) VALUES( #{id}, #{tableName}, @@ -38,9 +40,11 @@ #{fieldType}, #{fieldDefault}, #{dictionaryId}, - #{verifytype}, + #{verifyType}, #{verifyRegular}, - #{fieldSort} + #{fieldSort}, + #{listShow}, + #{formShow} ) @@ -56,7 +60,9 @@ dictionary_id = #{dictionaryId}, verify_type = #{verifyType}, verify_regular = #{verifyRegular}, - field_sort = #{fieldSort} + field_sort = #{fieldSort}, + list_show = #{listShow}, + form_show = #{formShow} WHERE table_name = #{tableName} AND