新增动态数据新增

This commit is contained in:
wenc000 2019-12-04 23:14:41 +08:00
parent b55f243f74
commit 7c4a058ee9
6 changed files with 130 additions and 28 deletions

View File

@ -1,7 +1,11 @@
package com.cm.common.plugin.dao.dynamic; 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 org.springframework.stereotype.Repository;
import java.util.Map;
/** /**
* When you feel like quitting. Think about why you started * When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始 * 当你想要放弃的时候想想当初你为何开始
@ -13,8 +17,14 @@ import org.springframework.stereotype.Repository;
* @Version: 1.0 * @Version: 1.0
**/ **/
@Repository @Repository
public class IDynamicDataDao { public interface IDynamicDataDao {
/**
* 新增动态数据
*
* @param params
* @return
* @throws SaveException
*/
void saveDynamicData(Map<String, Object> params) throws SaveException;
} }

View File

@ -1,6 +1,7 @@
package com.cm.common.plugin.pojo.vos.dynamicform; package com.cm.common.plugin.pojo.vos.dynamicform;
import com.cm.common.annotation.CheckEmptyAnnotation; import com.cm.common.annotation.CheckEmptyAnnotation;
import com.cm.common.annotation.CheckNumberAnnotation;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -31,11 +32,18 @@ public class DynamicFormFieldVO {
@ApiModelProperty(name = "dictionaryId", value = "字典ID") @ApiModelProperty(name = "dictionaryId", value = "字典ID")
private String dictionaryId; private String dictionaryId;
@ApiModelProperty(name = "verifyType", value = "校验类型") @ApiModelProperty(name = "verifyType", value = "校验类型")
@CheckEmptyAnnotation(name = "校验类型", types = {"none", "required", "phone", "email", "url", "number", "date", "identity", "custom"})
private String verifyType; private String verifyType;
@ApiModelProperty(name = "verifyRegular", value = "校验正则") @ApiModelProperty(name = "verifyRegular", value = "校验正则")
private String verifyRegular; private String verifyRegular;
@ApiModelProperty(name = "fieldSort", value = "排序") @ApiModelProperty(name = "fieldSort", value = "排序")
private Integer fieldSort; 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() { public String getFieldName() {
return fieldName == null ? "" : fieldName.trim(); return fieldName == null ? "" : fieldName.trim();
@ -101,6 +109,22 @@ public class DynamicFormFieldVO {
this.fieldSort = 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 @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder("{"); final StringBuilder sb = new StringBuilder("{");
@ -120,6 +144,10 @@ public class DynamicFormFieldVO {
.append("\"").append(verifyRegular).append("\""); .append("\"").append(verifyRegular).append("\"");
sb.append(",\"fieldSort\":") sb.append(",\"fieldSort\":")
.append(fieldSort); .append(fieldSort);
sb.append(",\"listShow\":")
.append(listShow);
sb.append(",\"formShow\":")
.append(formShow);
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }

View File

@ -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.IDynamicFormService;
import com.cm.common.plugin.service.dynamic.IDynamicTableService; import com.cm.common.plugin.service.dynamic.IDynamicTableService;
import com.cm.common.result.SuccessResult; 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 com.cm.common.utils.WStringUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
/** /**
* When you feel like quitting. Think about why you started * When you feel like quitting. Think about why you started
@ -41,8 +46,25 @@ public class DynamicDataServiceImpl extends AbstractService implements IDynamicD
@Override @Override
public SuccessResult saveDynamicData(String tableName, Map<String, Object> params) throws SearchException, SaveException { public SuccessResult saveDynamicData(String tableName, Map<String, Object> params) throws SearchException, SaveException {
List<DynamicFormDTO> dynamicFormDTOs = dynamicFormService.listDynamicForm(tableName); List<DynamicFormDTO> dynamicFormDTOs = dynamicFormService.listDynamicForm(tableName);
LOG.debug("校验参数");
requireData(params, dynamicFormDTOs); requireData(params, dynamicFormDTOs);
return null; LOG.debug("获取插入列和插入值");
List<String> insertFieldList = new ArrayList<>();
List<Object> 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 * @param dynamicFormDTOs
*/ */
private void requireData(Map<String, Object> params, List<DynamicFormDTO> dynamicFormDTOs) { private void requireData(Map<String, Object> params, List<DynamicFormDTO> dynamicFormDTOs) {
if (params.isEmpty()) {
throw new SaveException("新增数据为空");
}
if (dynamicFormDTOs == null || dynamicFormDTOs.isEmpty()) {
throw new SaveException("新增表列为空");
}
for (Map.Entry<String, Object> entry : params.entrySet()) { for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
Object value = entry.getValue(); Object value = entry.getValue();
@ -60,37 +88,37 @@ public class DynamicDataServiceImpl extends AbstractService implements IDynamicD
if (dynamicFormDTO.getFormShow() == 1 if (dynamicFormDTO.getFormShow() == 1
&& StringUtils.equals(key, dynamicFormDTO.getFieldName()) && StringUtils.equals(key, dynamicFormDTO.getFieldName())
&& !StringUtils.equals(FieldRequireTypeEnum.NONE.getValue(), dynamicFormDTO.getVerifyType())) { && !StringUtils.equals(FieldRequireTypeEnum.NONE.getValue(), dynamicFormDTO.getVerifyType())) {
if (StringUtils.equals(FieldRequireTypeEnum.REQUIRED.getValue(), dynamicFormDTO.getVerifyType())) { if (value == null || StringUtils.isBlank(value.toString())) {
if (value == null || StringUtils.isBlank(value.toString())) { throw new ParamsException(String.format("%s不能为空", key));
throw new ParamsException(String.format("%s不能为空", key)); }
} if (StringUtils.equals(FieldRequireTypeEnum.PHONE.getValue(), dynamicFormDTO.getVerifyType())) {
} else if (StringUtils.equals(FieldRequireTypeEnum.PHONE.getValue(), dynamicFormDTO.getVerifyType())) { if (RegexUtil.isPhone(value.toString())) {
if (value == null || StringUtils.isBlank(value.toString())) { throw new ParamsException(String.format("%s必须为手机号码格式", key));
throw new ParamsException(String.format("%s不能为空", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.EMAIL.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.EMAIL.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { if (RegexUtil.isEmail(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key)); throw new ParamsException(String.format("%s必须为邮箱格式", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.URL.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.URL.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { if (RegexUtil.isEmail(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key)); throw new ParamsException(String.format("%s必须为链接格式", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.NUMBER.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.NUMBER.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { if (NumberUtils.isNumber(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key)); throw new ParamsException(String.format("%s必须为数字格式", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.DATE.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.DATE.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { if (RegexUtil.isDate(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key)); throw new ParamsException(String.format("%s必须为日期格式", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.IDENTITY.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.IDENTITY.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { if (RegexUtil.isIdentity(value.toString())) {
throw new ParamsException(String.format("%s不能为空", key)); throw new ParamsException(String.format("%s必须为身份证格式", key));
} }
} else if (StringUtils.equals(FieldRequireTypeEnum.CUSTOM.getValue(), dynamicFormDTO.getVerifyType())) { } else if (StringUtils.equals(FieldRequireTypeEnum.CUSTOM.getValue(), dynamicFormDTO.getVerifyType())) {
if (value == null || StringUtils.isBlank(value.toString())) { Pattern pattern = Pattern.compile(dynamicFormDTO.getVerifyRegular());
throw new ParamsException(String.format("%s不能为空", key)); if (RegexUtil.isMatch(pattern, value.toString())) {
throw new ParamsException(String.format("%s不符合自定义格式", key));
} }
} }
break; break;

View File

@ -73,7 +73,7 @@ public class DynamicTableServiceImpl extends AbstractService implements IDynamic
*/ */
private String getSaveTableSQL(String tableName, List<TableColumnVO> tableColumns) { private String getSaveTableSQL(String tableName, List<TableColumnVO> tableColumns) {
String underLineTableName = WStringUtil.lowerUpper2UnderLine(tableName); 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("`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',");
sql.append(String.format("`%s_id` CHAR(36) NOT NULL COMMENT '主键',", underLineTableName)); sql.append(String.format("`%s_id` CHAR(36) NOT NULL COMMENT '主键',", underLineTableName));
for (TableColumnVO tableColumn : tableColumns) { for (TableColumnVO tableColumn : tableColumns) {

View File

@ -0,0 +1,30 @@
<?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.dynamic.IDynamicDataDao">
<!-- 新增动态数据 -->
<insert id="saveDynamicData" parameterType="map">
INSERT INTO ${tableName}(
${uuidField},
<foreach collection="insertFieldList" item="insertField" open="" separator="," close=",">
${insertField}
</foreach>
creator,
gmt_create,
modifier,
gmt_modified,
is_delete
) VALUES(
#{uuidValue},
<foreach collection="insertValueList" item="insertValue" open="" separator="," close=",">
#{insertValue}
</foreach>
#{creator},
#{gmtCreate},
#{modifier},
#{gmtModified},
#{isDelete}
)
</insert>
</mapper>

View File

@ -29,7 +29,9 @@
dictionary_id, dictionary_id,
verify_type, verify_type,
verify_regular, verify_regular,
field_sort field_sort,
list_show,
form_show
) VALUES( ) VALUES(
#{id}, #{id},
#{tableName}, #{tableName},
@ -38,9 +40,11 @@
#{fieldType}, #{fieldType},
#{fieldDefault}, #{fieldDefault},
#{dictionaryId}, #{dictionaryId},
#{verifytype}, #{verifyType},
#{verifyRegular}, #{verifyRegular},
#{fieldSort} #{fieldSort},
#{listShow},
#{formShow}
) )
</insert> </insert>
@ -56,7 +60,9 @@
dictionary_id = #{dictionaryId}, dictionary_id = #{dictionaryId},
verify_type = #{verifyType}, verify_type = #{verifyType},
verify_regular = #{verifyRegular}, verify_regular = #{verifyRegular},
field_sort = #{fieldSort} field_sort = #{fieldSort},
list_show = #{listShow},
form_show = #{formShow}
WHERE WHERE
table_name = #{tableName} table_name = #{tableName}
AND AND