退款金额修改

localizer
ljw 2025-10-02 21:48:04 +08:00
parent 33fcdb728c
commit 5500d57b73
6 changed files with 57 additions and 42 deletions

View File

@ -38,7 +38,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
/**
*
@ -90,8 +89,7 @@ public class PaymentController {
throw Exceptions.clierr("支付方式不合法");
}
// 校验数据库中结算金额是否与请求一致,并统计总金额
BigDecimal dbTotalAmount = BigDecimal.ZERO;
// 校验数据库中结算金额是否与请求一致
for (PaymentItemParam it : paymentParam.getItems()) {
OrderExpenseItemsEntity entity = orderExpenseItemsService.getById(it.getId());
if (entity == null) {
@ -106,26 +104,24 @@ public class PaymentController {
throw Exceptions.clierr("结算金额不一致【" + it.getExpenseItemName() + "】(id=" + it.getId()
+ "),系统:" + dbSettle + ",请求:" + reqSettle);
}
// 累加数据库中的结算金额
dbTotalAmount = dbTotalAmount.add(dbSettle);
}
// 验证总金额是否与参数中的结算总金额一致
if (paymentParam.getSettleTotalMoney() == null ||
paymentParam.getSettleTotalMoney().compareTo(dbTotalAmount) != 0) {
throw Exceptions.clierr("结算总金额与实际总金额不一致");
}
// 验证总金额是否与参数中的结算总金额一致(这里已经是第二层验证了)
// 第一层验证在循环中已经完成:每个清单项的金额与数据库一致
// 第二层验证:参数中的结算总金额与数据库统计总金额一致
// 查询订单及相关支付上下文(公司、司机账户)
PaymentContextResult ctx = orderInfoService.paymentContext(paymentParam.getOrderId());
if (ctx == null || ctx.getOrderId() == null) {
throw Exceptions.clierr("订单不存在");
}
// 验证总金额是否与参数中的结算总金额一致
if (paymentParam.getSettleTotalMoney() == null ||
paymentParam.getSettleTotalMoney().compareTo(ctx.getSettleMoney()) != 0) {
throw Exceptions.clierr("结算总金额与实际总金额不一致");
}
// 验证总金额是否与参数中的结算总金额一致(这里已经是第二层验证了)
// 第一层验证在循环中已经完成:每个清单项的金额与数据库一致
// 第二层验证:参数中的结算总金额与数据库统计总金额一致
// 根据支付方式处理支付
if (PaymentWay.COMPANY.getVal().equals(paymentParam.getPaymentCategory())) {
// 公司支付:根据订单 trans_company_id -> biz_company.user_id -> money_account
@ -136,20 +132,20 @@ public class PaymentController {
// 根据结算方式判断是否需要检查余额
if (SettlementWay.BALANCE.getVal().equals(ctx.getSettlementWay())) {
// 余额结算:需要检查余额是否充足
if (ctx.getCompanyBalance() == null || ctx.getCompanyBalance().compareTo(dbTotalAmount) < 0) {
if (ctx.getCompanyBalance() == null || ctx.getCompanyBalance().compareTo(ctx.getSettleMoney()) < 0) {
throw Exceptions.clierr("公司账户余额不足");
}
}
// 月结方式:不需要检查余额,允许余额为负数
// 直接扣减公司账户余额
deductCompanyBalance(ctx, dbTotalAmount, paymentParam.getOrderId());
deductCompanyBalance(ctx, ctx.getSettleMoney(), paymentParam.getOrderId());
} else if (PaymentWay.WX.getVal().equals(paymentParam.getPaymentCategory())) {
// 微信支付:创建微信支付订单
WechatPayJsapiOrderDto orderDto = new WechatPayJsapiOrderDto();
orderDto.setOutTradeNo("ORDER_" + ctx.getSn() + "_" + System.currentTimeMillis());
orderDto.setDescription("订单支付-" + ctx.getSn());
orderDto.setTotal(dbTotalAmount.multiply(new BigDecimal("100")).longValue()); // 转换为分
orderDto.setTotal(ctx.getSettleMoney().multiply(new BigDecimal("100")).longValue()); // 转换为分
orderDto.setOpenid(ctx.getWechatOpenid());
WechatPayJsapiOrderResponseDto response = wechatPayService.createMiniProgramOrder(orderDto);
@ -179,7 +175,7 @@ public class PaymentController {
.update();
log.info("订单支付成功订单ID{},支付方式:{},支付金额:{}",
paymentParam.getOrderId(), paymentParam.getPaymentCategory(), dbTotalAmount);
paymentParam.getOrderId(), paymentParam.getPaymentCategory(), ctx.getSettleMoney());
}
return R.success();
@ -386,25 +382,13 @@ public class PaymentController {
throw Exceptions.clierr("退款金额必须大于0");
}
List<OrderExpenseItemsEntity> entityList = orderExpenseItemsService.list(Wrappers.<OrderExpenseItemsEntity>lambdaQuery()
.eq(OrderExpenseItemsEntity::getDeleted, Boolean.FALSE)
.eq(OrderExpenseItemsEntity::getOrderId, refundRequest.getOrderId()));
BigDecimal dbTotalAmount = BigDecimal.ZERO;
if (null == entityList || entityList.size() <= 0) {
throw Exceptions.clierr("结算清单不存在");
}
for (OrderExpenseItemsEntity it : entityList) {
BigDecimal dbSettle = it.getSettleMoney();
// 当dbSettle为null时按0计算
if (dbSettle == null) {
dbSettle = BigDecimal.ZERO;
}
// 累加数据库中的结算金额
dbTotalAmount = dbTotalAmount.add(dbSettle);
if (refundRequest.getRefundAmount().compareTo(ctx.getSettleMoney()) > 0) {
throw Exceptions.clierr("退款金额不能超过订单已结算金额");
}
if (refundRequest.getRefundAmount().compareTo(dbTotalAmount) > 0) {
throw Exceptions.clierr("退款金额不能超过订单已结算金额");
// 如果订单再次退款,本次退款金额+已退款金额不能大于结算金额
if (refundRequest.getRefundAmount().add(ctx.getRefundMoney()).compareTo(ctx.getSettleMoney()) > 0) {
throw Exceptions.clierr("退款金额总和不能超过订单已结算金额");
}
// 恢复公司账户余额

View File

@ -280,4 +280,9 @@ public class OrderInfoEntity {
*/
private LocalDateTime payTime;
/**
* 退
*/
private BigDecimal refundMoney;
}

