feat:提供流水号生成能力

This commit is contained in:
wangyu 2022-01-07 16:25:37 +08:00
parent 28284cddec
commit 111b7f68e8
10 changed files with 190 additions and 28 deletions

View File

@ -3,8 +3,6 @@ package com.flyfish.framework.config;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.function.Supplier;
/** /**
* 框架配置 * 框架配置
* *
@ -20,8 +18,6 @@ public class FrameworkConfiguration {
private boolean shortPassword = false; private boolean shortPassword = false;
private Supplier<String> codeSupplier = null;
public FrameworkConfiguration resultStyle() { public FrameworkConfiguration resultStyle() {
this.resultStyle = true; this.resultStyle = true;
return this; return this;
@ -36,9 +32,4 @@ public class FrameworkConfiguration {
this.shortPassword = true; this.shortPassword = true;
return this; return this;
} }
public FrameworkConfiguration codeSupplier(Supplier<String> codeSupplier) {
this.codeSupplier = codeSupplier;
return this;
}
} }

View File

@ -2,8 +2,6 @@ package com.flyfish.framework.constant;
import com.flyfish.framework.config.FrameworkConfiguration; import com.flyfish.framework.config.FrameworkConfiguration;
import java.util.function.Supplier;
/** /**
* 框架静态配置 * 框架静态配置
* *
@ -25,16 +23,4 @@ public interface Frameworks {
return config.debug(); return config.debug();
} }
static FrameworkConfiguration codeSupplier(Supplier<String> codeSupplier) {
return config.codeSupplier(codeSupplier);
}
static String getCode() {
if (null != config.getCodeSupplier()) {
return config.getCodeSupplier().get();
}
return null;
}
} }

View File

@ -33,11 +33,12 @@ public class ReflectionUtils {
* @param clazz * @param clazz
* @return 结果 * @return 结果
*/ */
public static Object instantiate(Class<?> clazz) { @SuppressWarnings("unchecked")
public static <T> T instantiate(Class<?> clazz) {
try { try {
return clazz.newInstance(); return (T) clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) { } catch (InstantiationException | IllegalAccessException e) {
return new RuntimeException("对象实例化失败!" + e.getMessage()); throw new RuntimeException("对象实例化失败!" + e.getMessage());
} }
} }

View File

@ -0,0 +1,23 @@
package com.flyfish.framework.annotations;
import com.flyfish.framework.generation.CodeRuleStrategy;
import com.flyfish.framework.generation.CodeRules;
import java.lang.annotation.*;
/**
* 代码生成规则指定
* 用于code值得初始化区分场景
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CodeRule {
/**
* 策略实现类
*
* @return 结果
*/
Class<? extends CodeRuleStrategy> value() default CodeRules.UUID;
}

View File

@ -0,0 +1,14 @@
package com.flyfish.framework.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 代码规则配置
*
* @author wangyu
*/
@Configuration
@EnableScheduling
public class CodeRuleConfig {
}

View File

@ -3,9 +3,11 @@ package com.flyfish.framework.domain.base;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.flyfish.framework.annotations.Generation; import com.flyfish.framework.annotations.Generation;
import com.flyfish.framework.annotations.Property; import com.flyfish.framework.annotations.Property;
import com.flyfish.framework.constant.Frameworks; import com.flyfish.framework.generation.CodeRules;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.index.Indexed;
@ -34,7 +36,8 @@ public abstract class Domain implements Po, Named, Serializable {
@Property(title = "编码", inherited = true) @Property(title = "编码", inherited = true)
@NotBlank(message = "编码不可为空") @NotBlank(message = "编码不可为空")
@Generation(Generation.Strategy.CODE) @Generation(Generation.Strategy.CODE)
protected String code = Frameworks.getCode(); @Getter(AccessLevel.NONE)
protected String code;
/** /**
* 名称 * 名称
@ -53,6 +56,18 @@ public abstract class Domain implements Po, Named, Serializable {
@Property(readonly = true) @Property(readonly = true)
private IUser currentUser; private IUser currentUser;
/**
* 自动生成code环节
*
* @return 结果
*/
public String getCode() {
if (StringUtils.isBlank(id) && StringUtils.isBlank(code)) {
code = CodeRules.getCode(this);
}
return code;
}
@Override @Override
public int hashCode() { public int hashCode() {
if (id != null) { if (id != null) {

View File

@ -0,0 +1,19 @@
package com.flyfish.framework.generation;
import com.flyfish.framework.domain.base.Domain;
/**
* 代码规则策略
*
* @author wangyu
*/
public interface CodeRuleStrategy {
/**
* 生成代码
*
* @param domain 实体
* @return 结果
*/
String generate(Domain domain);
}

View File

@ -0,0 +1,43 @@
package com.flyfish.framework.generation;
import com.flyfish.framework.annotations.CodeRule;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.generation.strategy.TransactionCodeRuleStrategy;
import com.flyfish.framework.generation.strategy.UUIDCodeRuleStrategy;
import com.flyfish.framework.utils.ReflectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 代码规则工具类
*
* @author wangyu
*/
public interface CodeRules {
// 通用uuid生成
Class<? extends CodeRuleStrategy> UUID = UUIDCodeRuleStrategy.class;
// 流水号生成
Class<? extends CodeRuleStrategy> TRANSACTION = TransactionCodeRuleStrategy.class;
// 静态策略map
Map<Class<? extends CodeRuleStrategy>, CodeRuleStrategy> strategies = new ConcurrentHashMap<>();
/**
* 获取code
*
* @param instance 实体
* @return 结果
*/
static String getCode(Domain instance) {
CodeRule rule = instance.getClass().getAnnotation(CodeRule.class);
if (null != rule && StringUtils.isBlank(instance.getId())) {
CodeRuleStrategy strategy = strategies.computeIfAbsent(rule.value(), ReflectionUtils::instantiate);
return strategy.generate(instance);
}
return null;
}
}

View File

@ -0,0 +1,44 @@
package com.flyfish.framework.generation.strategy;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.generation.CodeRuleStrategy;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 流水号生成策略
*
* @author wangyu
*/
@Component
public class TransactionCodeRuleStrategy implements CodeRuleStrategy {
// 每天凌晨自动清零
private final AtomicInteger atomicInteger = new AtomicInteger(0);
/**
* 每天0点自动清零
*/
@Scheduled(cron = "0 0 0 * * ?")
public void clear() {
atomicInteger.set(0);
}
/**
* 生成代码
*
* @param domain 实体
* @return 结果
*/
@Override
public String generate(Domain domain) {
String date = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
String flow = StringUtils.leftPad(String.valueOf(atomicInteger.incrementAndGet()), 4, "0");
return date + flow;
}
}

View File

@ -0,0 +1,26 @@
package com.flyfish.framework.generation.strategy;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.generation.CodeRuleStrategy;
import com.flyfish.framework.utils.UUIDUtils;
import org.springframework.stereotype.Component;
/**
* UUID代码生成
*
* @author wangyu
*/
@Component
public class UUIDCodeRuleStrategy implements CodeRuleStrategy {
/**
* 生成代码
*
* @param domain 实体
* @return 结果
*/
@Override
public String generate(Domain domain) {
return UUIDUtils.generateShortUuid();
}
}