mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
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
package com.sinata.rest.modular.mall.controller;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.modular.auth.util.ThreadPoolUtil;
import com.sinata.rest.modular.mall.controller.body.BodyIds;
import com.sinata.rest.modular.mall.controller.body.BodyMallGoodsCart;
import com.sinata.rest.modular.mall.controller.vo.VoGoodsCart;
import com.sinata.rest.modular.mall.model.MallGoodsCart;
import com.sinata.rest.modular.mall.model.MallTag;
import com.sinata.rest.modular.mall.service.IMallGoodsCartService;
import com.sinata.rest.modular.mall.service.IMallTagService;
import com.sinata.rest.modular.system.service.impl.SystemSetServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 购物车接口
 * @author goku
 * @date 2023/3/11
 */
@Slf4j
@RestController
@RequestMapping("/mall/goodsCart")
@Api(tags = "商城-购物车接口", description = "购物车接口")
public class MallGoodsCartController {
 
    @Autowired
    IMallGoodsCartService goodsCartService;
 
    @Autowired
    SystemSetServiceImpl systemSetService;
 
    @Autowired
    private IMallTagService mallTagService;
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取购物车列表", notes = "获取购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
    })
    public ApiUtils<List<VoGoodsCart>> list(Integer userId) {
        // 查询所有标签
        List<MallTag> mallTags = mallTagService.getMallTags();
 
        // 查询用户购物车
        LambdaQueryWrapper<MallGoodsCart> wrapper = new LambdaQueryWrapper();
        wrapper.eq(MallGoodsCart::getUserId, userId);
        List<VoGoodsCart> list = goodsCartService.getGoodsCarByUserIdList(userId);
        return ApiUtils.returnOK(
                list.stream()
                        .map(o -> {
                            o.setTagList(mallTagService.getTagListByGoodsId(mallTags, o.getTagIds()));
                            return o;
                        })
                        .collect(Collectors.toList())
        );
    }
 
    @PutMapping(value = "/add")
    @ApiOperation(value = "新增商品/修改数量", notes = "新增商品/修改数量")
    public ApiUtils update(@RequestBody BodyMallGoodsCart body) {
        MallGoodsCart goodsCart = JSON.parseObject(JSON.toJSONString(body), MallGoodsCart.class);
        MallGoodsCart dbCart = goodsCartService.getOne(
                new LambdaQueryWrapper<MallGoodsCart>()
                        .eq(MallGoodsCart::getGoodsSkuId, body.getGoodsSkuId())
                        .eq(MallGoodsCart::getUserId, body.getUserId())
        );
        if(dbCart != null) {
            // 增加购物车数量
            goodsCart.setId(dbCart.getId());
        }
 
        goodsCartService.saveOrUpdate(goodsCart);
        return ApiUtils.returnOK();
    }
 
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除购物车", notes = "删除购物车")
    @ApiImplicitParam(name = "ids", value = "购物车Id串”,“号分隔", dataType = "String", paramType = "query", required = true)
    public ApiUtils delete(@RequestBody BodyIds body) {
        if (StringUtils.isEmpty(body.getIds())) {
            return ApiUtils.returnNG();
        }
 
        // 分隔ID
        String[] idArray = body.getIds().split(",");
        // 批量删除
        goodsCartService.removeByIds(Arrays.asList(idArray));
        return ApiUtils.returnOK();
    }
 
    @DeleteMapping(value = "/clear")
    @ApiOperation(value = "清空购物车", notes = "清空购物车")
    public ApiUtils clear() {
        // 获取用户ID
        Integer userId = ThreadPoolUtil.getUserId();
 
        // 批量删除
        goodsCartService.remove(new LambdaQueryWrapper<MallGoodsCart>().eq(MallGoodsCart::getUserId, userId));
        return ApiUtils.returnOK();
    }
 
    @GetMapping(value = "/goodsNum")
    @ApiOperation(value = "用户购物车数量", notes = "获取购物车数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1", dataType = "Int", paramType = "query", required = true)
    })
    public ApiUtils<Integer> goodsNum(Integer userId) {
        return ApiUtils.returnOK(goodsCartService.getGoodsCarNum(userId));
    }
 
}