package cn.autoform.web.client.util.auto; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; public class ParamFactory { private static final Map RULE_ACTUATOR = new HashMap<>(); private static String begain; static{ /** * 日期 */ RULE_ACTUATOR.put("d", ruleParam->{ return new SimpleDateFormat(ruleParam.getCondition()).format(new Date()); }); /** * 随机字符 */ RULE_ACTUATOR.put("rs", ruleParam->{ return RandomStringUtils.randomAlphanumeric(Integer.parseInt(ruleParam.getCondition())); }); /** * 随机数字 */ RULE_ACTUATOR.put("rn", ruleParam->{ StringBuilder num = new StringBuilder(); for (int i = 0; i < Integer.parseInt(ruleParam.getCondition()); i++) { num.append('9'); } return String.format("%0"+ruleParam.getCondition()+"d", RandomUtils.nextInt(0, Integer.parseInt(num.toString()))); }); RULE_ACTUATOR.put("in", ruleParam->{ StringBuilder num = new StringBuilder(); Integer begain = getBegain().length(); for (int i = 0; i < Integer.parseInt(ruleParam.getCondition())-begain; i++) { num.append('0'); } num.append(getBegain()); return num.toString(); }); } /** * 获得参数执行器 * @param type * @return */ public static RuleActuator getRuleActuator(String type){ type = type.toLowerCase(); return RULE_ACTUATOR.get(type); } private static String getBegain() { return begain; } public static void setBegain(String begain) { ParamFactory.begain = begain; } }