spring增强策略模式-原始
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring;
|
||||||
|
|
||||||
|
import top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 原始逻辑
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:38
|
||||||
|
*/
|
||||||
|
public class FormItemConverter {
|
||||||
|
/**
|
||||||
|
* 将输入的配置转变为表单项
|
||||||
|
*
|
||||||
|
* @param config 前端输入的配置
|
||||||
|
* @return 表单项
|
||||||
|
*/
|
||||||
|
public FormItem convert(FormItemConfig config) {
|
||||||
|
FormItem formItem = new FormItem();
|
||||||
|
|
||||||
|
// 公共的表单项属性
|
||||||
|
formItem.setTitle(config.getTitle());
|
||||||
|
formItem.setCode(config.getCode());
|
||||||
|
formItem.setComponent(config.getComponent());
|
||||||
|
|
||||||
|
// 创建表单组件的属性
|
||||||
|
FormComponentProps props = new FormComponentProps();
|
||||||
|
formItem.setComponentProps(props);
|
||||||
|
|
||||||
|
// 公共的组件属性
|
||||||
|
if (config.isReadOnly()) {
|
||||||
|
props.setReadOnly(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentTypeEnum type = config.getType();
|
||||||
|
|
||||||
|
// 下拉选择框的特殊属性处理
|
||||||
|
if (type == ComponentTypeEnum.DROPDOWN_SELECT) {
|
||||||
|
props.setAutoWidth(false);
|
||||||
|
|
||||||
|
if (config.isMultiple()) {
|
||||||
|
props.setMode("multiple");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 模糊搜索框的特殊属性处理
|
||||||
|
if (type == ComponentTypeEnum.FUZZY_SEARCH) {
|
||||||
|
formItem.setFuzzySearch(true);
|
||||||
|
props.setAutoWidth(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... 其他组件的特殊处理
|
||||||
|
|
||||||
|
// 创建约束规则
|
||||||
|
List<FormItemRule> rules = new ArrayList<>(2);
|
||||||
|
formItem.setRules(rules);
|
||||||
|
|
||||||
|
// 每个表单项都可有的约束规则
|
||||||
|
if (config.isRequired()) {
|
||||||
|
FormItemRule requiredRule = new FormItemRule();
|
||||||
|
requiredRule.setRequired(true);
|
||||||
|
requiredRule.setMessage("请输入" + config.getTitle());
|
||||||
|
|
||||||
|
rules.add(requiredRule);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文本输入框才有的规则
|
||||||
|
if (type == ComponentTypeEnum.TEXT_INPUT || type == ComponentTypeEnum.TEXT_AREA) {
|
||||||
|
Integer minLength = config.getMinLength();
|
||||||
|
|
||||||
|
if (minLength != null && minLength > 0) {
|
||||||
|
FormItemRule minRule = new FormItemRule();
|
||||||
|
minRule.setMin(minLength);
|
||||||
|
minRule.setMessage("请至少输入 " + minLength + " 个字");
|
||||||
|
|
||||||
|
rules.add(minRule);
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer maxLength = config.getMaxLength();
|
||||||
|
|
||||||
|
if (maxLength != null && maxLength > 0) {
|
||||||
|
FormItemRule maxRule = new FormItemRule();
|
||||||
|
maxRule.setMax(maxLength);
|
||||||
|
maxRule.setMessage("请最多输入 " + maxLength + " 个字");
|
||||||
|
|
||||||
|
rules.add(maxRule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... 其他约束规则
|
||||||
|
|
||||||
|
return formItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:43
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum ComponentTypeEnum {
|
||||||
|
DROPDOWN_SELECT,
|
||||||
|
FUZZY_SEARCH,
|
||||||
|
TEXT_INPUT,
|
||||||
|
TEXT_AREA;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:40
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FormComponentProps {
|
||||||
|
|
||||||
|
private boolean readOnly;
|
||||||
|
|
||||||
|
private boolean autoWidth;
|
||||||
|
|
||||||
|
private String mode;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:39
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FormItem {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private String component;
|
||||||
|
|
||||||
|
private FormComponentProps componentProps;
|
||||||
|
|
||||||
|
private boolean fuzzySearch;
|
||||||
|
|
||||||
|
private List<FormItemRule> rules;
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:39
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FormItemConfig {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private String component;
|
||||||
|
|
||||||
|
private boolean readOnly;
|
||||||
|
|
||||||
|
private ComponentTypeEnum type;
|
||||||
|
|
||||||
|
private boolean multiple;
|
||||||
|
|
||||||
|
private boolean required;
|
||||||
|
|
||||||
|
private Integer minLength;
|
||||||
|
|
||||||
|
private Integer maxLength;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:46
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FormItemRule {
|
||||||
|
|
||||||
|
private boolean required;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
private Integer min;
|
||||||
|
|
||||||
|
private Integer max;
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.templatemethod.spring.bean;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO:
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2022/1/26 09:41
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum FormItemTypeEnum {
|
||||||
|
;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user