您的位置:首页 > 房产 > 家装 > 移动终端开发技术_信阳市人民政府领导_今日重要新闻_阿里云免费域名

移动终端开发技术_信阳市人民政府领导_今日重要新闻_阿里云免费域名

2024/12/27 14:11:56 来源:https://blog.csdn.net/Gemini1995/article/details/144614335  浏览:    关键词:移动终端开发技术_信阳市人民政府领导_今日重要新闻_阿里云免费域名
移动终端开发技术_信阳市人民政府领导_今日重要新闻_阿里云免费域名
package org.gin.utils;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.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;@Component
public class OrderNumBuilder {@Autowiredprivate RedisTemplate<String, Long> customRedisTemplate;private static final String ORDER_NUMBER_PREFIX = "";private static final String REDIS_ORDER_NUMBER_KEY = "gin:orderNo";// 按需修改private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyMMdd");// 按需修改private static final Lock lock = new ReentrantLock();public String getOrderNo() {lock.lock();String orderNo = null;try {Object exitOrderNo=customRedisTemplate.opsForValue().get(REDIS_ORDER_NUMBER_KEY);// 获取redis此时订单号if (exitOrderNo==null) {// 初始化customRedisTemplate.opsForValue().set(REDIS_ORDER_NUMBER_KEY, Long.valueOf(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).format(dateTimeFormatter) + "00000"));}// 获取当前订单号long currentOrder = customRedisTemplate.opsForValue().increment(REDIS_ORDER_NUMBER_KEY, 1L);long current = Long.valueOf(String.valueOf(currentOrder).substring(0, 6)); // 按需修改// 当前时间long now = Long.valueOf(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).format(dateTimeFormatter));if (current != now) {if (now < current) {while (true) {now = Long.valueOf(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).format(dateTimeFormatter));if (now == current) {// 重新  开始customRedisTemplate.opsForValue().set(REDIS_ORDER_NUMBER_KEY, Long.valueOf(now + "00000"));// 按需修改orderNo = ORDER_NUMBER_PREFIX + customRedisTemplate.opsForValue().increment(REDIS_ORDER_NUMBER_KEY, 1L);break;}}} else {// 重新  开始customRedisTemplate.opsForValue().set(REDIS_ORDER_NUMBER_KEY, Long.valueOf(now + "00000"));// 按需修改orderNo = ORDER_NUMBER_PREFIX + customRedisTemplate.opsForValue().increment(REDIS_ORDER_NUMBER_KEY, 1L);}} else {// 直接返回当前的订单号orderNo = ORDER_NUMBER_PREFIX + currentOrder;}} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();}return orderNo;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com