| | |
| | | import com.google.common.collect.Sets; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.connection.RedisConnection; |
| | | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| | | import org.springframework.data.redis.core.*; |
| | | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | |
| | | @SuppressWarnings({"unchecked","all"}) |
| | | public class RedisUtils { |
| | | private static final Logger log = LoggerFactory.getLogger(RedisUtils.class); |
| | | |
| | | @Value("${jwt.generate-order-sn}") |
| | | private String generateOrderSn; |
| | | @Value("${wx.enabled}") |
| | | private Boolean wxEnabled; |
| | | private static final String T = "-T-"; |
| | | private RedisTemplate<Object, Object> redisTemplate; |
| | | |
| | | public RedisUtils(RedisTemplate<Object, Object> redisTemplate) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 生成18位订单编号:8位日期+4位秒+7位以上自增id |
| | | * |
| | | * @param i |
| | | * @return |
| | | */ |
| | | public String generateOrderSn(Integer i) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | String key = generateOrderSn + date; |
| | | Long increment = increment(key); |
| | | sb.append(date); |
| | | sb.append(String.format("%04d", i)); |
| | | String incrementStr = increment.toString(); |
| | | if (incrementStr.length() <= 7) { |
| | | sb.append(String.format("%07d", increment)); |
| | | } else { |
| | | sb.append(incrementStr); |
| | | } |
| | | if (wxEnabled) { |
| | | // 生产环境 |
| | | return sb.toString(); |
| | | } else { |
| | | // 测试环境 |
| | | return T + sb.toString(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断key是否过期 |
| | | * |
| | | * @param key |