基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.manager;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.yami.shop.bean.app.dto.*;
import com.yami.shop.bean.app.param.OrderShopParam;
import com.yami.shop.bean.app.param.SubmitOrderParam;
import com.yami.shop.bean.app.vo.SkuVO;
import com.yami.shop.bean.dto.SkuStockLockDTO;
import com.yami.shop.bean.dto.UserScoreLockDTO;
import com.yami.shop.bean.enums.DvyType;
import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.enums.ShopCityStatus;
import com.yami.shop.bean.event.SubmitOrderActivityEvent;
import com.yami.shop.bean.event.TryLockStockEvent;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.vo.EsProdUpdateVO;
import com.yami.shop.bean.vo.GiveawayProdVO;
import com.yami.shop.bean.vo.GiveawayVO;
import com.yami.shop.bean.vo.VirtualRemarkVO;
import com.yami.shop.common.constants.OrderCacheNames;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.CacheManagerUtil;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.coupon.common.dto.CouponRecordDTO;
import com.yami.shop.coupon.common.service.CouponUseRecordService;
import com.yami.shop.manager.SubmitOrderManager;
import com.yami.shop.service.SkuStockLockService;
import com.yami.shop.user.common.service.UserScoreLockService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
import java.util.*;
 
 
/**
 * 提交订单适配
 * @author FrozenWatermelon
 * @date 2020/12/07
 */
@Component
public class SubmitOrderImplManager implements SubmitOrderManager {
 
    @Autowired
    private CacheManagerUtil cacheManagerUtil;
 
    @Autowired
    private UserScoreLockService userScoreLockService;
 
    @Autowired
    private SkuStockLockService skuStockLockService;
 
    @Autowired
    private Snowflake snowflake;
 
    @Autowired
    private CouponUseRecordService couponLockService;
 
    @Autowired
    private ApplicationContext applicationContext;
 
