trade:简化价格调整的逻辑

This commit is contained in:
YunaiV 2023-09-27 19:14:48 +08:00
parent c5f2d07c87
commit b9018baa03
7 changed files with 70 additions and 61 deletions

View File

@ -29,8 +29,8 @@ public interface ErrorCodeConstants {
ErrorCode ORDER_DELIVERY_FAIL_DELIVERY_TYPE_NOT_EXPRESS = new ErrorCode(1011000024, "交易订单发货失败,发货类型不是快递");
ErrorCode ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID = new ErrorCode(1011000025, "交易订单取消失败,订单不是【待支付】状态");
ErrorCode ORDER_UPDATE_PRICE_FAIL_PAID = new ErrorCode(1011000026, "支付订单调价失败,原因:支付订单已付款,不能调价");
ErrorCode ORDER_UPDATE_PRICE_FAIL_EQUAL = new ErrorCode(1011000027, "支付订单调价失败,原因:价格没有变化");
ErrorCode ORDER_UPDATE_PRICE_FAIL_NOT_ITEM = new ErrorCode(1011000028, "支付订单调价失败,原因:订单项不存在");
ErrorCode ORDER_UPDATE_PRICE_FAIL_ALREADY = new ErrorCode(1011000027, "支付订单调价失败,原因:已经修改过价格");
ErrorCode ORDER_UPDATE_PRICE_FAIL_PRICE_ERROR = new ErrorCode(1011000028, "支付订单调价失败,原因:调整后支付价格不能小于 0.01 元");
ErrorCode ORDER_DELETE_FAIL_STATUS_NOT_CANCEL = new ErrorCode(1011000029, "交易订单删除失败,订单不是【已取消】状态");
// ========== After Sale 模块 1011000100 ==========

View File

@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor;
public enum TradeOrderOperateTypeEnum {
MEMBER_CREATE(1, "用户下单"),
ADMIN_UPDATE_PRICE(2, "订单价格 {oldPayPrice} 修改,实际支付金额为 {newPayPrice} 元"),
MEMBER_PAY(10, "用户付款成功"),
ADMIN_DELIVERY(20, "已发货,快递公司:{deliveryName},快递单号:{logisticsNo}"),
MEMBER_RECEIVE(30, "用户已收货"),

View File

@ -73,7 +73,7 @@ public class TradeOrderController {
// 查询订单项
List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(id);
// orderLog
// TODO @puhui999orderLog
// 拼接数据
MemberUserRespDTO user = memberUserApi.getUser(order.getUserId());
return success(TradeOrderConvert.INSTANCE.convert(order, orderItems, user));
@ -120,4 +120,6 @@ public class TradeOrderController {
return success(true);
}
// TODO 核销逻辑
}

View File

@ -75,6 +75,7 @@ public class AppTradeOrderController {
}
// TODO @芋艿如果拼团活动秒杀活动砍价活动时是不是要额外在返回活动之类的信息
// TODO @puhui999需要的
@GetMapping("/get-detail")
@Operation(summary = "获得交易订单")
@Parameter(name = "id", description = "交易订单编号")

View File

@ -11,6 +11,7 @@ import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.member.api.address.AddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
@ -64,6 +65,7 @@ import cn.iocoder.yudao.module.trade.service.order.handler.TradeOrderHandler;
import cn.iocoder.yudao.module.trade.service.price.TradePriceService;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.scheduling.annotation.Async;
@ -696,86 +698,58 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Override
@Transactional(rollbackFor = Exception.class)
@TradeOrderLog(operateType = TradeOrderOperateTypeEnum.ADMIN_UPDATE_PRICE)
public void updateOrderPrice(TradeOrderUpdatePriceReqVO reqVO) {
// 1校验交易订单
// 1.1 校验交易订单
TradeOrderDO order = validateOrderExists(reqVO.getId());
if (order.getPayStatus()) {
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);
// 1.2 校验调价金额是否变化
if (order.getAdjustPrice() > 0) {
throw exception(ORDER_UPDATE_PRICE_FAIL_ALREADY);
}
// 3校验调价金额是否变化
if (ObjectUtil.equal(order.getAdjustPrice(), reqVO.getAdjustPrice())) {
throw exception(ORDER_UPDATE_PRICE_FAIL_EQUAL);
// 1.3 支付价格不能为 0
int newPayPrice = order.getPayPrice() + order.getAdjustPrice();
if (newPayPrice <= 0) {
throw exception(ORDER_UPDATE_PRICE_FAIL_PRICE_ERROR);
}
// 4更新订单
TradeOrderDO update = new TradeOrderDO();
update.setId(order.getId());
update.setAdjustPrice(reqVO.getAdjustPrice());
int orderPayPrice = order.getAdjustPrice() != null ? (order.getPayPrice() - order.getAdjustPrice())
+ reqVO.getAdjustPrice() : order.getPayPrice() + reqVO.getAdjustPrice();
update.setPayPrice(orderPayPrice);
tradeOrderMapper.updateById(update);
// TODO @芋艿改价时赠送的积分要不要做改动
// 2. 更新订单
tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId())
.setAdjustPrice(reqVO.getAdjustPrice()).setPayPrice(newPayPrice));
// 5更新 TradeOrderItem
// TradeOrderItemDO 需要做 adjustPrice 的分摊
List<Integer> dividePrices = dividePrice(items, orderPayPrice);
// 3. 更新 TradeOrderItem需要做 adjustPrice 的分摊
List<TradeOrderItemDO> orderOrderItems = tradeOrderItemMapper.selectListByOrderId(order.getId());
List<Integer> dividePrices = TradePriceCalculatorHelper.dividePrice2(orderOrderItems, newPayPrice);
List<TradeOrderItemDO> updateItems = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
TradeOrderItemDO item = items.get(i);
Integer adjustPrice = item.getPrice() - dividePrices.get(i); // 计算调整的金额
updateItems.add(new TradeOrderItemDO().setId(item.getId()).setAdjustPrice(adjustPrice)
.setPayPrice(item.getPayPrice() - adjustPrice));
for (int i = 0; i < orderOrderItems.size(); i++) {
TradeOrderItemDO item = orderOrderItems.get(i);
updateItems.add(new TradeOrderItemDO().setId(item.getId()).setAdjustPrice(dividePrices.get(i))
.setPayPrice(item.getPayPrice() + dividePrices.get(i)));
}
tradeOrderItemMapper.updateBatch(updateItems);
// 6更新支付订单
payOrderApi.updatePayOrderPrice(order.getPayOrderId(), update.getPayPrice());
}
// 4. 更新支付订单
payOrderApi.updatePayOrderPrice(order.getPayOrderId(), newPayPrice);
/**
* 计算订单调价价格分摊
*
* @param items 订单项
* @param orderPayPrice 订单支付金额
* @return 分摊金额数组和传入的 orderItems 一一对应
*/
private List<Integer> dividePrice(List<TradeOrderItemDO> items, Integer orderPayPrice) {
Integer total = getSumValue(items, TradeOrderItemDO::getPrice, Integer::sum);
assert total != null;
// 遍历每一个进行分摊
List<Integer> prices = new ArrayList<>(items.size());
int remainPrice = orderPayPrice;
for (int i = 0; i < items.size(); i++) {
TradeOrderItemDO orderItem = items.get(i);
int partPrice;
if (i < items.size() - 1) { // 减一的原因是因为拆分时如果按照比例可能会出现.所以最后一个使用反减
partPrice = (int) (orderPayPrice * (1.0D * orderItem.getPayPrice() / total));
remainPrice -= partPrice;
} else {
partPrice = remainPrice;
}
Assert.isTrue(partPrice >= 0, "分摊金额必须大于等于 0");
prices.add(partPrice);
}
return prices;
// 5. 记录订单日志
TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), order.getStatus(),
MapUtil.<String, Object>builder().put("oldPayPrice", MoneyUtils.fenToYuanStr(order.getPayPrice()))
.put("newPayPrice", MoneyUtils.fenToYuanStr(newPayPrice)).build());
}
@Override
public void updateOrderAddress(TradeOrderUpdateAddressReqVO reqVO) {
// 校验交易订单
validateOrderExists(reqVO.getId());
// TODO 是否需要校验订单是否发货
// TODO @puhui999是否需要校验订单是否发货
// TODO 发货后是否支持修改收货地址
// 更新
TradeOrderDO update = TradeOrderConvert.INSTANCE.convert(reqVO);
tradeOrderMapper.updateById(update);
tradeOrderMapper.updateById(TradeOrderConvert.INSTANCE.convert(reqVO));
// TODO @puhui999操作日志
}
// =================== Order Item ===================

