Bang Hu
2025-09-03 bd28d26d3da636718aeb73edca00f3da6ecbe4b2
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package com.webmanage.service.impl;
 
import com.webmanage.common.BusinessException;
import com.webmanage.dto.CartItemDTO;
import com.webmanage.entity.Cart;
import com.webmanage.entity.ProductPricing;
import com.webmanage.mapper.CartMapper;
import com.webmanage.mapper.ProductPricingMapper;
import com.webmanage.service.CartService;
import com.webmanage.service.CartPersistenceService;
import com.webmanage.vo.CartItemVO;
import com.webmanage.vo.CartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import com.webmanage.config.CartProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
 * 购物车服务实现类
 */
@Slf4j
@Service
public class CartServiceImpl implements CartService {
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    @Resource
    private CartMapper cartMapper;
    
    @Resource
    private ProductPricingMapper productPricingMapper;
    
    @Resource
    private CartPersistenceService cartPersistenceService;
    
    @Resource
    private CartProperties cartProperties;
    
    // Redis key前缀
    private static final String CART_KEY_PREFIX = "cart:";
    private static final String CART_ITEM_KEY_PREFIX = "cart_item:";
    private static final int CART_EXPIRE_DAYS = 30; // 购物车过期时间30天
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addToCart(String userId, String unitId, CartItemDTO cartItemDTO) {
        try {
            // 验证商品定价是否存在
            ProductPricing pricing = productPricingMapper.selectById(cartItemDTO.getPricingId());
            if (pricing == null) {
                throw new BusinessException("商品定价不存在");
            }
 
            // 构建购物车key
            String cartKey = buildCartKey(userId,unitId);
            String cartItemKey = buildCartItemKey(userId, unitId, cartItemDTO.getPricingId());
 
            // 检查商品是否已在购物车中
            CartItemVO existingItem = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
            if (existingItem != null) {
                // 如果已存在,更新数量
                existingItem.setQuantity(existingItem.getQuantity() + cartItemDTO.getQuantity());
                existingItem.setTotalPrice(existingItem.getUnitPrice().multiply(BigDecimal.valueOf(existingItem.getQuantity())));
                existingItem.setUpdateTime(LocalDateTime.now());
            } else {
                // 如果不存在,创建新的购物车项
                existingItem = new CartItemVO();
                BeanUtils.copyProperties(cartItemDTO, existingItem);
                existingItem.setAddTime(LocalDateTime.now());
                existingItem.setUpdateTime(LocalDateTime.now());
                // 计算小计金额
                if (existingItem.getTotalPrice() == null) {
                    existingItem.setTotalPrice(existingItem.getUnitPrice().multiply(BigDecimal.valueOf(existingItem.getQuantity())));
                }
            }
 
            // 保存到Redis
            redisTemplate.opsForValue().set(cartItemKey, existingItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
 
            // 更新购物车商品列表
            updateCartItemList(userId, unitId, cartItemDTO.getPricingId(), true);
 
            // 设置购物车过期时间
            redisTemplate.expire(cartKey, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
 
            // 异步持久化到数据库(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.saveOrUpdate(userId, unitId, existingItem);
            }
 
            log.info("用户{}成功添加商品{}到购物车", userId, cartItemDTO.getProductName());
            return true;
        } catch (Exception e) {
            log.error("添加商品到购物车失败", e);
            throw new BusinessException("添加商品到购物车失败:" + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeFromCart(String userId, String unitId, Long pricingId) {
        try {
            String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
 
            // 从Redis中删除商品项
            Boolean removed = redisTemplate.delete(cartItemKey);
            if (Boolean.TRUE.equals(removed)) {
                // 更新购物车商品列表
                updateCartItemList(userId, unitId, pricingId, false);
 
                // 异步从数据库中删除(根据配置)
                if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                    cartPersistenceService.remove(userId, unitId, pricingId);
                }
 
                log.info("用户{}成功从购物车移除商品{}", userId, pricingId);
                return true;
            }
            return false;
        } catch (Exception e) {
            log.error("从购物车移除商品失败", e);
            throw new BusinessException("从购物车移除商品失败:" + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCartItemQuantity(String userId, String unitId, Long pricingId, Integer quantity) {
        try {
            if (quantity <= 0) {
                return removeFromCart(userId, unitId, pricingId);
            }
 
            String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
            CartItemVO cartItem = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
            if (cartItem == null) {
                throw new BusinessException("购物车商品不存在");
            }
 
            cartItem.setQuantity(quantity);
            cartItem.setTotalPrice(cartItem.getUnitPrice().multiply(BigDecimal.valueOf(quantity)));
            cartItem.setUpdateTime(LocalDateTime.now());
 
            // 更新到Redis
            redisTemplate.opsForValue().set(cartItemKey, cartItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
 
            // 异步持久化到数据库(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.saveOrUpdate(userId, unitId, cartItem);
            }
 
            log.info("用户{}成功更新购物车商品{}数量为{}", userId, pricingId, quantity);
            return true;
        } catch (Exception e) {
            log.error("更新购物车商品数量失败", e);
            throw new BusinessException("更新购物车商品数量失败:" + e.getMessage());
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean clearCart(String userId, String unitId) {
        try {
            String cartKey = buildCartKey(userId, unitId);
            List<Long> pricingIds = getCartItemPricingIds(userId, unitId);
 
            // 删除所有商品项
            for (Long pricingId : pricingIds) {
                String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
                redisTemplate.delete(cartItemKey);
            }
 
            // 删除购物车列表
            redisTemplate.delete(cartKey);
 
            // 异步清空数据库中的购物车数据(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.clear(userId, unitId);
            }
 
            log.info("用户{}成功清空购物车", userId);
            return true;
        } catch (Exception e) {
            log.error("清空购物车失败", e);
            throw new BusinessException("清空购物车失败:" + e.getMessage());
        }
    }
 
    @Override
    public CartVO getCart(String userId, String unitId) {
        try {
            CartVO cartVO = new CartVO();
            cartVO.setUserId(userId);
            if (StringUtils.hasText(unitId)){
                cartVO.setUnitId(unitId);
            }
            List<CartItemVO> items = getCartItems(userId, unitId);
            cartVO.setItems(items);
 
            // 计算总数量和总金额
            int totalQuantity = items.stream().mapToInt(item -> item.getQuantity()).sum();
            BigDecimal totalAmount = items.stream()
                    .map(item -> item.getTotalPrice())
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
            cartVO.setTotalQuantity(totalQuantity);
            cartVO.setTotalAmount(totalAmount);
            cartVO.setLastUpdateTime(LocalDateTime.now());
 
            return cartVO;
        } catch (Exception e) {
            log.error("获取购物车信息失败", e);
            throw new BusinessException("获取购物车信息失败:" + e.getMessage());
        }
    }
 
    @Override
    public List<CartItemVO> getCartItems(String userId, String unitId) {
        try {
            // 优先从Redis获取
            List<CartItemVO> items = getCartItemsFromRedis(userId, unitId);
            if (items != null && !items.isEmpty()) {
                return items;
            }
 
            // Redis中没有数据,从数据库加载
            log.info("Redis中无购物车数据,从数据库加载用户{}的购物车", userId);
            return loadCartFromDatabase(userId, unitId) ? getCartItemsFromRedis(userId, unitId) : new ArrayList<>();
        } catch (Exception e) {
            log.error("获取购物车商品列表失败", e);
            // 降级到数据库查询
            return getCartItemsFromDatabase(userId, unitId);
        }
    }
 
    @Override
    public boolean checkCartItemStock(String userId, String unitId, Long pricingId) {
        // TODO: 实现库存检查逻辑
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemoveFromCart(String userId, String unitId, List<Long> pricingIds) {
        try {
            if (CollectionUtils.isEmpty(pricingIds)) {
                return true;
            }
 
            for (Long pricingId : pricingIds) {
                removeFromCart(userId, unitId, pricingId);
            }
 
            return true;
        } catch (Exception e) {
            log.error("批量删除购物车商品失败", e);
            throw new BusinessException("批量删除购物车商品失败:" + e.getMessage());
        }
    }
 
    @Override
    public Integer getCartItemCount(String userId, String unitId) {
        try {
            // 优先从Redis获取
            String cartKey = buildCartKey(userId, unitId);
            List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
            if (pricingIds != null) {
                return pricingIds.size();
            }
 
            // 从数据库获取
            return cartMapper.countByUserIdAndUnitId(userId, unitId);
        } catch (Exception e) {
            log.error("获取购物车商品数量失败", e);
            return 0;
        }
    }
 
    @Override
    public boolean loadCartFromDatabase(String userId, String unitId) {
        try {
            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
            if (CollectionUtils.isEmpty(cartItems)) {
                return false;
            }
 
            String cartKey = buildCartKey(userId, unitId);
            List<Long> pricingIds = new ArrayList<>();
 
            for (Cart cartItem : cartItems) {
                CartItemVO itemVO = convertCartToCartItemVO(cartItem);
                String cartItemKey = buildCartItemKey(userId, unitId, cartItem.getPricingId());
 
                // 保存到Redis
                redisTemplate.opsForValue().set(cartItemKey, itemVO, CART_EXPIRE_DAYS, TimeUnit.DAYS);
                pricingIds.add(cartItem.getPricingId());
            }
 
            // 保存购物车列表到Redis
            redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS);
 
            log.info("成功从数据库加载用户{}的购物车数据到Redis", userId);
            return true;
        } catch (Exception e) {
            log.error("从数据库加载购物车数据失败", e);
            return false;
        }
    }
 
    @Override
    public boolean syncCartToDatabase(String userId, String unitId) {
        try {
            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId);
            if (CollectionUtils.isEmpty(redisItems)) {
                return true;
            }
 
            // 清空数据库中的购物车数据
            clearCartFromDatabase(userId, unitId);
 
            // 同步Redis数据到数据库
            for (CartItemVO item : redisItems) {
                syncCartItemToDatabase(userId, unitId, item);
            }
 
            log.info("成功同步Redis购物车数据到数据库,用户{}", userId);
            return true;
        } catch (Exception e) {
            log.error("同步购物车数据到数据库失败", e);
            return false;
        }
    }
 
    @Override
    public boolean checkCartConsistency(String userId, String unitId) {
        try {
            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId);
            List<Cart> dbItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
 
            if (redisItems.size() != dbItems.size()) {
                log.warn("购物车数据不一致:Redis数量{},数据库数量{}", redisItems.size(), dbItems.size());
                return false;
            }
 
            // 检查每个商品项是否一致
            for (CartItemVO redisItem : redisItems) {
                boolean found = false;
                for (Cart dbItem : dbItems) {
                    if (redisItem.getPricingId().equals(dbItem.getPricingId()) &&
                        redisItem.getQuantity().equals(dbItem.getQuantity())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    log.warn("购物车商品项不一致:pricingId={}", redisItem.getPricingId());
                    return false;
                }
            }
 
            return true;
        } catch (Exception e) {
            log.error("检查购物车数据一致性失败", e);
            return false;
        }
    }
 
    // ==================== 私有方法 ====================
 
    private String buildCartKey(String userId, String unitId) {
        if(StringUtils.hasText(unitId)){
            return CART_KEY_PREFIX + userId + ":" + unitId;
        }
        return CART_KEY_PREFIX + userId;
    }
 
    private String buildCartItemKey(String userId, String unitId, Long pricingId) {
        if(StringUtils.hasText(unitId)){
            return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + pricingId;
        }
        return CART_ITEM_KEY_PREFIX + userId + ":" + pricingId;
    }
 
    private void updateCartItemList(String userId, String unitId, Long pricingId, boolean add) {
        String cartKey = buildCartKey(userId, unitId);
        List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
 
        if (pricingIds == null) {
            pricingIds = new ArrayList<>();
        }
 
        if (add && !pricingIds.contains(pricingId)) {
            pricingIds.add(pricingId);
        } else if (!add) {
            pricingIds.remove(pricingId);
        }
 
        redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS);
    }
 
    private List<Long> getCartItemPricingIds(String userId, String unitId) {
        String cartKey = buildCartKey(userId, unitId);
        List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
        return pricingIds != null ? pricingIds : new ArrayList<>();
    }
 
    private List<CartItemVO> getCartItemsFromRedis(String userId, String unitId) {
        try {
            String cartKey = buildCartKey(userId, unitId);
            List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
 
            if (CollectionUtils.isEmpty(pricingIds)) {
                return new ArrayList<>();
            }
 
            List<CartItemVO> items = new ArrayList<>();
            for (Long pricingId : pricingIds) {
                String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
                CartItemVO item = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
                if (item != null) {
                    items.add(item);
                }
            }
 
            return items;
        } catch (Exception e) {
            log.error("从Redis获取购物车商品列表失败", e);
            return new ArrayList<>();
        }
    }
    
    private List<CartItemVO> getCartItemsFromDatabase(String userId, String unitId) {
        try {
            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
            List<CartItemVO> items = new ArrayList<>();
            
            for (Cart cartItem : cartItems) {
                items.add(convertCartToCartItemVO(cartItem));
            }
            
            return items;
        } catch (Exception e) {
            log.error("从数据库获取购物车商品列表失败", e);
            return new ArrayList<>();
        }
    }
    
    private CartItemVO convertCartToCartItemVO(Cart cart) {
        CartItemVO itemVO = new CartItemVO();
        BeanUtils.copyProperties(cart, itemVO);
        return itemVO;
    }
    
    private void syncCartItemToDatabase(String userId, String unitId, CartItemVO item) {
        try {
            Cart cart = new Cart();
            BeanUtils.copyProperties(item, cart);
            cart.setUserId(userId);
            cart.setUnitId(unitId);
            cart.setUpdateTime(LocalDateTime.now());
            
            // 检查是否已存在
            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, item.getPricingId());
            if (existingCart != null) {
                // 更新
                cart.setId(existingCart.getId());
                cartMapper.updateById(cart);
            } else {
                // 新增
                cart.setAddTime(LocalDateTime.now());
                cartMapper.insert(cart);
            }
        } catch (Exception e) {
            log.error("同步购物车商品到数据库失败", e);
        }
    }
    
    private void removeCartItemFromDatabase(String userId, String unitId, Long pricingId) {
        try {
            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId);
            if (existingCart != null) {
                cartMapper.deleteByCustomerCondition(existingCart.getId());
            }
        } catch (Exception e) {
            log.error("从数据库删除购物车商品失败", e);
        }
    }
    
    private void clearCartFromDatabase(String userId, String unitId) {
        try {
            // 使用逻辑删除
            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
            for (Cart item : cartItems) {
                cartMapper.deleteById(item.getId());
            }
        } catch (Exception e) {
            log.error("清空数据库购物车失败", e);
        }
    }
}