    @Override
    public ServerResponseEntity<ShopCartOrderMergerDto> checkSubmitInfo(SubmitOrderParam submitOrderParam, String userId) {
        ShopCartOrderMergerDto mergerOrder = cacheManagerUtil.getCache(OrderCacheNames.ORDER_CONFIRM_KEY, String.valueOf(userId));
        // 看看订单有没有过期
        if (Objects.isNull(mergerOrder)) {
            // 订单已过期,请重新下单
            throw new YamiShopBindException("yami.order.expired");
        }
        // 如果是快递配送,判断用户地址是否不在配送范围内
        if (Objects.equals(mergerOrder.getDvyType(), 1)) {
            for (ShopCartOrderDto shopCartOrder : mergerOrder.getShopCartOrders()) {
                for (ShopCartItemDiscountDto discount : shopCartOrder.getShopCartItemDiscounts()) {
                    for (ShopCartItemDto shopCartItem : discount.getShopCartItems()) {
                        if (!shopCartItem.getIsDelivery()) {
                            // 当前区域不支持配送
                            throw new YamiShopBindException(ResponseEnum.DELIVERY_OVER, "yami.delivery.over");
                        }
                    }
                }
            }
        }
        // 防止重复、同时提交
        boolean cad = RedisUtil.cad(OrderCacheNames.ORDER_CONFIRM_UUID_KEY + OrderCacheNames.UNION + userId, userId);
        if (!cad) {
            // 订单状态已经发生改变,请重新下单
//            throw new YamiShopBindException("yami.order.status.check.change");
            OrderNumbersDto orderNumbersDto = new OrderNumbersDto(null);
            orderNumbersDto.setDuplicateError(1);
            return ServerResponseEntity.fail(ResponseEnum.REPEAT_ORDER);
        }
        // 看看订单的标记有没有过期
        if (cacheManagerUtil.getCache(OrderCacheNames.ORDER_CONFIRM_KEY, userId) == null) {
            // 订单已过期,请重新下单
            throw new YamiShopBindException("yami.order.expired");
        }
        // 检查下活动有没有过期
        applicationContext.publishEvent(new SubmitOrderActivityEvent(mergerOrder));
        if (CollectionUtil.isNotEmpty(submitOrderParam.getOrderInvoiceList())) {
            mergerOrder.setOrderInvoiceList(submitOrderParam.getOrderInvoiceList());
        }
        if (!Objects.equals(mergerOrder.getDvyType(), DvyType.DELIVERY.value()) && mergerOrder.getShopCartOrders().size() > 1) {
            // 多家店铺提交订单时,配送方式只能是快递
            throw new YamiShopBindException("yami.order.delivery.error");
        }
        if (Objects.equals(mergerOrder.getDvyType(), DvyType.SAME_CITY.value())) {
            if (Objects.nonNull(mergerOrder.getShopCityStatus()) && Objects.equals(mergerOrder.getShopCityStatus(), ShopCityStatus.NO_CONFIG.value())) {
                // 当前店铺未开启同城配送
                throw new YamiShopBindException("yami.order.same.city.error");
            }
            if (Objects.nonNull(mergerOrder.getShopCityStatus()) && mergerOrder.getShopCityStatus() < ShopCityStatus.USABLE.value()) {
                // 超出商家配送距离或起送费不够
                throw new YamiShopBindException("yami.order.same.city.error2");
            }
        }
        if (Objects.equals(mergerOrder.getDvyType(), DvyType.STATION.value()) && Objects.isNull(mergerOrder.getStationId()) && Objects.isNull(submitOrderParam.getOrderSelfStationDto())) {
            // 请选择自提点并填写完整的自提信息
            throw new YamiShopBindException("yami.station.detail.check");
        }
        // 检查下虚拟商品的所需留言是否填写完成
        if (Objects.equals(mergerOrder.getMold(), 1) && CollectionUtil.isNotEmpty(submitOrderParam.getVirtualRemarkList())) {
            List<VirtualRemarkVO> virtualRemarkList = submitOrderParam.getVirtualRemarkList();
            boolean isRequired = false;
            for (VirtualRemarkVO virtualRemarkVO : mergerOrder.getVirtualRemarkList()) {
                if (virtualRemarkVO.getIsRequired()) {
                    isRequired = true;
                    break;
                }
            }
            // 如果至少要输入一个备注信息,则不能为空
            if (isRequired && CollectionUtil.isEmpty(virtualRemarkList)) {
                // 不能为空
                throw new YamiShopBindException("yami.order.not.null");
            }
            for (VirtualRemarkVO virtualRemarkVO : virtualRemarkList) {
                if (virtualRemarkVO.getIsRequired() && StrUtil.isBlank(virtualRemarkVO.getValue())) {
                    // 不能为空
                    throw new YamiShopBindException("yami.order.not.null");
                }
            }
        }
        mergerOrder.setUserId(userId);
        mergerOrder.setVirtualRemarkList(submitOrderParam.getVirtualRemarkList());
        // 设置自提信息
        mergerOrder.setOrderSelfStationDto(submitOrderParam.getOrderSelfStationDto());
        // 设置备注
        List<OrderShopParam> orderParams = submitOrderParam.getOrderShopParams();
        List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders();
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            if (CollectionUtil.isNotEmpty(orderParams)) {
                for (OrderShopParam orderParam : orderParams) {
                    if (!Objects.equals(shopCartOrder.getShopId(), orderParam.getShopId())) {
                        continue;
                    }
                    shopCartOrder.setRemarks(orderParam.getRemarks());
                }
            }
        }
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            // 使用雪花算法生成的订单号
            String orderNumber = String.valueOf(snowflake.nextId());
            shopCartOrder.setOrderNumber(orderNumber);
        }
        return ServerResponseEntity.success(mergerOrder);
    }
 
    @Override
    @EventListener(TryLockStockEvent.class)
    public void tryLockStock(TryLockStockEvent event) {
        if(Objects.equals(event.getMergerOrder().getOrderType(),OrderType.SECKILL)){
            return;
        }
        List<ShopCartOrderDto> shopCartOrders = event.getMergerOrder().getShopCartOrders();
        List<Order> orders = new ArrayList<>();
        List<SkuStockLockDTO> skuStockLocks = new ArrayList<>();
        // 避免因赠品,套餐重复
        Map<Long,SkuStockLockDTO> skuStockMap = new HashMap<>(16);
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            Order order = new Order();
            order.setShopId(shopCartOrder.getShopId());
            order.setOrderNumber(shopCartOrder.getOrderNumber());
            List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCartOrder.getShopCartItemDiscounts();
            List<OrderItem> orderItems = new ArrayList<>();
            for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) {
                List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems();
                for (ShopCartItemDto orderItem : shopCartItems) {
                    OrderItem item = new OrderItem();
                    item.setProdId(orderItem.getProdId());
                    item.setSkuId(orderItem.getSkuId());
                    item.setStatus(-1);
                    item.setProdCount(orderItem.getProdCount());
                    orderItems.add(item);
                    SkuStockLockDTO skuStockLockDTO = new SkuStockLockDTO(orderItem.getProdId(), orderItem.getSkuId(), shopCartOrder.getOrderNumber(), orderItem.getProdCount());
                    // 放入map
                    skuStockMap.put(orderItem.getSkuId(),skuStockLockDTO);
                    GiveawayVO giveaway = orderItem.getGiveaway();
                    if (Objects.isNull(giveaway)) {
                        continue;
                    }
                    // 赠品库存计算
                    int productNums = shopCartOrder.getTotalCount();
                    for (GiveawayProdVO giveawayProd : giveaway.getGiveawayProds()) {
                        // 出入库用到的信息
                        OrderItem giveawayItem = new OrderItem();
                        giveawayItem.setProdId(giveawayProd.getProdId());
                        giveawayItem.setSkuId(giveawayProd.getSkuId());
                        giveawayItem.setStatus(-1);
                        giveawayItem.setProdCount(giveawayProd.getGiveawayNum());
                        orderItems.add(giveawayItem);
                        productNums = productNums + orderItem.getProdCount();
                        SkuStockLockDTO comboSkuStockLock = new SkuStockLockDTO(giveawayProd.getProdId(), giveawayProd.getSkuId(), shopCartOrder.getOrderNumber(), giveawayProd.getGiveawayNum());
                        // 判断是否重复.重复重新put
                        if(skuStockMap.containsKey(giveawayProd.getSkuId())){
                            SkuStockLockDTO skuStockLock = skuStockMap.get(giveawayProd.getSkuId());
                            comboSkuStockLock.setCount(skuStockLock.getCount() + comboSkuStockLock.getCount());
                        }
                        skuStockMap.put(giveawayProd.getSkuId(), comboSkuStockLock);
                    }
                }
            }
            order.setOrderItems(orderItems);
            orders.add(order);
        }
        Map<Long, EsProdUpdateVO> prodMap = new HashMap<>(skuStockMap.size());
        // 添加赠品库存扣减项
        for (Long skuId : skuStockMap.keySet()) {
            SkuStockLockDTO skuStockLockDTO = skuStockMap.get(skuId);
            skuStockLocks.add(skuStockMap.get(skuId));
 
            // 统计商品的购买数量--用于es更新商品库存
            EsProdUpdateVO prodUpdateVO = prodMap.get(skuStockLockDTO.getProdId());
            if (Objects.isNull(prodUpdateVO)) {
                prodUpdateVO = new EsProdUpdateVO();
                prodUpdateVO.setProdId(skuStockLockDTO.getProdId());
                // 减少商品库存
                prodUpdateVO.setType(0);
                prodUpdateVO.setCount(0);
                prodMap.put(skuStockLockDTO.getProdId(), prodUpdateVO);
            }
            prodUpdateVO.setCount(prodUpdateVO.getCount() + skuStockLockDTO.getCount());
        }
        event.setProdList(new ArrayList<>(prodMap.values()));
        // 锁定库存
        ServerResponseEntity<?> lockStockResponse = skuStockLockService.lock(skuStockLocks,orders);
        // 提示具体哪个商品库存不足
        if (Objects.equals(ResponseEnum.NOT_STOCK.value(), lockStockResponse.getCode())) {
            for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
                List<ShopCartItemDiscountDto> shopCartItemDiscounts = shopCartOrder.getShopCartItemDiscounts();
                for (ShopCartItemDiscountDto shopCartItemDiscount : shopCartItemDiscounts) {
                    List<ShopCartItemDto> shopCartItems = shopCartItemDiscount.getShopCartItems();
                    for (ShopCartItemDto orderItem : shopCartItems) {
                        if (Objects.equals(orderItem.getSkuId().toString(), lockStockResponse.getData().toString())) {
                            // 拼接库存不足商品的名称(有规格sku,要把规格也拼接上)
                            // split(" ")[0]是因为有些商品名已经拼接了‘prodName skuName’,只取prodName,避免skuName重复
                            String prodName;
                            if ((prodName=orderItem.getSkuName()) == null) {
                                prodName = orderItem.getProdName().split(" ")[0];
                            } else {
                                prodName = orderItem.getProdName().split(" ")[0] + "(" + prodName + ")";
                            }
                            throw new YamiShopBindException(prodName + I18nMessage.getMessage("yami.insufficient.inventory"));
                        }
                    }
                }
            }
        }
        if (!lockStockResponse.isSuccess()) {
            throw new YamiShopBindException(ResponseEnum.EXCEPTION);
        }
    }
 
    @Override
    @EventListener(TryLockStockEvent.class)
    public void tryLockCoupon(TryLockStockEvent event) {
        ShopCartOrderMergerDto mergerOrder = event.getMergerOrder();
        String userId = event.getUserId();
        if (!Objects.equals(mergerOrder.getOrderType(), OrderType.ORDINARY)) {
            return;
        }
        List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders();
        List<String> orderNumbers = new ArrayList<>();
 
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            String orderNumber = shopCartOrder.getOrderNumber();
            List<CouponOrderDto> coupons = shopCartOrder.getCoupons();
            if (CollectionUtil.isNotEmpty(coupons)) {
                for (CouponOrderDto coupon : coupons) {
                    if (Objects.equals(Boolean.TRUE, coupon.isChoose())) {
                        coupon.setOrderNumber(orderNumber);
                    }
                }
            }
            orderNumbers.add(orderNumber);
        }
 
        // 锁优惠券
        List<CouponRecordDTO> lockCouponParams = getLockCouponDto(mergerOrder, orderNumbers);
        if (CollectionUtil.isNotEmpty(lockCouponParams)) {
            couponLockService.lockCoupon(lockCouponParams,userId);
        }
    }
 
    /**
     * 尝试锁定优惠券
     * @param mergerOrder
     * @param orderIds
     */
    private List<CouponRecordDTO> getLockCouponDto(ShopCartOrderMergerDto mergerOrder, List<String> orderIds) {
        // 锁定优惠券
        // 平台优惠券
        List<CouponRecordDTO> lockCouponParams = new ArrayList<>();
        CouponRecordDTO platformLockCouponParam = getLockCouponDTO(mergerOrder.getCoupons());
        if (platformLockCouponParam != null) {
            // 平台优惠券涉及多个订单,所以设置订单id为多个订单id以逗号分割
            platformLockCouponParam.setOrderNumbers(StrUtil.join(StrUtil.COMMA, orderIds));
            lockCouponParams.add(platformLockCouponParam);
        }
        // 店铺优惠券
        for (ShopCartOrderDto shopCartOrder : mergerOrder.getShopCartOrders()) {
            CouponRecordDTO shopLockCouponParam = getLockCouponDTO(shopCartOrder.getCoupons());
            if (shopLockCouponParam != null) {
                lockCouponParams.add(shopLockCouponParam);
            }
        }
 
        return lockCouponParams;
    }
 
    private CouponRecordDTO getLockCouponDTO(List<CouponOrderDto> couponOrders) {
        if (CollectionUtil.isEmpty(couponOrders)) {
            return null;
        }
        for (CouponOrderDto couponOrder : couponOrders) {
            if ((Objects.equals(Boolean.TRUE, couponOrder.isChoose())) && couponOrder.isCanUse()) {
                CouponRecordDTO param = new CouponRecordDTO();
                param.setOrderNumbers(couponOrder.getOrderNumber());
                param.setCouponId(couponOrder.getCouponId());
                param.setCouponUserId(couponOrder.getCouponUserId());
                param.setReduceAmount(couponOrder.getReduceAmount());
                return param;
            }
        }
        return null;
    }
 
    @Override
    @EventListener(TryLockStockEvent.class)
    public void tryLockScore(TryLockStockEvent event) {
        String userId = event.getUserId();
        ShopCartOrderMergerDto mergerOrder = event.getMergerOrder();
        // 如果没有使用积分,就不用锁定积分啦
        if (Objects.isNull(mergerOrder.getUsableScore()) || Objects.equals(mergerOrder.getUsableScore(), 0L)) {
            return;
        }
        List<ShopCartOrderDto> shopCartOrders = mergerOrder.getShopCartOrders();
        List<UserScoreLockDTO> userScoreLocks = new ArrayList<>();
        for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
            if (Objects.equals(shopCartOrder.getUseScore(), 0L)) {
                continue;
            }
            userScoreLocks.add(new UserScoreLockDTO(shopCartOrder.getOrderNumber(), shopCartOrder.getUseScore(), mergerOrder.getOrderType().value()));
        }
 
        // 锁定积分
        userScoreLockService.lock(userScoreLocks,userId);
    }
 
}