Authored by TANLING

极速优化

  1 +package com.yohoufo.order.event;
  2 +
  3 +import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
  4 +import com.yohoufo.common.alarm.Event;
  5 +import lombok.AllArgsConstructor;
  6 +import lombok.Builder;
  7 +import lombok.Data;
  8 +import lombok.NoArgsConstructor;
  9 +
  10 +@Data
  11 +@Builder
  12 +@NoArgsConstructor
  13 +@AllArgsConstructor
  14 +public class FastDeliveryChangeEvent extends Event {
  15 +
  16 + FastDeliveryReq fastDeliveryReq;
  17 +
  18 + Opt opt;
  19 +
  20 + public enum Opt{
  21 + Delivery, Cancel
  22 + }
  23 +
  24 +}
@@ -27,6 +27,7 @@ import com.yohoufo.dal.order.model.*; @@ -27,6 +27,7 @@ import com.yohoufo.dal.order.model.*;
27 import com.yohoufo.order.constants.MetaKey; 27 import com.yohoufo.order.constants.MetaKey;
28 import com.yohoufo.order.event.DeliverNoticeEvent; 28 import com.yohoufo.order.event.DeliverNoticeEvent;
29 import com.yohoufo.order.event.ErpBuyerOrderEvent; 29 import com.yohoufo.order.event.ErpBuyerOrderEvent;
  30 +import com.yohoufo.order.event.FastDeliveryChangeEvent;
