Authored by chenchao

split logic

package com.yohoufo.order.event;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Builder;
/**
* Created by chao.chen on 2018/11/14.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BeforeDepotReceiveEvent extends BuyerCancelEvent {
private OrderStatus target;
private OrderStatus expected;
}
... ...
package com.yohoufo.order.event;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import lombok.Data;
/**
* Created by chao.chen on 2018/11/14.
*/
@Data
public class BeforeSellerDeliverEvent extends BuyerCancelEvent {
private OrderStatus target;
private OrderStatus expected;
}
... ...
package com.yohoufo.order.service.impl;
import com.yohoufo.order.event.BeforeDepotReceiveEvent;
import com.yohoufo.order.event.BeforeSellerDeliverEvent;
import com.yohoufo.order.event.BuyerCancelEvent;
import com.yohoufo.order.utils.LoggerUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
/**
... ... @@ -7,8 +12,20 @@ import org.springframework.stereotype.Service;
*/
@Service
public class BuyerOrderCancelService {
private Logger logger = LoggerUtils.getOrderCloseLogger();
public void cancel(){
public void cancel(BeforeSellerDeliverEvent bsdEvent){
logger.info("in buyer cancel BeforeSellerDeliver, event {}", bsdEvent);
}
public void cancel(BeforeDepotReceiveEvent bdrEvent){
logger.info("in buyer cancel BeforeDepotReceive, event {}", bdrEvent);
}
}
... ...
... ... @@ -19,6 +19,8 @@ import com.yohoufo.dal.order.model.BuyerOrderGoods;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.order.common.ActionStatusHold;
import com.yohoufo.order.common.DelStatus;
import com.yohoufo.order.event.BeforeDepotReceiveEvent;
import com.yohoufo.order.event.BeforeSellerDeliverEvent;
import com.yohoufo.order.model.request.OrderListRequest;
import com.yohoufo.order.model.request.OrderRequest;
import com.yohoufo.order.model.response.OrderDetailInfo;
... ... @@ -71,6 +73,9 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
@Autowired
private CacheCleaner cacheCleaner;
@Autowired
private BuyerOrderCancelService buyerOrderCancelService;
/**
* 提交订单
* @param orderRequest
... ... @@ -78,7 +83,8 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
public void confirm(OrderRequest orderRequest){
OrderStatus expectStatus = OrderStatus.WAITING_RECEIVE;
// 参数检查 ![待收货] 不合法
DataNode node = checkStatusAndRequest(orderRequest, expectStatus);
DataNode node = checkBase(orderRequest);
checkStatus(node,orderRequest, expectStatus);
int uid;
long orderCode;
int sellerUid = node.buyerOrderInDB.getSellerUid();
... ... @@ -105,9 +111,7 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
@Override
public void cancel(OrderRequest orderRequest){
logger.info("Buyer Order cancel (OrderRequest) {}", orderRequest);
OrderStatus expectStatus = OrderStatus.WAITING_PAY;
OrderStatus targetStatus = OrderStatus.BUYER_CANCEL_BEFORE_PAY;
cancelBeforePaid(orderRequest, expectStatus, targetStatus);
doCancel(orderRequest);
}
@Override
... ... @@ -115,14 +119,55 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
logger.info("Buyer Order cancel time out ,(OrderRequest) {}", orderRequest);
OrderStatus expectStatus = OrderStatus.WAITING_PAY;
OrderStatus targetStatus = OrderStatus.BUYER_CANCEL_TIMEOUT;
cancelBeforePaid(orderRequest, expectStatus, targetStatus);
DataNode node = checkBase(orderRequest);
cancelBeforePaid(orderRequest, node, expectStatus, targetStatus);
}
private void doCancel(OrderRequest orderRequest){
DataNode node = checkBase(orderRequest);
BuyerOrder buyerOrder = node.buyerOrderInDB;
Integer orderStatus = buyerOrder.getStatus();
OrderStatus expectStatus = null ;
if (orderStatus == null || (expectStatus = OrderStatus.getOrderStatus(orderStatus))== null){
logger.warn("in buyer cancel check orderStatus fail, orderStatus {} expectStatus {}", orderStatus,expectStatus );
throw new ServiceException(ServiceError.ORDER_STATUS_INVALIDATE);
}
logger.info("in buyer cancel, req {} expectStatus {}", orderRequest, expectStatus);
//case 1 :支付前
//case 2 :支付后,卖家发货前
//case 3 :卖家发货后,鉴定中心接受前
OrderStatus targetStatus;
switch (expectStatus){
case WAITING_PAY:
targetStatus = OrderStatus.BUYER_CANCEL_BEFORE_PAY;
cancelBeforePaid(orderRequest, node, expectStatus, targetStatus);
break;
case HAS_PAYED:
targetStatus = OrderStatus.BUYER_CANCEL_BEFORE_SELLER_DELIVER;
BeforeSellerDeliverEvent bsde = new BeforeSellerDeliverEvent();
bsde.setUid(orderRequest.getUid());
bsde.setOrderCode(orderRequest.getOrderCode());
bsde.setExpected(expectStatus);
bsde.setTarget(targetStatus);
buyerOrderCancelService.cancel(bsde);
break;
case SELLER_SEND_OUT:
targetStatus = OrderStatus.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE;
BeforeDepotReceiveEvent bdre = new BeforeDepotReceiveEvent();
bdre.setUid(orderRequest.getUid());
bdre.setOrderCode(orderRequest.getOrderCode());
bdre.setExpected(expectStatus);
bdre.setTarget(targetStatus);
buyerOrderCancelService.cancel(bdre);
break;
}
}
public void cancelBeforePaid(OrderRequest orderRequest, OrderStatus exceptStatus, OrderStatus targetStatus){
public void cancelBeforePaid(OrderRequest orderRequest, DataNode dataNode, OrderStatus exceptStatus, OrderStatus targetStatus){
// 状态 ![待付款] 不合法
DataNode dataNode = checkStatusAndRequest(orderRequest, exceptStatus);
checkStatus(dataNode, orderRequest, exceptStatus);
// 更新订单状态
final OrderStatus orderStatus = targetStatus;
... ... @@ -166,12 +211,8 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
private class DataNode{
BuyerOrder buyerOrderInDB;
}
/**
*
* @param orderRequest
* @param orderStatusEx
*/
private DataNode checkStatusAndRequest(OrderRequest orderRequest, OrderStatus orderStatusEx ) {
private DataNode checkBase(OrderRequest orderRequest){
// 参数检查
if (orderRequest.getUid() < 0
|| orderRequest.getOrderCode() < 0){
... ... @@ -187,7 +228,19 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
orderRequest.getUid(), orderRequest.getOrderCode() );
throw new ServiceException(ServiceError.ORDER_NULL);
}
DataNode node = new DataNode();
node.buyerOrderInDB = buyerOrder;
return node;
}
/**
*
* @param orderRequest
* @param orderStatusEx
*/
private void checkStatus(DataNode node, OrderRequest orderRequest, OrderStatus orderStatusEx ) {
BuyerOrder buyerOrder = node.buyerOrderInDB;
// 检查 订单状态
int status = buyerOrder.getStatus() == null ? -1 : buyerOrder.getStatus().intValue();
OrderStatus orderStatus = OrderStatus.getOrderStatus(status);
... ... @@ -196,9 +249,6 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
orderRequest.getUid(), orderRequest.getOrderCode(), buyerOrder.getStatus());
throw new ServiceException(ServiceError.ORDER_STATUS_INVALIDATE);
}
DataNode node = new DataNode();
node.buyerOrderInDB = buyerOrder;
return node;
}
... ...