mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.service_community.service.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.shop.ComShopCartDTO;
import com.panzhihua.common.model.dtos.shop.ComShopEditNubCartDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.shop.ComShopCartListVO;
import com.panzhihua.common.model.vos.shop.ComShopCartStoreVO;
import com.panzhihua.common.model.vos.shop.ComShopCartVO;
import com.panzhihua.service_community.dao.ComShopCartDAO;
import com.panzhihua.service_community.dao.ComShopGoodsAttrDAO;
import com.panzhihua.service_community.dao.ComShopGoodsDAO;
import com.panzhihua.service_community.dao.ComShopStoreDAO;
import com.panzhihua.service_community.model.dos.ComShopCartDO;
import com.panzhihua.service_community.model.dos.ComShopGoodsAttrDO;
import com.panzhihua.service_community.model.dos.ComShopGoodsDO;
import com.panzhihua.service_community.model.dos.ComShopStoreDO;
import com.panzhihua.service_community.service.ComShopCartService;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * @auther lyq
 * @create 2021-04-14 15:02:14
 * @describe 购物车表服务实现类
 */
@Slf4j
@Service
public class ComShopCartServiceImpl extends ServiceImpl<ComShopCartDAO, ComShopCartDO> implements ComShopCartService {
 
    @Resource
    private ComShopGoodsDAO shopGoodsDAO;
    @Resource
    private ComShopStoreDAO shopStoreDAO;
    @Resource
    private ComShopGoodsAttrDAO shopGoodsAttrDAO;
 
    /**
     * 根据用户id查询购物车列表
     * 
     * @param userId
     *            用户id
     * @return 购物车列表
     */
    @Override
    public R shopCartList(Long userId) {
        // 返回参数
        ComShopCartListVO comShopCart = new ComShopCartListVO();
        // 失效宝贝集合
        List<ComShopCartVO> invalidCartList = new ArrayList<>();
        // 有效宝贝集合
        List<ComShopCartStoreVO> cartList = new ArrayList<>();
 
        List<ComShopCartDO> shopCartDOList =
            this.baseMapper.selectList(new QueryWrapper<ComShopCartDO>().eq("user_id", userId));
        if (!shopCartDOList.isEmpty()) {
            shopCartDOList.forEach(shopCart -> {
                // 查询当前商品是否是有效商品
                ComShopGoodsDO goodsDO =
                    shopGoodsDAO.selectOne(new QueryWrapper<ComShopGoodsDO>().eq("id", shopCart.getGoodsId())
                        .eq("delete_status", ComShopGoodsDO.deleteStatus.no).eq("status", ComShopGoodsDO.status.sell));
                if (goodsDO == null) {
                    ComShopCartVO invalidCart = new ComShopCartVO();
                    BeanUtils.copyProperties(shopCart, invalidCart);
                    invalidCartList.add(invalidCart);
                    return;
                }
 
                // 有效商品区分店铺
                if (cartList.isEmpty()) {
                    ComShopCartStoreVO cartStoreVO = new ComShopCartStoreVO();
                    // 查询商品所属店铺
                    ComShopStoreDO shopStoreDO = shopStoreDAO.selectById(goodsDO.getStoreId());
                    if (shopStoreDO != null) {
                        cartStoreVO.setStoreId(shopStoreDO.getId());
                        cartStoreVO.setStoreName(shopStoreDO.getName());
                        cartStoreVO.setStoreLogo(shopStoreDO.getLogo());
                    }
                    List<ComShopCartVO> cartsList = new ArrayList<>();
                    ComShopCartVO cartVO = new ComShopCartVO();
                    BeanUtils.copyProperties(shopCart, cartVO);
                    cartsList.add(cartVO);
                    cartStoreVO.setCartList(cartsList);
                    cartList.add(cartStoreVO);
                } else {
                    boolean isOk = true;
                    for (ComShopCartStoreVO vo : cartList) {
                        if (vo.getStoreId().equals(goodsDO.getStoreId())) {
                            isOk = false;
                            ComShopCartVO cartVO = new ComShopCartVO();
                            BeanUtils.copyProperties(shopCart, cartVO);
                            vo.getCartList().add(cartVO);
                        }
                    }
 
                    if (isOk) {
                        ComShopCartStoreVO cartStoreVO = new ComShopCartStoreVO();
                        // 查询商品所属店铺
                        ComShopStoreDO shopStoreDO = shopStoreDAO.selectById(goodsDO.getStoreId());
                        if (shopStoreDO != null) {
                            cartStoreVO.setStoreId(shopStoreDO.getId());
                            cartStoreVO.setStoreName(shopStoreDO.getName());
                            cartStoreVO.setStoreLogo(shopStoreDO.getLogo());
                        }
                        List<ComShopCartVO> cartsList = new ArrayList<>();
                        ComShopCartVO cartVO = new ComShopCartVO();
                        BeanUtils.copyProperties(shopCart, cartVO);
                        cartsList.add(cartVO);
                        cartStoreVO.setCartList(cartsList);
                        cartList.add(cartStoreVO);
                    }
 
                }
 
            });
            // 设置返回参数
            comShopCart.setInvalidCartList(invalidCartList);
            comShopCart.setCartList(cartList);
        }
        return R.ok(comShopCart);
    }
 
