seatonwan9
2025-08-28 40616c5802932c2216e97a44ec9abed182590bbe
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
package com.webmanage.service.impl;
 
import com.webmanage.entity.Cart;
import com.webmanage.mapper.CartMapper;
import com.webmanage.service.CartPersistenceService;
import com.webmanage.vo.CartItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
 
@Service
public class CartPersistenceServiceImpl implements CartPersistenceService {
 
    @Resource
    private CartMapper cartMapper;
 
    @Override
    @Async("asyncExecutor")
    public void saveOrUpdate(String userId, String unitId, CartItemVO item) {
        try {
            Cart cart = new Cart();
            BeanUtils.copyProperties(item, cart);
            cart.setUserId(userId);
            if(StringUtils.hasText(unitId)){
                cart.setUnitId(unitId);
            }
            cart.setUpdateTime(LocalDateTime.now());
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, item.getPricingId());
            if (existing != null) {
                cart.setId(existing.getId());
                cartMapper.updateById(cart);
            } else {
                cart.setAddTime(LocalDateTime.now());
                cartMapper.insert(cart);
            }
        } catch (Exception ignored) {}
    }
 
    @Override
    @Async("asyncExecutor")
    public void remove(String userId, String unitId, Long pricingId) {
        try {
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId);
            if (existing != null) {
                cartMapper.deleteById(existing.getId());
            }
        } catch (Exception ignored) {}
    }
 
    @Override
    @Async("asyncExecutor")
    public void clear(String userId, String unitId) {
        try {
            java.util.List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
            for (Cart item : cartItems) {
                cartMapper.deleteById(item.getId());
            }
        } catch (Exception ignored) {}
    }
}