基于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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.UserCollectionShopDto;
import com.yami.shop.bean.model.UserCollectionShop;
import com.yami.shop.common.exception.YamiShopBindException;
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.ShopDetailService;
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.Operation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/shop/collection")
@Tag(name = "店铺收藏接口")
@AllArgsConstructor
public class UserCollectionShopController {
 
    private final UserCollectionShopService userCollectionShopService;
 
    private final ShopDetailService shopDetailService;
 
    @GetMapping("/page")
    @Operation(summary = "分页返回收藏数据" , description = "根据用户id获取")
    @Parameter(name = "shopName", description = "店铺名称" )
    public ServerResponseEntity<IPage<UserCollectionShopDto>> getUserCollectionDtoPageByUserId(PageParam page, String shopName) {
        return ServerResponseEntity.success(userCollectionShopService.getUserCollectionShopPageByUserId(page, SecurityUtils.getUser().getUserId(), shopName));
    }
 
    @GetMapping("isCollection")
    @Operation(summary = "根据店铺id获取该店铺是否在收藏夹中" , description = "传入收藏店铺id")
    @Parameter(name = "shopId", description = "店铺id" )
    public ServerResponseEntity<Boolean> isCollection(Long shopId) {
        return ServerResponseEntity.success(userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                .eq(UserCollectionShop::getShopId, shopId)
                .eq(UserCollectionShop::getUserId, SecurityUtils.getUser().getUserId())) > 0);
    }
 
    @PostMapping("/addOrCancel")
    @Operation(summary = "添加/取消收藏" , description = "传入收藏店铺id,如果店铺未收藏则收藏店铺,已收藏则取消收藏")
    @Parameter(name = "shopId", description = "店铺id" , required = true)
    public ServerResponseEntity<Boolean> addOrCancel(@RequestBody Long shopId) {
        if (Objects.isNull(shopDetailService.getById(shopId))) {
            // 该店铺不存在
            throw new YamiShopBindException("yami.store.not.exist");
        }
        boolean isAdd = false;
        String userId = SecurityUtils.getUser().getUserId();
        if (userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                .eq(UserCollectionShop::getShopId, shopId)
                .eq(UserCollectionShop::getUserId, userId)) > 0) {
            userCollectionShopService.remove(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
        } else {
            UserCollectionShop userCollectionShop = new UserCollectionShop();
            userCollectionShop.setCreateTime(new Date());
            userCollectionShop.setUserId(userId);
            userCollectionShop.setShopId(shopId);
            userCollectionShopService.save(userCollectionShop);
            isAdd = true;
        }
        return ServerResponseEntity.success(isAdd);
    }
 
    @PostMapping("/batachCancel")
    @Operation(summary = "批量取消收藏" , description = "传入收藏店铺id")
    @Parameter(name = "shopIds", description = "店铺Id" , required = true)
    public ServerResponseEntity<Boolean> batchCancel(@RequestBody List<Long> shopIds) {
        String userId = SecurityUtils.getUser().getUserId();
        if (CollectionUtils.isEmpty(shopIds)) {
            return ServerResponseEntity.success(false);
        }
        boolean remove = false;
        for (Long shopId : shopIds) {
            if (Objects.isNull(shopDetailService.getById(shopId))) {
                continue;
            }
            int count = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
            if (count <= 0) {
                continue;
            }
            remove = userCollectionShopService.remove(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
        }
        return ServerResponseEntity.success(remove);
    }
 
    @GetMapping("count")
    @Operation(summary = "查询用户收藏店铺数量" , description = "查询用户收藏店铺数量")
    public int findUserCollectionCount() {
        String userId = SecurityUtils.getUser().getUserId();
        return userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getUserId, userId));
    }
}