diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/enums/BusinessTypeEnum.java b/youlai-common/common-core/src/main/java/com/youlai/common/enums/BusinessTypeEnum.java new file mode 100644 index 000000000..13b49f389 --- /dev/null +++ b/youlai-common/common-core/src/main/java/com/youlai/common/enums/BusinessTypeEnum.java @@ -0,0 +1,34 @@ +package com.youlai.common.enums; + +import lombok.Getter; +import lombok.Setter; + +/** + * + * @author haoxr + * @date 2021-02-17 13:13 + * + */ +public enum BusinessTypeEnum { + + USER("100", "用户类型编号"), + MEMBER("200", "会员类型编号"), + ORDER("300", "订单类型编号"); + + @Getter + @Setter + private String code; + + BusinessTypeEnum(String code, String desc) { + this.code = code; + } + + public static BusinessTypeEnum getValue(String code){ + for (BusinessTypeEnum value : values()) { + if (value.getCode().equals(code)) { + return value; + } + } + return null; + } +} diff --git a/youlai-common/common-redis/pom.xml b/youlai-common/common-redis/pom.xml index 5e46fa2a9..8d95a1c83 100644 --- a/youlai-common/common-redis/pom.xml +++ b/youlai-common/common-redis/pom.xml @@ -11,6 +11,11 @@ common-redis + + com.youlai + common-core + + org.springframework.boot spring-boot-starter-data-redis diff --git a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/component/BusinessNoGenerator.java b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/component/BusinessNoGenerator.java new file mode 100644 index 000000000..0cc8b2661 --- /dev/null +++ b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/component/BusinessNoGenerator.java @@ -0,0 +1,43 @@ +package com.youlai.common.redis.component; + +import com.youlai.common.enums.BusinessTypeEnum; +import com.youlai.common.redis.constant.RedisKeyConstants; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; + + +@Component +@Slf4j +public class BusinessNoGenerator { + + @Autowired + private RedisTemplate redisTemplate; + + /** + * @param businessCode 业务类型编号 + * @param digit 业务序号位数 + * @return + */ + public String generate(String businessCode, Integer digit) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); + // String date = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); + + String date="20210216"; + String key = RedisKeyConstants.BUSINESS_NO_PREFIX + BusinessTypeEnum.getValue(businessCode).toString().toLowerCase() + ":" + date; + Long increment = redisTemplate.opsForValue().increment(key); + return date + businessCode + String.format("%0" + digit + "d", increment); + } + + + public String generate(String businessCode) { + Integer defaultDigit = 6; + return generate(businessCode, defaultDigit); + } + +} diff --git a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/constant/RedisKeyConstants.java b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/constant/RedisKeyConstants.java index 875ee5c9c..6ea6e2163 100644 --- a/youlai-common/common-redis/src/main/java/com/youlai/common/redis/constant/RedisKeyConstants.java +++ b/youlai-common/common-redis/src/main/java/com/youlai/common/redis/constant/RedisKeyConstants.java @@ -8,5 +8,7 @@ public interface RedisKeyConstants { String MALL_CART_KEY = YOU_LAI + "cart:"; - String TOKEN_VERIFY ="token_verify:"; + String TOKEN_VERIFY = "token_verify:"; + + String BUSINESS_NO_PREFIX = "businessno:"; } diff --git a/youlai-common/common-redis/src/main/resources/META-INF/spring.factories b/youlai-common/common-redis/src/main/resources/META-INF/spring.factories index d88909a35..fe5c0c95c 100644 --- a/youlai-common/common-redis/src/main/resources/META-INF/spring.factories +++ b/youlai-common/common-redis/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.youlai.common.redis.RedisConfig + com.youlai.common.redis.RedisConfig,\ + com.youlai.common.redis.component.BusinessNoGenerator