|
|
@@ -147,7 +147,22 @@ public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> impleme
|
|
|
? Integer.valueOf(p.get("quantity").toString()) : 1);
|
|
|
item.setPrice(p.get("price") != null
|
|
|
? new BigDecimal(p.get("price").toString()) : BigDecimal.ZERO);
|
|
|
- item.setRefundAmount(item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())));
|
|
|
+ // 精确计算退款金额:优先用 money/productNum*quantity,避免 price 的 1 分钱舍入差
|
|
|
+ if (p.get("money") != null && p.get("productNum") != null) {
|
|
|
+ BigDecimal money = new BigDecimal(p.get("money").toString());
|
|
|
+ int originalNum = Integer.parseInt(p.get("productNum").toString());
|
|
|
+ int refundQty = item.getQuantity();
|
|
|
+ if (originalNum > 0 && refundQty == originalNum) {
|
|
|
+ item.setRefundAmount(money);
|
|
|
+ } else if (originalNum > 0) {
|
|
|
+ item.setRefundAmount(money.divide(BigDecimal.valueOf(originalNum), 4, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .multiply(BigDecimal.valueOf(refundQty)));
|
|
|
+ } else {
|
|
|
+ item.setRefundAmount(money);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ item.setRefundAmount(item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())));
|
|
|
+ }
|
|
|
item.setCreateTime(LocalDateTime.now());
|
|
|
items.add(item);
|
|
|
}
|
|
|
@@ -299,11 +314,31 @@ public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> impleme
|
|
|
}
|
|
|
BigDecimal total = BigDecimal.ZERO;
|
|
|
for (Map<String, Object> p : products) {
|
|
|
- BigDecimal price = p.get("price") != null
|
|
|
- ? new BigDecimal(p.get("price").toString()) : BigDecimal.ZERO;
|
|
|
int quantity = p.get("quantity") != null
|
|
|
? Integer.parseInt(p.get("quantity").toString()) : 1;
|
|
|
- total = total.add(price.multiply(BigDecimal.valueOf(quantity)));
|
|
|
+ // 优先使用 money/productNum 计算实际单价,避免 haha 平台 price 的 1 分钱舍入差
|
|
|
+ // 例如: money=4.25, productNum=2, quantity=2 → 4.25(正确)
|
|
|
+ // price=2.13, quantity=2 → 4.26(多1分钱)
|
|
|
+ BigDecimal itemAmount;
|
|
|
+ if (p.get("money") != null && p.get("productNum") != null) {
|
|
|
+ BigDecimal money = new BigDecimal(p.get("money").toString());
|
|
|
+ int originalNum = Integer.parseInt(p.get("productNum").toString());
|
|
|
+ if (originalNum > 0 && quantity == originalNum) {
|
|
|
+ // 全量退款 → 直接用 money
|
|
|
+ itemAmount = money;
|
|
|
+ } else if (originalNum > 0) {
|
|
|
+ // 部分退款 → money / productNum × quantity
|
|
|
+ itemAmount = money.divide(BigDecimal.valueOf(originalNum), 4, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .multiply(BigDecimal.valueOf(quantity));
|
|
|
+ } else {
|
|
|
+ itemAmount = money;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ BigDecimal price = p.get("price") != null
|
|
|
+ ? new BigDecimal(p.get("price").toString()) : BigDecimal.ZERO;
|
|
|
+ itemAmount = price.multiply(BigDecimal.valueOf(quantity));
|
|
|
+ }
|
|
|
+ total = total.add(itemAmount);
|
|
|
}
|
|
|
return total;
|
|
|
}
|