基于mall4j产品的二开项目后端
lee
2024-12-18 921461a3f906d74403aeb6a27051deb77eca10fc
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * 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.CategoryDto;
import com.yami.shop.bean.dto.DerivativeCategoryDto;
import com.yami.shop.bean.enums.EsOperationType;
import com.yami.shop.bean.event.EsProductUpdateEvent;
import com.yami.shop.bean.model.Category;
import com.yami.shop.bean.model.CategoryShop;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.vo.CategoryShopVO;
import com.yami.shop.bean.vo.CategoryVO;
import com.yami.shop.common.annotation.RedisLock;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
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.*;
import io.swagger.v3.oas.annotations.media.Schema;
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 ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
import static java.util.stream.Collectors.groupingBy;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/prod/category")
@Tag(name = "分类接口")
public class CategoryController {
 
    @Autowired
    private CategoryService categoryService;
 
    @Autowired
    private MapperFacade mapperFacade;
 
    @Autowired
    private ProductService productService;
 
    @Autowired
    private CategoryShopService categoryShopService;
 
    @Autowired
    private ApplicationEventPublisher eventPublisher;
 
    @Autowired
    private CategoryExcelService categoryExcelService;
 
    @Autowired
    private LifeServiceCategoryService lifeServiceCategoryService;
 
    @Autowired
    private ShopDetailService shopDetailService;
 
    public static final Integer SHOP = 0;
    public static final Integer LIFE_SERVICE_SHOP = 1;
    public static final Integer TECHNICIAN = 2;
 
    @GetMapping("/categoryInfo")
    @Operation(summary = "分类信息列表" , description = "获取所有的产品分类信息,顶级分类的parentId为0,默认为顶级分类")
    @Parameters({
            @Parameter(name = "parentId", description = "分类ID" ),
            @Parameter(name = "shopId", description = "店铺id" )
    })
    public ServerResponseEntity<List<CategoryDto>> categoryInfo(@RequestParam(value = "parentId", defaultValue = "0") Long parentId,
                                                          @RequestParam(value = "shopId", defaultValue = "0") Long shopId) {
        List<Category> categories = categoryService.listByParentIdAndShopId(parentId, shopId, I18nMessage.getDbLang());
        List<CategoryDto> categoryDtos = mapperFacade.mapAsList(categories, CategoryDto.class);
        return ServerResponseEntity.success(categoryDtos);
    }
 
    /**
     * 根据店铺类型获取签约的分类列表
     * @param status
     * @param shopType
     * @return
     */
    @GetMapping("/listSigningCategoryByShopType/{shopType}")
    @Operation(summary = "根据店铺类型获取签约的分类列表(状态参数为空则返回所有)" , description = "根据店铺类型获取签约的分类列表(状态参数为空则返回所有)")
    @Parameter(name = "status", description = "签约状态:1:已通过 0待审核 -1未通过" )
    public ServerResponseEntity<List<String>> listByShopType(@RequestParam(value = "status", required = false) Integer status,
                                                                     @PathVariable("shopType") Integer shopType) {
//        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        List<String> categoryShopVOList = null;
        if (Objects.equals(SHOP,shopType)) {
//            categoryShopVOList = categoryShopService.listSigningCategoryByShopId(shopId, I18nMessage.getLang());
            categoryShopVOList = categoryShopService.getCategory();
        }else{
//            categoryShopVOList = lifeServiceCategoryService.listSigningCategoryByShopId(shopId, I18nMessage.getLang());
            categoryShopVOList = lifeServiceCategoryService.getCategory();
        }
        return ServerResponseEntity.success(categoryShopVOList);
    }
 
    /**
     * 批量签约分类
     * @param categoryShopList
     * @param shopType
     * @return
     */
    @PostMapping("/signing/{shopType}")
    @Operation(summary = "批量签约分类(申请店铺时使用)" , description = "批量签约分类(申请店铺时使用)")
    public ServerResponseEntity signingCategoryByShopType(@RequestBody List categoryShopList,
                                                                @PathVariable("shopType") Integer shopType) {
        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        if (Objects.equals(shopType,SHOP)){
            categoryShopService.signingCategory(categoryShopList, shopId, false);
        }else{
            lifeServiceCategoryService.signingCategory(categoryShopList,shopId,false);
        }
 
        return ServerResponseEntity.success();
    }
 
