Browse Source

单包里的大奖去重

chunping 3 years ago
parent
commit
9a9ddd29fb

+ 19 - 5
mp-service/src/main/java/com/qs/mp/admin/service/impl/TicketBoxServiceImpl.java

@@ -150,11 +150,21 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
       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开始
-          List<Integer> randomList = getRandomList(ticketAwards.getQuantity(), pkgNum);
+          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(
@@ -301,17 +311,20 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
    * @param total
    * @return
    */
-  private List<Integer> getRandomList(int n, int total) {
+  private List<Integer> getRandomList(List<Integer> excludeList, int n, int total) {
     List<Integer> randomList = new ArrayList<>();
     Random rand = new Random();
     boolean[] bool = new boolean[total];
+    for (Integer exInt : excludeList) {
+      bool[exInt] = true;
+    }
     int randInt = 0;
     for (int i = 0; i < n; i++) {
       do {
         randInt = rand.nextInt(total);
       } while (bool[randInt]);
       bool[randInt] = true;
-      randomList.add(randInt);
+      randomList.add(randInt + 1);
     }
     return randomList;
   }
@@ -351,9 +364,10 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
     prizes.add(new PkgAwards("3", "三等奖", 3, 3));
     prizes.add(new PkgAwards("4", "四等奖", 4, 4));
     TicketBoxServiceImpl boxService = new TicketBoxServiceImpl();
-    for (int i = 0 ; i < 10; i ++ ) {
+    System.out.println(boxService.getRandomList(new ArrayList<>(), 10, 10));
+    /*for (int i = 0 ; i < 10; i ++ ) {
       int random = boxService.getPrizeIndex(prizes);
       System.out.println(random);
-    }
+    }*/
   }
 }