提交 cca5b784 authored 作者: liruixin's avatar liruixin

提前生成运单号

上级 595cc54b
/*
package com.clx.performance.job;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
......@@ -8,7 +7,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
......@@ -29,26 +27,22 @@ public class OrderNoGenerateJob implements InitializingBean {
@Autowired
private ThreadPoolExecutor executor;
@Resource
private ListOperations<String, Object> opsForListJson;
@Resource
private RedisTemplate<String, Object> jsonTemplate;
*/
/**
/**
* 每天早上2点生成第二天的订单号 cron = "0 0 2 * * ?"
*
* 提前生成订单号
*//*
*/
@XxlJob("generateOrderNo")
public void generateOrderNo() throws InterruptedException {
public void generateOrderNo()throws InterruptedException {
LocalDateTime today = LocalDateTime.now();
//检验今天的订单池有没有,没有就生成一套
String todayKey = RedisConstants.CARRIER_ORDER_NUM_POOL_KEY.replace("{date}", today.format(DateTimeFormatter.BASIC_ISO_DATE));
Integer orderNum = (Integer) opsForListJson.leftPop(todayKey);
Integer orderNum = (Integer) jsonTemplate.opsForList().leftPop(todayKey);
if(null == orderNum){
log.warn("开始生成订单池: key->{}", todayKey);
saveUniqueOrderNumList(todayKey, generateOrderNumList());
......@@ -58,7 +52,7 @@ public class OrderNoGenerateJob implements InitializingBean {
LocalDateTime tomorrow = today.plusDays(1);
//检验明天的订单池,没有就生成一套
String tomorrowKey = RedisConstants.CARRIER_ORDER_NUM_POOL_KEY.replace("{date}", tomorrow.format(DateTimeFormatter.BASIC_ISO_DATE));
orderNum = (Integer) opsForListJson.leftPop(tomorrowKey);
orderNum = (Integer) jsonTemplate.opsForList().leftPop(tomorrowKey);
if(null == orderNum){
log.warn("开始生成订单池: key->{}", tomorrowKey);
saveUniqueOrderNumList(tomorrowKey, generateOrderNumList());
......@@ -71,22 +65,22 @@ public class OrderNoGenerateJob implements InitializingBean {
*/
/**
/**
* 保存到Redis
* @author
* @param key
* @param data
*//*
*/
private void saveUniqueOrderNumList(String key, List data) {
if(StringUtils.isBlank(key)){
log.info(key, "key不能为空");
return;
}
if(CollectionUtils.isEmpty(data)){
log.info(key, "data不能为空");
return;
}
for (int i = 0, j = 0; i < data.size(); i += 10000){
......@@ -98,7 +92,7 @@ public class OrderNoGenerateJob implements InitializingBean {
j = data.size();
}
try {
opsForListJson.leftPushAll(key, data.subList(i, j));
jsonTemplate.opsForList().leftPushAll(key, data.subList(i, j));
} catch (Exception e){
e.printStackTrace();
continue;
......@@ -107,13 +101,11 @@ public class OrderNoGenerateJob implements InitializingBean {
}
*/
/**
/**
* 生成乱序的6位的订单编号
* @author
* @return 乱序的6位订单编号从100000到999999
*//*
*/
private List<Integer> generateOrderNumList(){
List<Integer> orderNumList = new ArrayList<>();
for (Integer i = 100000; i < 999999; ++i){
......@@ -123,15 +115,13 @@ public class OrderNoGenerateJob implements InitializingBean {
return orderNumList;
}
*/
/**
/**
* 交换
* @author
* @param data
* @param i
* @param j
*//*
*/
private static void swap(List<Integer> data, int i, int j) {
if (i == j) {
return;
......@@ -141,13 +131,11 @@ public class OrderNoGenerateJob implements InitializingBean {
data.set(j, temp);
}
*/
/**
/**
* 洗牌
* @author
* @param data
*//*
*/
private static void shuffleSort(List<Integer> data) {
for (int i = 0; i < data.size() - 1; i++) {
int j = ThreadLocalRandom.current().nextInt(data.size());
......@@ -155,11 +143,11 @@ public class OrderNoGenerateJob implements InitializingBean {
}
}
*/
/**
/**
* 初始化
* @throws Exception
*//*
*/
@Override
public void afterPropertiesSet() throws Exception {
......@@ -178,6 +166,4 @@ public class OrderNoGenerateJob implements InitializingBean {
}
*/
package com.clx.performance.service.impl;
import com.clx.performance.constant.RedisConstants;
import com.clx.performance.enums.PerformanceResultEnum;
import com.msl.common.exception.ServiceSystemException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.ThreadLocalRandom;
@Slf4j
@Service
public class UniqueOrderNumService {
@Autowired
RedisTemplate<String, Object> jsonTemplate;
/**
* 获取指定日期唯一的订单号
* @param date 日期
* @return 所指定日期的唯一订单号
*/
public String getUniqueOrderNum(String date) {
if(StringUtils.isBlank(date)){
throw new ServiceSystemException(PerformanceResultEnum.DATA_NOT_FIND);
}
Integer orderNum = (Integer) jsonTemplate.opsForList().leftPop(RedisConstants.CARRIER_ORDER_NUM_POOL_KEY.replace("{date}", date));
if(null == orderNum){
throw new ServiceSystemException(PerformanceResultEnum.DATA_NOT_FIND);
}
return new StringBuilder().append(date).append(orderNum).append(String.format("%02d", ThreadLocalRandom.current().nextInt(99))).toString();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论