    @GetMapping("/info/{categoryId}")
    @Operation(summary = "根据分类id获取分类信息" , description = "根据分类id获取分类信息")
    public ServerResponseEntity<Category> info(@PathVariable("categoryId") Long categoryId){
        // 获取数据库中的分类,如果分类为空则抛异常
        Category category = categoryService.getCategoryByCategoryIdAndShopId(categoryId, shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        if (!Objects.equals(SecurityUtils.getUser().getShopId(), category.getShopId())) {
            throw new YamiShopBindException("yami.no.auth");
        }
        return ServerResponseEntity.success(category);
    }
 
    @GetMapping("/getCdnCategory")
    @Operation(summary = "获取CDN分类信息" , description = "获取CDN分类信息")
    public ServerResponseEntity<List<Category>> getCdnCategory(){
        //获取二级分类
        List<Category> categories = categoryService.listByParentIdAndShopId(342L, 0L, I18nMessage.getDbLang());
        for(Category categoryTwo : categories) {
            List<Category> categoriesThree = categoryService.listByParentIdAndShopId(categoryTwo.getCategoryId(),0L, I18nMessage.getDbLang());
            List<DerivativeCategoryDto> derivativeCategories = mapperFacade.mapAsList(categoriesThree, DerivativeCategoryDto.class);
            categoryTwo.setCategories(derivativeCategories);
        }
        return ServerResponseEntity.success(categories);
    }
 
    @SysLog("保存分类")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('prod:category:save')")
    @Operation(summary = "保存分类信息" , description = "保存分类信息")
    @RedisLock(lockName = "saveCategoryLock",key = "#Category")
    public ServerResponseEntity<Long> save(@RequestBody Category category){
        category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        category.setRecTime(new Date());
        Integer count = categoryService.getCategoryName(category);
        if(count > 0){
            // 类目名称已存在!
            throw new YamiShopBindException("yami.category.name.exist");
        }
        category.setSeq(Objects.isNull(category.getSeq()) ? 0 : category.getSeq());
        categoryService.saveCategroy(category);
        categoryService.removeCacheByParentIdAndLang(category.getParentId(),category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang());
        categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang());
        return ServerResponseEntity.success(category.getCategoryId());
    }
 
