基于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
/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许,不可做商业用途!
 *
 * 版权所有,侵权必究!
 */
package com.yami.shop.api.controller;
 
 
import cn.hutool.core.collection.CollectionUtil;
import com.yami.shop.bean.dto.HotSearchDto;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.HotSearchService;
import com.yami.shop.service.ProductService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Collections;
import java.util.List;
 
/**
 * @author LGH
 */
@RestController
@RequestMapping("/search")
@Tag(name = "搜索接口")
public class SearchController {
 
    @Autowired
    private HotSearchService hotSearchService;
 
    @Autowired
    private ProductService productService;
 
    @GetMapping("/hotSearchByShopId")
    @Operation(summary = "查看店铺热搜" , description = "根据店铺id,热搜数量获取热搜")
    @Parameters({
            @Parameter(name = "shopId", description = "店铺id" , required = true),
            @Parameter(name = "number", description = "取数" , required = true),
            @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)" ),
    })
    public ServerResponseEntity<List<HotSearchDto>> hotSearchByShopId(Long shopId, Integer number, Integer sort) {
        List<HotSearchDto> list = hotSearchService.getHotSearchDtoByshopId(shopId);
        return getListResponseEntity(number, sort, list);
    }
 
    @GetMapping("/hotSearch")
    @Operation(summary = "查看全局热搜" , description = "根据店铺id,热搜数量获取热搜")
    @Parameters({
            @Parameter(name = "number", description = "取数" , required = true),
            @Parameter(name = "sort", description = "是否按照顺序(0 否 1是)" ),
    })
    public ServerResponseEntity<List<HotSearchDto>> hotSearch(Integer number, Integer sort) {
        List<HotSearchDto> list = hotSearchService.getHotSearchDtoByshopId(0L);
        return getListResponseEntity(number, sort, list);
    }
 
    private ServerResponseEntity<List<HotSearchDto>> getListResponseEntity(Integer number, Integer sort, List<HotSearchDto> list) {
        if (sort == null || sort == 0) {
            Collections.shuffle(list);
        }
/*        if(sort==1){
            Collections.sort(list, new SeqComparator()); // 根据排序号排序
        }*/
        if (!CollectionUtil.isNotEmpty(list) || list.size() < number) {
            return ServerResponseEntity.success(list);
        }
 
        return ServerResponseEntity.success(list.subList(0, number));
    }
}