订单管理:完善订单调价分摊逻辑

This commit is contained in:
puhui999 2023-09-10 00:50:13 +08:00
parent 51dab96889
commit c023209aa5
4 changed files with 58 additions and 28 deletions

View File

@ -53,8 +53,8 @@ import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum; import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.*; import cn.iocoder.yudao.module.trade.enums.order.*;
import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties; import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
import cn.iocoder.yudao.module.trade.service.brokerage.record.BrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO; import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.BrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.cart.CartService; import cn.iocoder.yudao.module.trade.service.cart.CartService;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService; import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService;
import cn.iocoder.yudao.module.trade.service.message.TradeMessageService; import cn.iocoder.yudao.module.trade.service.message.TradeMessageService;
@ -70,14 +70,13 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.ORDER_UPDATE_PRICE_FAIL_EQUAL;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.ORDER_UPDATE_PRICE_FAIL_PAID;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
/** /**
@ -562,35 +561,67 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
} }
@Override @Override
// TODO @puhui999考虑事务性 @Transactional(rollbackFor = Exception.class)
public void updateOrderPrice(TradeOrderUpdatePriceReqVO reqVO) { public void updateOrderPrice(TradeOrderUpdatePriceReqVO reqVO) {
// 校验交易订单 // 1校验交易订单
TradeOrderDO order = validateOrderExists(reqVO.getId()); TradeOrderDO order = validateOrderExists(reqVO.getId());
if (order.getPayStatus()) { if (order.getPayStatus()) {
throw exception(ORDER_UPDATE_PRICE_FAIL_PAID); throw exception(ORDER_UPDATE_PRICE_FAIL_PAID);
} }
// 2校验订单项
List<TradeOrderItemDO> items = tradeOrderItemMapper.selectListByOrderId(order.getId());
if (CollUtil.isEmpty(items)) {
throw exception(ORDER_UPDATE_PRICE_FAIL_NOT_ITEM);
}
// 3校验调价金额是否变化
if (ObjectUtil.equal(order.getAdjustPrice(), reqVO.getAdjustPrice())) { if (ObjectUtil.equal(order.getAdjustPrice(), reqVO.getAdjustPrice())) {
throw exception(ORDER_UPDATE_PRICE_FAIL_EQUAL); throw exception(ORDER_UPDATE_PRICE_FAIL_EQUAL);
} }
// TODO @puhui999应该是按照 payPrice 分配并且要考虑取余问题payPrice 也要考虑item 里的 // 4更新订单
List<TradeOrderItemDO> itemDOs = tradeOrderItemMapper.selectListByOrderId(order.getId()); TradeOrderDO update = new TradeOrderDO();
// TradeOrderItemDO 需要做 adjustPrice 的分摊 update.setId(order.getId());
int price = reqVO.getAdjustPrice() / itemDOs.size(); update.setAdjustPrice(reqVO.getAdjustPrice());
itemDOs.forEach(item -> { int orderPayPrice = order.getAdjustPrice() != null ? (order.getPayPrice() - order.getAdjustPrice())
item.setAdjustPrice(price); + reqVO.getAdjustPrice() : order.getPayPrice() + reqVO.getAdjustPrice();
}); update.setPayPrice(orderPayPrice);
// 更新 TradeOrderItem
// TODO @puhui999不要整个对象去更新哈应该 new 一下
tradeOrderItemMapper.updateBatch(itemDOs);
// 更新订单
// TODO @puhui999要考虑多次修改价格不能单单的 payPrice + 价格
TradeOrderDO update = TradeOrderConvert.INSTANCE.convert(reqVO);
update.setPayPrice(update.getPayPrice() + update.getAdjustPrice());
// TODO @芋艿改价时赠送的积分要不要做改动 // TODO @芋艿改价时赠送的积分要不要做改动
tradeOrderMapper.updateById(update); tradeOrderMapper.updateById(update);
// 更新支付订单
payOrderApi.updatePayOrderPriceById(order.getPayOrderId(), update.getPayPrice()); // TODO @puhui999应该是按照 payPrice 分配并且要考虑取余问题payPrice 也要考虑item 里的
// TODO先按 adjustPrice 实现没明白 payPrice 怎么搞哈哈哈
// 5更新 TradeOrderItem
if (items.size() > 1) {
// TradeOrderItemDO 需要做 adjustPrice 的分摊
int price = reqVO.getAdjustPrice() / items.size();
int remainderPrice = reqVO.getAdjustPrice() % items.size();
List<TradeOrderItemDO> orders = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
// 把平摊后剩余的金额加到第一个订单项
if (remainderPrice != 0 && i == 0) {
orders.add(convertOrderItemPrice(items.get(i), price + remainderPrice));
}
orders.add(convertOrderItemPrice(items.get(i), price));
}
tradeOrderItemMapper.updateBatch(orders);
} else {
TradeOrderItemDO orderItem = items.get(0);
TradeOrderItemDO updateItem = convertOrderItemPrice(orderItem, reqVO.getAdjustPrice());
tradeOrderItemMapper.updateById(updateItem);
}
// 6更新支付订单
payOrderApi.updatePayOrderPrice(order.getPayOrderId(), update.getPayPrice());
}
private TradeOrderItemDO convertOrderItemPrice(TradeOrderItemDO orderItem, Integer price) {
TradeOrderItemDO newOrderItem = new TradeOrderItemDO();
newOrderItem.setId(orderItem.getId());
newOrderItem.setAdjustPrice(price);
int payPrice = orderItem.getAdjustPrice() != null ? (orderItem.getPayPrice() - orderItem.getAdjustPrice())
+ price : orderItem.getPayPrice() + price;
newOrderItem.setPayPrice(payPrice);
return newOrderItem;
} }
@Override @Override

View File

@ -29,13 +29,12 @@ public interface PayOrderApi {
*/ */
PayOrderRespDTO getOrder(Long id); PayOrderRespDTO getOrder(Long id);
// TODO @puhui999可以去掉 byId然后 payOrderId 参数改成 id
/** /**
* 更新支付订单价格 * 更新支付订单价格
* *
* @param payOrderId 支付单编号 * @param id 支付单编号
* @param payPrice 支付单价格 * @param payPrice 支付单价格
*/ */
void updatePayOrderPriceById(Long payOrderId, Integer payPrice); void updatePayOrderPrice(Long id, Integer payPrice);
} }

View File

@ -32,8 +32,8 @@ public class PayOrderApiImpl implements PayOrderApi {
} }
@Override @Override
public void updatePayOrderPriceById(Long payOrderId, Integer payPrice) { public void updatePayOrderPrice(Long id, Integer payPrice) {
payOrderService.updatePayOrderPriceById(payOrderId, payPrice); payOrderService.updatePayOrderPrice(id, payPrice);
} }
} }

View File

@ -101,10 +101,10 @@ public interface PayOrderService {
/** /**
* 更新支付订单价格 * 更新支付订单价格
* *
* @param payOrderId 支付单编号 * @param id 支付单编号
* @param payPrice 支付单价格 * @param payPrice 支付单价格
*/ */
void updatePayOrderPriceById(Long payOrderId, Integer payPrice); void updatePayOrderPrice(Long id, Integer payPrice);
/** /**
* 获得支付订单 * 获得支付订单