update 计算退款金额

This commit is contained in:
2023-09-14 17:36:45 +08:00
parent ed031749e6
commit 74cf20b8a5
2 changed files with 63 additions and 43 deletions

View File

@@ -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<BigDecimal> calculateRefund(BigDecimal totalAmount, List<BigDecimal> payments) {
List<BigDecimal> 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<BigDecimal> payments = Lists.newArrayList(new BigDecimal("0.97"), new BigDecimal("1.00"));
List<BigDecimal> 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<OrderPayRecord> 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<Map<String, Object>> maps = orderBasicInfoService.calculateUnfreezeAmount(orderAmount, payRecordList);
AbstractOrderLogic orderLogic = OrderLogicFactory.getOrderLogic("1");
List<Map<String, Object>> maps = orderLogic.calculateUnfreezeAmount(orderAmount, payRecordList);
System.out.println(maps);
}