Browse Source

序列号生成

chunping 3 years ago
parent
commit
c0d57de7b7

+ 13 - 2
mp-admin/src/test/java/com/qs/mp/service/ServiceImplTest.java

@@ -36,7 +36,7 @@ public class ServiceImplTest {
 
 
   public static void main(String[] args) {
-    int n = 500;
+    /*int n = 500;
     Random rand = new Random();
     boolean[] bool = new boolean[n];
     int randInt = 0;
@@ -47,5 +47,16 @@ public class ServiceImplTest {
       bool[randInt] = true;
       System.out.println(randInt);
     }
+  */
+    String s = "z";
+
+    int i = Character.codePointAt(s, 0);
+
+    System.out.println(i + "");
+
+    char[] a = Character.toChars(123);
+
+    System.out.println(new String(a));
+  }
+
   }
-}

+ 4 - 1
mp-service/src/main/java/com/qs/mp/admin/service/ITicketBoxSerialService.java

@@ -11,7 +11,10 @@ import com.qs.mp.common.enums.TicketTypeEnum;
 public interface ITicketBoxSerialService {
 
   /**
-   * 格式,共7位 T(票类型)+ 22(年份) + 00001(序号)
+   * 格式,共7位,分三段,票类型(1位)-年份(1位)-序号(5位)
+   * T(票类型,T代表通用票)
+   * H(年份后两位+50后的ascii码,从2022年可以用到2072年)
+   * 00001(序号,每年从年份数字开始,比如2022年,那起始序号为2022)
    *
    * @param ticketType
    * @return

+ 6 - 4
mp-service/src/main/java/com/qs/mp/admin/service/impl/TicketBoxSerialServiceImpl.java

@@ -36,7 +36,8 @@ public class TicketBoxSerialServiceImpl implements ITicketBoxSerialService {
   @Override
   public String generateSerial(TicketTypeEnum ticketType) {
     //年份后两位
-    String prefix = sdf.format(new Date()).substring(2);
+    String strYear = sdf.format(new Date());
+    String prefix = strYear.substring(2);
 
     //如果日期前缀未过期,则序号自增
     //否则,将日期作为Key,1作为Value重置,并设置第二年0点过期
@@ -44,11 +45,12 @@ public class TicketBoxSerialServiceImpl implements ITicketBoxSerialService {
       redisTemplate.opsForValue().increment(prefix, 1);
 
     } else {
-      int start = 1;
+      int start = Integer.valueOf(strYear);
       List<TicketBox> ticketBoxList = ticketBoxMapper.selectList(new QueryWrapper<TicketBox>().orderByDesc("created_time").last("limit 1"));
       if (!CollectionUtils.isEmpty(ticketBoxList)) {
         String boxNo = ticketBoxList.get(0).getBoxNo();
-        int year = Integer.valueOf(boxNo.substring(1, 3));
+
+        int year = Character.codePointAt(boxNo.substring(1, 2), 0) - 50;
         // 同一年份,做累加
         if (year == Integer.valueOf(prefix)) {
           start = Integer.valueOf(boxNo.substring(3)) + 1;
@@ -58,7 +60,7 @@ public class TicketBoxSerialServiceImpl implements ITicketBoxSerialService {
       redisTemplate.expireAt(prefix, getNextYearDate());
 
     }
-    return "T" + prefix + String.format("%1$05d", redisTemplate.opsForValue().get(prefix));
+    return "T" + Character.toChars(Integer.parseInt(prefix) + 50) + String.format("%1$05d", redisTemplate.opsForValue().get(prefix));
   }
 
   // 获取第二年 1月 1日 00:00的时间

+ 64 - 53
mp-service/src/main/java/com/qs/mp/admin/service/impl/TicketBoxServiceImpl.java

@@ -140,62 +140,17 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
     ticketAwardsService.saveBatch(ticketAwardsList);
     ticketAwardsPrizeService.saveBatch(awardsPrizeList);
 
+    generateTicket(ticketBox, ticketAwardsList);
+    return true;
+  }
+
+  private void generateTicket(TicketBox ticketBox, List<TicketAwards> ticketAwardsList) {
     // 分包
     int pkgNum = ticketBox.getPkgQty();
     int pkgUnit = ticketBox.getPkgUnit();
 
-    // 1.把各奖级的中奖数量分摊到每包上
-    Map<Integer, List<PkgAwards>> pkgAwardsMap = new HashMap<>();
-    for (int m = 1; m <= pkgNum; m++) {
-      pkgAwardsMap.put(m, new ArrayList<PkgAwards>());
-    }
-    List<Integer> excludePkgList = new ArrayList<>();
-    for (int k = 0; k < ticketAwardsList.size(); k++) {
-      TicketAwards ticketAwards = ticketAwardsList.get(k);
-      if (ticketAwards.getQuantity() < pkgNum) {
-        // 奖级数量少于包数的,随机不重复分配,随机数从1开始
-        int totalNone = pkgNum - ticketAwards.getQuantity(); // 轮空数
-        int moreExInt = excludePkgList.size() - totalNone; // 本轮要排除的数 - 轮空数
-        if (moreExInt > 0) {
-          // 多出来的数,从末尾开始删除
-          for (int l = 0; l < moreExInt; l++) {
-            excludePkgList.remove(excludePkgList.size() - 1);
-          }
-        }
-        List<Integer> randomList = getRandomList(excludePkgList, ticketAwards.getQuantity(), pkgNum);
-        excludePkgList.addAll(randomList);
-        LogUtil.debug(logger, "随机分配到的包序号为:{0}", new Object[]{JSONObject.toJSONString(randomList)});
-        for (Integer pkgId : randomList) {
-          pkgAwardsMap.get(pkgId).add(
-              new PkgAwards(ticketAwards.getAwardsId(), ticketAwards.getName(),
-                  ticketAwards.getSort(), 1));
-        }
-      } else {
-        // 奖级数量大于包数的,平均分配,四舍五入
-        int remainQty = ticketAwards.getQuantity();
-        for (int i = 1; i <= pkgNum; i++) {
-          int quantity;
-          if (k == ticketAwardsList.size() - 1) {
-            // 最后一个奖项直接分配奖级数量差额即可
-            int hasQty = pkgAwardsMap.get(i).stream().mapToInt(PkgAwards::getQuantity).sum();
-            quantity = pkgUnit - hasQty;
-          } else {
-            if (i == pkgNum) {
-              quantity = remainQty;
-            } else {
-              quantity = new BigDecimal(remainQty).divide(new BigDecimal(pkgNum - i + 1), 0,
-                  RoundingMode.HALF_UP).intValue();
-            }
-          }
-          remainQty -= quantity;
-          Assert.isTrue(remainQty >= 0, "剩余奖级数量不足分配。boxId:" + ticketBox.getBoxId());
-          pkgAwardsMap.get(i).add(
-              new PkgAwards(ticketAwards.getAwardsId(), ticketAwards.getName(),
-                  ticketAwards.getSort(), quantity));
-        }
-      }
-      LogUtil.debug(logger, "奖级{0}分包结果:{1}", new Object[]{k, JSONObject.toJSONString(pkgAwardsMap)});
-    }
+    Map<Integer, List<PkgAwards>> pkgAwardsMap = generatePkgAwards(
+        ticketBox, ticketAwardsList, pkgNum, pkgUnit);
 
     // 生成票包记录和盲票记录
     int pkgCnt = 0;
@@ -258,7 +213,63 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
       ticketService.saveBatch(ticketList);
     }
     ticketPackageService.saveBatch(ticketPackageList);
-    return true;
+  }
+
+  private Map<Integer, List<PkgAwards>> generatePkgAwards(TicketBox ticketBox,
+      List<TicketAwards> ticketAwardsList, int pkgNum, int pkgUnit) {
+    // 1.把各奖级的中奖数量分摊到每包上
+    Map<Integer, List<PkgAwards>> pkgAwardsMap = new HashMap<>();
+    for (int m = 1; m <= pkgNum; m++) {
+      pkgAwardsMap.put(m, new ArrayList<PkgAwards>());
+    }
+    List<Integer> excludePkgList = new ArrayList<>();
+    for (int k = 0; k < ticketAwardsList.size(); k++) {
+      TicketAwards ticketAwards = ticketAwardsList.get(k);
+      if (ticketAwards.getQuantity() < pkgNum) {
+        // 奖级数量少于包数的,随机不重复分配,随机数从1开始
+        int totalNone = pkgNum - ticketAwards.getQuantity(); // 轮空数
+        int moreExInt = excludePkgList.size() - totalNone; // 本轮要排除的数 - 轮空数
+        if (moreExInt > 0) {
+          // 多出来的数,从末尾开始删除
+          for (int l = 0; l < moreExInt; l++) {
+            excludePkgList.remove(excludePkgList.size() - 1);
+          }
+        }
+        List<Integer> randomList = getRandomList(excludePkgList, ticketAwards.getQuantity(), pkgNum);
+        excludePkgList.addAll(randomList);
+        LogUtil.debug(logger, "随机分配到的包序号为:{0}", new Object[]{JSONObject.toJSONString(randomList)});
+        for (Integer pkgId : randomList) {
+          pkgAwardsMap.get(pkgId).add(
+              new PkgAwards(ticketAwards.getAwardsId(), ticketAwards.getName(),
+                  ticketAwards.getSort(), 1));
+        }
+      } else {
+        // 奖级数量大于包数的,平均分配,四舍五入
+        int remainQty = ticketAwards.getQuantity();
+        for (int i = 1; i <= pkgNum; i++) {
+          int quantity;
+          if (k == ticketAwardsList.size() - 1) {
+            // 最后一个奖项直接分配奖级数量差额即可
+            int hasQty = pkgAwardsMap.get(i).stream().mapToInt(PkgAwards::getQuantity).sum();
+            quantity = pkgUnit - hasQty;
+          } else {
+            if (i == pkgNum) {
+              quantity = remainQty;
+            } else {
+              quantity = new BigDecimal(remainQty).divide(new BigDecimal(pkgNum - i + 1), 0,
+                  RoundingMode.HALF_UP).intValue();
+            }
+          }
+          remainQty -= quantity;
+          Assert.isTrue(remainQty >= 0, "剩余奖级数量不足分配。boxId:" + ticketBox.getBoxId());
+          pkgAwardsMap.get(i).add(
+              new PkgAwards(ticketAwards.getAwardsId(), ticketAwards.getName(),
+                  ticketAwards.getSort(), quantity));
+        }
+      }
+      LogUtil.debug(logger, "奖级{0}分包结果:{1}", new Object[]{k, JSONObject.toJSONString(pkgAwardsMap)});
+    }
+    return pkgAwardsMap;
   }
 
   /**