|  |  |  | 
|---|
|  |  |  | import org.apache.commons.lang3.time.DateFormatUtils; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.security.SecureRandom; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.Random; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class CodeFactoryUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static RedisService redisService = null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //静态住入到自己的类中 | 
|---|
|  |  |  | private static RedisService redisService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将静态属性以入参(形参)的方式传入一个方法中,然后将此方法通过 @Resource 注入到Spring容器中 | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | public void setService(RedisService redisService) { | 
|---|
|  |  |  | CodeFactoryUtil.redisService = redisService; | 
|---|
|  |  |  | private static RedisService getRedisService() { | 
|---|
|  |  |  | if (redisService == null) { | 
|---|
|  |  |  | redisService = ApplicationContextUtil.getBean(RedisService.class); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return redisService; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.printf("getShopNo()"+getShopNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | private static String getNoSuffixNumber(String prefix){ | 
|---|
|  |  |  | Integer suffixNumberInt; | 
|---|
|  |  |  | String suffixNumber = "00001"; | 
|---|
|  |  |  | if(redisService.hasKey(prefix)){ | 
|---|
|  |  |  | suffixNumberInt = redisService.getCacheObject(prefix); | 
|---|
|  |  |  | if(getRedisService().hasKey(prefix)){ | 
|---|
|  |  |  | suffixNumberInt = getRedisService().getCacheObject(prefix); | 
|---|
|  |  |  | suffixNumber = toFillZeroSuffixNumber(suffixNumberInt,SHOP_LENGTH); | 
|---|
|  |  |  | suffixNumberInt = suffixNumberInt + 1; | 
|---|
|  |  |  | redisService.setCacheObject(prefix, suffixNumberInt); | 
|---|
|  |  |  | getRedisService().setCacheObject(prefix, suffixNumberInt); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | redisService.setCacheObject(prefix, 1); | 
|---|
|  |  |  | getRedisService().setCacheObject(prefix, 1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return suffixNumber; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | private static String getOrderSuffixNumber(String prefix){ | 
|---|
|  |  |  | Integer suffixNumberInt; | 
|---|
|  |  |  | String suffixNumber = "0001"; | 
|---|
|  |  |  | if(redisService.hasKey(prefix)){ | 
|---|
|  |  |  | suffixNumberInt = redisService.getCacheObject(prefix); | 
|---|
|  |  |  | if(getRedisService().hasKey(prefix)){ | 
|---|
|  |  |  | suffixNumberInt = getRedisService().getCacheObject(prefix); | 
|---|
|  |  |  | suffixNumber = toFillZeroSuffixNumber(suffixNumberInt,ORDER_LENGTH); | 
|---|
|  |  |  | suffixNumberInt = suffixNumberInt + 1; | 
|---|
|  |  |  | redisService.setCacheObject(prefix, suffixNumberInt, 24L, TimeUnit.HOURS); | 
|---|
|  |  |  | getRedisService().setCacheObject(prefix, suffixNumberInt, 24L, TimeUnit.HOURS); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | redisService.setCacheObject(prefix, 1, 24L, TimeUnit.HOURS); | 
|---|
|  |  |  | getRedisService().setCacheObject(prefix, 1, 24L, TimeUnit.HOURS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return suffixNumber; | 
|---|
|  |  |  | } | 
|---|