|
|
1
|
+package com.yohoufo.order.service;
|
|
|
2
|
+
|
|
|
3
|
+import com.alibaba.fastjson.JSON;
|
|
|
4
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
5
|
+import com.yoho.core.common.utils.DateUtil;
|
|
|
6
|
+import com.yoho.core.dal.datasource.annotation.Database;
|
|
|
7
|
+import com.yoho.error.ServiceError;
|
|
|
8
|
+import com.yoho.error.exception.ServiceException;
|
|
|
9
|
+import com.yohobuy.ufo.model.order.bo.OrderInfo;
|
|
|
10
|
+import com.yohobuy.ufo.model.order.common.OrderCodeType;
|
|
|
11
|
+import com.yohobuy.ufo.model.order.common.SellerOrderStatus;
|
|
|
12
|
+import com.yohobuy.ufo.model.order.common.SkupStatus;
|
|
|
13
|
+import com.yohoufo.common.alarm.EventBusPublisher;
|
|
|
14
|
+import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
15
|
+import com.yohoufo.dal.order.SellerOrderMapper;
|
|
|
16
|
+import com.yohoufo.dal.order.SellerOrderMetaMapper;
|
|
|
17
|
+import com.yohoufo.dal.order.model.SellerOrder;
|
|
|
18
|
+import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
19
|
+import com.yohoufo.dal.order.model.SellerOrderMeta;
|
|
|
20
|
+import com.yohoufo.order.common.ActionStatusHold;
|
|
|
21
|
+import com.yohoufo.order.constants.MetaKey;
|
|
|
22
|
+import com.yohoufo.order.event.SellerBidOrderPaySuccessEvent;
|
|
|
23
|
+import com.yohoufo.order.model.request.PaymentRequest;
|
|
|
24
|
+import com.yohoufo.order.model.response.PrepayResponse;
|
|
|
25
|
+import com.yohoufo.order.mq.DelayTime;
|
|
|
26
|
+import com.yohoufo.order.service.cache.CacheCleaner;
|
|
|
27
|
+import com.yohoufo.order.service.impl.TradeBillsService;
|
|
|
28
|
+import com.yohoufo.order.service.proxy.SellerNoticeFacade;
|
|
|
29
|
+import com.yohoufo.order.service.seller.SellerGoodsStatusFlowService;
|
|
|
30
|
+import com.yohoufo.order.utils.LoggerUtils;
|
|
|
31
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
32
|
+import org.slf4j.Logger;
|
|
|
33
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
34
|
+import org.springframework.stereotype.Service;
|
|
|
35
|
+
|
|
|
36
|
+import java.util.Objects;
|
|
|
37
|
+
|
|
|
38
|
+@Service
|
|
|
39
|
+public class SellerBidOrderPaymentService extends AbstractOrderPaymentService {
|
|
|
40
|
+ private final Logger logger = LoggerUtils.getSellerOrderLogger();
|
|
|
41
|
+
|
|
|
42
|
+ @Autowired
|
|
|
43
|
+ SellerOrderMapper sellerOrderMapper;
|
|
|
44
|
+
|
|
|
45
|
+ @Autowired
|
|
|
46
|
+ private SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
47
|
+
|
|
|
48
|
+ @Autowired
|
|
|
49
|
+ SellerOrderMetaMapper sellerOrderMetaMapper;
|
|
|
50
|
+
|
|
|
51
|
+ @Autowired
|
|
|
52
|
+ private CacheCleaner cacheCleaner;
|
|
|
53
|
+
|
|
|
54
|
+ @Autowired
|
|
|
55
|
+ private SellerNoticeFacade sellerNoticeFacade;
|
|
|
56
|
+
|
|
|
57
|
+ @Autowired
|
|
|
58
|
+ private SellerGoodsStatusFlowService sellerGoodsStatusFlowService;
|
|
|
59
|
+
|
|
|
60
|
+ @Autowired
|
|
|
61
|
+ private TradeBillsService tradeBillsService;
|
|
|
62
|
+
|
|
|
63
|
+ /**
|
|
|
64
|
+ * 更新订单状态
|
|
|
65
|
+ *
|
|
|
66
|
+ * @param orderInfo
|
|
|
67
|
+ */
|
|
|
68
|
+ @Override
|
|
|
69
|
+ public int updateOrderStatusPaid(OrderInfo orderInfo) {
|
|
|
70
|
+ //
|
|
|
71
|
+ logger.info("in update seller OrderStatus after Paid, orderInfo {}", orderInfo);
|
|
|
72
|
+ Long orderCode = orderInfo.getOrderCode();
|
|
|
73
|
+
|
|
|
74
|
+ SellerOrder sellerOrder = new SellerOrder();
|
|
|
75
|
+ sellerOrder.setOrderCode(orderCode);
|
|
|
76
|
+ sellerOrder.setUid(orderInfo.getUid());
|
|
|
77
|
+ SellerOrderStatus soTargetStatus = SellerOrderStatus.HAS_PAYED;
|
|
|
78
|
+ sellerOrder.setStatus(soTargetStatus.getCode());
|
|
|
79
|
+
|
|
|
80
|
+ orderInfo.setStatus(soTargetStatus.getCode());
|
|
|
81
|
+ sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
82
|
+ sellerOrder.setExceptStatus(SellerOrderStatus.WAITING_PAY.getCode());
|
|
|
83
|
+ int updateCnt = sellerOrderMapper.updateByOrderCode(sellerOrder);
|
|
|
84
|
+ if (1 == updateCnt) {
|
|
|
85
|
+ //
|
|
|
86
|
+ SellerOrderGoods targetSog = new SellerOrderGoods();
|
|
|
87
|
+ targetSog.setId(orderInfo.getSkup());
|
|
|
88
|
+ targetSog.setStatus(SkupStatus.SELL_OUT.getCode());
|
|
|
89
|
+ sellerOrderGoodsMapper.updateByPrimaryKeySelective(targetSog);
|
|
|
90
|
+ }
|
|
|
91
|
+ return updateCnt;
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+
|
|
|
95
|
+ @Override
|
|
|
96
|
+ public void processAfterPay(OrderInfo orderInfo) {
|
|
|
97
|
+ logger.info("SellerBidOrderPaymentService.processAfterPay in, orderInfo is {}", orderInfo);
|
|
|
98
|
+ //
|
|
|
99
|
+ int sellerUid = orderInfo.getUid();
|
|
|
100
|
+ Long orderCode = orderInfo.getOrderCode();
|
|
|
101
|
+
|
|
|
102
|
+ Integer payment = orderInfo.getPayment();
|
|
|
103
|
+ tradeBillsService.addPayEnsureRecord(orderInfo.getUid(), orderInfo.getSkup(), orderCode, payment,
|
|
|
104
|
+ orderInfo.getAmount());
|
|
|
105
|
+
|
|
|
106
|
+ SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(orderCode);
|
|
|
107
|
+ SellerOrderGoods sellerOrderGoods = null;
|
|
|
108
|
+ if (Objects.nonNull(sellerOrder)) {
|
|
|
109
|
+ sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
|
|
|
110
|
+ String prdName = sellerOrderGoods.getProductName();
|
|
|
111
|
+ String sizeName = sellerOrderGoods.getSizeName();
|
|
|
112
|
+ Integer productId = sellerOrderGoods.getProductId();
|
|
|
113
|
+
|
|
|
114
|
+ //async
|
|
|
115
|
+ SkupStatus skupStatus = SkupStatus.CAN_SELL;
|
|
|
116
|
+ sellerGoodsStatusFlowService.pushStatusFlowEvent(sellerOrderGoods.getId(), skupStatus.getCode());
|
|
|
117
|
+ //卖家bid订单保证金支付完成事件
|
|
|
118
|
+ EventBusPublisher.publishEvent(SellerBidOrderPaySuccessEvent.builder().uid(sellerUid).skup(sellerOrderGoods.getId()).build());
|
|
|
119
|
+
|
|
|
120
|
+ sellerNoticeFacade.saleShelf(sellerUid, prdName, sizeName, productId, sellerOrderGoods.getAttributes());
|
|
|
121
|
+
|
|
|
122
|
+ //
|
|
|
123
|
+ cacheCleaner.cleanSellerOrderDetailAndList(sellerOrderGoods, sellerOrder.getOrderCode());
|
|
|
124
|
+ }
|
|
|
125
|
+ }
|
|
|
126
|
+
|
|
|
127
|
+
|
|
|
128
|
+ /**
|
|
|
129
|
+ * 订单是否是待支付状态
|
|
|
130
|
+ *
|
|
|
131
|
+ * @param orderInfo
|
|
|
132
|
+ * @return
|
|
|
133
|
+ */
|
|
|
134
|
+ public boolean isOrderWaitingPay(OrderInfo orderInfo) {
|
|
|
135
|
+ if (isWaitingPay(orderInfo.getStatus())) {
|
|
|
136
|
+ return true;
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ return false;
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ /**
|
|
|
143
|
+ * 订单是否 已支付 状态
|
|
|
144
|
+ *
|
|
|
145
|
+ * @param orderInfo
|
|
|
146
|
+ * @return
|
|
|
147
|
+ */
|
|
|
148
|
+ public boolean isOrderPaid(OrderInfo orderInfo) {
|
|
|
149
|
+
|
|
|
150
|
+ if (orderInfo.getStatus() == null ||
|
|
|
151
|
+ !Objects.equals(SellerOrderStatus.HAS_PAYED.getCode(), orderInfo.getStatus().intValue())) {
|
|
|
152
|
+ return false;
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ return true;
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+
|
|
|
159
|
+ public boolean canRefund(OrderInfo orderInfo) {
|
|
|
160
|
+ Integer sourceStatus = orderInfo.getSourceStatus();
|
|
|
161
|
+ if (sourceStatus != null &&
|
|
|
162
|
+ ActionStatusHold.getSellerCanRefundEarnestMoneyStatus().contains(sourceStatus.intValue())) {
|
|
|
163
|
+ return true;
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ return false;
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+
|
|
|
170
|
+ /**
|
|
|
171
|
+ * 保存前回预支付结果
|
|
|
172
|
+ *
|
|
|
173
|
+ * @param orderInfo
|
|
|
174
|
+ * @param response
|
|
|
175
|
+ */
|
|
|
176
|
+ public void saveAppPayRequest(OrderInfo orderInfo, PrepayResponse response) {
|
|
|
177
|
+
|
|
|
178
|
+ SellerOrderMeta buyerOrderMeta = new SellerOrderMeta();
|
|
|
179
|
+ buyerOrderMeta.setUid(orderInfo.getUid());
|
|
|
180
|
+ buyerOrderMeta.setSkup(orderInfo.getSkup());
|
|
|
181
|
+ buyerOrderMeta.setMetaKey(MetaKey.APP_PAY_REQUEST);
|
|
|
182
|
+ buyerOrderMeta.setMetaValue(JSONObject.toJSONString(response));
|
|
|
183
|
+
|
|
|
184
|
+ // 记录appPayRequest
|
|
|
185
|
+ sellerOrderMetaMapper.insert(buyerOrderMeta);
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+
|
|
|
189
|
+ /**
|
|
|
190
|
+ * 获取前回的访问结果
|
|
|
191
|
+ *
|
|
|
192
|
+ * @param request
|
|
|
193
|
+ * @return
|
|
|
194
|
+ */
|
|
|
195
|
+ public PrepayResponse getAppPayRequest(PaymentRequest request) {
|
|
|
196
|
+ SellerOrderMeta buyerOrderMeta = sellerOrderMetaMapper.selectByMetaKey(request.getUid(), request.getSkup(), MetaKey.APP_PAY_REQUEST);
|
|
|
197
|
+
|
|
|
198
|
+ if (buyerOrderMeta == null || StringUtils.isEmpty(buyerOrderMeta.getMetaValue())) {
|
|
|
199
|
+ return null;
|
|
|
200
|
+ }
|
|
|
201
|
+ try {
|
|
|
202
|
+ PrepayResponse response = JSON.parseObject(buyerOrderMeta.getMetaValue(), PrepayResponse.class);
|
|
|
203
|
+ return response;
|
|
|
204
|
+ } catch (Exception e) {
|
|
|
205
|
+
|
|
|
206
|
+ }
|
|
|
207
|
+ return null;
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+
|
|
|
211
|
+ /**
|
|
|
212
|
+ * 预支付方式更新
|
|
|
213
|
+ *
|
|
|
214
|
+ * @param orderInfo
|
|
|
215
|
+ */
|
|
|
216
|
+ public void updatePayment(OrderInfo orderInfo, int payment) {
|
|
|
217
|
+
|
|
|
218
|
+ SellerOrder sellerOrder = new SellerOrder();
|
|
|
219
|
+ // 更新预支付方式
|
|
|
220
|
+ sellerOrder.setId(orderInfo.getId());
|
|
|
221
|
+ sellerOrder.setPayment(payment);
|
|
|
222
|
+ sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
223
|
+ sellerOrderMapper.updateByPrimaryKeySelective(sellerOrder);
|
|
|
224
|
+
|
|
|
225
|
+ }
|
|
|
226
|
+
|
|
|
227
|
+
|
|
|
228
|
+ /**
|
|
|
229
|
+ * 检查状态
|
|
|
230
|
+ *
|
|
|
231
|
+ * @param orderInfo
|
|
|
232
|
+ */
|
|
|
233
|
+ public void checkOrderStatus(OrderInfo orderInfo) {
|
|
|
234
|
+ // 订单不是未付款的状态
|
|
|
235
|
+ if (!isWaitingPay(orderInfo.getStatus())) {
|
|
|
236
|
+ logger.warn("payment status not allow {}, orderCode is {}, uid is {}", orderInfo.getStatus(),
|
|
|
237
|
+ orderInfo.getOrderCode(), orderInfo.getUid());
|
|
|
238
|
+ throw new ServiceException(ServiceError.ORDER_PAY_NOT_ALLOW);
|
|
|
239
|
+ }
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+
|
|
|
243
|
+ @Override
|
|
|
244
|
+ public boolean isTimeoutCancelStatus(OrderInfo orderInfo) {
|
|
|
245
|
+ if (orderInfo.getStatus() != null &&
|
|
|
246
|
+ (orderInfo.getStatus().intValue() == SellerOrderStatus.TIMEOUT_CANCEL.getCode() || orderInfo.getStatus().intValue() == SellerOrderStatus.SELf_CANCEL_PAY.getCode())) {
|
|
|
247
|
+ return true;
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ return false;
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ /**
|
|
|
254
|
+ * 获取订单情报
|
|
|
255
|
+ *
|
|
|
256
|
+ * @param orderCode
|
|
|
257
|
+ * @return
|
|
|
258
|
+ */
|
|
|
259
|
+ public OrderInfo getOrderInfo(long orderCode, int uid) {
|
|
|
260
|
+
|
|
|
261
|
+ SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
|
|
|
262
|
+
|
|
|
263
|
+ SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
|
|
|
264
|
+
|
|
|
265
|
+ OrderInfo orderInfo = buildOrderInfo(orderCode, uid, sellerOrder);
|
|
|
266
|
+ if (sellerOrderGoods != null) {
|
|
|
267
|
+ orderInfo.setAttributes(sellerOrderGoods.getAttributes());
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ return orderInfo;
|
|
|
271
|
+ }
|
|
|
272
|
+
|
|
|
273
|
+ private OrderInfo buildOrderInfo(long orderCode, int uid, SellerOrder sellerOrder) {
|
|
|
274
|
+ OrderInfo orderInfo = OrderInfo.builder()
|
|
|
275
|
+ .id(sellerOrder.getId())
|
|
|
276
|
+ .sellerUid(sellerOrder.getUid())
|
|
|
277
|
+ .uid(uid)
|
|
|
278
|
+ .orderCode(orderCode)
|
|
|
279
|
+ .payment(sellerOrder.getPayment())
|
|
|
280
|
+ .skup(sellerOrder.getSkup())
|
|
|
281
|
+ .status(sellerOrder.getStatus())
|
|
|
282
|
+ .orderCodeType(OrderCodeType.SELLER_TYPE)
|
|
|
283
|
+ .createTime(sellerOrder.getCreateTime())
|
|
|
284
|
+ .amount(sellerOrder.getEarnestMoney())
|
|
|
285
|
+ .payExpireTime(DelayTime.SELLER_ORDER_WAITING_PAY)
|
|
|
286
|
+ .payExpire(sellerOrder.getCreateTime() + covertFen(DelayTime.SELLER_ORDER_WAITING_PAY))
|
|
|
287
|
+ .build();
|
|
|
288
|
+ return orderInfo;
|
|
|
289
|
+ }
|
|
|
290
|
+
|
|
|
291
|
+ /**
|
|
|
292
|
+ * 获取订单情报
|
|
|
293
|
+ *
|
|
|
294
|
+ * @param orderCode
|
|
|
295
|
+ * @return
|
|
|
296
|
+ */
|
|
|
297
|
+ @Database(ForceMaster = true)
|
|
|
298
|
+ @Override
|
|
|
299
|
+ public OrderInfo getOrderInfo(long orderCode) {
|
|
|
300
|
+
|
|
|
301
|
+ SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
|
|
|
302
|
+
|
|
|
303
|
+ OrderInfo orderInfo = OrderInfo.builder()
|
|
|
304
|
+ .uid(sellerOrder.getUid())
|
|
|
305
|
+ .orderCode(orderCode)
|
|
|
306
|
+ .payment(sellerOrder.getPayment())
|
|
|
307
|
+ .skup(sellerOrder.getSkup())
|
|
|
308
|
+ .status(sellerOrder.getStatus())
|
|
|
309
|
+ .orderCodeType(OrderCodeType.SELLER_TYPE)
|
|
|
310
|
+ .amount(sellerOrder.getEarnestMoney())
|
|
|
311
|
+ .sourceStatus(sellerOrder.getStatus())
|
|
|
312
|
+ .createTime(sellerOrder.getCreateTime())
|
|
|
313
|
+ .build();
|
|
|
314
|
+ return orderInfo;
|
|
|
315
|
+ }
|
|
|
316
|
+
|
|
|
317
|
+ private SellerOrder checkAndGetSellerOrder(long orderCode) {
|
|
|
318
|
+ SellerOrder sellerOrder = sellerOrderMapper.selectByOrderCode(orderCode);
|
|
|
319
|
+
|
|
|
320
|
+ if (sellerOrder == null) {
|
|
|
321
|
+ logger.warn("getOrderInfo order not exist, orderCode is {}", orderCode);
|
|
|
322
|
+ throw new ServiceException(ServiceError.ORDER_NULL);
|
|
|
323
|
+ }
|
|
|
324
|
+
|
|
|
325
|
+ return sellerOrder;
|
|
|
326
|
+ }
|
|
|
327
|
+
|
|
|
328
|
+ /**
|
|
|
329
|
+ * 定金状态
|
|
|
330
|
+ *
|
|
|
331
|
+ * @param orderCode
|
|
|
332
|
+ * @return
|
|
|
333
|
+ */
|
|
|
334
|
+ public boolean isSupport(long orderCode) {
|
|
|
335
|
+ SellerOrder sellerOrder = checkAndGetSellerOrder(orderCode);
|
|
|
336
|
+ if (isWaitingPay(sellerOrder.getStatus())) {
|
|
|
337
|
+ SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMapper.selectByPrimaryKey(sellerOrder.getSkup());
|
|
|
338
|
+ return sellerOrderGoods.isBidSkup();
|
|
|
339
|
+ } else {
|
|
|
340
|
+ return false;
|
|
|
341
|
+ }
|
|
|
342
|
+ }
|
|
|
343
|
+
|
|
|
344
|
+
|
|
|
345
|
+ private boolean isWaitingPay(Integer status) {
|
|
|
346
|
+ return status != null && SellerOrderStatus.WAITING_PAY.getCode() == status;
|
|
|
347
|
+ }
|
|
|
348
|
+} |