add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.iotechn.unimall.app.api.cart;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AppServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.iotechn.unimall.biz.service.category.CategoryBizService;
import com.iotechn.unimall.data.domain.CartDO;
import com.iotechn.unimall.data.dto.CartDTO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * Created by rize on 2019/7/3.
 */
@Service
public class CartServiceImpl implements CartService {
 
    @Autowired
    private CartMapper cartMapper;
 
    @Autowired
    private CategoryBizService categoryBizService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addCartItem(Long skuId, Integer num, Long userId) throws ServiceException {
        CartDO cartFromDB = cartMapper.selectOne(
                new QueryWrapper<CartDO>()
                        .eq("sku_id", skuId)
                        .eq("user_id", userId));
        CartDO cartDO = new CartDO();
        Date now = new Date();
        if (cartFromDB != null) {
            //若非空
            cartDO.setId(cartFromDB.getId());
            cartDO.setNum(cartFromDB.getNum() + num);
            cartDO.setGmtUpdate(now);
            return cartMapper.updateById(cartDO) > 0;
        } else {
            //不存在,则添加购物车
            cartDO.setSkuId(skuId);
            cartDO.setNum(num);
            cartDO.setUserId(userId);
            cartDO.setGmtUpdate(now);
            cartDO.setGmtCreate(now);
            return cartMapper.insert(cartDO) > 0;
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean subCartItem(Long skuId, Integer num, Long userId) throws ServiceException {
        CartDO cartFromDB = cartMapper.selectOne(
                new QueryWrapper<CartDO>()
                        .eq("sku_id", skuId)
                        .eq("user_id", userId));
 
        CartDO cartDO = new CartDO();
        if (cartDO != null) {
            cartDO.setId(cartFromDB.getId());
            cartDO.setNum(cartFromDB.getNum() - num);
            if (cartDO.getNum() <= 0) {
                //直接删除此商品
                return cartMapper.deleteById(cartDO.getId()) > 0;
            } else {
                return cartMapper.updateById(cartDO) > 0;
            }
        }
        return false;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeCartItem(Long cartId, Long userId) throws ServiceException {
        return cartMapper.delete(
                new QueryWrapper<CartDO>()
                        .eq("id", cartId)
                        .eq("user_id", userId)) > 0;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeCartItemBatch(String cartIdList, Long userId) throws ServiceException {
        if (StringUtils.isEmpty(cartIdList)) {
            throw new AppServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
        }
        String[] split = cartIdList.split(",");
        if (split.length == 0) {
            throw new AppServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
        }
        List<Long> array = new ArrayList<>(split.length);
        for (String idRaw : split) {
            array.add(Long.parseLong(idRaw));
        }
        return cartMapper.delete(
                new QueryWrapper<CartDO>()
                        .in("id", array)
                        .eq("user_id", userId)) > 0;
    }
 
    @Override
    @Transactional
    public Boolean removeCartAll(Long userId) throws ServiceException {
        return cartMapper.delete(
                new QueryWrapper<CartDO>()
                        .eq("user_id", userId)) > 0;
    }
 
    @Override
    public Integer updateCartItemNum(Long cartId, Integer num, Long userId) throws ServiceException {
        CartDO cartDO = new CartDO();
        cartDO.setNum(num);
        Integer update = cartMapper.update(cartDO,
                new QueryWrapper<CartDO>()
                        .eq("id", cartId)
                        .eq("user_id", userId));
        if (update > 0) {
            return num;
        }
        throw new AppServiceException(ExceptionDefinition.CART_UPDATE_FAILED);
    }
 
    @Override
    public Integer countCart(Long userId) throws ServiceException {
        return cartMapper.countCart(userId);
    }
 
    @Override
    public List<CartDTO> getCartList(Long userId) throws ServiceException {
        List<CartDTO> cartList = cartMapper.getCartList(userId);
        for (CartDTO cartDTO : cartList) {
            List<Long> categoryFamily = categoryBizService.getCategoryFamily(cartDTO.getCategoryId());
            cartDTO.setCategoryIdList(categoryFamily);
        }
        return cartList;
    }
}