30 import com.yohoufo.order.event.SellerCancelDeliverEvent; 31 import com.yohoufo.order.event.SellerCancelDeliverEvent;
31 import com.yohoufo.order.model.OperateTransferExpressInfo; 32 import com.yohoufo.order.model.OperateTransferExpressInfo;
32 import com.yohoufo.order.model.dto.PreSaleOrderConfig; 33 import com.yohoufo.order.model.dto.PreSaleOrderConfig;
@@ -201,37 +202,13 @@ public class BuyerOrderPaymentService extends AbstractOrderPaymentService { @@ -201,37 +202,13 @@ public class BuyerOrderPaymentService extends AbstractOrderPaymentService {
201 orderInfo.setStatus(orderStatusCode); 202 orderInfo.setStatus(orderStatusCode);
202 203
203 // 极速 不管是寄存还是发货 都需要通知卖家发货 204 // 极速 不管是寄存还是发货 都需要通知卖家发货
204 - if (fastDeliveryProxyService.isFastDeliveryGoods(orderInfo.getSkup())){  
205 -  
206 - String wayBillCode = fastDeliveryProxyService.getVRWaybillCode(orderInfo.getOrderCode());  
207 - try{  
208 - retryer.call(()->{  
209 - // 同时生成物流单号,通知第三方发货  
210 - FastDeliveryReq fastDeliveryReq = FastDeliveryReq.builder()  
211 - .sellerUid(orderInfo.getSellerUid())  
212 - .skup(orderInfo.getSkup())  
213 - .waybillCode(wayBillCode)  
214 - .orderCode(orderInfo.getOrderCode())  
215 - .build();  
216 - fastDeliveryProxyService.delivery(fastDeliveryReq);  
217 - return null;  
218 - });  
219 - }catch (Exception e){  
220 - logger.warn("fast delivery delivery fail, e {}", e);  
221 - } 205 + FastDeliveryChangeEvent fastDeliveryEvent = new FastDeliveryChangeEvent(FastDeliveryReq.builder()
  206 + .sellerUid(orderInfo.getSellerUid())
  207 + .skup(orderInfo.getSkup())
  208 + .orderCode(orderInfo.getOrderCode())
  209 + .build(), FastDeliveryChangeEvent.Opt.Delivery);
  210 + EventBusPublisher.publishEvent(fastDeliveryEvent);
222 211
223 - // 卖家自动发货  
224 - executors.submit(()->{  
225 - SellerDeliverToDepotReq sdtdReq = SellerDeliverToDepotReq.builder()  
226 - .sellerUid(orderInfo.getSellerUid())  
227 - .orderCode(orderInfo.getOrderCode())  
228 - .wayBillCode(wayBillCode)  
229 - .depotNum(DepotType.NJ.getCode())  
230 - .expressCompanyId(0)  
231 - .build();  
232 - expressInfoService.deliverToDepot(sdtdReq);  
233 - });  
234 - }  
235 return result; 212 return result;
236 } 213 }
237 214
  1 +package com.yohoufo.order.service.handler;
  2 +
  3 +import com.github.rholder.retry.Retryer;
  4 +import com.github.rholder.retry.RetryerBuilder;
  5 +import com.github.rholder.retry.StopStrategies;
  6 +import com.github.rholder.retry.WaitStrategies;
  7 +import com.yohobuy.ufo.model.order.constants.DepotType;
  8 +import com.yohobuy.ufo.model.order.req.SellerDeliverToDepotReq;
  9 +import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
  10 +import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
  11 +import com.yohoufo.common.alarm.IEventHandler;
  12 +import com.yohoufo.order.event.FastDeliveryChangeEvent;
  13 +import com.yohoufo.order.service.IExpressInfoService;
  14 +import com.yohoufo.order.service.proxy.FastDeliveryProxyService;
  15 +import org.slf4j.Logger;
  16 +import org.slf4j.LoggerFactory;
  17 +import org.springframework.beans.factory.annotation.Autowired;
  18 +import org.springframework.stereotype.Component;
  19 +
  20 +import java.util.concurrent.ExecutorService;
  21 +import java.util.concurrent.Executors;
  22 +import java.util.concurrent.TimeUnit;
  23 +
  24 +@Component
  25 +public class FastDeliveryChangeHandler implements IEventHandler<FastDeliveryChangeEvent> {
  26 +
  27 + private final Logger logger = LoggerFactory.getLogger(getClass());
  28 +
  29 + @Autowired
  30 + FastDeliveryProxyService fastDeliveryProxyService;
  31 +
  32 + Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder()
  33 + .retryIfRuntimeException()
  34 + .withStopStrategy(StopStrategies.stopAfterAttempt(2))
  35 + .withWaitStrategy(WaitStrategies.fixedWait(50, TimeUnit.MILLISECONDS)).build();
  36 +
  37 + private ExecutorService executors = Executors.newFixedThreadPool(1);
  38 +
  39 + @Autowired
  40 + private IExpressInfoService expressInfoService;
  41 +
  42 + @Override
  43 + public void handle(FastDeliveryChangeEvent event) {
  44 +
  45 + if (event.getOpt() == null || event.getFastDeliveryReq()== null){
  46 + logger.warn("req required, please check param");
  47 + return;
  48 + }
  49 +
  50 + if (!fastDeliveryProxyService.isFastDeliveryGoods(event.getFastDeliveryReq().getSkup())){
  51 + logger.info("skup is not fastDelivery, no need handle");
  52 + return;
  53 + }
  54 +
  55 + switch (event.getOpt()){
  56 + case Delivery:
  57 +
  58 + FastDeliveryReq fastDeliveryReq = event.getFastDeliveryReq();
  59 +
  60 + // 生成虚拟单号
  61 + String wayBillCode = fastDeliveryProxyService.getVRWaybillCode(fastDeliveryReq.getOrderCode());
  62 + fastDeliveryReq.setWaybillCode(wayBillCode);
  63 +
  64 + try{
  65 + retryer.call(()->{
  66 + fastDeliveryProxyService.delivery(fastDeliveryReq);
  67 + return null;
  68 + });
  69 + }catch (Exception e){
  70 + // 多次重试失败后,报警
  71 +
  72 + CommonAlarmEventPublisher.publish("极速订单通知第三方发货", "fast.delivery.delivery", "sku:["+fastDeliveryReq.getSkup()+"]");
  73 +
  74 + logger.warn("multi fast delivery delivery fail, alarm, e {}", e);
  75 + }
  76 +
  77 + // 卖家自动发货
  78 + executors.submit(()->{
  79 + SellerDeliverToDepotReq sdtdReq = SellerDeliverToDepotReq.builder()
  80 + .sellerUid(fastDeliveryReq.getSellerUid())
  81 + .orderCode(fastDeliveryReq.getOrderCode())
  82 + .wayBillCode(wayBillCode)
  83 + .depotNum(DepotType.NJ.getCode())
  84 + .expressCompanyId(0)
  85 + .build();
  86 + expressInfoService.deliverToDepot(sdtdReq);
  87 + });
  88 +
  89 +
  90 + case Cancel:
  91 +
  92 + try{
  93 + retryer.call(()->{
  94 + fastDeliveryProxyService.cancel(event.getFastDeliveryReq());
  95 + return null;
  96 + });
  97 + }catch (Exception e){
  98 + // 多次重试失败后,报警
  99 +
  100 + CommonAlarmEventPublisher.publish("极速订单释放库存", "fast.delivery.cancel", "sku:["+event.getFastDeliveryReq().getSkup()+"]");
  101 +
  102 + logger.warn("multi fast delivery cancel fail, alarm, e {}", e);
  103 + }
  104 +
  105 +
  106 +
  107 + default:
  108 + logger.warn("no exist opt type, please check param");
  109 + }
  110 +
  111 + }
  112 +}
