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);
}

View File

@@ -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<OrderPayRecord> 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<Map<String, Object>> calculateUnfreezeAmount(BigDecimal orderAmount, List<OrderPayRecord> payRecordList) {
public List<Map<String, Object>> calculateUnfreezeAmount(BigDecimal orderAmount, List<OrderPayRecord> payRecordList) {
List<Map<String, Object>> resultList = Lists.newArrayList();
BigDecimal tempAmount = new BigDecimal(orderAmount.toString()); // 临时金额
for (OrderPayRecord record : payRecordList) {
// String deductionRecord = record.getDeductionRecord();
// JSONObject jsonObject = JSON.parseObject(deductionRecord);
List<JSONObject> 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<String, Object> 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<String, Object> map = Maps.newHashMap();
map.put("paymentId", paymentId);
map.put("unfreezeAmount", unfreezeAmount);