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