Authored by LUOXC

refactor

... ... @@ -22,12 +22,16 @@ import com.yohoufo.order.model.request.PaymentRequest;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.service.impl.function.SellerNoticeSender;
import com.yohoufo.order.service.proxy.OrderStatusFlowService;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
... ... @@ -40,8 +44,6 @@ class BuyerOrderCancelHandler {
private final int uid;
private final long orderCode;
private int skup;
private SellerOrderGoods sellerOrderGoods;
private OrderStatus expectStatus;
... ... @@ -56,16 +58,12 @@ class BuyerOrderCancelHandler {
private TransferCase transferCase;
private SellerWalletDetail.Type sellerWalletDetailType;
private BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> goodsMoneyRefundConsumer;
private RefundCase goodsMoneyRefundCase;
private RefundGoodsMoney refundGoodsMoney;
private SellerNoticeSender sellerNoticeSender;
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
private int sellerNoticeSenderTimes;
private NoticeSeller noticeSeller;
private BuyerRefundCouponEvent.BizCase refundCouponCase;
private AlarmConfig failAlarm;
BuyerOrderCancelHandler(int uid, long orderCode) {
... ... @@ -73,16 +71,6 @@ class BuyerOrderCancelHandler {
this.orderCode = orderCode;
}
public BuyerOrderCancelHandler withSkup(int skup) {
this.skup = skup;
return this;
}
public BuyerOrderCancelHandler withSellerOrderGoods(SellerOrderGoods sellerOrderGoods) {
this.sellerOrderGoods = sellerOrderGoods;
return this;
}
public BuyerOrderCancelHandler withBuyerOrderSupplier(BuyerOrderMapper buyerOrderMapper, BuyerOrderGoodsMapper buyerOrderGoodsMapper) {
this.buyerOrderMapper = buyerOrderMapper;
this.buyerOrderGoodsMapper = buyerOrderGoodsMapper;
... ... @@ -107,17 +95,12 @@ class BuyerOrderCancelHandler {
return this;
}
public BuyerOrderCancelHandler withRefundGoodsMoney(BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer, RefundCase refundCase) {
this.goodsMoneyRefundConsumer = refundConsumer;
this.goodsMoneyRefundCase = refundCase;
return this;
public RefundGoodsMoney withRefundGoodsMoney(BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> goodsMoneyRefundConsumer) {
return new RefundGoodsMoney(goodsMoneyRefundConsumer);
}
public BuyerOrderCancelHandler withNoticeSeller(SellerNoticeSender sellerNoticeSender, int sellerNoticeSenderTimes, SellerOrderGoodsMapper sellerOrderGoodsMapper) {
this.sellerNoticeSender = sellerNoticeSender;
this.sellerNoticeSenderTimes = sellerNoticeSenderTimes;
this.sellerOrderGoodsMapper = sellerOrderGoodsMapper;
return this;
public NoticeSeller withNoticeSeller(SellerNoticeSender sellerNoticeSender, int sellerNoticeSenderTimes) {
return new NoticeSeller(sellerNoticeSender, sellerNoticeSenderTimes);
}
public BuyerOrderCancelHandler withRefundCoupon(BuyerRefundCouponEvent.BizCase refundCouponCase) {
... ... @@ -208,13 +191,13 @@ class BuyerOrderCancelHandler {
}
private void refundPayGoodsMoney(BuyerOrder buyerOrder) {
if (Objects.isNull(goodsMoneyRefundConsumer)) {
if (Objects.isNull(refundGoodsMoney)) {
return;
}
BigDecimal goodsMoney = buyerOrder.getAmount();
val paymentRequest = PaymentRequest.builder()
.uid(uid)
.refundCase(goodsMoneyRefundCase)
.refundCase(refundGoodsMoney.refundCase())
.orderCode(orderCode)
.refundAmount(goodsMoney.doubleValue())
.build();
... ... @@ -223,11 +206,13 @@ class BuyerOrderCancelHandler {
.sellerUid(buyerOrder.getSellerUid())
.orderCode(orderCode)
.payType(buyerOrder.getPayment())
.refundCase(goodsMoneyRefundCase)
.refundCase(refundGoodsMoney.refundCase())
.amount(goodsMoney);
try {
log.info("refund pay goods money by order {} request is {} ", orderCode, paymentRequest);
goodsMoneyRefundConsumer.accept(paymentRequest, billLogEventBuilder.skup(getSkup()));
billLogEventBuilder.skup(Optional.ofNullable(refundGoodsMoney.skup()).orElseGet(skupSupplier()));
refundGoodsMoney.refundConsumer().accept(paymentRequest, billLogEventBuilder);
log.info("refund pay goods money by order {} success", orderCode);
} catch (Exception e) {
log.info("refund pay goods money by order {} fail request is {}", orderCode, paymentRequest, e);
... ... @@ -246,29 +231,64 @@ class BuyerOrderCancelHandler {
}
private void noticeSeller() {
if (Objects.nonNull(sellerNoticeSender)) {
if (Objects.nonNull(noticeSeller)) {
try {
log.info("notice seller buyer order has closed by order {}", orderCode);
SellerOrderGoods sog = getSellerOrderGoods();
sellerNoticeSender.notice(sog, orderCode, sellerNoticeSenderTimes);
int times = noticeSeller.times();
SellerOrderGoods sog = noticeSeller.sellerOrderGoods();
if (Objects.isNull(sog)) {
val skup = skupSupplier().get();
val sellerOrderGoodsMapper = noticeSeller.sellerOrderGoodsMapper();
sog = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
}
noticeSeller.sellerNoticeSender().notice(sog, orderCode, times);
} catch (Exception e) {
log.info("notice seller buyer order has closed by order {} fail", orderCode, e);
}
}
}
private SellerOrderGoods getSellerOrderGoods() {
if (Objects.isNull(sellerOrderGoods)) {
sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(getSkup());
private Integer skupCache;
private Supplier<Integer> skupSupplier() {
return () -> {
if (Objects.isNull(skupCache)) {
skupCache = buyerOrderGoodsMapper.selectByOrderCode(uid, orderCode).getSkup();
}
return sellerOrderGoods;
return skupCache;
};
}
private int getSkup() {
if (skup <= 0) {
skup = buyerOrderGoodsMapper.selectByOrderCode(uid, orderCode).getSkup();
@Data
@Accessors(fluent = true)
class RefundGoodsMoney {
private final BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer;
private RefundCase refundCase;
private Integer skup;
BuyerOrderCancelHandler and() {
BuyerOrderCancelHandler.this.refundGoodsMoney = this;
return BuyerOrderCancelHandler.this;
}
}
@Data
@Accessors(fluent = true)
class NoticeSeller {
private final SellerNoticeSender sellerNoticeSender;
private final int times;
private SellerOrderGoods sellerOrderGoods;
private SellerOrderGoodsMapper sellerOrderGoodsMapper;
BuyerOrderCancelHandler and() {
BuyerOrderCancelHandler.this.noticeSeller = this;
return BuyerOrderCancelHandler.this;
}
return skup;
}
... ...
... ... @@ -150,18 +150,16 @@ public class BuyerOrderCancelService {
public void cancelForSellerSendOutTimeout(int uid, long orderCode,int skup, SellerOrderGoods sellerOrderGoods){
new BuyerOrderCancelHandler(uid, orderCode)
.withSkup(skup)
.withSellerOrderGoods(sellerOrderGoods)
.withBuyerOrderSupplier(buyerOrderMapper, buyerOrderGoodsMapper)
// 已付款 -> 超时未发货取消
.withStateTransition(OrderStatus.HAS_PAYED, OrderStatus.SEND_OUT_TIMEOUT, orderStatusFlowService)
// 保证金分账给平台和买家
.withTransfer(transferService, TransferCase.SELLER_PLAY_BUYER.getCode(), TransferCase.DELIVER_TIME_OUT, SellerWalletDetail.Type.SELLER_OVER_TIME)
// 退买家货款
.withRefundGoodsMoney(payRefundService::refund, RefundCase.BUYER_GOODS_MONEY)
.withRefundGoodsMoney(payRefundService::refund).refundCase(RefundCase.BUYER_GOODS_MONEY).skup(skup).and()
.withFailAlarm(AlarmConfig.DELIVER_TIME_OUT)
// 通知卖家商品发货超时
.withNoticeSeller(inBoxFacade::sellerDeliverNotice, 3, sellerOrderGoodsMapper)
.withNoticeSeller(inBoxFacade::sellerDeliverNotice, 3).sellerOrderGoods(sellerOrderGoods).and()
// 退优惠券
.withRefundCoupon(BuyerRefundCouponEvent.BizCase.SELLER_DELIVER_TIMEOUT)
.cancel();
... ... @@ -175,9 +173,9 @@ public class BuyerOrderCancelService {
// 保证金分账给平台和买家
.withTransfer(transferService, TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE.getCode(), TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE, SellerWalletDetail.Type.CS_CANCEL_BEFORE_DEPOT_RECEIVE)
// 退买家货款
.withRefundGoodsMoney(payRefundService::refund, RefundCase.BUYER_GOODS_MONEY)
.withRefundGoodsMoney(payRefundService::refund).refundCase(RefundCase.BUYER_GOODS_MONEY).and()
// 通知卖家涉及虚假发货
.withNoticeSeller(inBoxFacade::sellerDeliverNotice, 4, sellerOrderGoodsMapper)
.withNoticeSeller(inBoxFacade::sellerDeliverNotice, 4).sellerOrderGoodsMapper(sellerOrderGoodsMapper).and()
// 退优惠券
.withRefundCoupon(BuyerRefundCouponEvent.BizCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE)
.withFailAlarm(AlarmConfig.CS_CANCEL_BEFORE_DEPOT_RECEIVE)
... ...