Commit 8b3bfcbd6b42530107b6f58d76f466ba521f11ca

Authored by YunaiV
1 parent bbee7207

📖 MALL:code review 商品浏览记录

yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/history/AppProductBrowseHistoryController.java
... ... @@ -28,7 +28,6 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
28 28 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
29 29 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
30 30  
31   -// TODO 芋艿:后面再看
32 31 @Tag(name = "用户 APP - 商品浏览记录")
33 32 @RestController
34 33 @RequestMapping("/product/browse-history")
... ... @@ -50,18 +49,11 @@ public class AppProductBrowseHistoryController {
50 49 @DeleteMapping(value = "/clean")
51 50 @Operation(summary = "清空商品浏览记录")
52 51 @PreAuthenticated
53   - public CommonResult<Boolean> cleanBrowseHistory() {
  52 + public CommonResult<Boolean> deleteBrowseHistory() {
54 53 productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), null);
55 54 return success(Boolean.TRUE);
56 55 }
57 56  
58   - @GetMapping(value = "/get-count")
59   - @Operation(summary = "获得商品浏览记录数量")
60   - @PreAuthenticated
61   - public CommonResult<Long> getBrowseHistoryCount() {
62   - return success(productBrowseHistoryService.getBrowseHistoryCount(getLoginUserId(), false));
63   - }
64   -
65 57 @GetMapping(value = "/page")
66 58 @Operation(summary = "获得商品浏览记录分页")
67 59 @PreAuthenticated
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/history/ProductBrowseHistoryMapper.java
... ... @@ -20,6 +20,12 @@ import java.util.Collection;
20 20 @Mapper
21 21 public interface ProductBrowseHistoryMapper extends BaseMapperX<ProductBrowseHistoryDO> {
22 22  
  23 + default ProductBrowseHistoryDO selectByUserIdAndSpuId(Long userId, Long spuId) {
  24 + return selectOne(new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
  25 + .eq(ProductBrowseHistoryDO::getUserId, userId)
  26 + .eq(ProductBrowseHistoryDO::getSpuId, spuId));
  27 + }
  28 +
23 29 default PageResult<ProductBrowseHistoryDO> selectPage(ProductBrowseHistoryPageReqVO reqVO) {
24 30 return selectPage(reqVO, new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
25 31 .eqIfPresent(ProductBrowseHistoryDO::getUserId, reqVO.getUserId())
... ... @@ -36,12 +42,6 @@ public interface ProductBrowseHistoryMapper extends BaseMapperX&lt;ProductBrowseHis
36 42 .set(ProductBrowseHistoryDO::getUserDeleted, userDeleted));
37 43 }
38 44  
39   - default Long selectCountByUserIdAndUserDeleted(Long userId, Boolean userDeleted) {
40   - return selectCount(new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
41   - .eq(ProductBrowseHistoryDO::getUserId, userId)
42   - .eqIfPresent(ProductBrowseHistoryDO::getUserDeleted, userDeleted));
43   - }
44   -
45 45 default Page<ProductBrowseHistoryDO> selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) {
46 46 Page<ProductBrowseHistoryDO> page = Page.of(pageNo, pageSize);
47 47 return selectPage(page, new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryService.java
... ... @@ -32,15 +32,6 @@ public interface ProductBrowseHistoryService {
32 32 void hideUserBrowseHistory(Long userId, Collection<Long> spuId);
33 33  
34 34 /**
35   - * 获取用户记录数量
36   - *
37   - * @param userId 用户编号
38   - * @param userDeleted 用户是否删除
39   - * @return 数量
40   - */
41   - Long getBrowseHistoryCount(Long userId, Boolean userDeleted);
42   -
43   - /**
44 35 * 获得商品浏览记录分页
45 36 *
46 37 * @param pageReqVO 分页查询
... ...
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryServiceImpl.java
... ... @@ -20,6 +20,7 @@ import java.util.Collection;
20 20 @Service
21 21 @Validated
22 22 public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryService {
  23 +
23 24 private static final int USER_STORE_MAXIMUM = 100;
24 25  
25 26 @Resource
... ... @@ -33,14 +34,14 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ
33 34 }
34 35  
35 36 // 情况一:同一个商品,只保留最新的一条记录
36   - ProductBrowseHistoryDO historyDO = browseHistoryMapper.selectOne(ProductBrowseHistoryDO::getUserId, userId, ProductBrowseHistoryDO::getSpuId, spuId);
37   - if (historyDO != null) {
38   - browseHistoryMapper.deleteById(historyDO);
  37 + ProductBrowseHistoryDO history = browseHistoryMapper.selectByUserIdAndSpuId(userId, spuId);
  38 + if (history != null) {
  39 + browseHistoryMapper.deleteById(history);
39 40 } else {
40 41 // 情况二:限制每个用户的浏览记录的条数(只查一条最早地记录、记录总数)
  42 + // TODO @疯狂:这里最好先查询一次数量。如果发现超过了,再删除;主要考虑,可能有部分不超过,提前就多了一次 sql 查询了
41 43 Page<ProductBrowseHistoryDO> pageResult = browseHistoryMapper.selectPageByUserIdOrderByCreateTimeAsc(userId, 1, 1);
42 44 if (pageResult.getTotal() >= USER_STORE_MAXIMUM) {
43   - // 删除最早的一条
44 45 browseHistoryMapper.deleteById(CollUtil.getFirst(pageResult.getRecords()));
45 46 }
46 47 }
... ... @@ -58,11 +59,6 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ
58 59 }
59 60  
60 61 @Override
61   - public Long getBrowseHistoryCount(Long userId, Boolean userDeleted) {
62   - return browseHistoryMapper.selectCountByUserIdAndUserDeleted(userId, userDeleted);
63   - }
64   -
65   - @Override
66 62 public PageResult<ProductBrowseHistoryDO> getBrowseHistoryPage(ProductBrowseHistoryPageReqVO pageReqVO) {
67 63 return browseHistoryMapper.selectPage(pageReqVO);
68 64 }
... ...