Commit b9018baa0368c5cd8451c633c4cb3c11b3498ceb
1 parent
c5f2d07c
trade:简化价格调整的逻辑
Showing
7 changed files
with
71 additions
and
62 deletions
yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java
| ... | ... | @@ -29,8 +29,8 @@ public interface ErrorCodeConstants { |
| 29 | 29 | ErrorCode ORDER_DELIVERY_FAIL_DELIVERY_TYPE_NOT_EXPRESS = new ErrorCode(1011000024, "交易订单发货失败,发货类型不是快递"); |
| 30 | 30 | ErrorCode ORDER_CANCEL_FAIL_STATUS_NOT_UNPAID = new ErrorCode(1011000025, "交易订单取消失败,订单不是【待支付】状态"); |
| 31 | 31 | ErrorCode ORDER_UPDATE_PRICE_FAIL_PAID = new ErrorCode(1011000026, "支付订单调价失败,原因:支付订单已付款,不能调价"); |
| 32 | - ErrorCode ORDER_UPDATE_PRICE_FAIL_EQUAL = new ErrorCode(1011000027, "支付订单调价失败,原因:价格没有变化"); | |
| 33 | - ErrorCode ORDER_UPDATE_PRICE_FAIL_NOT_ITEM = new ErrorCode(1011000028, "支付订单调价失败,原因:订单项不存在"); | |
| 32 | + ErrorCode ORDER_UPDATE_PRICE_FAIL_ALREADY = new ErrorCode(1011000027, "支付订单调价失败,原因:已经修改过价格"); | |
| 33 | + ErrorCode ORDER_UPDATE_PRICE_FAIL_PRICE_ERROR = new ErrorCode(1011000028, "支付订单调价失败,原因:调整后支付价格不能小于 0.01 元"); | |
| 34 | 34 | ErrorCode ORDER_DELETE_FAIL_STATUS_NOT_CANCEL = new ErrorCode(1011000029, "交易订单删除失败,订单不是【已取消】状态"); |
| 35 | 35 | |
| 36 | 36 | // ========== After Sale 模块 1011000100 ========== | ... | ... |
yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/order/TradeOrderOperateTypeEnum.java
| ... | ... | @@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor; |
| 14 | 14 | public enum TradeOrderOperateTypeEnum { |
| 15 | 15 | |
| 16 | 16 | MEMBER_CREATE(1, "用户下单"), |
| 17 | + ADMIN_UPDATE_PRICE(2, "订单价格 {oldPayPrice} 修改,实际支付金额为 {newPayPrice} 元"), | |
| 17 | 18 | MEMBER_PAY(10, "用户付款成功"), |
| 18 | 19 | ADMIN_DELIVERY(20, "已发货,快递公司:{deliveryName},快递单号:{logisticsNo}"), |
| 19 | 20 | MEMBER_RECEIVE(30, "用户已收货"), | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/admin/order/TradeOrderController.java
| ... | ... | @@ -73,7 +73,7 @@ public class TradeOrderController { |
| 73 | 73 | |
| 74 | 74 | // 查询订单项 |
| 75 | 75 | List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(id); |
| 76 | - // orderLog | |
| 76 | + // TODO @puhui999:orderLog | |
| 77 | 77 | // 拼接数据 |
| 78 | 78 | MemberUserRespDTO user = memberUserApi.getUser(order.getUserId()); |
| 79 | 79 | return success(TradeOrderConvert.INSTANCE.convert(order, orderItems, user)); |
| ... | ... | @@ -120,4 +120,6 @@ public class TradeOrderController { |
| 120 | 120 | return success(true); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | + // TODO :核销逻辑 | |
| 124 | + | |
| 123 | 125 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java
| ... | ... | @@ -11,6 +11,7 @@ import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 11 | 11 | import cn.iocoder.yudao.framework.common.enums.TerminalEnum; |
| 12 | 12 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 13 | 13 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 14 | +import cn.iocoder.yudao.framework.common.util.number.MoneyUtils; | |
| 14 | 15 | import cn.iocoder.yudao.module.member.api.address.AddressApi; |
| 15 | 16 | import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO; |
| 16 | 17 | import cn.iocoder.yudao.module.member.api.level.MemberLevelApi; |
| ... | ... | @@ -64,6 +65,7 @@ import cn.iocoder.yudao.module.trade.service.order.handler.TradeOrderHandler; |
| 64 | 65 | import cn.iocoder.yudao.module.trade.service.price.TradePriceService; |
| 65 | 66 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| 66 | 67 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; |
| 68 | +import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper; | |
| 67 | 69 | import lombok.extern.slf4j.Slf4j; |
| 68 | 70 | import org.jetbrains.annotations.NotNull; |
| 69 | 71 | import org.springframework.scheduling.annotation.Async; |
| ... | ... | @@ -696,86 +698,58 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { |
| 696 | 698 | |
| 697 | 699 | @Override |
| 698 | 700 | @Transactional(rollbackFor = Exception.class) |
| 701 | + @TradeOrderLog(operateType = TradeOrderOperateTypeEnum.ADMIN_UPDATE_PRICE) | |
| 699 | 702 | public void updateOrderPrice(TradeOrderUpdatePriceReqVO reqVO) { |
| 700 | - // 1、校验交易订单 | |
| 703 | + // 1.1 校验交易订单 | |
| 701 | 704 | TradeOrderDO order = validateOrderExists(reqVO.getId()); |
| 702 | 705 | if (order.getPayStatus()) { |
| 703 | 706 | throw exception(ORDER_UPDATE_PRICE_FAIL_PAID); |
| 704 | 707 | } |
| 705 | - // 2、校验订单项 | |
| 706 | - List<TradeOrderItemDO> items = tradeOrderItemMapper.selectListByOrderId(order.getId()); | |
| 707 | - if (CollUtil.isEmpty(items)) { | |
| 708 | - throw exception(ORDER_UPDATE_PRICE_FAIL_NOT_ITEM); | |
| 708 | + // 1.2 校验调价金额是否变化 | |
| 709 | + if (order.getAdjustPrice() > 0) { | |
| 710 | + throw exception(ORDER_UPDATE_PRICE_FAIL_ALREADY); | |
| 709 | 711 | } |
| 710 | - // 3、校验调价金额是否变化 | |
| 711 | - if (ObjectUtil.equal(order.getAdjustPrice(), reqVO.getAdjustPrice())) { | |
| 712 | - throw exception(ORDER_UPDATE_PRICE_FAIL_EQUAL); | |
| 712 | + // 1.3 支付价格不能为 0 | |
| 713 | + int newPayPrice = order.getPayPrice() + order.getAdjustPrice(); | |
| 714 | + if (newPayPrice <= 0) { | |
| 715 | + throw exception(ORDER_UPDATE_PRICE_FAIL_PRICE_ERROR); | |
| 713 | 716 | } |
| 714 | 717 | |
| 715 | - // 4、更新订单 | |
| 716 | - TradeOrderDO update = new TradeOrderDO(); | |
| 717 | - update.setId(order.getId()); | |
| 718 | - update.setAdjustPrice(reqVO.getAdjustPrice()); | |
| 719 | - int orderPayPrice = order.getAdjustPrice() != null ? (order.getPayPrice() - order.getAdjustPrice()) | |
| 720 | - + reqVO.getAdjustPrice() : order.getPayPrice() + reqVO.getAdjustPrice(); | |
| 721 | - update.setPayPrice(orderPayPrice); | |
| 722 | - tradeOrderMapper.updateById(update); | |
| 723 | - // TODO @芋艿:改价时,赠送的积分,要不要做改动??? | |
| 724 | - | |
| 725 | - // 5、更新 TradeOrderItem | |
| 726 | - // TradeOrderItemDO 需要做 adjustPrice 的分摊 | |
| 727 | - List<Integer> dividePrices = dividePrice(items, orderPayPrice); | |
| 718 | + // 2. 更新订单 | |
| 719 | + tradeOrderMapper.updateById(new TradeOrderDO().setId(order.getId()) | |
| 720 | + .setAdjustPrice(reqVO.getAdjustPrice()).setPayPrice(newPayPrice)); | |
| 721 | + | |
| 722 | + // 3. 更新 TradeOrderItem,需要做 adjustPrice 的分摊 | |
| 723 | + List<TradeOrderItemDO> orderOrderItems = tradeOrderItemMapper.selectListByOrderId(order.getId()); | |
| 724 | + List<Integer> dividePrices = TradePriceCalculatorHelper.dividePrice2(orderOrderItems, newPayPrice); | |
| 728 | 725 | List<TradeOrderItemDO> updateItems = new ArrayList<>(); |
| 729 | - for (int i = 0; i < items.size(); i++) { | |
| 730 | - TradeOrderItemDO item = items.get(i); | |
| 731 | - Integer adjustPrice = item.getPrice() - dividePrices.get(i); // 计算调整的金额 | |
| 732 | - updateItems.add(new TradeOrderItemDO().setId(item.getId()).setAdjustPrice(adjustPrice) | |
| 733 | - .setPayPrice(item.getPayPrice() - adjustPrice)); | |
| 726 | + for (int i = 0; i < orderOrderItems.size(); i++) { | |
| 727 | + TradeOrderItemDO item = orderOrderItems.get(i); | |
| 728 | + updateItems.add(new TradeOrderItemDO().setId(item.getId()).setAdjustPrice(dividePrices.get(i)) | |
| 729 | + .setPayPrice(item.getPayPrice() + dividePrices.get(i))); | |
| 734 | 730 | } |
| 735 | 731 | tradeOrderItemMapper.updateBatch(updateItems); |
| 736 | 732 | |
| 737 | - // 6、更新支付订单 | |
| 738 | - payOrderApi.updatePayOrderPrice(order.getPayOrderId(), update.getPayPrice()); | |
| 739 | - } | |
| 733 | + // 4. 更新支付订单 | |
| 734 | + payOrderApi.updatePayOrderPrice(order.getPayOrderId(), newPayPrice); | |
| 740 | 735 | |
| 741 | - /** | |
| 742 | - * 计算订单调价价格分摊 | |
| 743 | - * | |
| 744 | - * @param items 订单项 | |
| 745 | - * @param orderPayPrice 订单支付金额 | |
| 746 | - * @return 分摊金额数组,和传入的 orderItems 一一对应 | |
| 747 | - */ | |
| 748 | - private List<Integer> dividePrice(List<TradeOrderItemDO> items, Integer orderPayPrice) { | |
| 749 | - Integer total = getSumValue(items, TradeOrderItemDO::getPrice, Integer::sum); | |
| 750 | - assert total != null; | |
| 751 | - // 遍历每一个,进行分摊 | |
| 752 | - List<Integer> prices = new ArrayList<>(items.size()); | |
| 753 | - int remainPrice = orderPayPrice; | |
| 754 | - for (int i = 0; i < items.size(); i++) { | |
| 755 | - TradeOrderItemDO orderItem = items.get(i); | |
| 756 | - int partPrice; | |
| 757 | - if (i < items.size() - 1) { // 减一的原因,是因为拆分时,如果按照比例,可能会出现.所以最后一个,使用反减 | |
| 758 | - partPrice = (int) (orderPayPrice * (1.0D * orderItem.getPayPrice() / total)); | |
| 759 | - remainPrice -= partPrice; | |
| 760 | - } else { | |
| 761 | - partPrice = remainPrice; | |
| 762 | - } | |
| 763 | - Assert.isTrue(partPrice >= 0, "分摊金额必须大于等于 0"); | |
| 764 | - prices.add(partPrice); | |
| 765 | - } | |
| 766 | - return prices; | |
| 736 | + // 5. 记录订单日志 | |
| 737 | + TradeOrderLogUtils.setOrderInfo(order.getId(), order.getStatus(), order.getStatus(), | |
| 738 | + MapUtil.<String, Object>builder().put("oldPayPrice", MoneyUtils.fenToYuanStr(order.getPayPrice())) | |
| 739 | + .put("newPayPrice", MoneyUtils.fenToYuanStr(newPayPrice)).build()); | |
| 767 | 740 | } |
| 768 | 741 | |
| 769 | 742 | @Override |
| 770 | 743 | public void updateOrderAddress(TradeOrderUpdateAddressReqVO reqVO) { |
| 771 | 744 | // 校验交易订单 |
| 772 | 745 | validateOrderExists(reqVO.getId()); |
| 773 | - // TODO 是否需要校验订单是否发货 | |
| 746 | + // TODO @puhui999:是否需要校验订单是否发货 | |
| 774 | 747 | // TODO 发货后是否支持修改收货地址 |
| 775 | 748 | |
| 776 | 749 | // 更新 |
| 777 | - TradeOrderDO update = TradeOrderConvert.INSTANCE.convert(reqVO); | |
| 778 | - tradeOrderMapper.updateById(update); | |
| 750 | + tradeOrderMapper.updateById(TradeOrderConvert.INSTANCE.convert(reqVO)); | |
| 751 | + | |
| 752 | + // TODO @puhui999:操作日志 | |
| 779 | 753 | } |
| 780 | 754 | |
| 781 | 755 | // =================== Order Item =================== | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePriceCalculatorHelper.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.hutool.core.lang.Assert; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 5 | 5 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; |
| 7 | +import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO; | |
| 7 | 8 | import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; |
| 8 | 9 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| 9 | 10 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; |
| ... | ... | @@ -233,6 +234,36 @@ public class TradePriceCalculatorHelper { |
| 233 | 234 | } |
| 234 | 235 | |
| 235 | 236 | /** |
| 237 | + * 计算订单调价价格分摊 | |
| 238 | + * | |
| 239 | + * 和 {@link #dividePrice(List, Integer)} 逻辑一致,只是传入的是 TradeOrderItemDO 对象 | |
| 240 | + * | |
| 241 | + * @param items 订单项 | |
| 242 | + * @param price 订单支付金额 | |
| 243 | + * @return 分摊金额数组,和传入的 orderItems 一一对应 | |
| 244 | + */ | |
| 245 | + public static List<Integer> dividePrice2(List<TradeOrderItemDO> items, Integer price) { | |
| 246 | + Integer total = getSumValue(items, TradeOrderItemDO::getPrice, Integer::sum); | |
| 247 | + assert total != null; | |
| 248 | + // 遍历每一个,进行分摊 | |
| 249 | + List<Integer> prices = new ArrayList<>(items.size()); | |
| 250 | + int remainPrice = price; | |
| 251 | + for (int i = 0; i < items.size(); i++) { | |
| 252 | + TradeOrderItemDO orderItem = items.get(i); | |
| 253 | + int partPrice; | |
| 254 | + if (i < items.size() - 1) { // 减一的原因,是因为拆分时,如果按照比例,可能会出现.所以最后一个,使用反减 | |
| 255 | + partPrice = (int) (price * (1.0D * orderItem.getPayPrice() / total)); | |
| 256 | + remainPrice -= partPrice; | |
| 257 | + } else { | |
| 258 | + partPrice = remainPrice; | |
| 259 | + } | |
| 260 | + Assert.isTrue(partPrice >= 0, "分摊金额必须大于等于 0"); | |
| 261 | + prices.add(partPrice); | |
| 262 | + } | |
| 263 | + return prices; | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 236 | 267 | * 添加【匹配】单个 OrderItem 的营销明细 |
| 237 | 268 | * |
| 238 | 269 | * @param result 价格计算结果 | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/KdNiaoExpressClientIntegrationTest.java
| ... | ... | @@ -38,7 +38,7 @@ public class KdNiaoExpressClientIntegrationTest { |
| 38 | 38 | public void testGetExpressTrackList() { |
| 39 | 39 | ExpressTrackQueryReqDTO reqDTO = new ExpressTrackQueryReqDTO(); |
| 40 | 40 | reqDTO.setExpressCode("STO"); |
| 41 | - reqDTO.setLogisticsNo("663220402764314"); | |
| 41 | + reqDTO.setLogisticsNo("777168349863987"); | |
| 42 | 42 | List<ExpressTrackRespDTO> tracks = client.getExpressTrackList(reqDTO); |
| 43 | 43 | System.out.println(JsonUtils.toJsonPrettyString(tracks)); |
| 44 | 44 | } | ... | ... |