Authored by chenchao

use async process

package com.yohoufo.order.service.impl.statechange;
import com.google.common.collect.Lists;
import com.yoho.message.sdk.common.model.SendMessageRspBo;
import com.yohobuy.ufo.model.enums.InboxBusinessTypeEnum;
import com.yohobuy.ufo.model.order.common.OrderStatus;
import com.yohobuy.ufo.model.order.common.SkupStatus;
... ... @@ -22,17 +23,21 @@ import com.yohoufo.order.mq.TopicConstants;
import com.yohoufo.order.service.AbstractBuyerOrderStateChanger;
import com.yohoufo.order.service.IBuyerOrderService;
import com.yohoufo.order.service.impl.BuyerChangePriceRecordService;
import com.yohoufo.order.service.proxy.SellerNoticeFacade;
import com.yohoufo.order.service.seller.SkupService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Supplier;
/**
* 支付定金完成
*
* @see OrderStatus.WAITING_PAY_DEPOSIT -> OrderStatus.BIDING
* @see {@link OrderStatus.WAITING_PAY_DEPOSIT } -> {@link OrderStatus.BIDING}
*/
@Service
public class BuyerOrderPayDepositSuccessChanger extends AbstractBuyerOrderStateChanger<RequestedStatusChangeBuyerOrder> {
... ... @@ -47,6 +52,9 @@ public class BuyerOrderPayDepositSuccessChanger extends AbstractBuyerOrderStateC
private MsgService msgService;
@Autowired
private SellerNoticeFacade sellerNoticeFacade;
@Autowired
private SkupService skupService;
@Override
... ... @@ -144,36 +152,53 @@ public class BuyerOrderPayDepositSuccessChanger extends AbstractBuyerOrderStateC
* @param sellerOrderGoods
*/
public void pushMsg2Seller(SellerOrderGoods sellerOrderGoods){
//TODO
//1. check
// 1.1 不能低于最低现货价90%
//1.2 没有最低现货价
Set<Integer> skupTypes = new HashSet<>();
skupTypes.add(SkupType.IN_STOCK.getCode());
List<Integer> statusList = new ArrayList<>();
statusList.add(SkupStatus.CAN_SELL.getCode());
Integer sku;
BigDecimal leastPrice = skupService.getLeastPriceOfSku(sku=sellerOrderGoods.getStorageId(), statusList, skupTypes);
if (Objects.isNull(leastPrice)){
logger.warn("push msg 2 seller find no sale goods,sku {}", sku);
return;
}
BigDecimal minThreshold = BigDecimalHelper.halfUp(leastPrice.multiply(new BigDecimal(0.9)));
if (sellerOrderGoods.getGoodsPrice().compareTo(minThreshold)<0){
logger.warn("push msg 2 seller, buyer bid price too low, bid-price {} lease price {} threshold {}",
sellerOrderGoods.getGoodsPrice(), leastPrice, minThreshold);
return;
}
// 2. push
// 2.1 find out all seller who own this kind of sku
List<Integer> uidList = skupService.findUidBySku(sku, statusList, skupTypes);
Product product = productMapper.selectByPrimaryKey(sellerOrderGoods.getProductId());
//2.2 check every seller push frequency one by one ;----可能有性能问题 最耗时的一步
msgService.noticeSellerWhenBuyerBidSuccess(uidList, sellerOrderGoods, product);
Supplier<SendMessageRspBo> pushSupplier = () -> {
SendMessageRspBo bo = new SendMessageRspBo();
//TODO
//1. check
// 1.1 不能低于最低现货价90%
//1.2 没有最低现货价
Set < Integer > skupTypes = new HashSet<>();
skupTypes.add(SkupType.IN_STOCK.getCode());
List<Integer> statusList = new ArrayList<>();
statusList.add(SkupStatus.CAN_SELL.getCode());
Integer sku;
BigDecimal leastPrice = skupService.getLeastPriceOfSku(sku = sellerOrderGoods.getStorageId(), statusList, skupTypes);
if (Objects.isNull(leastPrice)) {
logger.warn("push msg 2 seller find no sale goods,sku {}", sku);
bo.setCode(500);
bo.setMessage("pushMsg2Seller fail, no leastPrice");
return bo;
}
BigDecimal minThreshold = BigDecimalHelper.halfUp(leastPrice.multiply(new BigDecimal(0.9)));
if (sellerOrderGoods.getGoodsPrice().compareTo(minThreshold) < 0) {
logger.warn("push msg 2 seller, buyer bid price too low, sku {} bid-price {} lease price {} threshold {}",
sellerOrderGoods.getId(), sellerOrderGoods.getGoodsPrice(), leastPrice, minThreshold);
bo.setCode(500);
bo.setMessage("pushMsg2Seller fail, leastPrice bigger than threshold");
return bo;
}
// 2. push
// 2.1 find out all seller who own this kind of sku
List<Integer> uidList = skupService.findUidBySku(sku, statusList, skupTypes);
if (CollectionUtils.isEmpty(uidList)){
logger.warn("push msg 2 seller, uid empty, sku {}",
sellerOrderGoods.getId());
bo.setCode(500);
bo.setMessage("pushMsg2Seller fail, uid empty");
return bo;
}
Product product = productMapper.selectByPrimaryKey(sellerOrderGoods.getProductId());
//2.2 check every seller push frequency one by one ;----可能有性能问题 最耗时的一步
msgService.noticeSellerWhenBuyerBidSuccess(uidList, sellerOrderGoods, product);
return bo;
};
sellerNoticeFacade.pushSellerWhenBidOrderPaid(Integer.MAX_VALUE, pushSupplier);
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.yohoufo.order.service.proxy;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yoho.message.sdk.common.constants.UFOMessageScene;
import com.yoho.message.sdk.common.model.SendMessageRspBo;
import com.yoho.message.sdk.service.ufo.IUFOSendService;
import com.yoho.message.sdk.service.ufo.UFOMessageService;
import com.yohobuy.ufo.model.enums.InboxBusinessTypeEnum;
... ... @@ -22,6 +23,7 @@ import com.yohoufo.order.service.seller.SkupService;
import lombok.Builder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -154,7 +156,7 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
@Builder
private static class PushDataNode{
Supplier pushSupplier;
Supplier<SendMessageRspBo> pushSupplier;
boolean pushEnable;
}
... ... @@ -172,16 +174,23 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
boolean pushEnable = skupType.equals(SkupType.IN_STOCK)
&& Objects.nonNull(leastPrice=skupService.getLeastPriceOfSku(sku , statusList, skupTypes))
&& leastPrice.compareTo(soldProduct.getPrice())<=0;
Supplier pushSupplier = null;
Supplier<SendMessageRspBo> pushSupplier = null;
if (pushEnable){
List<Integer> uids = bidProductProxyService.getAllUidBySku(sku);
SellerOrderGoods tsog = new SellerOrderGoods();
tsog.setStorageId(sku);
tsog.setProductId(productId);
tsog.setProductName(prdName);
tsog.setGoodsPrice(soldProduct.getPrice());
tsog.setSizeName(sizeName);
pushSupplier = ()-> msgService.noticeBuyerWhenSellerOnShelve(uids, tsog, product);
//because of whole method is not atomic operation
//so at current time, uid may be null
if (CollectionUtils.isEmpty(uids)){
log.warn("in buildPushDataNode by GoodsInfo,bidProductProxyService.getAllUidBySku find no uid, sku {}", sku);
pushEnable = false;
}else {
SellerOrderGoods tsog = new SellerOrderGoods();
tsog.setStorageId(sku);
tsog.setProductId(productId);
tsog.setProductName(prdName);
tsog.setGoodsPrice(soldProduct.getPrice());
tsog.setSizeName(sizeName);
pushSupplier = () -> msgService.noticeBuyerWhenSellerOnShelve(uids, tsog, product);
}
}
return PushDataNode.builder().pushEnable(pushEnable).pushSupplier(pushSupplier).build();
... ... @@ -200,7 +209,14 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
Supplier pushSupplier = null;
if (pushEnable){
List<Integer> uids = bidProductProxyService.getAllUidBySku(sku);
pushSupplier = ()-> msgService.noticeBuyerWhenSellerOnShelve(uids, tsog, productSupplier.get());
//because of whole method is not atomic operation
//so at current time, uid may be null
if (CollectionUtils.isEmpty(uids)){
log.warn("in buildPushDataNode by SellerOrderGoods,bidProductProxyService.getAllUidBySku find no uid, sku {}", sku);
pushEnable = false;
}else {
pushSupplier = () -> msgService.noticeBuyerWhenSellerOnShelve(uids, tsog, productSupplier.get());
}
}
return PushDataNode.builder().pushEnable(pushEnable).pushSupplier(pushSupplier).build();
... ... @@ -209,15 +225,25 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
public void pushMsg2BuyerWhenSellerChangePrice(SellerOrderGoods sog){
Integer sellerUid = sog.getUid();
//
Supplier<Product> productSupplier = () -> productMapper.selectByPrimaryKey(sog.getProductId());
PushDataNode pushDataNode = buildPushDataNode(sog, productSupplier);
if (pushDataNode.pushEnable){
pushDataNode.pushSupplier.get();
}else {
log.warn("pushMsg2BuyerWhenSellerChangePrice not match condition ,seller {} sku {}",
sellerUid, sog.getStorageId());
}
Supplier<SendMessageRspBo> supplier = ()->{
//
Supplier<Product> productSupplier = () -> productMapper.selectByPrimaryKey(sog.getProductId());
PushDataNode pushDataNode = buildPushDataNode(sog, productSupplier);
SendMessageRspBo bo = null;
if (pushDataNode.pushEnable){
bo = pushDataNode.pushSupplier.get();
}else {
log.warn("pushMsg2BuyerWhenSellerChangePrice not match condition ,seller {} sku {}",
sellerUid, sog.getStorageId());
}
return bo;
};
//uid not used in push method of baseNoticeFacade
newNotice(sellerUid).withLogPrefix("pushMsg2BuyerWhenSellerChangePrice")
.needSendPush(false)
.needSendSms(false)
.withPush(supplier).send();
}
/**
... ... @@ -233,7 +259,15 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
Integer productId = soldProduct.getProductId();
Product product = productMapper.selectByPrimaryKey(productId);
//
PushDataNode pushDataNode = buildPushDataNode(soldProduct, product);
Supplier<SendMessageRspBo> pushSupplier = ()->{
PushDataNode pushDataNode = buildPushDataNode(soldProduct, product);
SendMessageRspBo bo = null;
if (pushDataNode.pushEnable){
bo = pushDataNode.pushSupplier.get();
}
return bo;
};
newNotice(sellerUid)
.withLogPrefix("notice seller sale shelf")
.withInBox(() -> {
... ... @@ -242,7 +276,7 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
String goodsTypeType = buildGoodsTypeTagForSeller(skupAttributes);
return buildInboxContent(InboxBusinessTypeEnum.SALE_SHELF, goodsTypeType, prdName, sizeName, productCode);
})
.withPushIf(pushDataNode.pushEnable, pushDataNode.pushSupplier)
.withPush(pushSupplier)
.send();
} catch (Exception e) {
log.warn("notice seller sale shelf fail, sellerUid {}, prdName {} ,sizeName {} ", sellerUid, prdName, sizeName, e);
... ... @@ -1034,4 +1068,11 @@ public class SellerNoticeFacade extends BaseNoticeFacade {
}
}
public void pushSellerWhenBidOrderPaid(Integer sellerUid,Supplier<SendMessageRspBo> pushSupplier){
newNotice(sellerUid).withLogPrefix("pushSellerWhenBidOrderPaid").needSendSms(false)
.needSendInbox(false).withPush(pushSupplier)
.send();
}
}
... ...