|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.base.Verify;
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.order.common.OrderStatus;
|
|
|
import com.yohobuy.ufo.model.order.common.TabType;
|
|
|
import com.yohoufo.common.alarm.EventBusPublisher;
|
...
|
...
|
@@ -39,7 +42,7 @@ import java.util.List; |
|
|
import java.util.Objects;
|
|
|
import java.util.function.BiConsumer;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Supplier;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
|
|
|
|
...
|
...
|
@@ -70,6 +73,8 @@ class BuyerOrderCancelHandler { |
|
|
|
|
|
private RefundGoodsMoney refundGoodsMoney;
|
|
|
|
|
|
private RefundEarnestMoney refundEarnestMoney;
|
|
|
|
|
|
private NoticeSeller noticeSeller;
|
|
|
|
|
|
private NoticeBuyer noticeBuyer;
|
...
|
...
|
@@ -115,6 +120,10 @@ class BuyerOrderCancelHandler { |
|
|
return new RefundGoodsMoney(goodsMoneyRefundConsumer);
|
|
|
}
|
|
|
|
|
|
public RefundEarnestMoney withRefundEarnestMoney(BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> goodsMoneyRefundConsumer, Function<Integer, SellerOrder> sellerOrderFunction) {
|
|
|
return new RefundEarnestMoney(goodsMoneyRefundConsumer, sellerOrderFunction);
|
|
|
}
|
|
|
|
|
|
public NoticeSeller withNoticeSeller(SellerNoticeSender sellerNoticeSender) {
|
|
|
return new NoticeSeller(sellerNoticeSender);
|
|
|
}
|
...
|
...
|
@@ -169,6 +178,7 @@ class BuyerOrderCancelHandler { |
|
|
refundCoupon();
|
|
|
transferEarnestMoney2Buyer(buyerOrderGoods.getSkup());
|
|
|
refundPayGoodsMoney(buyerOrder, buyerOrderGoods);
|
|
|
refundEarnestMoney(buyerOrderGoods.getSkup());
|
|
|
noticeSeller(sellerOrderGoods);
|
|
|
noticeBuyer(buyerOrder);
|
|
|
cleanCache(buyerOrder, sellerOrderGoods);
|
...
|
...
|
@@ -219,11 +229,11 @@ class BuyerOrderCancelHandler { |
|
|
if (Objects.isNull(transferService)) {
|
|
|
return;
|
|
|
}
|
|
|
SellerOrderMeta feeMeta = sellerOrderMetaMapper.selectByMetaKey(sellerUid,skup,"fee");
|
|
|
SellerOrderMeta feeMeta = sellerOrderMetaMapper.selectByMetaKey(sellerUid, skup, "fee");
|
|
|
PenaltyResult penaltyResult = SellerEarnestMoney2BuyerPenaltyCalculator.from(feeMeta)
|
|
|
.calculate()
|
|
|
.orElse(null);
|
|
|
if(Objects.nonNull(penaltyResult)){
|
|
|
if (Objects.nonNull(penaltyResult)) {
|
|
|
TranseferCellNode transeferCellNode = new TranseferCellNode();
|
|
|
transeferCellNode.setUid(uid);
|
|
|
transeferCellNode.setAmount(penaltyResult.getPenaltyAmountWithoutYhServiceAmount());
|
...
|
...
|
@@ -282,6 +292,72 @@ class BuyerOrderCancelHandler { |
|
|
}
|
|
|
}
|
|
|
|
|
|
private void refundEarnestMoney(Integer skup) {
|
|
|
if (Objects.isNull(refundEarnestMoney)) {
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
SellerOrderMeta feeMeta = sellerOrderMetaMapper.selectByMetaKey(sellerUid, skup, "fee");
|
|
|
BigDecimal earnestMoney = optEarnestMoney(feeMeta);
|
|
|
if (Objects.isNull(earnestMoney)) {
|
|
|
return;
|
|
|
}
|
|
|
SellerOrder sellerOrder = refundEarnestMoney.sellerOrderFunction().apply(skup);
|
|
|
val paymentRequest = PaymentRequest.builder()
|
|
|
.uid(sellerOrder.getUid())
|
|
|
.refundCase(refundEarnestMoney.refundCase())
|
|
|
.orderCode(sellerOrder.getOrderCode())
|
|
|
.refundAmount(earnestMoney.doubleValue())
|
|
|
.build();
|
|
|
val billLogEventBuilder = BillLogEvent.builder()
|
|
|
.buyerUid(uid)
|
|
|
.sellerUid(sellerOrder.getUid())
|
|
|
.orderCode(sellerOrder.getOrderCode())
|
|
|
.payType(sellerOrder.getPayment())
|
|
|
.refundCase(refundEarnestMoney.refundCase())
|
|
|
.amount(earnestMoney)
|
|
|
.skup(skup);
|
|
|
|
|
|
log.info("refund earnest money by order {} request is {} ", orderCode, paymentRequest);
|
|
|
refundGoodsMoney.refundConsumer().accept(paymentRequest, billLogEventBuilder);
|
|
|
log.info("refund earnest money by order {} success", orderCode);
|
|
|
} catch (Exception e) {
|
|
|
log.warn("refund earnest money by order {} fail", orderCode, e);
|
|
|
if (Objects.nonNull(failAlarm)) {
|
|
|
SmsAlarmEvent smsAlarmEvent = new SmsAlarmEvent(
|
|
|
AlarmConfig.REFUND_EARNEST_MONEY_NAME,
|
|
|
failAlarm.name(),
|
|
|
expectStatus.getDesc() + ",取消订单" + orderCode + "退保证金失败"
|
|
|
);
|
|
|
EventBusPublisher.publishEvent(smsAlarmEvent);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private BigDecimal optEarnestMoney(SellerOrderMeta feeMeta) {
|
|
|
if (feeMeta == null) {
|
|
|
log.warn("feeMeta is null");
|
|
|
throw new ServiceException(500, "计费信息不完整");
|
|
|
}
|
|
|
JSONObject fee;
|
|
|
try {
|
|
|
fee = JSON.parseObject(feeMeta.getMetaValue());
|
|
|
} catch (Exception e) {
|
|
|
log.warn("feeMeta value parse err, uid is {}, skup is {}", feeMeta.getUid(), feeMeta.getSkup(), e);
|
|
|
return null;
|
|
|
}
|
|
|
if (Objects.isNull(fee)) {
|
|
|
log.warn("feeMeta value is null, uid is {}, skup is {}", feeMeta.getUid(), feeMeta.getSkup());
|
|
|
return null;
|
|
|
}
|
|
|
val ensure = fee.getJSONObject("earnestMoney").getBigDecimal("earnestMoney");
|
|
|
if (Objects.isNull(ensure) || ensure.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
log.warn("feeMeta info err, uid is {}, skup is {}, ensure is {}", uid, feeMeta.getSkup(), ensure);
|
|
|
return null;
|
|
|
}
|
|
|
return ensure;
|
|
|
}
|
|
|
|
|
|
private void noticeSeller(SellerOrderGoods sellerOrderGoods) {
|
|
|
if (Objects.nonNull(noticeSeller)) {
|
|
|
try {
|
...
|
...
|
@@ -326,6 +402,21 @@ class BuyerOrderCancelHandler { |
|
|
|
|
|
@Data
|
|
|
@Accessors(fluent = true)
|
|
|
class RefundEarnestMoney {
|
|
|
|
|
|
private final BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer;
|
|
|
private final Function<Integer, SellerOrder> sellerOrderFunction;
|
|
|
private RefundCase refundCase;
|
|
|
|
|
|
BuyerOrderCancelHandler and() {
|
|
|
BuyerOrderCancelHandler.this.refundEarnestMoney = this;
|
|
|
return BuyerOrderCancelHandler.this;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
@Data
|
|
|
@Accessors(fluent = true)
|
|
|
class RefundGoodsMoney {
|
|
|
|
|
|
private final BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer;
|
...
|
...
|
|