feat:添加业务编号生成方法

This commit is contained in:
haoxr 2021-02-17 14:56:07 +08:00
parent 25b5dc1cbc
commit b73cc79e5b
5 changed files with 87 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -11,6 +11,11 @@
<artifactId>common-redis</artifactId>
<dependencies>
<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>

View File

@ -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);
}
}

View File

@ -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:";
}

View File

@ -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