View File

@ -4,6 +4,7 @@ import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
@ -232,6 +233,36 @@ public class TradePriceCalculatorHelper {
return prices;
}
/**
* 计算订单调价价格分摊
*
* {@link #dividePrice(List, Integer)} 逻辑一致只是传入的是 TradeOrderItemDO 对象
*
* @param items 订单项
* @param price 订单支付金额
* @return 分摊金额数组和传入的 orderItems 一一对应
*/
public static List<Integer> dividePrice2(List<TradeOrderItemDO> items, Integer price) {
Integer total = getSumValue(items, TradeOrderItemDO::getPrice, Integer::sum);
assert total != null;
// 遍历每一个进行分摊
List<Integer> prices = new ArrayList<>(items.size());
int remainPrice = price;
for (int i = 0; i < items.size(); i++) {
TradeOrderItemDO orderItem = items.get(i);
int partPrice;
if (i < items.size() - 1) { // 减一的原因是因为拆分时如果按照比例可能会出现.所以最后一个使用反减
partPrice = (int) (price * (1.0D * orderItem.getPayPrice() / total));
remainPrice -= partPrice;
} else {
partPrice = remainPrice;
}
Assert.isTrue(partPrice >= 0, "分摊金额必须大于等于 0");
prices.add(partPrice);
}
return prices;
}
/**
* 添加匹配单个 OrderItem 的营销明细
*

View File

@ -38,7 +38,7 @@ public class KdNiaoExpressClientIntegrationTest {
public void testGetExpressTrackList() {
ExpressTrackQueryReqDTO reqDTO = new ExpressTrackQueryReqDTO();
reqDTO.setExpressCode("STO");
reqDTO.setLogisticsNo("663220402764314");
reqDTO.setLogisticsNo("777168349863987");
List<ExpressTrackRespDTO> tracks = client.getExpressTrackList(reqDTO);
System.out.println(JsonUtils.toJsonPrettyString(tracks));
}