Authored by sailing-PC\sailing

Merge branch 'dev'

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){
for(OrderCodeType v : values()){
if(v.type == code){
return true;
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()){
cache.put(v.type, v);
}
return false;
}
}
... ...
... ... @@ -16,26 +16,27 @@ public interface IExpressInfoService {
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void deliverToDepot(Integer uid,Integer expressCompanyId, Long orderCode, String wayBillCode);
void deliverToDepot(Integer sellerUid,Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 商品鉴定不通过,鉴定中心寄回商品给卖家
*
* @param sellerUid 卖家uid
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void appraiseFail(Integer expressCompanyId, Long orderCode, String wayBillCode);
void appraiseFail(Integer sellerUid,Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 商品鉴定通过,鉴定中心发货给买家
*
* @param uid 买家id
* @param expressCompanyId 快递公司id
* @param orderCode 订单号
* @param wayBillCode 快递单号
*/
void appraiseSuccess(Integer expressCompanyId, Long orderCode, String wayBillCode);
void appraiseSuccess(Integer uid,Integer expressCompanyId, Long orderCode, String wayBillCode);
/**
* 根据订单号查询快递信息
... ...
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.BuyerOrderMapper;
import com.yohoufo.dal.order.model.BuyerOrder;
import com.yohoufo.order.common.OrderStatus;
import com.yohoufo.order.service.IExpressInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -10,23 +18,55 @@ import org.springframework.stereotype.Service;
@Service
public class AppraiseService {
private static final Logger LOGGER = LoggerFactory.getLogger(AppraiseService.class);
@Autowired
private IExpressInfoService expressInfoService;
@Autowired
private BuyerOrderMapper buyerOrderMapper;
public boolean appraiseSuccess(Integer expressCompanyId, Long orderCode, String wayBillCode){
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
expressInfoService.appraiseSuccess(expressCompanyId, orderCode, wayBillCode);
//TODO 更新卖家订单状态,退款给卖家(仅退一次)
return false;
if (buyerOrder == null){
LOGGER.warn("appraiseSuccess getOrderInfo order not exist, orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
//记录物流信息
expressInfoService.appraiseSuccess(buyerOrder.getUid(),expressCompanyId, orderCode, wayBillCode);
// 更新买家订单状态
buyerOrder.setStatus(OrderStatus.WAITING_RECEIVE.getCode());
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("appraiseSuccess update buyer order {} ", buyerOrder);
buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder);
//TODO 更新卖家订单状态,退款给卖家(仅退一次)
return true;
}
public boolean appraiseFail(Integer expressCompanyId, Long orderCode, String wayBillCode){
expressInfoService.appraiseFail(expressCompanyId, orderCode, wayBillCode);
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
if (buyerOrder == null){
LOGGER.warn("appraiseFail getOrderInfo order not exist, orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
//更新物流信息
expressInfoService.appraiseFail(buyerOrder.getSellerUid(),expressCompanyId, orderCode, wayBillCode);
// 更新买家订单状态
buyerOrder.setStatus(OrderStatus.CHECKING_FAKE.getCode());
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("appraiseFail update buyer order {} ", buyerOrder);
buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder);
//TODO 更新卖家订单状态,买家订单状态;开始分账
return false;
return true;
}
}
... ...
... ... @@ -64,20 +64,20 @@ public class ExpressInfoServiceImpl implements IExpressInfoService {
/**
*
* @param uid 卖家的uid
* @param sellerUid 卖家的uid
* @param expressCompanyId 快递公司id
* @param orderCode 订单号 :一定是买家订单编号
* @param wayBillCode 快递单号
*/
@Override
public void deliverToDepot(Integer uid,Integer expressCompanyId, Long orderCode, String wayBillCode) {
public void deliverToDepot(Integer sellerUid,Integer expressCompanyId, Long orderCode, String wayBillCode) {
//保存物流信息+更新订单状态;
//本阶段的物流类型和订单状态
OrderStatus orderStatus=OrderStatus.SELLER_SEND_OUT;
Integer expressType = ExpressInfoConstant.EXPRESS_TYPE_1;
LOGGER.info("deliverToDepot saveExpressAndUpdateBuyerOrderStatus uid={} ,expressCompanyId = {}, orderCode = {}, wayBillCode = {} " +
",expressType = {} ,orderStatus = {}", new Object[]{uid ,expressCompanyId, orderCode, wayBillCode,expressType,orderStatus});
LOGGER.info("deliverToDepot saveExpressAndUpdateBuyerOrderStatus sellerUid={} ,expressCompanyId = {}, orderCode = {}, wayBillCode = {} " +
",expressType = {} ,orderStatus = {}", new Object[]{sellerUid ,expressCompanyId, orderCode, wayBillCode,expressType,orderStatus});
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
if (buyerOrder == null){
... ... @@ -85,85 +85,53 @@ public class ExpressInfoServiceImpl implements IExpressInfoService {
throw new ServiceException(ServiceError.ORDER_NULL);
}
//卖家的uid
if(uid.intValue()!=buyerOrder.getSellerUid().intValue()){
/* //卖家的uid
if(sellerUid.intValue()!=buyerOrder.getSellerUid().intValue()){
LOGGER.warn("deliverToDepot saveExpressAndUpdateBuyerOrderStatus uid {} not equal buyer order sellerUid {}",uid, buyerOrder.getSellerUid());
throw new ServiceException(400,"参数错误,传入的卖家uid与订单卖家uid不一致");
}
Integer dealUid=buyerOrder.getSellerUid();
}*/
// 保存订单物流信息
saveExpress(dealUid,expressCompanyId,orderCode,wayBillCode,expressType);
saveExpress(sellerUid,expressCompanyId,orderCode,wayBillCode,expressType);
// 发送mq获取物流信息
sendExpressMQ(sellerUid,expressCompanyId,orderCode,wayBillCode);
LOGGER.info("deliverToDepot end ! send express to erp ");
// 更新买家订单状态
buyerOrder.setStatus(orderStatus.getCode());
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("deliverToDepot update buyer order {} ", buyerOrder);
buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder);
// 发送mq获取物流信息
sendExpressMQ(dealUid,expressCompanyId,orderCode,wayBillCode);
LOGGER.info("deliverToDepot end ! send express to erp ");
}
@Override
public void appraiseFail(Integer expressCompanyId, Long orderCode, String wayBillCode) {
OrderStatus orderStatus=OrderStatus.CHECKING_FAKE;
public void appraiseFail(Integer sellerUid ,Integer expressCompanyId, Long orderCode, String wayBillCode) {
Integer expressType = ExpressInfoConstant.EXPRESS_TYPE_3;
LOGGER.info("appraiseFail expressCompanyId = {}, orderCode = {}, wayBillCode = {} " +
",expressType = {} ,orderStatus = {}", new Object[]{expressCompanyId, orderCode, wayBillCode,expressType,orderStatus});
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
if (buyerOrder == null){
LOGGER.warn("appraiseFail getOrderInfo order not exist, orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
Integer dealUid=buyerOrder.getSellerUid();
",expressType = {}", new Object[]{expressCompanyId, orderCode, wayBillCode,expressType});
// 保存订单物流信息
saveExpress(dealUid,expressCompanyId,orderCode,wayBillCode,expressType);
// 更新买家订单状态
buyerOrder.setStatus(orderStatus.getCode());
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("appraiseFail update buyer order {} ", buyerOrder);
buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder);
saveExpress(sellerUid,expressCompanyId,orderCode,wayBillCode,expressType);
// 发送mq获取物流信息
sendExpressMQ(dealUid,expressCompanyId,orderCode,wayBillCode);
sendExpressMQ(sellerUid,expressCompanyId,orderCode,wayBillCode);
LOGGER.info("appraiseFail end ! send express to erp ");
}
@Override
public void appraiseSuccess(Integer expressCompanyId, Long orderCode, String wayBillCode) {
OrderStatus orderStatus=OrderStatus.WAITING_RECEIVE;
public void appraiseSuccess(Integer uid,Integer expressCompanyId, Long orderCode, String wayBillCode) {
Integer expressType = ExpressInfoConstant.EXPRESS_TYPE_2;
LOGGER.info("appraiseSuccess expressCompanyId = {}, orderCode = {}, wayBillCode = {} " +
",expressType = {} ,orderStatus = {}", new Object[]{expressCompanyId, orderCode, wayBillCode,expressType,orderStatus});
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
if (buyerOrder == null){
LOGGER.warn("appraiseSuccess getOrderInfo order not exist, orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
Integer dealUid=buyerOrder.getUid();
",expressType = {} ", new Object[]{expressCompanyId, orderCode, wayBillCode,expressType});
// 保存订单物流信息
saveExpress(dealUid,expressCompanyId,orderCode,wayBillCode,expressType);
// 更新买家订单状态
buyerOrder.setStatus(orderStatus.getCode());
buyerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("appraiseSuccess update buyer order {} ", buyerOrder);
buyerOrderMapper.updateByPrimaryKeySelective(buyerOrder);
saveExpress(uid,expressCompanyId,orderCode,wayBillCode,expressType);
// 发送mq获取物流信息
sendExpressMQ(dealUid,expressCompanyId,orderCode,wayBillCode);
sendExpressMQ(uid,expressCompanyId,orderCode,wayBillCode);
LOGGER.info("appraiseSuccess end ! send express to erp ");
}
... ...
... ... @@ -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
... ...