Authored by wujiexiang

Merge branch 'dev_bargain' into test6.9.5

@@ -37,6 +37,10 @@ public class BuyerOrderChangeEvent extends OrderChangeEvent { @@ -37,6 +37,10 @@ public class BuyerOrderChangeEvent extends OrderChangeEvent {
37 return buyerOrder; 37 return buyerOrder;
38 } 38 }
39 39
  40 + public OrderStatus getTargetStatus() {
  41 + return targetStatus;
  42 + }
  43 +
40 @Override 44 @Override
41 public String toShortString() { 45 public String toShortString() {
42 return new StringBuilder(128).append("BuyerOrderChangeEvent[") 46 return new StringBuilder(128).append("BuyerOrderChangeEvent[")
@@ -48,6 +52,9 @@ public class BuyerOrderChangeEvent extends OrderChangeEvent { @@ -48,6 +52,9 @@ public class BuyerOrderChangeEvent extends OrderChangeEvent {
48 } 52 }
49 53
50 public enum BizCase { 54 public enum BizCase {
51 - CANCEL_BYBUYER, CANCEL_BYCS, CANCEL_BYSYSAUTO, CANCEL_BYSELLER, PAY_SUCCESS; 55 + //cancel
  56 + CANCEL_BYBUYER, CANCEL_BYCS, CANCEL_BYSYSAUTO, CANCEL_BYSELLER,
  57 + //paySuccess
  58 + PAY_SUCCESS;
52 } 59 }
53 } 60 }
@@ -23,7 +23,7 @@ public abstract class OrderChangeEvent extends Event { @@ -23,7 +23,7 @@ public abstract class OrderChangeEvent extends Event {
23 * 23 *
24 * @return 24 * @return
25 */ 25 */
26 - abstract OrderCodeType getOrderCodeType(); 26 + public abstract OrderCodeType getOrderCodeType();
27 27
28 28
29 @Override 29 @Override
1 package com.yohoufo.order.service.listener; 1 package com.yohoufo.order.service.listener;
2 2
3 -import com.yohoufo.dal.order.BuyerOrderMetaMapper;  
4 -import com.yohoufo.dal.order.model.BuyerOrder;  
5 -import com.yohoufo.order.constants.ActivityTypeEnum;  
6 -import com.yohoufo.order.constants.MetaKey;  
7 import com.yohoufo.order.event.BuyerOrderChangeEvent; 3 import com.yohoufo.order.event.BuyerOrderChangeEvent;
8 -import com.yohoufo.order.event.OrderChangeEvent;  
9 -import com.yohoufo.order.model.bo.ActivityBo;  
10 -import com.yohoufo.order.service.proxy.BargainProxyService;  
11 -import com.yohoufo.order.service.support.BuyerOrderMetaMapperSupport;  
12 -import com.yohoufo.order.utils.LoggerUtils;  
13 -import org.slf4j.Logger; 4 +import com.yohoufo.order.service.listener.processor.BuyerOrderChangeBusinessPostProcessor;
14 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component; 6 import org.springframework.stereotype.Component;
16 7
17 -import java.util.Objects;  
18 -  
19 /** 8 /**
20 * Created by jiexiang.wu on 2019/5/23. 9 * Created by jiexiang.wu on 2019/5/23.
21 */ 10 */
22 @Component 11 @Component
23 public class BuyOrderChangeListener implements IOrderChangeListener<BuyerOrderChangeEvent> { 12 public class BuyOrderChangeListener implements IOrderChangeListener<BuyerOrderChangeEvent> {
24 - final private Logger logger = LoggerUtils.getBuyerOrderLogger();  
25 -  
26 - @Autowired  
27 - private BuyerOrderMetaMapperSupport buyerOrderMetaMapperSupport;  
28 13
29 @Autowired 14 @Autowired
30 - private BargainProxyService bargainProxyService;  
31 -  
32 - @Override  
33 - public boolean shouldHandle(OrderChangeEvent event) {  
34 - return event instanceof BuyerOrderChangeEvent;  
35 - } 15 + private BuyerOrderChangeBusinessPostProcessor businessPostProcessor;
36 16
37 @Override 17 @Override
38 - public void handleEvent(BuyerOrderChangeEvent buyerOrderChangeEvent) {  
39 - BuyerOrder buyerOrder = buyerOrderChangeEvent.getBuyerOrder();  
40 - if (Objects.equals(buyerOrder.getActivityType(), ActivityTypeEnum.BARGAIN.getCode())) {  
41 - //砍价订单  
42 - handleBargainOrderEvent(buyerOrderChangeEvent.getBizCase(), buyerOrder);  
43 - }  
44 - }  
45 -  
46 - /**  
47 - * 砍价订单  
48 - *  
49 - * @param bizCase  
50 - * @param buyerOrder  
51 - */  
52 - private void handleBargainOrderEvent(BuyerOrderChangeEvent.BizCase bizCase, BuyerOrder buyerOrder) {  
53 - ActivityBo activityBo = buyerOrderMetaMapperSupport.selectByMetaKey(buyerOrder.getUid(), buyerOrder.getOrderCode(), MetaKey.ACTIVITY_KEY, ActivityBo.class);  
54 -  
55 - if (activityBo == null || activityBo.getUserActivityId() < 0) {  
56 - logger.warn("[{}] has no userActivityId for orderCode: {}", buyerOrder.getOrderCode());  
57 - return;  
58 - } 18 + public void apply(BuyerOrderChangeEvent changeEvent) {
  19 + BuyerOrderChangeEvent.BizCase bizCase = changeEvent.getBizCase();
59 switch (bizCase) { 20 switch (bizCase) {
60 case CANCEL_BYSYSAUTO: 21 case CANCEL_BYSYSAUTO:
61 case CANCEL_BYBUYER: 22 case CANCEL_BYBUYER:
62 case CANCEL_BYCS: 23 case CANCEL_BYCS:
63 case CANCEL_BYSELLER: 24 case CANCEL_BYSELLER:
64 - bargainProxyService.cancelCutPriceUseRecord(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId()); 25 + businessPostProcessor.cancel(changeEvent.getBuyerOrder(), changeEvent.getBizCase(), changeEvent.getTargetStatus());
65 break; 26 break;
66 case PAY_SUCCESS: 27 case PAY_SUCCESS:
67 - bargainProxyService.payCutPrice(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId()); 28 + businessPostProcessor.paySuccess(changeEvent.getBuyerOrder(), changeEvent.getBizCase(), changeEvent.getTargetStatus());
68 break; 29 break;
69 default: 30 default:
70 throw new IllegalStateException("unknown bizCase" + bizCase); 31 throw new IllegalStateException("unknown bizCase" + bizCase);
@@ -7,7 +7,5 @@ import com.yohoufo.order.event.OrderChangeEvent; @@ -7,7 +7,5 @@ import com.yohoufo.order.event.OrderChangeEvent;
7 */ 7 */
8 public interface IOrderChangeListener<T extends OrderChangeEvent> { 8 public interface IOrderChangeListener<T extends OrderChangeEvent> {
9 9
10 - boolean shouldHandle(OrderChangeEvent event);  
11 -  
12 - void handleEvent(T t); 10 + void apply(T event);
13 } 11 }
1 package com.yohoufo.order.service.listener; 1 package com.yohoufo.order.service.listener;
2 2
3 import com.google.common.eventbus.Subscribe; 3 import com.google.common.eventbus.Subscribe;
  4 +import com.yohobuy.ufo.model.order.common.OrderCodeType;
4 import com.yohoufo.common.alarm.EventBusPublisher; 5 import com.yohoufo.common.alarm.EventBusPublisher;
5 import com.yohoufo.common.alarm.IEventHandler; 6 import com.yohoufo.common.alarm.IEventHandler;
6 import com.yohoufo.common.alarm.SmsAlarmEvent; 7 import com.yohoufo.common.alarm.SmsAlarmEvent;
  8 +import com.yohoufo.order.event.BuyerOrderChangeEvent;
7 import com.yohoufo.order.event.OrderChangeEvent; 9 import com.yohoufo.order.event.OrderChangeEvent;
8 import com.yohoufo.order.utils.LoggerUtils; 10 import com.yohoufo.order.utils.LoggerUtils;
9 import org.slf4j.Logger; 11 import org.slf4j.Logger;
@@ -22,7 +24,8 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv @@ -22,7 +24,8 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv
22 24
23 final private Logger logger = LoggerUtils.getBuyerOrderLogger(); 25 final private Logger logger = LoggerUtils.getBuyerOrderLogger();
24 26
25 - private List<IOrderChangeListener> listeners = new ArrayList<>(); 27 + //购买者监听器
  28 + private List<IOrderChangeListener> buyerListeners = new ArrayList<>();
26 29
27 @Autowired 30 @Autowired
28 private EventBusPublisher eventBusPublisher; 31 private EventBusPublisher eventBusPublisher;
@@ -33,7 +36,7 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv @@ -33,7 +36,7 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv
33 36
34 @PostConstruct 37 @PostConstruct
35 public void initListener() { 38 public void initListener() {
36 - listeners.add(buyOrderChangeListener); 39 + buyerListeners.add(buyOrderChangeListener);
37 } 40 }
38 41
39 //触发异步处理 42 //触发异步处理
@@ -44,21 +47,27 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv @@ -44,21 +47,27 @@ public class OrderChangeListenerContainer implements IEventHandler<OrderChangeEv
44 @Override 47 @Override
45 @Subscribe 48 @Subscribe
46 public void handle(OrderChangeEvent event) { 49 public void handle(OrderChangeEvent event) {
47 - logger.info("handle order change event:{}", event); 50 + if (event.getOrderCodeType() == OrderCodeType.BUYER_TYPE) {
  51 + buyerOrderChangeHandle((BuyerOrderChangeEvent) event, buyerListeners);
  52 + } else {
  53 + logger.warn("there is not any listener to handle event:{}", event);
  54 + }
  55 + }
  56 +
  57 + private void buyerOrderChangeHandle(BuyerOrderChangeEvent event, List<IOrderChangeListener> listeners) {
  58 + logger.info("listeners apply buyerOrderChangeEvent:{}", event);
48 for (IOrderChangeListener listener : listeners) { 59 for (IOrderChangeListener listener : listeners) {
49 try { 60 try {
50 - if (listener.shouldHandle(event)) {  
51 - listener.handleEvent(event);  
52 - } 61 + listener.apply(event);
53 } catch (Exception ex) { 62 } catch (Exception ex) {
54 - logger.warn("handler:{} handle event:{} error", listener.getClass().getName(), event, ex); 63 + logger.warn("{} apply buyerOrderChangeEvent:{} error", listener.getClass().getName(), event, ex);
55 //告警 64 //告警
56 EventBusPublisher.publishEvent( 65 EventBusPublisher.publishEvent(
57 - new SmsAlarmEvent("ufo.orderChangeEvent", "handle_exception", 66 + new SmsAlarmEvent("ufo.buyerOrderChangeEvent", "handle_exception",
58 listener.getClass().getName() + "(" + event.toShortString() + ")", ex) 67 listener.getClass().getName() + "(" + event.toShortString() + ")", ex)
59 ); 68 );
60 } 69 }
61 } 70 }
62 - logger.info("handle order change event end"); 71 + logger.info("listeners apply buyerOrderChangeEvent end");
63 } 72 }
64 } 73 }
  1 +package com.yohoufo.order.service.listener.processor;
  2 +
  3 +import com.google.common.collect.Lists;
  4 +import com.yohobuy.ufo.model.order.common.OrderStatus;
  5 +import com.yohoufo.common.alarm.EventBusPublisher;
  6 +import com.yohoufo.common.alarm.SmsAlarmEvent;
  7 +import com.yohoufo.dal.order.model.BuyerOrder;
  8 +import com.yohoufo.order.constants.ActivityTypeEnum;
  9 +import com.yohoufo.order.constants.MetaKey;
  10 +import com.yohoufo.order.event.BuyerOrderChangeEvent;
  11 +import com.yohoufo.order.model.bo.ActivityBo;
  12 +import com.yohoufo.order.service.proxy.BargainProxyService;
  13 +import com.yohoufo.order.service.support.BuyerOrderMetaMapperSupport;
  14 +import com.yohoufo.order.utils.LoggerUtils;
  15 +import org.slf4j.Logger;
  16 +import org.springframework.beans.factory.annotation.Autowired;
  17 +import org.springframework.stereotype.Component;
  18 +
  19 +import java.util.List;
  20 +import java.util.Objects;
  21 +
  22 +/**
  23 + * Created by jiexiang.wu on 2019/5/24.
  24 + * 买家订单状态发生变化后,可以异步处理的业务逻辑,如
  25 + * 1.砍价订单需要通知活动模块
  26 + * 等等
  27 + */
  28 +@Component
  29 +public class BuyerOrderChangeBusinessPostProcessor {
  30 +
  31 + final private Logger logger = LoggerUtils.getBuyerOrderLogger();
  32 +
  33 + @Autowired
  34 + private BuyerOrderMetaMapperSupport buyerOrderMetaMapperSupport;
  35 +
  36 + @Autowired
  37 + private BargainProxyService bargainProxyService;
  38 +
  39 + //业务处理器
  40 + private List<BusinessProcessor> processors = Lists.newArrayList(
  41 + //砍价
  42 + new BargainBusinessProcessor()
  43 + );
  44 +
  45 +
  46 + /**
  47 + * 取消业务
  48 + *
  49 + * @param buyerOrder
  50 + * @param bizCase
  51 + * @param targetStatus
  52 + */
  53 + public void cancel(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
  54 + BusinessProcessorContext context = buildBusinessProcessorContext(buyerOrder, bizCase, targetStatus);
  55 + for (BusinessProcessor processor : processors) {
  56 + try {
  57 + processor.cancel(context);
  58 + } catch (Exception ex) {
  59 + logger.warn("{} cancel error,context:{}", processor.getClass().getName(), context, ex);
  60 + //告警
  61 + EventBusPublisher.publishEvent(
  62 + new SmsAlarmEvent("ufo.buyerOrderBusinessPostProcessor", "cancel_exception",
  63 + processor.getClass().getName() + "(" + buyerOrder.getOrderCode() + ")", ex)
  64 + );
  65 + }
  66 + }
  67 + }
  68 +
  69 +
  70 + /**
  71 + * 支付成功
  72 + *
  73 + * @param buyerOrder
  74 + * @param bizCase
  75 + * @param targetStatus
  76 + */
  77 + public void paySuccess(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
  78 + BusinessProcessorContext context = buildBusinessProcessorContext(buyerOrder, bizCase, targetStatus);
  79 + for (BusinessProcessor processor : processors) {
  80 + try {
  81 + processor.paySuccess(context);
  82 + } catch (Exception ex) {
  83 + logger.warn("{} paySuccess error,context:{}", processor.getClass().getName(), context, ex);
  84 + //告警
  85 + EventBusPublisher.publishEvent(
  86 + new SmsAlarmEvent("ufo.buyerOrderBusinessPostProcessor", "paySuccess_exception",
  87 + processor.getClass().getName() + "(" + buyerOrder.getOrderCode() + ")", ex)
  88 + );
  89 + }
  90 + }
  91 + }
  92 +
  93 +
  94 + public BusinessProcessorContext buildBusinessProcessorContext(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
  95 + BusinessProcessorContext context = new BusinessProcessorContext();
  96 + context.buyerOrder = buyerOrder;
  97 + context.bizCase = bizCase;
  98 + context.targetStatus = targetStatus;
  99 + //sql超时,会在listenerContainer层告警
  100 + context.activityBo = buyerOrderMetaMapperSupport.selectByMetaKey(buyerOrder.getUid(), buyerOrder.getOrderCode(), MetaKey.ACTIVITY_KEY, ActivityBo.class);
  101 + return context;
  102 + }
  103 +
  104 +
  105 + public static class BusinessProcessorContext {
  106 + public BuyerOrder buyerOrder;
  107 + public BuyerOrderChangeEvent.BizCase bizCase;
  108 + //事件发生后的状态,可能为null
  109 + public OrderStatus targetStatus;
  110 + //活动信息
  111 + public ActivityBo activityBo;
  112 +
  113 + @Override
  114 + public String toString() {
  115 + return "BusinessProcessorContext{" +
  116 + "orderCode=" + buyerOrder.getOrderCode() +
  117 + ", bizCase=" + bizCase +
  118 + ", targetStatus=" + targetStatus +
  119 + '}';
  120 + }
  121 + }
  122 +
  123 + /**
  124 + * 业务处理
  125 + * 按照业务归类如下:
  126 + * 取消
  127 + * 支付成功
  128 + * 等等
  129 + */
  130 + private interface BusinessProcessor {
  131 + //取消
  132 + void cancel(BusinessProcessorContext context);
  133 +
  134 + //支付成功
  135 + void paySuccess(BusinessProcessorContext context);
  136 + }
  137 +
  138 + //砍价业务
  139 + private class BargainBusinessProcessor implements BusinessProcessor {
  140 +
  141 + @Override
  142 + public void cancel(BusinessProcessorContext context) {
  143 + BuyerOrder buyerOrder = context.buyerOrder;
  144 + ActivityBo activityBo = context.activityBo;
  145 + if (shouldProcess(buyerOrder, activityBo)) {
  146 + bargainProxyService.cancelCutPriceUseRecord(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId());
  147 + }
  148 + }
  149 +
  150 + @Override
  151 + public void paySuccess(BusinessProcessorContext context) {
  152 + BuyerOrder buyerOrder = context.buyerOrder;
  153 + ActivityBo activityBo = context.activityBo;
  154 + if (shouldProcess(buyerOrder, activityBo)) {
  155 + bargainProxyService.payCutPrice(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId());
  156 + }
  157 + }
  158 +
  159 + public boolean shouldProcess(BuyerOrder buyerOrder, ActivityBo activityBo) {
  160 + if (!Objects.equals(buyerOrder.getActivityType(), ActivityTypeEnum.BARGAIN.getCode())) {
  161 + return false;
  162 + }
  163 + if (activityBo == null || activityBo.getUserActivityId() < 0) {
  164 + logger.warn("[{}] has no userActivityId", buyerOrder.getOrderCode());
  165 + return false;
  166 + }
  167 + return true;
  168 + }
  169 + }
  170 +
  171 +}