Update SearchController.java

cyj
pbvfus8to 8 months ago
parent 47d2a40753
commit b026dfa3a6

@ -8,9 +8,9 @@
*
*/
// 该类所属的包名表明其位于商城API的控制器包下主要用于处理与搜索相关的各种接口请求及对应的业务逻辑例如热搜查询、商品搜索等功能。
package com.yami.shop.api.controller;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
@ -32,24 +32,44 @@ import java.util.Collections;
import java.util.List;
/**
* SearchControllerSpring RESTful
* HotSearchServiceProductServiceSwagger便使
*
* @author lanhai
*/
@RestController
// 定义该控制器类的基础请求路径,所有该类中的接口请求路径都将以此为前缀,表明是与搜索相关的操作接口。
@RequestMapping("/search")
// 使用Swagger的@Tag注解对该控制器类进行标记用于在API文档中生成对应的分类标签方便接口文档的分类展示和阅读这里表示该类下的接口都属于“搜索接口”这一分类。
@Tag(name = "搜索接口")
// 使用lombok的@AllArgsConstructor注解自动生成包含所有final字段的构造函数方便依赖注入这里会为HotSearchService和ProductService生成对应的构造函数参数以便在类中使用这两个服务类的实例。
@AllArgsConstructor
public class SearchController {
// 通过构造函数注入HotSearchService实例用于调用与热搜相关的业务逻辑方法比如根据店铺ID或获取全局热搜等操作来获取热搜数据。
private final HotSearchService hotSearchService;
// 通过构造函数注入ProductService实例用于调用与商品搜索相关的业务逻辑方法例如根据商品名、排序条件等进行分页搜索商品并获取相应的商品搜索结果数据。
private final ProductService productService;
/**
* ID
* IDshopIdnumbersortHotSearchServicegetHotSearchDtoByShopId
* getListResponseEntityServerResponseEntity
*
* @param shopId
* @param number
* @param sort 01
* @return ServerResponseEntityList<HotSearchDto>HotSearchDto
*/
@GetMapping("/hotSearchByShopId")
// 使用Swagger的@Operation注解对该接口方法进行描述用于在API文档中生成对应的接口说明信息这里简要说明了该接口的功能是查看店铺热搜。
@Operation(summary = "查看店铺热搜", description = "根据店铺id,热搜数量获取热搜")
// 使用Swagger的@Parameters注解对接口方法的多个参数进行统一描述通过包含多个@Parameter注解来分别详细说明每个参数的名称、描述以及是否必填等信息方便在API文档中展示参数详情。
@Parameters({
@Parameter(name = "shopId", description = "店铺id", required = true),
@Parameter(name = "number", description = "取数", required = true),
@Parameter(name = "sort", description = "是否按照顺序(0 否 1是)"),
@Parameter(name = "sort", description = "是否按照顺序(0 否 1是)")
})
public ServerResponseEntity<List<HotSearchDto>> hotSearchByShopId(Long shopId, Integer number, Integer sort) {
List<HotSearchDto> list = hotSearchService.getHotSearchDtoByShopId(shopId);
@ -57,17 +77,38 @@ public class SearchController {
return getListResponseEntity(number, sort, list);
}
/**
*
* numbersortHotSearchServicegetHotSearchDtoByShopIdID0L
* getListResponseEntityServerResponseEntity
*
* @param number
* @param sort 01
* @return ServerResponseEntityList<HotSearchDto>HotSearchDto
*/
@GetMapping("/hotSearch")
@Operation(summary = "查看全局热搜", description = "根据店铺id,热搜数量获取热搜")
@Parameters({
@Parameter(name = "number", description = "取数", required = true),
@Parameter(name = "sort", description = "是否按照顺序(0 否 1是)", required = false ),
@Parameter(name = "sort", description = "是否按照顺序(0 否 1是)", required = false)
})
public ServerResponseEntity<List<HotSearchDto>> hotSearch(Integer number, Integer sort) {
List<HotSearchDto> list = hotSearchService.getHotSearchDtoByShopId(0L);
return getListResponseEntity(number, sort, list);
}
/**
*
* numbersortlist
* null0使Collections.shuffle
* ServerResponseEntity
* numberServerResponseEntity
*
* @param number
* @param sort
* @param list HotSearchDto
* @return ServerResponseEntityList<HotSearchDto>
*/
private ServerResponseEntity<List<HotSearchDto>> getListResponseEntity(Integer number, Integer sort, List<HotSearchDto> list) {
if (sort == null || sort == 0) {
Collections.shuffle(list);
@ -78,20 +119,28 @@ public class SearchController {
return ServerResponseEntity.success(list.subList(0, number));
}
/**
*
* pageprodNamesortorderByIDshopIdProductServicegetSearchProdDtoPageByProdName
* IPage<SearchProdDto>ServerResponseEntity
*
* @param page
* @param prodName
* @param sort 012
* @param orderBy 01sort使
* @param shopId
* @return ServerResponseEntityIPage<SearchProdDto>
*/
@GetMapping("/searchProdPage")
@Operation(summary = "分页排序搜索商品", description = "根据商品名搜索")
@Parameters({
@Parameter(name = "prodName", description = "商品名", required = true),
@Parameter(name = "sort", description = "排序(0 默认排序 1销量排序 2价格排序)"),
@Parameter(name = "orderBy", description = "排序(0升序 1降序)"),
@Parameter(name = "shopId", description = "店铺id", required = true),
@Parameter(name = "sort", description = "排序(0 默认排序 1销量排序 2价格排序)")
})
public ServerResponseEntity<IPage<SearchProdDto>> searchProdPage(PageParam page, String prodName, Integer sort, Integer orderBy, Long shopId) {
return ServerResponseEntity.success(productService.getSearchProdDtoPageByProdName(page, prodName, sort, orderBy));
}
}
Loading…
Cancel
Save