From 74cf20b8a5aac194f9b9a4c251d95b8b7f101ad7 Mon Sep 17 00:00:00 2001 From: "autumn.g@foxmail.com" Date: Thu, 14 Sep 2023 17:36:45 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=AE=A1=E7=AE=97=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E9=87=91=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/SpringBootTestController.java | 74 ++++++++++++------- .../orderlogic/AbstractOrderLogic.java | 32 ++++---- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/jsowell-admin/src/test/java/SpringBootTestController.java b/jsowell-admin/src/test/java/SpringBootTestController.java index ed2beca6e..f8c2706a6 100644 --- a/jsowell-admin/src/test/java/SpringBootTestController.java +++ b/jsowell-admin/src/test/java/SpringBootTestController.java @@ -1,5 +1,6 @@ import cn.hutool.json.JSONUtil; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -50,6 +51,8 @@ import com.jsowell.pile.dto.lutongyunting.BindCouponDTO; import com.jsowell.pile.mapper.MemberBasicInfoMapper; import com.jsowell.pile.mapper.PileBillingTemplateMapper; import com.jsowell.pile.service.*; +import com.jsowell.pile.service.orderlogic.AbstractOrderLogic; +import com.jsowell.pile.service.orderlogic.OrderLogicFactory; import com.jsowell.pile.transaction.dto.OrderTransactionDTO; import com.jsowell.pile.transaction.service.TransactionService; import com.jsowell.pile.util.SnUtils; @@ -368,44 +371,61 @@ public class SpringBootTestController { transactionService.doUpdateOrder(dto); } + public static List calculateRefund(BigDecimal totalAmount, List payments) { + List refundList = new ArrayList<>(); + BigDecimal remainingAmount = totalAmount; - /** - * 测试多笔支付情况,解冻部分金额 - */ + for (BigDecimal payment : payments) { + if (remainingAmount.compareTo(BigDecimal.ZERO) <= 0) { + // refundList.add(BigDecimal.ZERO); + refundList.add(payment); + } else { + BigDecimal refundPerPayment = remainingAmount.min(payment); + // refundList.add(refundPerPayment); + refundList.add(payment.subtract(refundPerPayment)); + remainingAmount = remainingAmount.subtract(refundPerPayment); + } + } + + return refundList; + } + + public static void main(String[] args) { + BigDecimal totalAmount = new BigDecimal("0.1"); + List payments = Lists.newArrayList(new BigDecimal("0.97"), new BigDecimal("1.00")); + + List refunds = calculateRefund(totalAmount, payments); + System.out.println("每笔单需要退还的金额: " + refunds); + } + +/** + * 测试多笔支付情况,解冻部分金额 + */ @Test public void calculateUnfreezeAmountTest() { - BigDecimal orderAmount = new BigDecimal("0.99"); + BigDecimal orderAmount = new BigDecimal("0.06"); List payRecordList = Lists.newArrayList(); // 第一笔支付记录 JSONObject jsonObject = new JSONObject(); jsonObject.put("paymentId", "002212023083114213410543206907226374144"); - jsonObject.put("amount", "0.98"); + jsonObject.put("amount", "0.97"); + + JSONObject jsonObject2 = new JSONObject(); + jsonObject2.put("paymentId", "002212023083114213410543206907226374144"); + jsonObject2.put("amount", "1"); + + JSONArray jsonArray = new JSONArray(); + jsonArray.add(jsonObject); + jsonArray.add(jsonObject2); + OrderPayRecord build = OrderPayRecord.builder() - .payAmount(new BigDecimal("0.98")) - .deductionRecord(jsonObject.toJSONString()) + .payAmount(new BigDecimal("1.97")) + .deductionRecord(jsonArray.toJSONString()) .build(); payRecordList.add(build); - // 第二笔支付记录 - JSONObject jsonObject2 = new JSONObject(); - jsonObject2.put("paymentId", "002212023083114410510543211818773135360"); - jsonObject2.put("amount", "1.00"); - OrderPayRecord build2 = OrderPayRecord.builder() - .payAmount(new BigDecimal("1.00")) - .deductionRecord(jsonObject2.toJSONString()) - .build(); - payRecordList.add(build2); - - // 第三笔支付记录 - // JSONObject jsonObject3 = new JSONObject(); - // jsonObject3.put("paymentId", "3"); - // jsonObject3.put("amount", "10"); - // OrderPayRecord build3 = OrderPayRecord.builder() - // .payAmount(new BigDecimal("10")) - // .deductionRecord(jsonObject3.toJSONString()) - // .build(); - // payRecordList.add(build3); - List> maps = orderBasicInfoService.calculateUnfreezeAmount(orderAmount, payRecordList); + AbstractOrderLogic orderLogic = OrderLogicFactory.getOrderLogic("1"); + List> maps = orderLogic.calculateUnfreezeAmount(orderAmount, payRecordList); System.out.println(maps); } diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/orderlogic/AbstractOrderLogic.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/orderlogic/AbstractOrderLogic.java index 7d04e963d..f04063c3c 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/orderlogic/AbstractOrderLogic.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/orderlogic/AbstractOrderLogic.java @@ -405,41 +405,41 @@ public abstract class AbstractOrderLogic implements InitializingBean { return resultMap; } + public static void main(String[] args) { + BigDecimal orderAmount= new BigDecimal("0.06"); + List payRecordList = Lists.newArrayList(); + payRecordList.add(OrderPayRecord.builder().deductionRecord("[{\"paymentId\":\"002212023091416010010548305359259512832\",\"amount\":0.97},{\"paymentId\":\"002212023091416110010548307874223849472\",\"amount\":1.00}]").build()); + + } + /** * 计算解冻金额 * @param orderAmount 订单消费金额 * @param payRecordList 订单支付记录 */ - protected List> calculateUnfreezeAmount(BigDecimal orderAmount, List payRecordList) { + public List> calculateUnfreezeAmount(BigDecimal orderAmount, List payRecordList) { List> resultList = Lists.newArrayList(); BigDecimal tempAmount = new BigDecimal(orderAmount.toString()); // 临时金额 for (OrderPayRecord record : payRecordList) { - // String deductionRecord = record.getDeductionRecord(); - // JSONObject jsonObject = JSON.parseObject(deductionRecord); - List jsonObjects = parseDeductionRecord(record.getDeductionRecord()); for (JSONObject object : jsonObjects) { String paymentId = object.getString("paymentId"); BigDecimal payAmount = object.getBigDecimal("amount"); // 此交易单支付的金额 - // 该笔支付扣除金额 - BigDecimal deductionAmount; + // 该笔支付解冻金额 - BigDecimal unfreezeAmount = null; - // 临时消费金额 = 临时消费金额 - 该笔交易的剩余金额 - tempAmount = tempAmount.subtract(payAmount); - if (tempAmount.compareTo(BigDecimal.ZERO) >= 0) { - // 计算以后,大于等于0,说明这笔支付剩余金额需要扣完,还要继续扣下一笔 - deductionAmount = payAmount; - unfreezeAmount = payAmount.subtract(deductionAmount); // 支付金额 - 扣除金额 = 需要退回的金额 + BigDecimal unfreezeAmount; + + if (tempAmount.compareTo(BigDecimal.ZERO) <= 0) { + unfreezeAmount = payAmount; Map map = Maps.newHashMap(); map.put("paymentId", paymentId); map.put("unfreezeAmount", unfreezeAmount); resultList.add(map); } else { - // 如果小于0,则说明该笔交易的剩余金额用不完,扣除金额等于临时消费金额,并结束循环 - deductionAmount = payAmount.add(tempAmount); // 该笔交易的剩余金额加上一个负数临时消费金额,就是该笔交易扣除金额 - unfreezeAmount = payAmount.subtract(deductionAmount); // 支付金额 - 扣除金额 = 需要退回的金额 + BigDecimal refundPerPayment = tempAmount.min(payAmount); + unfreezeAmount = payAmount.subtract(refundPerPayment); + tempAmount = tempAmount.subtract(refundPerPayment); Map map = Maps.newHashMap(); map.put("paymentId", paymentId); map.put("unfreezeAmount", unfreezeAmount);