Ver Fonte

通道余额部分bug以及增加余额

LiYi há 1 semana atrás
pai
commit
e199446ec3

+ 11 - 4
src/main/java/org/jebot/handler/impl/channel/ChannelAccountBookHandler.java

@@ -8,6 +8,7 @@ import org.jebot.handler.AbstractHandler;
 import org.jebot.handler.dto.BotMessage;
 import org.jebot.models.jebot.BotAccountBook;
 import org.jebot.models.jebot.BotAccountBookHistory;
+import org.jebot.models.jebot.BotGroup;
 import org.jebot.service.dto.UpdateBalance;
 
 import javax.imageio.ImageIO;
@@ -33,7 +34,6 @@ public class ChannelAccountBookHandler extends AbstractHandler {
     private static final Pattern agentAccount = Pattern.compile("^" + AGENT_ACCOUNT_FLAG + "[0-9+\\-*/.]+$");
 
 
-
     @Override
     public boolean msgHandler(BotMessage message) {
 
@@ -76,15 +76,22 @@ public class ChannelAccountBookHandler extends AbstractHandler {
             if (paymentAccountAmount == 0) {
                 return true;
             }
-            double paymentBalanceOld = accountBook.getPaymentBalance();
+            List<BotGroup> botGroups = this.handlerManager.getGroupRepository().findAllByGroupIdAndDataType(message.getBotGroup().getGroupId(), DATA_TYPE_CHANNEL);
+            double paymentBalanceOld = 0;
+            for (BotGroup botGroup : botGroups) {
+                BotAccountBook accountBookchannl = handlerManager.getAccountBookRepository()
+                        .findByBelongIdAndType(botGroup.getDataId(), DATA_TYPE_CHANNEL);
+                paymentBalanceOld += accountBookchannl.getPaymentBalance();
+            }
+            paymentBalanceOld += accountBook.getPaymentBalance();
             double paymentBalanceNew = paymentBalanceOld + paymentAccountAmount;
             SendMessage sendMessage = null;
             try {
                 UpdateBalance updateBalance = new UpdateBalance();
                 updateBalance.setId(accountBook.getId());
                 updateBalance.setAmount(paymentAccountAmount);
-                updateBalance.setOldBalance(paymentBalanceOld);
-                updateBalance.setNewBalance(paymentBalanceNew);
+                updateBalance.setOldBalance(accountBook.getPaymentBalance());
+                updateBalance.setNewBalance(accountBook.getPaymentBalance() + paymentAccountAmount);
                 updateBalance.setMessage(message);
                 handlerManager.getAccountBookService().updatePaymentBalanceByIdAndPaymentBalance(updateBalance);
                 sendMessage = new SendMessage(message.getMessage().chat().id(), String.format("入账提醒\n入账金额: %.2f\n入账前: %.2f\n入账后: %.2f", paymentAccountAmount, paymentBalanceOld, paymentBalanceNew));

+ 9 - 2
src/main/java/org/jebot/handler/impl/channel/ChannelInfoHandler.java

@@ -114,8 +114,15 @@ public class ChannelInfoHandler extends AbstractHandler {
                     stringBuffer.append("应得余额: 0.0\n\n");
                 }
             }
-            stringBuffer.append("当前预付: ").append(accountBook.getPaymentBalance()).append("\n");
-            stringBuffer.append("剩余预付: ").append(accountBook.getPaymentBalance() - channelTotalAfterFeeAmount.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
+            double oldPaymentBalance = 0;
+            for (BotGroup botGroup : botGroups) {
+                BotAccountBook accountBookchannl = handlerManager.getAccountBookRepository()
+                        .findByBelongIdAndType(botGroup.getDataId(), DATA_TYPE_CHANNEL);
+                oldPaymentBalance += accountBookchannl.getPaymentBalance();
+            }
+            oldPaymentBalance += accountBook.getPaymentBalance();
+            stringBuffer.append("当前预付: ").append(oldPaymentBalance).append("\n");
+            stringBuffer.append("剩余预付: ").append(oldPaymentBalance - channelTotalAfterFeeAmount.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
             stringBuffer.append("统计时间: ").append(DateUtil.formatDateTime(endDate)).append("\n\n");
             botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), stringBuffer.toString()));
             return true;

+ 28 - 19
src/main/java/org/jebot/handler/impl/merchant/MerchantOneClickSettlementHandler.java

@@ -47,7 +47,7 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
             Date endDate = getYesnight();
             Date todayMidnight = getTodayMidnight(endDate);
             List<BotGroup> botGroups = handlerManager.getGroupRepository().findAllByAgentWarningThresholdOrPaymentWarningThresholdGreaterThanZero();
-            List<BotGroup> mchGroups = botGroups.stream().filter(botGroup -> Constant.DATA_TYPE_MERCHANT.equals(botGroup.getDataType())).collect(Collectors.toList());
+            List<BotGroup> mchGroups = botGroups.stream().filter(botGroup -> DATA_TYPE_MERCHANT.equals(botGroup.getDataType())).collect(Collectors.toList());
             if (!mchGroups.isEmpty()) {
                 List<Long> mchIds = mchGroups.stream().map(BotGroup::getDataId).collect(Collectors.toList());
                 List<MchAccount> mchAccounts = handlerManager.getMchAccountRepository().findByMchIds(mchIds);
@@ -157,7 +157,7 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
             Date endDate = getYesnight();
             Date todayMidnight = getTodayMidnight(endDate);
             List<BotGroup> botGroups = handlerManager.getGroupRepository().findAllByAgentWarningThresholdOrPaymentWarningThresholdGreaterThanZero();
-            List<BotGroup> mchGroups = botGroups.stream().filter(botGroup -> Constant.DATA_TYPE_CHANNEL.equals(botGroup.getDataType())).collect(Collectors.toList());
+            List<BotGroup> mchGroups = botGroups.stream().filter(botGroup -> DATA_TYPE_CHANNEL.equals(botGroup.getDataType())).collect(Collectors.toList());
             if (!mchGroups.isEmpty()) {
                 List<Long> mchIds = mchGroups.stream().map(BotGroup::getDataId).collect(Collectors.toList());
                 List<PayPassage> mchAccounts = handlerManager.getPassageRepository().findByPassageIds(mchIds);
@@ -224,7 +224,6 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
     }
 
 
-
     private boolean SettlementMch(BotMessage message, BotGroup mchGroup, List<PayOrder> payOrderList, Date todayMidnight, Date endDate) {
 
         StringBuilder stringBuilder = new StringBuilder();
@@ -240,18 +239,18 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
         // 扣除手续费后的总跑量
         BigDecimal totalAmountByMchRate = payOrderList.stream()
                 .map(order -> BigDecimal.ONE
-                        .subtract(order.getMchRate().movePointLeft(Constant.AMOUNT_MOVE_POINT))
+                        .subtract(order.getMchRate().movePointLeft(AMOUNT_MOVE_POINT))
                         .multiply(order.getAmount()))
                 .reduce(BigDecimal.ZERO, BigDecimal::add);
 
         stringBuilder.append(DateUtil.formatDate(endDate)).append("账单,请核对:\n");
-        stringBuilder.append("总跑量: ").append(totalAmount.movePointLeft(Constant.AMOUNT_MOVE_POINT).doubleValue()).append("\n");
+        stringBuilder.append("总跑量: ").append(totalAmount.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
         stringBuilder.append("成功笔数: ").append(totalOrders).append("\n");
-        stringBuilder.append("扣除手续费总跑量: ").append(totalAmountByMchRate.movePointLeft(Constant.AMOUNT_MOVE_POINT).doubleValue()).append("\n");
+        stringBuilder.append("扣除手续费总跑量: ").append(totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
 
         // 扣除金额(手续费)
         // 下发金额 = 总跑量 - 手续费
-        BigDecimal deductionAmount = totalAmountByMchRate.movePointLeft(Constant.AMOUNT_MOVE_POINT);
+        BigDecimal deductionAmount = totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT);
 
 
         // ================= 按费率分组汇总 =================
@@ -270,7 +269,7 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
 
                     stringBuilder.append("费率").append(mchRate.doubleValue())
                             .append(": ")
-                            .append(amount.movePointLeft(Constant.AMOUNT_MOVE_POINT).doubleValue())
+                            .append(amount.movePointLeft(AMOUNT_MOVE_POINT).doubleValue())
                             .append("\n");
                 });
 
@@ -356,18 +355,18 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
         // 扣除手续费后的总跑量
         BigDecimal totalAmountByMchRate = payOrderList.stream()
                 .map(order -> BigDecimal.ONE
-                        .subtract(order.getMchRate().movePointLeft(Constant.AMOUNT_MOVE_POINT))
+                        .subtract(order.getPayPassageRate().movePointLeft(AMOUNT_MOVE_POINT))
                         .multiply(order.getAmount()))
                 .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-        stringBuilder.append(DateUtil.formatDate(endDate)).append("账单,请核对:\n");
-        stringBuilder.append("总跑量: ").append(totalAmount.movePointLeft(Constant.AMOUNT_MOVE_POINT).doubleValue()).append("\n");
+        stringBuilder.append(DateUtil.formatDate(endDate)).append("【" + mchGroup.getDataName() + "】账单,请核对:\n");
+        stringBuilder.append("总跑量: ").append(totalAmount.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
         stringBuilder.append("成功笔数: ").append(totalOrders).append("\n");
-        stringBuilder.append("扣除手续费总跑量: ").append(totalAmountByMchRate.movePointLeft(Constant.AMOUNT_MOVE_POINT).doubleValue()).append("\n");
+        stringBuilder.append("扣除手续费总跑量: ").append(totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT).doubleValue()).append("\n");
 
         // 扣除金额(手续费)
         // 下发金额 = 总跑量 - 手续费
-        BigDecimal deductionAmount = totalAmountByMchRate.movePointLeft(Constant.AMOUNT_MOVE_POINT);
+        BigDecimal deductionAmount = totalAmountByMchRate.movePointLeft(AMOUNT_MOVE_POINT);
 
         // ================= 发送账单 =================
         SendMessage sendMessage = new SendMessage(mchGroup.getGroupId(), stringBuilder.toString());
@@ -378,20 +377,29 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
         } else {
             // ================= 更新商户账本 =================
             BotAccountBook accountBook = handlerManager.getAccountBookRepository()
-                    .findByBelongIdAndType(mchGroup.getDataId(), DATA_TYPE_MERCHANT);
+                    .findByBelongIdAndType(mchGroup.getDataId(), DATA_TYPE_CHANNEL);
 
             if (accountBook == null) {
                 accountBook = new BotAccountBook();
                 accountBook.setBelongId(mchGroup.getDataId());
-                accountBook.setType(DATA_TYPE_MERCHANT);
+                accountBook.setType(DATA_TYPE_CHANNEL);
                 accountBook.setPaymentBalance(0);
                 accountBook.setAgentBalance(0);
                 handlerManager.getAccountBookRepository().save(accountBook);
             }
 
             log.info("一键结算发送消息成功, 群组ID: {}, 结算记录: {}", mchGroup.getGroupId(), stringBuilder.toString());
-
-            double oldPaymentBalance = accountBook.getPaymentBalance();
+            //查询总下发
+            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;
+            for (BotGroup botGroup : botGroups) {
+                BotAccountBook accountBookchannl = handlerManager.getAccountBookRepository()
+                        .findByBelongIdAndType(botGroup.getDataId(), DATA_TYPE_CHANNEL);
+                oldPaymentBalance += accountBookchannl.getPaymentBalance();
+            }
+            oldPaymentBalance += accountBookAll.getPaymentBalance();
             double newPaymentBalance = BigDecimal.valueOf(oldPaymentBalance)
                     .subtract(deductionAmount).doubleValue();
 
@@ -402,8 +410,9 @@ public class MerchantOneClickSettlementHandler extends AbstractHandler {
             try {
                 UpdateBalance updateMerchantPaymentBalance = new UpdateBalance();
                 updateMerchantPaymentBalance.setId(accountBook.getId());
-                updateMerchantPaymentBalance.setOldBalance(oldPaymentBalance);
-                updateMerchantPaymentBalance.setNewBalance(newPaymentBalance);
+                updateMerchantPaymentBalance.setOldBalance(accountBook.getPaymentBalance());
+                updateMerchantPaymentBalance.setNewBalance(BigDecimal.valueOf(accountBook.getPaymentBalance())
+                        .subtract(deductionAmount).doubleValue());
                 updateMerchantPaymentBalance.setAmount(deductionAmount.doubleValue());
                 updateMerchantPaymentBalance.setMessage(botMessage);
 

+ 2 - 0
src/main/java/org/jebot/models/xxpay/PayOrder.java

@@ -262,4 +262,6 @@ public class PayOrder {
         this.status = status;
         this.createTime = createTime;
     }
+
+
 }

+ 1 - 1
src/main/java/org/jebot/repository/xxpay/PayOrderRepository.java

@@ -31,7 +31,7 @@ public interface PayOrderRepository extends JpaRepository<PayOrder, String> {
     @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE  tpo.mchId =:mchId and tpo.status in (2,3) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
     List<PayOrder> findSuccessPayOrderByMchIdAndCreateTime(Long mchId, Date startTime, Date endTime);
 
-    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE  tpo.passageId =:passageId and tpo.status in (2,3) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
+    @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.payPassageRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE  tpo.passageId =:passageId and tpo.status in (2,3) and tpo.createTime >= :startTime and tpo.createTime <= :endTime")
     List<PayOrder> findSuccessPayOrderByPassageIdAndCreateTime(Long passageId, Date startTime, Date endTime);
 
     @Query("SELECT new org.jebot.models.xxpay.PayOrder( tpo.amount,tpo.mchId,tpo.mchRate,tpo.payPassageRate,tpo.passageId,tpo.status,tpo.createTime) FROM t_pay_order as tpo WHERE tpo.passageId in (:passageIds) and tpo.createTime >= :startTime and tpo.createTime <= :endTime and tpo.status in(2,3)")