@@ -393,10 +393,10 @@ public class BuyerOrderCancelService { @@ -393,10 +393,10 @@ public class BuyerOrderCancelService {
393 inBoxFacade.buyerCancelBeforeDepotReceive(buyerOrder, penaltyAmount.toPlainString(), psog, useDepositGoods); 393 inBoxFacade.buyerCancelBeforeDepotReceive(buyerOrder, penaltyAmount.toPlainString(), psog, useDepositGoods);
394 } 394 }
395 395
396 - if (fastDeliveryProxyService.isFastDeliveryGoods(skup)) {  
397 - FastDeliveryReq req = FastDeliveryReq.builder().sellerUid(psog.getUid()).skup(skup).needReShelves(true).build();  
398 - fastDeliveryProxyService.cancel(req);  
399 - } 396 + FastDeliveryChangeEvent fastDeliverEvent = new FastDeliveryChangeEvent(
  397 + FastDeliveryReq.builder().sellerUid(psog.getUid()).skup(skup).needReShelves(true).build(),
  398 + FastDeliveryChangeEvent.Opt.Cancel);
  399 + EventBusPublisher.publishEvent(fastDeliverEvent);
400 400
401 //整个过程异步去执行(考虑退费依赖订单状态) 401 //整个过程异步去执行(考虑退费依赖订单状态)
402 //(退费)退保证金给卖家 402 //(退费)退保证金给卖家
@@ -416,8 +416,6 @@ public class BuyerOrderCancelService { @@ -416,8 +416,6 @@ public class BuyerOrderCancelService {
416 EventBusPublisher.publishEvent(new UfoInfluxdbEvent(new UfoInfluxdbVo.Builder().setMeasurement(InfluxdbMeasurementEnum.MEASUREMENT_ORDER_CANCEL) 416 EventBusPublisher.publishEvent(new UfoInfluxdbEvent(new UfoInfluxdbVo.Builder().setMeasurement(InfluxdbMeasurementEnum.MEASUREMENT_ORDER_CANCEL)
417 .addInitField(InfluxdbFieldEnum.FIELD_COUNT).build())); // 统计取消订单的次数 417 .addInitField(InfluxdbFieldEnum.FIELD_COUNT).build())); // 统计取消订单的次数
418 418
419 - //  
420 -  
421 if (useDepositGoods) { 419 if (useDepositGoods) {
422 String depositCode = bdrEvent.getDepositCode(); 420 String depositCode = bdrEvent.getDepositCode();
423 logger.info("cancel buyer order Before Depot Receive trigger DepositGoodsCompensateAsyncEvent orderCode {} depositCode {}", orderCode, depositCode); 421 logger.info("cancel buyer order Before Depot Receive trigger DepositGoodsCompensateAsyncEvent orderCode {} depositCode {}", orderCode, depositCode);
@@ -452,15 +452,14 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService { @@ -452,15 +452,14 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
452 boolean isReturnSuccess = productProxyService.returnStorage(skup); 452 boolean isReturnSuccess = productProxyService.returnStorage(skup);
453 453
454 // 急速发货的场合,取消订单 454 // 急速发货的场合,取消订单
455 - if (fastDeliveryProxyService.isFastDeliveryGoods(skup)){  
456 - fastDeliveryProxyService.cancel(FastDeliveryReq.builder().sellerUid(psog.getUid()).skup(skup).needReShelves(false).build());  
457 - } 455 + FastDeliveryChangeEvent fastDeliverEvent = new FastDeliveryChangeEvent(
  456 + FastDeliveryReq.builder().sellerUid(psog.getUid()).skup(skup).needReShelves(false).build(),
  457 + FastDeliveryChangeEvent.Opt.Cancel);
  458 + EventBusPublisher.publishEvent(fastDeliverEvent);
458 459
459 //如有用券则退券 460 //如有用券则退券
460 buyerOrderCancelService.asyncRefundCoupon(buyerUid, orderCode, BuyerRefundCouponEvent.BizCase.PAY_TIME_OUT); 461 buyerOrderCancelService.asyncRefundCoupon(buyerUid, orderCode, BuyerRefundCouponEvent.BizCase.PAY_TIME_OUT);
461 462
462 - //  
463 -  
464 inBoxFacade.noticSellerWhenBuyerCancel(sellerUid, psog); 463 inBoxFacade.noticSellerWhenBuyerCancel(sellerUid, psog);
465 buyerNoticeFacade.buyerCancelOrder(orderRequest.getUid(), orderCode); 464 buyerNoticeFacade.buyerCancelOrder(orderRequest.getUid(), orderCode);
466 465
  1 +package com.yohoufo.order.service.impl;
  2 +
  3 +import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
  4 +import com.yohoufo.common.alarm.EventBusPublisher;
  5 +import com.yohoufo.order.event.FastDeliveryChangeEvent;
  6 +import com.yohoufo.order.model.dto.OrderBuilder;
  7 +import org.springframework.stereotype.Component;
  8 +
  9 +@Component
  10 +public class FastDeliverySubmitServiceImpl extends SubmitOrderServiceImpl{
  11 +
  12 + @Override
  13 + protected void subtractStorage(OrderBuilder orderBuilder) {
  14 + // 先减我方库存
  15 + super.subtractStorage(orderBuilder);
  16 +
  17 + // 再减第三方库存,失败也无需恢复我方库存
  18 + FastDeliveryReq fastDeliveryReq = FastDeliveryReq.builder()
  19 + .price(orderBuilder.getSellerOrderGoods().getGoodsPrice())
  20 + .productCode(orderBuilder.getProductCode())
  21 + .sizeName(orderBuilder.getSellerOrderGoods().getSizeName())
  22 + .sellerUid(orderBuilder.getSellerOrderGoods().getUid())
  23 + .skup(orderBuilder.getSkup()).build();
  24 + fastDeliveryProxyService.lockSkup(fastDeliveryReq);
  25 + }
  26 +
  27 + @Override
  28 + protected void returnStrorageEx(OrderBuilder orderBuilder) {
  29 + super.returnStrorageEx(orderBuilder);
  30 +
  31 + FastDeliveryChangeEvent fastDeliverEvent = new FastDeliveryChangeEvent(
  32 + FastDeliveryReq.builder().sellerUid(orderBuilder.getUid()).skup(orderBuilder.getSkup()).needReShelves(false).build(),
  33 + FastDeliveryChangeEvent.Opt.Cancel);
  34 + EventBusPublisher.publishEvent(fastDeliverEvent);
  35 + }
  36 +}
  1 +package com.yohoufo.order.service.impl;
  2 +
  3 +import com.yohobuy.ufo.model.order.common.OrderAttributes;
  4 +import com.yohoufo.order.service.ISubmitOrderService;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +@Component
  9 +public class OrderSubmitServiceFactory {
  10 +
  11 + @Autowired
  12 + ISubmitOrderService ordeCreationService;
  13 +
  14 + @Autowired
  15 + FastDeliverySubmitServiceImpl fastDeliverySubmitService;
  16 +
  17 + public ISubmitOrderService getOrderSubmitService(Integer originalAttributes){
  18 +
  19 + if (originalAttributes == OrderAttributes.FAST_DELIVERY.getCode()){
  20 + return fastDeliverySubmitService;
  21 + }else{
  22 + return ordeCreationService;
  23 + }
  24 + }
  25 +
  26 +}
@@ -82,7 +82,7 @@ public class ShoppingServiceImpl implements IShoppingService { @@ -82,7 +82,7 @@ public class ShoppingServiceImpl implements IShoppingService {
82 OrderCodeGenerator orderCodeGenerator; 82 OrderCodeGenerator orderCodeGenerator;
83 83
84 @Autowired 84 @Autowired
85 - ISubmitOrderService ordeCreationService; 85 + OrderSubmitServiceFactory orderSubmitServiceFactory;
86 86
87 @Autowired 87 @Autowired
88 ServiceCaller serviceCaller; 88 ServiceCaller serviceCaller;
@@ -477,8 +477,9 @@ public class ShoppingServiceImpl implements IShoppingService { @@ -477,8 +477,9 @@ public class ShoppingServiceImpl implements IShoppingService {
477 DeliveryWayUtils::findDeliveryWayCodeBySkupType, 477 DeliveryWayUtils::findDeliveryWayCodeBySkupType,
478 psog, chargeContext, 478 psog, chargeContext,
479 shoppingRequest.getBusinessClient()); 479 shoppingRequest.getBusinessClient());
  480 + ISubmitOrderService submitOrderService = orderSubmitServiceFactory.getOrderSubmitService(orderBuilder.getOriginalAttributes());
480 481
481 - submitResult = ordeCreationService.doSubmitOrder(orderBuilder); 482 + submitResult = submitOrderService.doSubmitOrder(orderBuilder);
482 }finally { 483 }finally {
483 if(locked){ 484 if(locked){
484 distributedLock.unlock(); 485 distributedLock.unlock();
@@ -12,6 +12,7 @@ import com.yohobuy.ufo.model.order.common.OrderStatus; @@ -12,6 +12,7 @@ import com.yohobuy.ufo.model.order.common.OrderStatus;
12 import com.yohobuy.ufo.model.order.resp.FastDeliveryReq; 12 import com.yohobuy.ufo.model.order.resp.FastDeliveryReq;
13 import com.yohobuy.ufo.model.promotion.response.promotionActivity.PromotionActivityRspBo; 13 import com.yohobuy.ufo.model.promotion.response.promotionActivity.PromotionActivityRspBo;
14 import com.yohoufo.common.alarm.CommonAlarmEventPublisher; 14 import com.yohoufo.common.alarm.CommonAlarmEventPublisher;
  15 +import com.yohoufo.common.alarm.EventBusPublisher;
15 import com.yohoufo.common.exception.UfoServiceException; 16 import com.yohoufo.common.exception.UfoServiceException;
16 import com.yohoufo.dal.order.*; 17 import com.yohoufo.dal.order.*;
17 import com.yohoufo.dal.order.model.*; 18 import com.yohoufo.dal.order.model.*;
@@ -84,21 +85,9 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { @@ -84,21 +85,9 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService {
84 BuyerOrderSubmitResult result = null; 85 BuyerOrderSubmitResult result = null;
85 try { 86 try {
86 87
87 - // 极速发货通知第三方  
88 - if (orderBuilder.getOriginalAttributes() == OrderAttributes.FAST_DELIVERY.getCode()){  
89 -  
90 - FastDeliveryReq fastDeliveryReq = FastDeliveryReq.builder()  
91 - .price(orderBuilder.getSellerOrderGoods().getGoodsPrice())  
92 - .productCode(orderBuilder.getProductCode())  
93 - .sizeName(orderBuilder.getSellerOrderGoods().getSizeName())  
94 - .sellerUid(orderBuilder.getSellerOrderGoods().getUid())  
95 - .skup(orderBuilder.getSkup()).build();  
96 - fastDeliveryProxyService.lockSkup(fastDeliveryReq);  
97 - }  
98 tryLockDepositGoodsIf(orderBuilder); 88 tryLockDepositGoodsIf(orderBuilder);
99 89
100 - // 减库存  
101 - productProxyService.subtractStorage(orderBuilder.getProductId(), orderBuilder.getSkup()); 90 + subtractStorage(orderBuilder);
102 91
103 try{ 92 try{
104 93
@@ -139,14 +128,19 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService { @@ -139,14 +128,19 @@ public class SubmitOrderServiceImpl implements ISubmitOrderService {
139 } 128 }
140 } 129 }
141 130
142 - private void returnStrorageEx(OrderBuilder orderBuilder) { 131 +
  132 + protected void subtractStorage(OrderBuilder orderBuilder) {
  133 + // 减库存
  134 + productProxyService.subtractStorage(orderBuilder.getProductId(), orderBuilder.getSkup());
  135 +
  136 + }
  137 +
  138 +
  139 + protected void returnStrorageEx(OrderBuilder orderBuilder) {
143 try{ 140 try{
  141 + // 还库存
144 productProxyService.returnStorage(orderBuilder.getSkup()); 142 productProxyService.returnStorage(orderBuilder.getSkup());
145 143
146 - if (fastDeliveryProxyService.isFastDeliveryGoods(orderBuilder.getSkup())){  
147 - fastDeliveryProxyService.cancel(FastDeliveryReq.builder().sellerUid(orderBuilder.getUid()).skup(orderBuilder.getSkup()).needReShelves(false).build());  
148 - }  
149 -  
150 }catch (Exception ex0){ 144 }catch (Exception ex0){
151 145
152 CommonAlarmEventPublisher.publish("还库存失败", "ufo.product.cancelSaleSkup", "sku:["+orderBuilder.getSkup()+"]"); 146 CommonAlarmEventPublisher.publish("还库存失败", "ufo.product.cancelSaleSkup", "sku:["+orderBuilder.getSkup()+"]");
@@ -7,6 +7,7 @@ import com.yohoufo.common.ApiResponse; @@ -7,6 +7,7 @@ import com.yohoufo.common.ApiResponse;
7 import com.yohoufo.common.exception.UfoServiceException; 7 import com.yohoufo.common.exception.UfoServiceException;
8 import com.yohoufo.order.utils.LoggerUtils; 8 import com.yohoufo.order.utils.LoggerUtils;
9 import org.slf4j.Logger; 9 import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.stereotype.Component; 13 import org.springframework.stereotype.Component;
@@ -21,7 +22,7 @@ import java.util.concurrent.TimeUnit; @@ -21,7 +22,7 @@ import java.util.concurrent.TimeUnit;
21 @Component 22 @Component
22 public class BaseServiceCaller { 23 public class BaseServiceCaller {
23 24
24 - private final static Logger logger = LoggerUtils.getBuyerOrderLogger(); 25 + private final Logger logger = LoggerFactory.getLogger(getClass());
25 26
26 @Autowired 27 @Autowired
27 private ServiceCaller serviceCaller; 28 private ServiceCaller serviceCaller;