Authored by LUOXC

refactor

@@ -22,12 +22,16 @@ import com.yohoufo.order.model.request.PaymentRequest; @@ -22,12 +22,16 @@ import com.yohoufo.order.model.request.PaymentRequest;
22 import com.yohoufo.order.model.request.TransferMoneyRequest; 22 import com.yohoufo.order.model.request.TransferMoneyRequest;
23 import com.yohoufo.order.service.impl.function.SellerNoticeSender; 23 import com.yohoufo.order.service.impl.function.SellerNoticeSender;
24 import com.yohoufo.order.service.proxy.OrderStatusFlowService; 24 import com.yohoufo.order.service.proxy.OrderStatusFlowService;
  25 +import lombok.Data;
  26 +import lombok.experimental.Accessors;
25 import lombok.extern.slf4j.Slf4j; 27 import lombok.extern.slf4j.Slf4j;
26 import lombok.val; 28 import lombok.val;
27 29
28 import java.math.BigDecimal; 30 import java.math.BigDecimal;
29 import java.util.Objects; 31 import java.util.Objects;
  32 +import java.util.Optional;
30 import java.util.function.BiConsumer; 33 import java.util.function.BiConsumer;
  34 +import java.util.function.Supplier;
31 35
32 import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException; 36 import static com.yohoufo.order.utils.ServiceExceptions.throwServiceException;
33 37
@@ -40,8 +44,6 @@ class BuyerOrderCancelHandler { @@ -40,8 +44,6 @@ class BuyerOrderCancelHandler {
40 44
41 private final int uid; 45 private final int uid;
42 private final long orderCode; 46 private final long orderCode;
43 - private int skup;  
44 - private SellerOrderGoods sellerOrderGoods;  
45 47
46 48
47 private OrderStatus expectStatus; 49 private OrderStatus expectStatus;
@@ -56,16 +58,12 @@ class BuyerOrderCancelHandler { @@ -56,16 +58,12 @@ class BuyerOrderCancelHandler {
56 private TransferCase transferCase; 58 private TransferCase transferCase;
57 private SellerWalletDetail.Type sellerWalletDetailType; 59 private SellerWalletDetail.Type sellerWalletDetailType;
58 60
59 - private BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> goodsMoneyRefundConsumer;  
60 - private RefundCase goodsMoneyRefundCase; 61 + private RefundGoodsMoney refundGoodsMoney;
61 62
62 - private SellerNoticeSender sellerNoticeSender;  
63 - private SellerOrderGoodsMapper sellerOrderGoodsMapper;  
64 - private int sellerNoticeSenderTimes; 63 + private NoticeSeller noticeSeller;
65 64
66 private BuyerRefundCouponEvent.BizCase refundCouponCase; 65 private BuyerRefundCouponEvent.BizCase refundCouponCase;
67 66
68 -  
69 private AlarmConfig failAlarm; 67 private AlarmConfig failAlarm;
70 68
71 BuyerOrderCancelHandler(int uid, long orderCode) { 69 BuyerOrderCancelHandler(int uid, long orderCode) {
@@ -73,16 +71,6 @@ class BuyerOrderCancelHandler { @@ -73,16 +71,6 @@ class BuyerOrderCancelHandler {
73 this.orderCode = orderCode; 71 this.orderCode = orderCode;
74 } 72 }
75 73
76 - public BuyerOrderCancelHandler withSkup(int skup) {  
77 - this.skup = skup;  
78 - return this;  
79 - }  
80 -  
81 - public BuyerOrderCancelHandler withSellerOrderGoods(SellerOrderGoods sellerOrderGoods) {  
82 - this.sellerOrderGoods = sellerOrderGoods;  
83 - return this;  
84 - }  
85 -  
86 public BuyerOrderCancelHandler withBuyerOrderSupplier(BuyerOrderMapper buyerOrderMapper, BuyerOrderGoodsMapper buyerOrderGoodsMapper) { 74 public BuyerOrderCancelHandler withBuyerOrderSupplier(BuyerOrderMapper buyerOrderMapper, BuyerOrderGoodsMapper buyerOrderGoodsMapper) {
87 this.buyerOrderMapper = buyerOrderMapper; 75 this.buyerOrderMapper = buyerOrderMapper;
88 this.buyerOrderGoodsMapper = buyerOrderGoodsMapper; 76 this.buyerOrderGoodsMapper = buyerOrderGoodsMapper;
@@ -107,17 +95,12 @@ class BuyerOrderCancelHandler { @@ -107,17 +95,12 @@ class BuyerOrderCancelHandler {
107 return this; 95 return this;
108 } 96 }
109 97
110 - public BuyerOrderCancelHandler withRefundGoodsMoney(BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer, RefundCase refundCase) {  
111 - this.goodsMoneyRefundConsumer = refundConsumer;  
112 - this.goodsMoneyRefundCase = refundCase;  
113 - return this; 98 + public RefundGoodsMoney withRefundGoodsMoney(BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> goodsMoneyRefundConsumer) {
  99 + return new RefundGoodsMoney(goodsMoneyRefundConsumer);
114 } 100 }
115 101
116 - public BuyerOrderCancelHandler withNoticeSeller(SellerNoticeSender sellerNoticeSender, int sellerNoticeSenderTimes, SellerOrderGoodsMapper sellerOrderGoodsMapper) {  
117 - this.sellerNoticeSender = sellerNoticeSender;  
118 - this.sellerNoticeSenderTimes = sellerNoticeSenderTimes;  
119 - this.sellerOrderGoodsMapper = sellerOrderGoodsMapper;  
120 - return this; 102 + public NoticeSeller withNoticeSeller(SellerNoticeSender sellerNoticeSender, int sellerNoticeSenderTimes) {
  103 + return new NoticeSeller(sellerNoticeSender, sellerNoticeSenderTimes);
121 } 104 }
122 105
123 public BuyerOrderCancelHandler withRefundCoupon(BuyerRefundCouponEvent.BizCase refundCouponCase) { 106 public BuyerOrderCancelHandler withRefundCoupon(BuyerRefundCouponEvent.BizCase refundCouponCase) {
@@ -208,13 +191,13 @@ class BuyerOrderCancelHandler { @@ -208,13 +191,13 @@ class BuyerOrderCancelHandler {
208 } 191 }
209 192
210 private void refundPayGoodsMoney(BuyerOrder buyerOrder) { 193 private void refundPayGoodsMoney(BuyerOrder buyerOrder) {
211 - if (Objects.isNull(goodsMoneyRefundConsumer)) { 194 + if (Objects.isNull(refundGoodsMoney)) {
212 return; 195 return;
213 } 196 }
214 BigDecimal goodsMoney = buyerOrder.getAmount(); 197 BigDecimal goodsMoney = buyerOrder.getAmount();
215 val paymentRequest = PaymentRequest.builder() 198 val paymentRequest = PaymentRequest.builder()
216 .uid(uid) 199 .uid(uid)
217 - .refundCase(goodsMoneyRefundCase) 200 + .refundCase(refundGoodsMoney.refundCase())
218 .orderCode(orderCode) 201 .orderCode(orderCode)
219 .refundAmount(goodsMoney.doubleValue()) 202 .refundAmount(goodsMoney.doubleValue())
220 .build(); 203 .build();
@@ -223,11 +206,13 @@ class BuyerOrderCancelHandler { @@ -223,11 +206,13 @@ class BuyerOrderCancelHandler {
223 .sellerUid(buyerOrder.getSellerUid()) 206 .sellerUid(buyerOrder.getSellerUid())
224 .orderCode(orderCode) 207 .orderCode(orderCode)
225 .payType(buyerOrder.getPayment()) 208 .payType(buyerOrder.getPayment())
226 - .refundCase(goodsMoneyRefundCase) 209 + .refundCase(refundGoodsMoney.refundCase())
227 .amount(goodsMoney); 210 .amount(goodsMoney);
228 try { 211 try {
  212 +
229 log.info("refund pay goods money by order {} request is {} ", orderCode, paymentRequest); 213 log.info("refund pay goods money by order {} request is {} ", orderCode, paymentRequest);
230 - goodsMoneyRefundConsumer.accept(paymentRequest, billLogEventBuilder.skup(getSkup())); 214 + billLogEventBuilder.skup(Optional.ofNullable(refundGoodsMoney.skup()).orElseGet(skupSupplier()));
  215 + refundGoodsMoney.refundConsumer().accept(paymentRequest, billLogEventBuilder);
231 log.info("refund pay goods money by order {} success", orderCode); 216 log.info("refund pay goods money by order {} success", orderCode);
232 } catch (Exception e) { 217 } catch (Exception e) {
233 log.info("refund pay goods money by order {} fail request is {}", orderCode, paymentRequest, e); 218 log.info("refund pay goods money by order {} fail request is {}", orderCode, paymentRequest, e);
@@ -246,29 +231,64 @@ class BuyerOrderCancelHandler { @@ -246,29 +231,64 @@ class BuyerOrderCancelHandler {
246 } 231 }
247 232
248 private void noticeSeller() { 233 private void noticeSeller() {
249 - if (Objects.nonNull(sellerNoticeSender)) { 234 + if (Objects.nonNull(noticeSeller)) {
250 try { 235 try {
251 log.info("notice seller buyer order has closed by order {}", orderCode); 236 log.info("notice seller buyer order has closed by order {}", orderCode);
252 - SellerOrderGoods sog = getSellerOrderGoods();  
253 - sellerNoticeSender.notice(sog, orderCode, sellerNoticeSenderTimes); 237 + int times = noticeSeller.times();
  238 + SellerOrderGoods sog = noticeSeller.sellerOrderGoods();
  239 + if (Objects.isNull(sog)) {
  240 + val skup = skupSupplier().get();
  241 + val sellerOrderGoodsMapper = noticeSeller.sellerOrderGoodsMapper();
  242 + sog = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
  243 + }
  244 + noticeSeller.sellerNoticeSender().notice(sog, orderCode, times);
254 } catch (Exception e) { 245 } catch (Exception e) {
255 log.info("notice seller buyer order has closed by order {} fail", orderCode, e); 246 log.info("notice seller buyer order has closed by order {} fail", orderCode, e);
256 } 247 }
257 } 248 }
258 } 249 }
259 250
260 - private SellerOrderGoods getSellerOrderGoods() {  
261 - if (Objects.isNull(sellerOrderGoods)) {  
262 - sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(getSkup()); 251 + private Integer skupCache;
  252 +
  253 + private Supplier<Integer> skupSupplier() {
  254 + return () -> {
  255 + if (Objects.isNull(skupCache)) {
  256 + skupCache = buyerOrderGoodsMapper.selectByOrderCode(uid, orderCode).getSkup();
263 } 257 }
264 - return sellerOrderGoods; 258 + return skupCache;
  259 + };
265 } 260 }
266 261
267 - private int getSkup() {  
268 - if (skup <= 0) {  
269 - skup = buyerOrderGoodsMapper.selectByOrderCode(uid, orderCode).getSkup(); 262 + @Data
  263 + @Accessors(fluent = true)
  264 + class RefundGoodsMoney {
  265 +
  266 + private final BiConsumer<PaymentRequest, BillLogEvent.BillLogEventBuilder> refundConsumer;
  267 + private RefundCase refundCase;
  268 + private Integer skup;
  269 +
  270 + BuyerOrderCancelHandler and() {
  271 + BuyerOrderCancelHandler.this.refundGoodsMoney = this;
  272 + return BuyerOrderCancelHandler.this;
  273 + }
  274 +
  275 + }
  276 +
  277 + @Data
  278 + @Accessors(fluent = true)
  279 + class NoticeSeller {
  280 +
  281 + private final SellerNoticeSender sellerNoticeSender;
  282 + private final int times;
  283 +
  284 + private SellerOrderGoods sellerOrderGoods;
  285 +
  286 + private SellerOrderGoodsMapper sellerOrderGoodsMapper;
  287 +
  288 + BuyerOrderCancelHandler and() {
  289 + BuyerOrderCancelHandler.this.noticeSeller = this;
  290 + return BuyerOrderCancelHandler.this;
270 } 291 }
271 - return skup;  
272 } 292 }
273 293
274 294
@@ -150,18 +150,16 @@ public class BuyerOrderCancelService { @@ -150,18 +150,16 @@ public class BuyerOrderCancelService {
150 150
151 public void cancelForSellerSendOutTimeout(int uid, long orderCode,int skup, SellerOrderGoods sellerOrderGoods){ 151 public void cancelForSellerSendOutTimeout(int uid, long orderCode,int skup, SellerOrderGoods sellerOrderGoods){
152 new BuyerOrderCancelHandler(uid, orderCode) 152 new BuyerOrderCancelHandler(uid, orderCode)
153 - .withSkup(skup)  
154 - .withSellerOrderGoods(sellerOrderGoods)  
155 .withBuyerOrderSupplier(buyerOrderMapper, buyerOrderGoodsMapper) 153 .withBuyerOrderSupplier(buyerOrderMapper, buyerOrderGoodsMapper)
156 // 已付款 -> 超时未发货取消 154 // 已付款 -> 超时未发货取消
157 .withStateTransition(OrderStatus.HAS_PAYED, OrderStatus.SEND_OUT_TIMEOUT, orderStatusFlowService) 155 .withStateTransition(OrderStatus.HAS_PAYED, OrderStatus.SEND_OUT_TIMEOUT, orderStatusFlowService)
158 // 保证金分账给平台和买家 156 // 保证金分账给平台和买家
159 .withTransfer(transferService, TransferCase.SELLER_PLAY_BUYER.getCode(), TransferCase.DELIVER_TIME_OUT, SellerWalletDetail.Type.SELLER_OVER_TIME) 157 .withTransfer(transferService, TransferCase.SELLER_PLAY_BUYER.getCode(), TransferCase.DELIVER_TIME_OUT, SellerWalletDetail.Type.SELLER_OVER_TIME)
160 // 退买家货款 158 // 退买家货款
161 - .withRefundGoodsMoney(payRefundService::refund, RefundCase.BUYER_GOODS_MONEY) 159 + .withRefundGoodsMoney(payRefundService::refund).refundCase(RefundCase.BUYER_GOODS_MONEY).skup(skup).and()
162 .withFailAlarm(AlarmConfig.DELIVER_TIME_OUT) 160 .withFailAlarm(AlarmConfig.DELIVER_TIME_OUT)
163 // 通知卖家商品发货超时 161 // 通知卖家商品发货超时
164 - .withNoticeSeller(inBoxFacade::sellerDeliverNotice, 3, sellerOrderGoodsMapper) 162 + .withNoticeSeller(inBoxFacade::sellerDeliverNotice, 3).sellerOrderGoods(sellerOrderGoods).and()
165 // 退优惠券 163 // 退优惠券
166 .withRefundCoupon(BuyerRefundCouponEvent.BizCase.SELLER_DELIVER_TIMEOUT) 164 .withRefundCoupon(BuyerRefundCouponEvent.BizCase.SELLER_DELIVER_TIMEOUT)
167 .cancel(); 165 .cancel();
@@ -175,9 +173,9 @@ public class BuyerOrderCancelService { @@ -175,9 +173,9 @@ public class BuyerOrderCancelService {
175 // 保证金分账给平台和买家 173 // 保证金分账给平台和买家
176 .withTransfer(transferService, TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE.getCode(), TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE, SellerWalletDetail.Type.CS_CANCEL_BEFORE_DEPOT_RECEIVE) 174 .withTransfer(transferService, TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE.getCode(), TransferCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE, SellerWalletDetail.Type.CS_CANCEL_BEFORE_DEPOT_RECEIVE)
177 // 退买家货款 175 // 退买家货款
178 - .withRefundGoodsMoney(payRefundService::refund, RefundCase.BUYER_GOODS_MONEY) 176 + .withRefundGoodsMoney(payRefundService::refund).refundCase(RefundCase.BUYER_GOODS_MONEY).and()
179 // 通知卖家涉及虚假发货 177 // 通知卖家涉及虚假发货
180 - .withNoticeSeller(inBoxFacade::sellerDeliverNotice, 4, sellerOrderGoodsMapper) 178 + .withNoticeSeller(inBoxFacade::sellerDeliverNotice, 4).sellerOrderGoodsMapper(sellerOrderGoodsMapper).and()
181 // 退优惠券 179 // 退优惠券
182 .withRefundCoupon(BuyerRefundCouponEvent.BizCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE) 180 .withRefundCoupon(BuyerRefundCouponEvent.BizCase.CS_CANCEL_BEFORE_DEPOT_RECEIVE)
183 .withFailAlarm(AlarmConfig.CS_CANCEL_BEFORE_DEPOT_RECEIVE) 181 .withFailAlarm(AlarmConfig.CS_CANCEL_BEFORE_DEPOT_RECEIVE)