Jelajahi Sumber

结算时内容修改

LiYi 1 Minggu lalu
induk
melakukan
51c48be1fd
1 mengubah file dengan 33 tambahan dan 12 penghapusan
  1. 33 12
      src/main/java/org/jebot/rest/BotRest.java

+ 33 - 12
src/main/java/org/jebot/rest/BotRest.java

@@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -385,7 +386,6 @@ public class BotRest {
         stringBuilder.append("扣除手续费总跑量: ").append(totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
 
         // 扣除金额(手续费)
-        // 下发金额 = 总跑量 - 手续费
         BigDecimal deductionAmount = totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT);
 
         // ================= 发送账单 =================
@@ -413,21 +413,34 @@ public class BotRest {
             }
 
             log.info("一键结算发送消息成功, 群组ID: {}, 结算记录: {}", mchGroup.getGroupId(), stringBuilder.toString());
-            //查询总下发
+
+            // ============ 用缓存 Map 保存账本 ============
+            Map<Long, BotAccountBook> accountBookCache = new HashMap<>();
+
+            // 查询总下发账本
             BotAccountBook accountBookAll = handlerManager.getAccountBookRepository()
                     .findByBelongIdAndType(mchGroup.getGroupId(), DATA_TYPE_CHANNEL);
-            List<BotGroup> botGroups = this.handlerManager.getGroupRepository().findAllByGroupIdAndDataType(mchGroup.getGroupId(), DATA_TYPE_CHANNEL);
-            double oldPaymentBalance = 0;
+            if (accountBookAll != null) {
+                accountBookCache.put(mchGroup.getGroupId(), accountBookAll);
+            }
+
+            // 查询子群账本,加入缓存
+            List<BotGroup> botGroups = this.handlerManager.getGroupRepository()
+                    .findAllByGroupIdAndDataType(mchGroup.getGroupId(), DATA_TYPE_CHANNEL);
+
             for (BotGroup botGroup : botGroups) {
-                BotAccountBook accountBookchannl = handlerManager.getAccountBookRepository()
+                BotAccountBook accountBookChannel = handlerManager.getAccountBookRepository()
                         .findByBelongIdAndType(botGroup.getDataId(), DATA_TYPE_CHANNEL);
-                if (accountBookchannl == null) {
-                    oldPaymentBalance += 0;
-                } else {
-                    oldPaymentBalance += accountBookchannl.getPaymentBalance();
+                if (accountBookChannel != null) {
+                    accountBookCache.put(botGroup.getDataId(), accountBookChannel);
                 }
             }
-            oldPaymentBalance += accountBookAll.getPaymentBalance();
+
+            // 计算 oldPaymentBalance
+            double oldPaymentBalance = accountBookCache.values().stream()
+                    .mapToDouble(BotAccountBook::getPaymentBalance)
+                    .sum();
+
             double newPaymentBalance = BigDecimal.valueOf(oldPaymentBalance)
                     .subtract(deductionAmount).doubleValue();
 
@@ -446,6 +459,14 @@ public class BotRest {
 
                 handlerManager.getAccountBookService()
                         .updatePaymentBalanceByIdAndPaymentBalance(updateMerchantPaymentBalance);
+                handlerManager.getAccountBookRepository().flush();
+
+                // 同步更新缓存里的账本对象
+                accountBook.setPaymentBalance(
+                        BigDecimal.valueOf(accountBook.getPaymentBalance())
+                                .subtract(deductionAmount).doubleValue()
+                );
+                accountBookCache.put(mchGroup.getDataId(), accountBook);
 
                 sendMessage = new SendMessage(mchGroup.getGroupId(),
                         String.format("下发提醒\n下发金额: %.2f\n下发前: %.2f\n下发后: %.2f",
@@ -458,8 +479,7 @@ public class BotRest {
                         message.getBotUser().getUserId(),
                         message.getBotUser().getUserName(),
                         deductionAmount.doubleValue(),
-                        e.getMessage());
-                e.printStackTrace();
+                        e.getMessage(), e);
                 sendMessage = new SendMessage(message.getBotGroup().getDataId(),
                         "下发处理失败,请手动处理");
             } finally {
@@ -473,6 +493,7 @@ public class BotRest {
     }
 
 
+
 }