    @SysLog("更新分类")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('prod:category:update')")
    @Operation(summary = "更新分类信息" , description = "更新分类信息")
    public ServerResponseEntity<String> update(@RequestBody Category category){
        // 获取数据库中的分类,如果分类为空则抛异常
        categoryService.getCategoryByCategoryIdAndShopId(category.getCategoryId(),SecurityUtils.getUser().getShopId());
        category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        Category categoryDb = categoryService.getCategoryByCategoryId(category.getCategoryId());
        if (Objects.equals(categoryDb.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(category.getParentId(), Constant.CATEGORY_ID)){
            // 一级分类不能改为二级分类
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.one.check"));
        }else if(Objects.equals(category.getParentId(), Constant.CATEGORY_ID) && !Objects.equals(categoryDb.getParentId(), Constant.CATEGORY_ID)){
            // 二级分类不能改为一级分类
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.two.check"));
        }
        if (Objects.equals(category.getParentId(),category.getCategoryId())) {
            // 分类的上级不能是自己本身
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.superior.check"));
        }
        Integer count = categoryService.getCategoryName(category);
        if(count > 0){
            // 类目名称已存在!
            throw new YamiShopBindException("yami.category.name.exist");
        }
        category.setCategoryName(categoryDb.getCategoryName());
        category.setOldCategoryName(categoryDb.getCategoryName());
        category.setOldCategoryNameEn(categoryDb.getCategoryNameEn());
        categoryService.updateCategroy(category);
        categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang());
        categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang());
        eventPublisher.publishEvent(new EsProductUpdateEvent(category.getCategoryId(), null, EsOperationType.UPDATE_BY_SHOP_CATEGORY_ID));
        return ServerResponseEntity.success();
    }
 
    @SysLog("删除分类")
    @DeleteMapping("/{categoryId}")
    @PreAuthorize("@pms.hasPermission('prod:category:delete')")
    @Operation(summary = "根据分类id删除分类" , description = "根据分类id删除分类")
    public ServerResponseEntity<String> delete(@PathVariable("categoryId") Long categoryId){
        int categoryProdCount = productService.count(new LambdaQueryWrapper<Product>().eq(Product::getShopCategoryId, categoryId).ne(Product::getStatus, -1));
        if (categoryProdCount>0){
            // 该分类下还有商品,请先删除该分类下的商品
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.category.delete.check"));
        }
 
        Category category = categoryService.getCategoryByCategoryIdAndShopId(categoryId,shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        categoryService.deleteCategroy(category);
        categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_ZH_CN.getLang());
        categoryService.removeCacheByParentIdAndLang(category.getParentId(), category.getShopId(), LanguageEnum.LANGUAGE_EN.getLang());
        return ServerResponseEntity.success();
    }
 
    @GetMapping("/listCategory")
    @PreAuthorize("@pms.hasPermission('prod:category:listCategory')")
    @Operation(summary = "获取一级分类" , description = "获取一级分类")
    public ServerResponseEntity<List<Category>> listCategory(@ParameterObject Category category){
        category.setLang(I18nMessage.getLang());
        category.setGrade(1);
        category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        List<Category> list =  categoryService.categoryLangList(category);
        return ServerResponseEntity.success(list);
    }
 
    @GetMapping("/pageCategory")
    @PreAuthorize("@pms.hasPermission('prod:category:listCategory')")
    @Operation(summary = "分页获取一级分类" , description = "分页获取一级分类")
    public ServerResponseEntity<IPage<Category>> pageCategory(PageParam<Category> page, @ParameterObject Category category){
        category.setLang(I18nMessage.getLang());
        category.setGrade(1);
        category.setShopId(shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId());
        IPage<Category> categoryPage =  categoryService.pageCategoryLangList(page, category);
        return ServerResponseEntity.success(categoryPage);
    }
 
    @GetMapping("/platformCategory")
    @Operation(summary = "获取平台所有上架的分类" , description = "获取平台所有上架的分类")
    public ServerResponseEntity<List<Category>> listCategory(){
        List<Category> list =  categoryService.platformCategory();
        return ServerResponseEntity.success(list);
    }
 
    @PostMapping("/signing")
    @Operation(summary = "批量签约分类(申请店铺时使用)" , description = "批量签约分类(申请店铺时使用)")
    public ServerResponseEntity<Void> signingCategory(@RequestBody List<CategoryShop> categoryShopList) {
        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        categoryShopService.signingCategory(categoryShopList, shopId, false);
        return ServerResponseEntity.success();
    }
 
    @GetMapping("/listSigningCategory")
    @Operation(summary = "获取签约的分类列表(状态参数为空则返回所有)" , description = "获取签约的分类列表(状态参数为空则返回所有)")
    @Parameter(name = "status", description = "签约状态:1:已通过 0待审核 -1未通过" )
    public ServerResponseEntity<List<CategoryShopVO>> listByShopId(@RequestParam(value = "status", required = false) Integer status) {
        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        List<CategoryShopVO> categoryShopVOList = categoryShopService.listSigningCategoryByShopId(shopId, I18nMessage.getLang());
        if (Objects.nonNull(status)) {
            categoryShopVOList = categoryShopVOList.stream().filter(item -> Objects.equals(item.getStatus(), status)).collect(Collectors.toList());
        }
        return ServerResponseEntity.success(categoryShopVOList);
    }
 
    @GetMapping("/listAvailableSigningCategory")
    @Schema(description = "获取店铺签约的可用分类列表(发布商品时使用)" )
    public ServerResponseEntity<List<Category>> listAvailableSigningCategory() {
        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        List<Category> categories = categoryShopService.listAvailableSigningCategory(shopId, I18nMessage.getLang());
        return ServerResponseEntity.success(categories);
    }
 
    @GetMapping("/shopCategory")
    @Operation(summary = "获取店铺分类" , description = "获取店铺分类")
    public ServerResponseEntity<List<CategoryVO>> shopCategory(@RequestParam(value = "categoryType") Integer categoryType){
        Category category = new Category();
        category.setLang(I18nMessage.getLang());
        category.setGrade(1);
        category.setShopId(1L);
        category.setCategoryType(categoryType);
        List<Category> list =  categoryService.categoryLangList(category);
        List<CategoryVO> categoryVOS = mapperFacade.mapAsList(list, CategoryVO.class);
        return ServerResponseEntity.success(categoryVOS);
    }
 
 
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('prod:category:export')")
    @Operation(summary = "导出分类" , description = "导出分类")
    public void export(HttpServletResponse response) {
        Long shopId = shopDetailService.getShopDetailByUserId(SecurityUtils.getUser().getUserId()).getShopId();
        categoryExcelService.export(response, shopId);
    }
 
}