基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.app.dto.UserCollectionDto;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.UserCollection;
import com.yami.shop.bean.model.UserCollectionShop;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.BasketService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.UserCollectionService;
import com.yami.shop.service.UserCollectionShopService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/user/collection")
@Tag(name = "商品收藏接口")
@AllArgsConstructor
public class UserCollectionController {
 
    private final UserCollectionService userCollectionService;
    private final UserCollectionShopService userCollectionShopService;
 
    private final BasketService basketService;
 
    private final ProductService productService;
 
    @GetMapping("/page")
    @Operation(summary = "分页返回收藏数据" , description = "根据用户id获取")
    @Parameters({
            @Parameter(name = "prodName", description = "商品名称" , required = true),
            @Parameter(name = "prodType", description = "商品类型" , required = true)
    })
    public ServerResponseEntity<IPage<UserCollectionDto>> getUserCollectionDtoPageByUserId(PageParam page, String prodName, Integer prodType) {
        return ServerResponseEntity.success(userCollectionService.getUserCollectionDtoPageByUserId(page, SecurityUtils.getUser().getUserId(), prodName, prodType, I18nMessage.getDbLang()));
    }
 
    @GetMapping("/isCollection")
    @Operation(summary = "根据商品id获取该商品是否在收藏夹中" , description = "传入收藏商品id")
    @Parameter(name = "prodId", description = "商品id" , required = true)
    public ServerResponseEntity<Boolean> isCollection(String prodId) {
        String[] prodIds = prodId.split(StrUtil.COMMA);
        if (ArrayUtil.isEmpty(prodId)) {
            throw new YamiShopBindException("商品id不能为空");
        }
        int count = 0;
        for (String pLong : prodIds) {
            if (productService.count(new LambdaQueryWrapper<Product>()
                    .eq(Product::getProdId, pLong)) < 1) {
                // 该商品不存在
                throw new YamiShopBindException("yami.product.no.exist");
            }
            int i = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, pLong)
                    .eq(UserCollection::getUserId, SecurityUtils.getUser().getUserId()));
            count += i;
        }
        return ServerResponseEntity.success(count == prodIds.length);
    }
 
    @PostMapping("/addOrCancel")
    @Operation(summary = "添加/取消收藏" , description = "传入收藏商品id,如果商品未收藏则收藏商品,已收藏则取消收藏")
    @Parameter(name = "prodId", description = "商品id" , required = true)
    public ServerResponseEntity<Boolean> addOrCancel(@RequestBody Long prodId) {
        Product product = productService.getProductByProdId(prodId, I18nMessage.getDbLang());
        if (Objects.isNull(product)) {
            // 该商品不存在
            throw new YamiShopBindException("yami.product.no.exist");
        }
        boolean isAdd = false;
        String userId = SecurityUtils.getUser().getUserId();
        //pc端收藏商品时,清除购物车缓存
        basketService.removeCacheByUserIds(Collections.singletonList(userId));
        if (userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, userId)) > 0) {
            userCollectionService.remove(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, prodId)
                    .eq(UserCollection::getUserId, userId));
        } else {
            if (Objects.equals(product.getProdType(), ProdType.PROD_TYPE_ACTIVE.value())) {
                // 活动商品不能添加收藏
                throw new YamiShopBindException("yami.active.prod.cannot.add.collection");
            }
            UserCollection userCollection = new UserCollection();
            userCollection.setCreateTime(new Date());
            userCollection.setUserId(userId);
            userCollection.setProdId(prodId);
            userCollectionService.save(userCollection);
            isAdd = true;
        }
        return ServerResponseEntity.success(isAdd);
    }
 
    @PostMapping("/batachCancel")
    @Operation(summary = "批量取消收藏" , description = "传入收藏商品id")
    @Parameter(name = "prodIds", description = "商品id" , required = true)
    public ServerResponseEntity<Boolean> batachCancel(@RequestBody List<Long> prodIds) {
        String userId = SecurityUtils.getUser().getUserId();
        if (CollectionUtils.isEmpty(prodIds)) {
            return ServerResponseEntity.success(false);
        }
        boolean remove = false;
        for (Long prodId : prodIds) {
            if (Objects.isNull(productService.getProductByProdId(prodId, I18nMessage.getDbLang()))) {
                continue;
            }
            int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, prodId)
                    .eq(UserCollection::getUserId, userId));
            if (count <= 0) {
                continue;
            }
            remove = userCollectionService.remove(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, prodId)
                    .eq(UserCollection::getUserId, userId));
        }
        return ServerResponseEntity.success(remove);
    }
 
    @GetMapping("count")
    @Operation(summary = "查询用户收藏商品数量" , description = "查询用户收藏商品数量")
    public int findProdCollectionCount() {
        String userId = SecurityUtils.getUser().getUserId();
        return userCollectionService.count(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId, userId));
    }
 
    @GetMapping("collectionCount")
    @Operation(summary = "查询用户收藏数量(商品+店铺收藏数量)" , description = "查询用户收藏数量")
    public ServerResponseEntity<Integer> userCollectionCount() {
        String userId = SecurityUtils.getUser().getUserId();
        int prodCollectionCount = userCollectionService.count(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId, userId));
        int shopCollectionCount = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getUserId, userId));
        return ServerResponseEntity.success(prodCollectionCount + shopCollectionCount);
    }
 
    @GetMapping("/prods")
    @Operation(summary = "获取用户收藏商品列表" , description = "获取用户收藏商品列表")
    public ServerResponseEntity<IPage<ProductDto>> collectionProds(PageParam page) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<ProductDto> productDtoPage = productService.collectionProds(page, userId, I18nMessage.getDbLang());
        return ServerResponseEntity.success(productDtoPage);
    }
 
    @PostMapping("/orderProdCollectionAll")
    @Operation(summary = "订单商品收藏" , description = "传入商品id拼接字符串")
    @Parameters({
            @Parameter(name = "prodIds", description = "商品id" , required = true),
    })
    public ServerResponseEntity<Boolean> orderProdCollectionAll(@RequestBody String prodIds) {
        String userId = SecurityUtils.getUser().getUserId();
        //去重
        HashSet<String> prodSet = CollUtil.newHashSet(prodIds.split(StrUtil.COMMA));
        List<Product> productList = productService.listByIds(prodSet);
        List<Long> prodIdList = productList.stream()
                .filter(product -> !Objects.equals(product.getProdType(), ProdType.PROD_TYPE_ACTIVE.value()))
                .map(Product::getProdId)
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(prodIdList)) {
            userCollectionService.orderProdCollectionAll(prodIdList, userId);
        }
        return ServerResponseEntity.success();
    }
}