    /**
     * 购物车添加
     * 
     * @param comShopCartDTO
     *            请求参数
     * @return 添加结果
     */
    @Override
    public R shopAddCart(ComShopCartDTO comShopCartDTO) {
        // 查询当前商品是否已经在购物车中
        ComShopCartDO shopCartDO =
            this.baseMapper.selectOne(new QueryWrapper<ComShopCartDO>().eq("user_id", comShopCartDTO.getUserId())
                .eq("goods_id", comShopCartDTO.getGoodsId()).eq("goods_attr_id", comShopCartDTO.getGoodsAttrId()));
        if (shopCartDO == null) {// 购物车表里未有该商品的记录
            shopCartDO = new ComShopCartDO();
            shopCartDO.setUserId(comShopCartDTO.getUserId());
            shopCartDO.setAmount(comShopCartDTO.getNumber());
            // 查询商品
            ComShopGoodsDO shopGoodsDO = shopGoodsDAO.selectById(comShopCartDTO.getGoodsId());
            if (shopGoodsDO == null) {
                return R.fail("未查询到商品信息");
            }
            shopCartDO.setGoodsName(shopGoodsDO.getName());
            shopCartDO.setGoodsId(shopGoodsDO.getId());
            shopCartDO.setStoreId(shopGoodsDO.getStoreId());
            // 查询商品规格
            ComShopGoodsAttrDO shopGoodsAttrDO = shopGoodsAttrDAO.selectById(comShopCartDTO.getGoodsAttrId());
            if (shopGoodsAttrDO == null) {
                return R.fail("未查询到商品规格");
            }
            shopCartDO.setGoodsAttrId(shopGoodsAttrDO.getId());
            shopCartDO.setGoodsAttr(shopGoodsAttrDO.getGoodsAttr());
            shopCartDO.setGoodsAttrPic(shopGoodsAttrDO.getAttrPic());
            shopCartDO.setPrice(shopGoodsAttrDO.getPrice());
            // 执行添加操作
            if (this.baseMapper.insert(shopCartDO) > 0) {
                return R.ok();
            } else {
                return R.fail("添加购物车失败");
            }
        } else {// 购物车已有该商品
            shopCartDO.setAmount(shopCartDO.getAmount() + comShopCartDTO.getNumber());
            // 执行修改操作
            if (this.baseMapper.updateById(shopCartDO) > 0) {
                return R.ok();
            } else {
                return R.fail("添加购物车失败");
            }
        }
    }
 
    /**
     * 购物车内商品数量修改
     * 
     * @param shopEditNubCartDTO
     *            请求参数
     * @return 修改结果
     */
    @Override
    public R shopEditNubCart(ComShopEditNubCartDTO shopEditNubCartDTO) {
        // 查询购物车记录
        ComShopCartDO shopCartDO = this.baseMapper.selectById(shopEditNubCartDTO.getCartId());
        if (shopCartDO == null) {
            return R.fail("未查询到购物车记录");
        }
        shopCartDO.setAmount(shopEditNubCartDTO.getNumber());
        // 执行修改操作
        if (this.baseMapper.updateById(shopCartDO) > 0) {
            return R.ok();
        } else {
            return R.fail("修改失败");
        }
    }
 
    /**
     * 删除购物车记录
     * 
     * @param Ids
     *            购物车id集合
     * @return 删除结果
     */
    public R shopDelCart(List<Long> Ids) {
        if (this.baseMapper.deleteBatchIds(Ids) > 0) {
            return R.ok();
        } else {
            return R.fail("修改失败");
        }
    }
 
    /**
     * 查询用户购物车商品数量
     * 
     * @param userId
     *            用户id
     * @return 用户购物车商品数量
     */
    public R shopCartUserTotal(Long userId) {
        int goodsNum = 0;
        // 查询用户购物车所有商品集合
        List<ComShopCartDO> shopCartDOList =
            this.baseMapper.selectList(new QueryWrapper<ComShopCartDO>().eq("user_id", userId));
        if (!shopCartDOList.isEmpty()) {
            for (ComShopCartDO cartDO : shopCartDOList) {
                // 查询当前商品是否是有效商品
                ComShopGoodsDO goodsDO =
                    shopGoodsDAO.selectOne(new QueryWrapper<ComShopGoodsDO>().eq("id", cartDO.getGoodsId())
                        .eq("delete_status", ComShopGoodsDO.deleteStatus.no).eq("status", ComShopGoodsDO.status.sell));
 
                if (goodsDO != null) {
                    goodsNum++;
                }
            }
        }
        return R.ok(goodsNum);
    }
}