Authored by sailing-PC\sailing

fix

package com.yohoufo.order.common;
import java.util.Arrays;
import java.util.List;
/**
* Created by chenchao on 2018/9/27.
*/
public class ActionStatusHold {
public static List<Integer> getSellerCanCancelStatus(){
List<Integer> sellerCanCancelStatus;
sellerCanCancelStatus = Arrays.asList(SellerOrderStatus.WAITING_PAY.getCode(),
SellerOrderStatus.HAS_PAYED.getCode());
return sellerCanCancelStatus;
}
}
... ...
package com.yohoufo.order.common;
import java.util.HashMap;
import java.util.Map;
public enum OrderCodeType {
SELLER_TYPE(0L),
... ... @@ -22,11 +25,22 @@ public enum OrderCodeType {
* @return
*/
public static boolean isExistOrderCodeType(long code){
return cache.containsKey(code);
}
public static OrderCodeType getOrderCodeType(long type){
return cache.get(type);
}
private static Map<Long, OrderCodeType> cache;
static {
cache = new HashMap<>();
for(OrderCodeType v : values()){
if(v.type == code){
return true;
}
cache.put(v.type, v);
}
return false;
}
}
... ...
... ... @@ -3,12 +3,16 @@ package com.yohoufo.order.service.impl;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
import com.yohoufo.dal.order.BuyerOrderMapper;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
import com.yohoufo.dal.order.SellerOrderMapper;
import com.yohoufo.dal.order.model.BuyerOrder;
import com.yohoufo.dal.order.model.BuyerOrderGoods;
import com.yohoufo.dal.order.model.SellerOrder;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.order.common.ActionStatusHold;
import com.yohoufo.order.common.OrderCodeType;
import com.yohoufo.order.common.OrderStatus;
import com.yohoufo.order.common.SellerOrderStatus;
import com.yohoufo.order.event.ErpCancelSellerOrderEvent;
... ... @@ -21,6 +25,8 @@ import com.yohoufo.order.service.impl.visitor.AutoCancelCase;
import com.yohoufo.order.service.impl.visitor.OffShelveCancelCase;
import com.yohoufo.order.service.impl.visitor.UserCancelCase;
import com.yohoufo.order.service.proxy.ProductProxyService;
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
import com.yohoufo.order.service.support.codegenerator.bean.CodeMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -56,8 +62,14 @@ public class SellerOrderCancelService {
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
@Autowired
private BuyerOrderGoodsMapper buyerOrderGoodsMapper;
@Autowired
private ProductProxyService productProxyService;
@Autowired
private OrderCodeGenerator orderCodeGenerator;
/**
* TODO 如何控制好并发,必须控制不能重复转账 退款
* 使用乐观锁,带着查询到的状态且符合条件时再去更新
... ... @@ -68,26 +80,51 @@ public class SellerOrderCancelService {
OrderCancelEvent event = (OrderCancelEvent)cancelCase.getEvent();
int uid = event.getUid();
long orderCode = event.getOrderCode();
CodeMeta codeMeta = orderCodeGenerator.expId(orderCode);
if (Objects.isNull(codeMeta)){
logger.warn("seller cancel orderCode illegal, req {}", event);
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
OrderCodeType actor = OrderCodeType.getOrderCodeType(codeMeta.getType());
logger.info("in cancel uid {},orderCode {}, actor {}", uid, orderCode, actor);
//case 1: 未支付时
//case 2: 支付完成,没有买家下单
//case 3: 支付完成,有买家下单
cancel(uid, orderCode);
cancel(uid, orderCode, actor);
}
public int cancel(int uid, long orderCode){
public int cancel(int uid, long orderCode, OrderCodeType actor){
int result = 0;
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCodeUid(orderCode, uid);
SellerOrder sellerOrder = null;
if (OrderCodeType.SELLER_TYPE.equals(actor)){
sellerOrder = sellerOrderMapper.selectByOrderCodeUid(orderCode, uid);
}
BuyerOrder buyerOrder = null;
if (OrderCodeType.BUYER_TYPE.equals(actor)){
buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMapper.selectByOrderCode(buyerOrder.getUid(), orderCode);
sellerOrder = sellerOrderMapper.selectBySkup(buyerOrderGoods.getSkup());
}
if (Objects.isNull(sellerOrder)){
return result;
}
Integer status = Optional.ofNullable(sellerOrder).map(SellerOrder::getStatus).orElse(null);
logger.info("in seller cancel, uid {}, orderCode {}, SellerOrder status {}", uid, orderCode, status);
if (Objects.isNull(status)){
return result;
}
logger.info("in seller cancel, uid {}, orderCode {}, SellerOrder status {}", uid, orderCode, status);
List<Integer> sellerCanCancelStatus = ActionStatusHold.getSellerCanCancelStatus();
if (!sellerCanCancelStatus.contains(status)){
logger.warn("seller cancel can not execute in this status, uid {} orderCode {}, status {}", uid, orderCode, status);
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
}
//TODO check BuyerOrder
//target seller Order Status
SellerOrderStatus targetSOStatus;
//case 1: 未支付时
... ... @@ -99,7 +136,7 @@ public class SellerOrderCancelService {
//case 2: 支付完成,没有买家下单
//case 3: 支付完成,有买家下单
if (SellerOrderStatus.HAS_PAYED.getCode() == status){
result = cancelAfterPayAction(sellerOrder);
result = cancelAfterPayAction(sellerOrder, buyerOrder);
}
return result;
... ... @@ -203,8 +240,7 @@ public class SellerOrderCancelService {
return result;
}
private int cancelAfterPayExistBuyAction(SellerOrder sellerOrder,List<Integer> sellerCanCancelStatus,
int buyOrderCnt){
private int cancelAfterPayExistBuyAction(SellerOrder sellerOrder, BuyerOrder buyerOrder){
/**
* case 3: 支付完成,有买家下单
* actions as blow:
... ... @@ -213,30 +249,31 @@ public class SellerOrderCancelService {
* 3. sellerOrderGoods
* 4. 瓜分保证金
*/
final int sellerUid = sellerOrder.getUid();
final long orderCode = sellerOrder.getOrderCode();
logger.info("in seller cancel After Pay while Exist Buy Action, uid {}, orderCode {}",
sellerUid, orderCode);
final int sellerUid = sellerOrder.getUid(), buyerUid = buyerOrder.getUid();
final long sellerOrderCode = sellerOrder.getOrderCode(), buyerOrderCode = buyerOrder.getOrderCode();
logger.info("in seller cancel After Pay while Exist Buy Action, uid {}, orderCode {}, buyerOrderCode {}",
sellerUid, sellerOrderCode, buyerOrderCode);
int result = 0;
List<BuyerOrder> buyerOrderList = buyerOrderMapper.selectListBySellerUidStatus(sellerUid, sellerCanCancelStatus, 0, buyOrderCnt);
List<Long> orderCodes = buyerOrderList.parallelStream().map(BuyerOrder::getOrderCode).collect(Collectors.toList());
if (orderCodes.size() > 1){
throw new ServiceException(ServiceError.ERP_ORDER_CANCEL_FAILED);
}
//actions of buyer order and related
//update buyer Order
//target Buyer Order Status
Integer targetBOStatus;
targetBOStatus = OrderStatus.SELLER_CANCEL_AFTER_PAY.getCode();
result += buyerOrderMapper.updateBatchByOrderCodes(orderCodes, targetBOStatus, DateUtil.getCurrentTimeSecond());
BuyerOrder tbo = new BuyerOrder();
tbo.setUid(buyerUid);
tbo.setOrderCode(buyerOrderCode);
tbo.setStatus(targetBOStatus);
tbo.setUpdateTime(DateUtil.getCurrentTimeSecond());
result += buyerOrderMapper.updateByOrderCode(tbo);
//update seller order
SellerOrderStatus targetSOStatus;
targetSOStatus = SellerOrderStatus.PLAY_BUYER;
SellerOrder target = new SellerOrder();
target.setOrderCode(orderCode);
target.setOrderCode(sellerOrderCode);
target.setUid(sellerUid);
target.setStatus(targetSOStatus.getCode());
target.setUpdateTime(DateUtil.getCurrentTimeSecond());
... ... @@ -249,20 +286,18 @@ public class SellerOrderCancelService {
return result;
}
private int cancelAfterPayAction(SellerOrder sellerOrder){
private int cancelAfterPayAction(SellerOrder sellerOrder, BuyerOrder buyerOrder){
final int sellerUid = sellerOrder.getUid();
final long orderCode = sellerOrder.getOrderCode();
logger.info("in seller cancel After Buy Action, uid {}, orderCode {}",
sellerUid, orderCode);
int result = 0;
List<Integer> sellerCanCancelStatus;
int buyOrderCnt;
int buyOrderCnt = buyerOrder== null ? 0 : 1;
//查询买家订单,状态是支付成功的
sellerCanCancelStatus = Arrays.asList(OrderStatus.HAS_PAYED.getCode());
buyOrderCnt = buyerOrderMapper.selectCntBySellerUidStatus(sellerUid, sellerCanCancelStatus);
if (buyOrderCnt == 0){
/**case 2: 支付完成,没有买家下单
* actions as blow:
... ... @@ -281,10 +316,15 @@ public class SellerOrderCancelService {
* 3. sellerOrderGoods
* 4. 瓜分保证金
*/
result = cancelAfterPayExistBuyAction(sellerOrder, sellerCanCancelStatus, buyOrderCnt);
//查询买家订单,状态是支付成功的
result = cancelAfterPayExistBuyAction(sellerOrder, buyerOrder);
return result;
}
/**
*
* @param autoCancelCase
... ...