用户订单金额修改
This commit is contained in:
parent
8bf0ea436a
commit
e7fdd7e543
@ -1,5 +1,6 @@
|
|||||||
package com.ebike.feign.model.dto;
|
package com.ebike.feign.model.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.DecimalMin;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@ -22,6 +23,7 @@ public class FeignEbikePaymentAmountDto {
|
|||||||
@NotBlank(message = "车辆编号不能为空")
|
@NotBlank(message = "车辆编号不能为空")
|
||||||
private String bikeCode;
|
private String bikeCode;
|
||||||
|
|
||||||
|
@DecimalMin(value = "0.00", message = "修改后的价格不能小于0")
|
||||||
@NotNull(message = "价格不能为空")
|
@NotNull(message = "价格不能为空")
|
||||||
private BigDecimal price;
|
private BigDecimal price;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -338,6 +338,7 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Override
|
@Override
|
||||||
public void updateOrderAmount(FeignEbikePaymentAmountDto paymentAmountDto) {
|
public void updateOrderAmount(FeignEbikePaymentAmountDto paymentAmountDto) {
|
||||||
|
BigDecimal newPrice = paymentAmountDto.getPrice();
|
||||||
// 1 获取当前车辆未支付的订单
|
// 1 获取当前车辆未支付的订单
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
.select(EBIKE_ORDER.ALL_COLUMNS)
|
.select(EBIKE_ORDER.ALL_COLUMNS)
|
||||||
@ -345,25 +346,15 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
|
|||||||
.where(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.PENDING_PAYMENT));
|
.where(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.PENDING_PAYMENT));
|
||||||
EbikeOrder ebikeOrder = this.mapper.selectOneByQuery(queryWrapper);
|
EbikeOrder ebikeOrder = this.mapper.selectOneByQuery(queryWrapper);
|
||||||
if (Objects.isNull(ebikeOrder)) {
|
if (Objects.isNull(ebikeOrder)) {
|
||||||
throw new EbikeException("当前车辆不存在待支付的订单");
|
throw new EbikeException("当前车辆不存在待支付的订单,或订单已被处理");
|
||||||
|
}
|
||||||
|
if (newPrice.compareTo(BigDecimal.ZERO) == 0) {
|
||||||
|
log.info("开始处理免费订单,订单id:{}", ebikeOrder.getOrderId());
|
||||||
|
handleFreeOrder(ebikeOrder);
|
||||||
|
} else {
|
||||||
|
log.info("开始处理非免费订单,订单id:{}", ebikeOrder.getOrderId());
|
||||||
|
handlePriceChange(ebikeOrder, newPrice);
|
||||||
}
|
}
|
||||||
// 2 修改当前订单金额(实付金额)
|
|
||||||
UpdateChain.of(EbikeOrder.class)
|
|
||||||
.set(EbikeOrder::getActualAmount, paymentAmountDto.getPrice())
|
|
||||||
.where(EbikeOrder::getOrderId).eq(ebikeOrder.getOrderId())
|
|
||||||
.update();
|
|
||||||
// 3 同步修改支付订单的cost_price(用于支付时的实际金额)
|
|
||||||
ebikePaymentService.updateCostPrice(ebikeOrder.getOrderId(), paymentAmountDto.getPrice());
|
|
||||||
// 4 生成订单明细优惠金额
|
|
||||||
EbikeOrderDetail orderDetail = EbikeOrderDetail.builder()
|
|
||||||
.orderId(ebikeOrder.getOrderId())
|
|
||||||
.operatorId(ebikeOrder.getOperatorId())
|
|
||||||
.detailType(EbikeOrderDetailType.DISCOUNT_AMOUNT.getCode())
|
|
||||||
.detailDescription("运维人员修改订单减免金额")
|
|
||||||
.detailName(EbikeOrderDetailType.DISCOUNT_AMOUNT.getName())
|
|
||||||
.detailAmount(ebikeOrder.getTotalAmount().subtract(paymentAmountDto.getPrice()))
|
|
||||||
.build();
|
|
||||||
ebikeOrderDetailService.save(orderDetail);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -434,6 +425,63 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
|
|||||||
return this.mapper.paginateAs(pageParam, queryWrapper, EbikeDiffOperatorOrderListVo.class);
|
return this.mapper.paginateAs(pageParam, queryWrapper, EbikeDiffOperatorOrderListVo.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理改价逻辑
|
||||||
|
*/
|
||||||
|
private void handlePriceChange(EbikeOrder order, BigDecimal newPrice) {
|
||||||
|
long orderId = order.getOrderId();
|
||||||
|
BigDecimal totalAmount = order.getTotalAmount();
|
||||||
|
|
||||||
|
// 1. 更新订单实付金额
|
||||||
|
UpdateChain.of(EbikeOrder.class)
|
||||||
|
.set(EbikeOrder::getActualAmount, newPrice)
|
||||||
|
.where(EbikeOrder::getOrderId).eq(orderId)
|
||||||
|
.where(EbikeOrder::getOrderStatus).eq(OrderStatus.PENDING_PAYMENT)
|
||||||
|
.update();
|
||||||
|
|
||||||
|
// 2. 同步修改支付表的 cost_price
|
||||||
|
ebikePaymentService.updateCostPrice(orderId, newPrice);
|
||||||
|
|
||||||
|
// 3. 生成优惠明细
|
||||||
|
BigDecimal discountAmount = totalAmount.subtract(newPrice);
|
||||||
|
if (discountAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||||
|
saveDiscountDetail(order, discountAmount, "运维人员修改订单减免金额");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理免费订单逻辑
|
||||||
|
*/
|
||||||
|
private void handleFreeOrder(EbikeOrder order) {
|
||||||
|
// 1. 更新订单状态
|
||||||
|
order.setOrderStatus(OrderStatus.PAID);
|
||||||
|
order.setActualAmount(BigDecimal.ZERO);
|
||||||
|
order.setPaymentTime(LocalDateTime.now());
|
||||||
|
order.setIsFreeOrder(Boolean.TRUE);
|
||||||
|
this.updateById(order);
|
||||||
|
// 2. 逻辑删除支付记录
|
||||||
|
EbikePayment payment = ebikePaymentService.queryPaymentInfo(order.getOrderId());
|
||||||
|
ebikePaymentService.removeById(payment);
|
||||||
|
// 3. 记录优惠明细
|
||||||
|
saveDiscountDetail(order, order.getTotalAmount(), "全额减免");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存优惠明细
|
||||||
|
*/
|
||||||
|
private void saveDiscountDetail(EbikeOrder order, BigDecimal amount, String description) {
|
||||||
|
EbikeOrderDetail detail = EbikeOrderDetail.builder()
|
||||||
|
.orderId(order.getOrderId())
|
||||||
|
.operatorId(order.getOperatorId())
|
||||||
|
.detailType(EbikeOrderDetailType.DISCOUNT_AMOUNT.getCode())
|
||||||
|
.detailDescription(description)
|
||||||
|
.detailName(EbikeOrderDetailType.DISCOUNT_AMOUNT.getName())
|
||||||
|
.detailAmount(amount)
|
||||||
|
.createTime(LocalDateTime.now())
|
||||||
|
.build();
|
||||||
|
ebikeOrderDetailService.save(detail);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算增长率并格式化为百分比字符串
|
* 计算增长率并格式化为百分比字符串
|
||||||
*
|
*
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user