|
|
package com.yohoufo.order.service.listener.processor;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yohobuy.ufo.model.order.common.OrderStatus;
|
|
|
import com.yohoufo.common.alarm.EventBusPublisher;
|
|
|
import com.yohoufo.common.alarm.SmsAlarmEvent;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.order.constants.ActivityTypeEnum;
|
|
|
import com.yohoufo.order.constants.MetaKey;
|
|
|
import com.yohoufo.order.event.BuyerOrderChangeEvent;
|
|
|
import com.yohoufo.order.model.bo.ActivityBo;
|
|
|
import com.yohoufo.order.service.proxy.BargainProxyService;
|
|
|
import com.yohoufo.order.service.support.BuyerOrderMetaMapperSupport;
|
|
|
import com.yohoufo.order.utils.LoggerUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Created by jiexiang.wu on 2019/5/24.
|
|
|
* 买家订单状态发生变化后,可以异步处理的业务逻辑,如
|
|
|
* 1.砍价订单需要通知活动模块
|
|
|
* 等等
|
|
|
*/
|
|
|
@Component
|
|
|
public class BuyerOrderChangeBusinessPostProcessor {
|
|
|
|
|
|
final private Logger logger = LoggerUtils.getBuyerOrderLogger();
|
|
|
|
|
|
@Autowired
|
|
|
private BuyerOrderMetaMapperSupport buyerOrderMetaMapperSupport;
|
|
|
|
|
|
@Autowired
|
|
|
private BargainProxyService bargainProxyService;
|
|
|
|
|
|
//业务处理器
|
|
|
private List<BusinessProcessor> processors = Lists.newArrayList(
|
|
|
//砍价
|
|
|
new BargainBusinessProcessor()
|
|
|
);
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 取消业务
|
|
|
*
|
|
|
* @param buyerOrder
|
|
|
* @param bizCase
|
|
|
* @param targetStatus
|
|
|
*/
|
|
|
public void cancel(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
|
|
|
BusinessProcessorContext context = buildBusinessProcessorContext(buyerOrder, bizCase, targetStatus);
|
|
|
for (BusinessProcessor processor : processors) {
|
|
|
try {
|
|
|
processor.cancel(context);
|
|
|
} catch (Exception ex) {
|
|
|
logger.warn("{} cancel error,context:{}", processor.getClass().getName(), context, ex);
|
|
|
//告警
|
|
|
EventBusPublisher.publishEvent(
|
|
|
new SmsAlarmEvent("ufo.buyerOrderBusinessPostProcessor", "cancel_exception",
|
|
|
processor.getClass().getName() + "(" + buyerOrder.getOrderCode() + ")", ex)
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 支付成功
|
|
|
*
|
|
|
* @param buyerOrder
|
|
|
* @param bizCase
|
|
|
* @param targetStatus
|
|
|
*/
|
|
|
public void paySuccess(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
|
|
|
BusinessProcessorContext context = buildBusinessProcessorContext(buyerOrder, bizCase, targetStatus);
|
|
|
for (BusinessProcessor processor : processors) {
|
|
|
try {
|
|
|
processor.paySuccess(context);
|
|
|
} catch (Exception ex) {
|
|
|
logger.warn("{} paySuccess error,context:{}", processor.getClass().getName(), context, ex);
|
|
|
//告警
|
|
|
EventBusPublisher.publishEvent(
|
|
|
new SmsAlarmEvent("ufo.buyerOrderBusinessPostProcessor", "paySuccess_exception",
|
|
|
processor.getClass().getName() + "(" + buyerOrder.getOrderCode() + ")", ex)
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public BusinessProcessorContext buildBusinessProcessorContext(BuyerOrder buyerOrder, BuyerOrderChangeEvent.BizCase bizCase, OrderStatus targetStatus) {
|
|
|
BusinessProcessorContext context = new BusinessProcessorContext();
|
|
|
context.buyerOrder = buyerOrder;
|
|
|
context.bizCase = bizCase;
|
|
|
context.targetStatus = targetStatus;
|
|
|
//sql超时,会在listenerContainer层告警
|
|
|
context.activityBo = buyerOrderMetaMapperSupport.selectByMetaKey(buyerOrder.getUid(), buyerOrder.getOrderCode(), MetaKey.ACTIVITY_KEY, ActivityBo.class);
|
|
|
return context;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static class BusinessProcessorContext {
|
|
|
public BuyerOrder buyerOrder;
|
|
|
public BuyerOrderChangeEvent.BizCase bizCase;
|
|
|
//事件发生后的状态,可能为null
|
|
|
public OrderStatus targetStatus;
|
|
|
//活动信息
|
|
|
public ActivityBo activityBo;
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return "BusinessProcessorContext{" +
|
|
|
"orderCode=" + buyerOrder.getOrderCode() +
|
|
|
", bizCase=" + bizCase +
|
|
|
", targetStatus=" + targetStatus +
|
|
|
'}';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 业务处理
|
|
|
* 按照业务归类如下:
|
|
|
* 取消
|
|
|
* 支付成功
|
|
|
* 等等
|
|
|
*/
|
|
|
private interface BusinessProcessor {
|
|
|
//取消
|
|
|
void cancel(BusinessProcessorContext context);
|
|
|
|
|
|
//支付成功
|
|
|
void paySuccess(BusinessProcessorContext context);
|
|
|
}
|
|
|
|
|
|
//砍价业务
|
|
|
private class BargainBusinessProcessor implements BusinessProcessor {
|
|
|
|
|
|
@Override
|
|
|
public void cancel(BusinessProcessorContext context) {
|
|
|
BuyerOrder buyerOrder = context.buyerOrder;
|
|
|
ActivityBo activityBo = context.activityBo;
|
|
|
if (shouldProcess(buyerOrder, activityBo)) {
|
|
|
bargainProxyService.cancelCutPriceUseRecord(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void paySuccess(BusinessProcessorContext context) {
|
|
|
BuyerOrder buyerOrder = context.buyerOrder;
|
|
|
ActivityBo activityBo = context.activityBo;
|
|
|
if (shouldProcess(buyerOrder, activityBo)) {
|
|
|
bargainProxyService.payCutPrice(buyerOrder.getUid(), buyerOrder.getOrderCode(), activityBo.getUserActivityId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public boolean shouldProcess(BuyerOrder buyerOrder, ActivityBo activityBo) {
|
|
|
if (!Objects.equals(buyerOrder.getActivityType(), ActivityTypeEnum.BARGAIN.getCode())) {
|
|
|
return false;
|
|
|
}
|
|
|
if (activityBo == null || activityBo.getUserActivityId() < 0) {
|
|
|
logger.warn("[{}] has no userActivityId", buyerOrder.getOrderCode());
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|