Authored by wujiexiang

求购 -- 卖家支付

package com.yohoufo.order.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.core.dal.datasource.annotation.Database;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.bo.OrderInfo;
import com.yohobuy.ufo.model.order.common.OrderCodeType;
import com.yohobuy.ufo.model.order.common.SellerOrderStatus;
import com.yohobuy.ufo.model.order.common.SkupStatus;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
import com.yohoufo.dal.order.SellerOrderMapper;
import com.yohoufo.dal.order.SellerOrderMetaMapper;
import com.yohoufo.dal.order.model.SellerOrder;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.dal.order.model.SellerOrderMeta;
import com.yohoufo.order.common.ActionStatusHold;
import com.yohoufo.order.constants.MetaKey;
import com.yohoufo.order.event.SellerBidOrderPaySuccessEvent;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.response.PrepayResponse;
import com.yohoufo.order.mq.DelayTime;
import com.yohoufo.order.service.cache.CacheCleaner;
import com.yohoufo.order.service.impl.TradeBillsService;
import com.yohoufo.order.service.proxy.SellerNoticeFacade;
import com.yohoufo.order.service.seller.SellerGoodsStatusFlowService;
import com.yohoufo.order.utils.LoggerUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class SellerBidOrderPaymentService extends AbstractOrderPaymentService {
private final Logger logger = LoggerUtils.getSellerOrderLogger();
@Autowired
SellerOrderMapper sellerOrderMapper;
@Autowired
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
@Autowired
SellerOrderMetaMapper sellerOrderMetaMapper;
@Autowired
private CacheCleaner cacheCleaner;
@Autowired
private SellerNoticeFacade sellerNoticeFacade;
@Autowired
private SellerGoodsStatusFlowService sellerGoodsStatusFlowService;
@Autowired
private TradeBillsService tradeBillsService;
/**
* 更新订单状态
*
* @param orderInfo
*/
@Override
public int updateOrderStatusPaid(OrderInfo orderInfo) {
//
logger.info("in update seller OrderStatus after Paid, orderInfo {}", orderInfo);
Long orderCode = orderInfo.getOrderCode();
SellerOrder sellerOrder = new SellerOrder();
sellerOrder.setOrderCode(orderCode);
sellerOrder.setUid(orderInfo.getUid());
SellerOrderStatus soTargetStatus = SellerOrderStatus.HAS_PAYED;
sellerOrder.setStatus(soTargetStatus.getCode());
orderInfo.setStatus(soTargetStatus.getCode());
sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
sellerOrder.setExceptStatus(SellerOrderStatus.WAITING_PAY.getCode());
int updateCnt = sellerOrderMapper.updateByOrderCode(sellerOrder);
if (1 == updateCnt) {
//
SellerOrderGoods targetSog = new SellerOrderGoods();
targetSog.setId(orderInfo.getSkup());
targetSog.setStatus(SkupStatus.SELL_OUT.getCode());
sellerOrderGoodsMapper.updateByPrimaryKeySelective(targetSog);
}
return updateCnt;
}
@Override
public void processAfterPay(OrderInfo orderInfo) {
logger.info("SellerBidOrderPaymentService.processAfterPay in, orderInfo is {}", orderInfo);
//
int sellerUid = orderInfo.getUid();
Long orderCode = orderInfo.getOrderCode();
Integer payment = orderInfo.getPayment();
tradeBillsService.addPayEnsureRecord(orderInfo.getUid(), orderInfo.getSkup(), orderCode, payment,
orderInfo.getAmount());
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(orderCode);
SellerOrderGoods sellerOrderGoods = null;
if (Objects.nonNull(sellerOrder)) {
sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
String prdName = sellerOrderGoods.getProductName();
String sizeName = sellerOrderGoods.getSizeName();
Integer productId = sellerOrderGoods.getProductId();
//async
SkupStatus skupStatus = SkupStatus.CAN_SELL;
sellerGoodsStatusFlowService.pushStatusFlowEvent(sellerOrderGoods.getId(), skupStatus.getCode());
//卖家bid订单保证金支付完成事件
EventBusPublisher.publishEvent(SellerBidOrderPaySuccessEvent.builder().uid(sellerUid).skup(sellerOrderGoods.getId()).build());
sellerNoticeFacade.saleShelf(sellerUid, prdName, sizeName, productId, sellerOrderGoods.getAttributes());
//
cacheCleaner.cleanSellerOrderDetailAndList(sellerOrderGoods, sellerOrder.getOrderCode());
}
}
/**
* 订单是否是待支付状态
*
* @param orderInfo
* @return
*/
public boolean isOrderWaitingPay(OrderInfo orderInfo) {
if (isWaitingPay(orderInfo.getStatus())) {
return true;
}
return false;
}
/**
* 订单是否 已支付 状态
*
* @param orderInfo
* @return
*/
public boolean isOrderPaid(OrderInfo orderInfo) {
if (orderInfo.getStatus() == null ||
!Objects.equals(SellerOrderStatus.HAS_PAYED.getCode(), orderInfo.getStatus().intValue())) {
return false;
}
return true;
}
public boolean canRefund(OrderInfo orderInfo) {
Integer sourceStatus = orderInfo.getSourceStatus();
if (sourceStatus != null &&
ActionStatusHold.getSellerCanRefundEarnestMoneyStatus().contains(sourceStatus.intValue())) {
return true;
}
return false;
}
/**
* 保存前回预支付结果
*
* @param orderInfo
* @param response
*/
public void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response) {
SellerOrderMeta buyerOrderMeta = new SellerOrderMeta();
buyerOrderMeta.setUid(orderInfo.getUid());
buyerOrderMeta.setSkup(orderInfo.getSkup());
buyerOrderMeta.setMetaKey(MetaKey.APP_PAY_REQUEST);
buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response));
// 记录appPayRequest
sellerOrderMetaMapper.insert(buyerOrderMeta);
}
/**
* 获取前回的访问结果
*
* @param request
* @return
*/
public PrepayResponse getAppPayRequest(PaymentRequest request) {
SellerOrderMeta buyerOrderMeta = sellerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getSkup(), MetaKey.APP_PAY_REQUEST);
if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())) {
return null;
}
try {
PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class);
return response;
} catch (Exception e) {
}
return null;
}
/**
* 预支付方式更新
*
* @param orderInfo
*/
public void updatePayment(OrderInfo orderInfo, int payment) {
SellerOrder sellerOrder = new SellerOrder();
// 更新预支付方式
sellerOrder.setId(orderInfo.getId());
sellerOrder.setPayment(payment);
sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
sellerOrderMapper.updateByPrimaryKeySelective(sellerOrder);
}
/**
* 检查状态
*
* @param orderInfo
*/
public void checkOrderStatus(OrderInfo orderInfo) {
// 订单不是未付款的状态
if (!isWaitingPay(orderInfo.getStatus())) {
logger.warn("payment status not allow {}, orderCode is {}, uid is {}", orderInfo.getStatus(),
orderInfo.getOrderCode(), orderInfo.getUid());
throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW);
}
}
@Override
public boolean isTimeoutCancelStatus(OrderInfo orderInfo) {
if (orderInfo.getStatus() != null &&
(orderInfo.getStatus().intValue() == SellerOrderStatus.TIMEOUT_CANCEL.getCode() || orderInfo.getStatus().intValue() == SellerOrderStatus.SELf_CANCEL_PAY.getCode())) {
return true;
}
return false;
}
/**
* 获取订单情报
*
* @param orderCode
* @return
*/
public OrderInfo getOrderInfo(long orderCode, int uid) {
SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
OrderInfo orderInfo = buildOrderInfo(orderCode, uid, sellerOrder);
if (sellerOrderGoods != null) {
orderInfo.setAttributes(sellerOrderGoods.getAttributes());
}
return orderInfo;
}
private OrderInfo buildOrderInfo(long orderCode, int uid, SellerOrder sellerOrder) {
OrderInfo orderInfo = OrderInfo.builder()
.id(sellerOrder.getId())
.sellerUid(sellerOrder.getUid())
.uid(uid)
.orderCode(orderCode)
.payment(sellerOrder.getPayment())
.skup(sellerOrder.getSkup())
.status(sellerOrder.getStatus())
.orderCodeType(OrderCodeType.SELLER_TYPE)
.createTime(sellerOrder.getCreateTime())
.amount(sellerOrder.getEarnestMoney())
.payExpireTime(DelayTime.SELLER_ORDER_WAITING_PAY)
.payExpire(sellerOrder.getCreateTime() + covertFen(DelayTime.SELLER_ORDER_WAITING_PAY))
.build();
return orderInfo;
}
/**
* 获取订单情报
*
* @param orderCode
* @return
*/
@Database(ForceMaster = true)
@Override
public OrderInfo getOrderInfo(long orderCode) {
SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
OrderInfo orderInfo = OrderInfo.builder()
.uid(sellerOrder.getUid())
.orderCode(orderCode)
.payment(sellerOrder.getPayment())
.skup(sellerOrder.getSkup())
.status(sellerOrder.getStatus())
.orderCodeType(OrderCodeType.SELLER_TYPE)
.amount(sellerOrder.getEarnestMoney())
.sourceStatus(sellerOrder.getStatus())
.createTime(sellerOrder.getCreateTime())
.build();
return orderInfo;
}
private SellerOrder checkAndGetSellerOrder(long orderCode) {
SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(orderCode);
if (sellerOrder == null) {
logger.warn("getOrderInfo order not exist, orderCode is {}", orderCode);
throw new ServiceException(ServiceError.ORDER_NULL);
}
return sellerOrder;
}
/**
* 定金状态
*
* @param orderCode
* @return
*/
public boolean isSupport(long orderCode) {
SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
if (isWaitingPay(sellerOrder.getStatus())) {
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
return sellerOrderGoods.isBidSkup();
} else {
return false;
}
}
private boolean isWaitingPay(Integer status) {
return status != null && SellerOrderStatus.WAITING_PAY.getCode() == status;
}
}
... ...
... ... @@ -7,7 +7,6 @@ import com.yoho.core.dal.datasource.annotation.Database;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.common.SkupStatus;
import com.yohobuy.ufo.model.order.common.TabType;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
import com.yohoufo.dal.order.SellerOrderMapper;
... ... @@ -20,13 +19,11 @@ import com.yohobuy.ufo.model.order.common.OrderCodeType;
import com.yohobuy.ufo.model.order.common.SellerOrderStatus;
import com.yohoufo.order.constants.MetaKey;
import com.yohobuy.ufo.model.order.bo.OrderInfo;
import com.yohoufo.order.event.SellerBidOrderPaySuccessEvent;
import com.yohoufo.order.event.SellerOrderPriceChangeEvent;
import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.response.PrepayResponse;
import com.yohoufo.order.mq.DelayTime;
import com.yohoufo.order.service.cache.CacheCleaner;
import com.yohoufo.order.service.cache.CacheKeyBuilder;
import com.yohoufo.order.service.proxy.SellerNoticeFacade;
import com.yohoufo.order.service.seller.SkupService;
import com.yohoufo.order.service.impl.TradeBillsService;
... ... @@ -38,8 +35,6 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Objects;
... ... @@ -143,11 +138,6 @@ public class SellerOrderPaymentService extends AbstractOrderPaymentService {
logger.info("in processAfterPay change price processAfterChangePriceOrderPaid occur error , after syncPrdStatus 2 can-sell, invoke asyncHook, orderInfo {}", orderInfo);
asyncHook.callBack();
}
//卖家bid订单保证金支付完成事件
if (sellerOrderGoods.isBidSkup()) {
EventBusPublisher.publishEvent(SellerBidOrderPaySuccessEvent.builder().uid(sellerUid).skup(sellerOrderGoods.getId()).build());
}
}
sellerNoticeFacade.saleShelf(sellerUid, prdName,sizeName,productId,sellerOrderGoods.getAttributes());
... ...
... ... @@ -29,6 +29,9 @@ public class OrderPaymentServiceFactory {
SellerOrderPaymentService sellerOrderPaymentService;
@Autowired
SellerBidOrderPaymentService sellerBidOrderPaymentService;
@Autowired
MerchantOrderPaymentService merchantOrderPaymentService;
@Autowired
... ... @@ -66,7 +69,11 @@ public class OrderPaymentServiceFactory {
paymentService = this.buyerOrderPaymentService;
}
} else if (codeMeta.getType() == OrderCodeType.SELLER_TYPE.getType()) {
if (sellerBidOrderPaymentService.isSupport(orderCode)) {
paymentService = this.sellerBidOrderPaymentService;
} else {
paymentService = this.sellerOrderPaymentService;
}
} else if (codeMeta.getType() == OrderCodeType.SELLER_RECHARGE_EARNEST_TYPE.getType()) {
paymentService = this.merchantOrderPaymentService;
} else if (codeMeta.getType() == OrderCodeType.STORAGE_MANAGEMENT.getType()) {
... ...
... ... @@ -401,6 +401,7 @@ public class SellerOrderDetailService extends AbsOrderDetailService implements I
orderDetailInfo.setIsAdvance(OrderAssist.getSkupIsAdvance(soga));
orderDetailInfo.setAttributes(soga);
orderDetailInfo.setStatusDetail(statusDetail);
orderDetailInfo.setBidType(sellerOrderGoods.getBidType());
return orderDetailInfo;
}
... ... @@ -567,21 +568,24 @@ public class SellerOrderDetailService extends AbsOrderDetailService implements I
Integer createTime = null;
OrderCodeType orderCodeType = OrderCodeType.getOrderCodeType(codeMeta.getType());
OrderAttributes oa = null;
TimeoutBo timeoutBo = null;
if(orderCodeType.getType() == OrderCodeType.BUYER_TYPE.getType()){
BuyerOrder buyerOrder = buyerOrderMapper.selectOnlyByOrderCode(orderCode);
buyerUid = buyerOrder.getUid();
orderStatus = buyerOrder.getStatus();
createTime = buyerOrder.getCreateTime();
oa = OrderAttributes.getOrderAttributes(buyerOrder.getAttributes());
}else{
orderStatus = orderDetailInfo.getStatusDetail().getStatus();
createTime = orderDetailInfo.getSecendLevelCreateTime();
}
TimeoutBo timeoutBo = getTimeoutBo(buyerUid, orderCode,
timeoutBo = getTimeoutBo(buyerUid, orderCode,
orderDetailInfo.getBidType(),
createTime,
orderStatus,
oa, TabType.SELL);
}else{
orderStatus = orderDetailInfo.getStatusDetail().getStatus();
createTime = orderDetailInfo.getSecendLevelCreateTime();
timeoutBo = calTimeout(TabType.SELL, buyerUid, orderCode,
orderStatus, createTime, oa);
}
Integer leftTime = timeoutBo.getLeftTime();
orderDetailInfo.getStatusDetail().setLeftTime(leftTime);
... ...