View File

@ -179,6 +179,11 @@ public class OrderPagingResult {
*/
private BigDecimal settleMoney;
/**
* 退
*/
private BigDecimal refundMoney;
/**
* Id
*/

View File

@ -29,6 +29,10 @@ public class PaymentContextResult {
private String outTradeNo;
private BigDecimal refundMoney;
private BigDecimal settleMoney;
}

View File

@ -36,10 +36,7 @@ import com.njzscloud.supervisory.expense.contant.Scope;
import com.njzscloud.supervisory.expense.pojo.entity.ExpenseItemsConfigEntity;
import com.njzscloud.supervisory.expense.service.ExpenseItemsConfigService;
import com.njzscloud.supervisory.goods.contant.MoneyStrategy;
import com.njzscloud.supervisory.order.contant.CheckStatus;
import com.njzscloud.supervisory.order.contant.OrderCategory;
import com.njzscloud.supervisory.order.contant.OrderStatus;
import com.njzscloud.supervisory.order.contant.OrderViewType;
import com.njzscloud.supervisory.order.contant.*;
import com.njzscloud.supervisory.order.mapper.OrderInfoMapper;
import com.njzscloud.supervisory.order.pojo.entity.*;
import com.njzscloud.supervisory.order.pojo.param.*;
@ -927,7 +924,10 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
// 第三步:合并集合并计算金额,然后批量落库
extraItems.add(0, productItem);
BigDecimal totalDiscountMoney = BigDecimal.ZERO;
BigDecimal totalReviseMoney = BigDecimal.ZERO;
BigDecimal totalSettleMoney = BigDecimal.ZERO;
BigDecimal totalTotalMoney = BigDecimal.ZERO;
for (OrderExpenseItemsEntity item : extraItems) {
// total_money = quantity * unit_price 如果计费策略是按吨总金额计算时数量需要÷1000
BigDecimal quantity = new BigDecimal(item.getQuantity() == null ? 0 : item.getQuantity());
@ -943,9 +943,22 @@ public class OrderInfoService extends ServiceImpl<OrderInfoMapper, OrderInfoEnti
BigDecimal revise = item.getReviseMoney() == null ? BigDecimal.ZERO : item.getReviseMoney();
BigDecimal settle = totalMoney.add(discount).add(revise);
item.setTotalMoney(totalMoney).setSettleMoney(settle);
totalDiscountMoney = totalDiscountMoney.add(discount);
totalReviseMoney = totalReviseMoney.add(revise);
totalSettleMoney = totalSettleMoney.add(settle);
totalTotalMoney = totalTotalMoney.add(totalMoney);
}
orderExpenseItemsService.saveBatch(extraItems);
// 更新订单表的优惠金额; 手动修正金额;结算金额;总金额;
this.lambdaUpdate()
.eq(OrderInfoEntity::getId, orderId)
.set(OrderInfoEntity::getDiscountMoney, totalDiscountMoney)
.set(OrderInfoEntity::getReviseMoney, totalReviseMoney)
.set(OrderInfoEntity::getSettleMoney, totalSettleMoney)
.set(OrderInfoEntity::getTotalMoney, totalTotalMoney)
.update();
}
public OrderPagingResult pendingOrder(String licensePlate) {

View File

@ -29,6 +29,8 @@
a.discount_money,
a.revise_money,
a.settle_money,
a.total_money,
a.refund_money,
a.payment_status,
a.customer_memo,
a.checker_memo,
@ -215,7 +217,9 @@
ma1.id AS companyAccountId,
ma1.money AS companyBalance,
bc.settlement_way,
sua.wechat_openid
sua.wechat_openid,
a.refund_money,
a.settle_money
FROM order_info a
LEFT JOIN biz_company bc ON bc.id = a.trans_company_id
LEFT JOIN money_account ma1 ON ma1.station_id = a.trans_company_id