1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   | 
 
  public abstract class ArchiveSequence {
      public static Pattern p = Pattern.compile("[0-9]{1,}");
      
 
      private final Map<String, Map<String, AtomicInteger>> archiveNos = new ConcurrentHashMap<String, Map<String, AtomicInteger>>();     
 
      private final int length = 10;
      protected abstract String getArchiveNoFromDB(String userId);
      protected String generateArchiveNo(String userId) {         Map<String, AtomicInteger> atomicIntegerMap = archiveNos.computeIfAbsent(userId, k -> initSequence(userId));         AtomicInteger sequence = atomicIntegerMap.get(getLocalDate());         if (Objects.isNull(sequence)) {             atomicIntegerMap.clear();             atomicIntegerMap.putIfAbsent(getLocalDate(), new AtomicInteger(0));         }         int no = atomicIntegerMap.get(getLocalDate()).incrementAndGet();         if (no > 99) {             throw new RuntimeException("每天最多允许归档99次");         }         return String.format(getLocalDate() + "%02d", no);     }
      private Map<String, AtomicInteger> initSequence(String userId) {         String sequence = getArchiveNoFromDB(userId);         int startSequence = 0;         if (StringUtils.isNotBlank(sequence) && sequence.length() > length && isNumeric(sequence.substring(sequence.length() - 10))) {             startSequence = Integer.parseInt(sequence.substring(sequence.length() - 2));         }         if (startSequence > 99) {             throw new RuntimeException("每天最多允许归档99次");         }         HashMap<String, AtomicInteger> map = new HashMap<>(1);         map.put(getLocalDate(), new AtomicInteger(startSequence));         return map;     }
      private String getLocalDate() {         return DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now());     }
      private boolean isNumeric(String s){         Matcher m = p.matcher(s);         return m.matches();     }
  }
 
  |