From 8b3bfcbd6b42530107b6f58d76f466ba521f11ca Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 13 Jan 2024 20:23:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=96=20MALL=EF=BC=9Acode=20review=20?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=B5=8F=E8=A7=88=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../history/AppProductBrowseHistoryController.java | 10 +--------- .../mysql/history/ProductBrowseHistoryMapper.java | 12 ++++++------ .../history/ProductBrowseHistoryService.java | 9 --------- .../history/ProductBrowseHistoryServiceImpl.java | 14 +++++--------- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/history/AppProductBrowseHistoryController.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/history/AppProductBrowseHistoryController.java index 61bca49c5..5b0d292b1 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/app/history/AppProductBrowseHistoryController.java +++ b/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. import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; -// TODO 芋艿:后面再看 @Tag(name = "用户 APP - 商品浏览记录") @RestController @RequestMapping("/product/browse-history") @@ -50,18 +49,11 @@ public class AppProductBrowseHistoryController { @DeleteMapping(value = "/clean") @Operation(summary = "清空商品浏览记录") @PreAuthenticated - public CommonResult cleanBrowseHistory() { + public CommonResult deleteBrowseHistory() { productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), null); return success(Boolean.TRUE); } - @GetMapping(value = "/get-count") - @Operation(summary = "获得商品浏览记录数量") - @PreAuthenticated - public CommonResult getBrowseHistoryCount() { - return success(productBrowseHistoryService.getBrowseHistoryCount(getLoginUserId(), false)); - } - @GetMapping(value = "/page") @Operation(summary = "获得商品浏览记录分页") @PreAuthenticated diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/history/ProductBrowseHistoryMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/history/ProductBrowseHistoryMapper.java index 24ad124cc..124357cac 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/history/ProductBrowseHistoryMapper.java +++ b/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; @Mapper public interface ProductBrowseHistoryMapper extends BaseMapperX { + default ProductBrowseHistoryDO selectByUserIdAndSpuId(Long userId, Long spuId) { + return selectOne(new LambdaQueryWrapperX() + .eq(ProductBrowseHistoryDO::getUserId, userId) + .eq(ProductBrowseHistoryDO::getSpuId, spuId)); + } + default PageResult selectPage(ProductBrowseHistoryPageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() .eqIfPresent(ProductBrowseHistoryDO::getUserId, reqVO.getUserId()) @@ -36,12 +42,6 @@ public interface ProductBrowseHistoryMapper extends BaseMapperX() - .eq(ProductBrowseHistoryDO::getUserId, userId) - .eqIfPresent(ProductBrowseHistoryDO::getUserDeleted, userDeleted)); - } - default Page selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) { Page page = Page.of(pageNo, pageSize); return selectPage(page, new LambdaQueryWrapperX() diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryService.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryService.java index fd696e639..10f3a71d5 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryService.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryService.java @@ -31,15 +31,6 @@ public interface ProductBrowseHistoryService { */ void hideUserBrowseHistory(Long userId, Collection spuId); - /** - * 获取用户记录数量 - * - * @param userId 用户编号 - * @param userDeleted 用户是否删除 - * @return 数量 - */ - Long getBrowseHistoryCount(Long userId, Boolean userDeleted); - /** * 获得商品浏览记录分页 * diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryServiceImpl.java index 6415197fb..b0152995f 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/history/ProductBrowseHistoryServiceImpl.java +++ b/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; @Service @Validated public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryService { + private static final int USER_STORE_MAXIMUM = 100; @Resource @@ -33,14 +34,14 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ } // 情况一:同一个商品,只保留最新的一条记录 - ProductBrowseHistoryDO historyDO = browseHistoryMapper.selectOne(ProductBrowseHistoryDO::getUserId, userId, ProductBrowseHistoryDO::getSpuId, spuId); - if (historyDO != null) { - browseHistoryMapper.deleteById(historyDO); + ProductBrowseHistoryDO history = browseHistoryMapper.selectByUserIdAndSpuId(userId, spuId); + if (history != null) { + browseHistoryMapper.deleteById(history); } else { // 情况二:限制每个用户的浏览记录的条数(只查一条最早地记录、记录总数) + // TODO @疯狂:这里最好先查询一次数量。如果发现超过了,再删除;主要考虑,可能有部分不超过,提前就多了一次 sql 查询了 Page pageResult = browseHistoryMapper.selectPageByUserIdOrderByCreateTimeAsc(userId, 1, 1); if (pageResult.getTotal() >= USER_STORE_MAXIMUM) { - // 删除最早的一条 browseHistoryMapper.deleteById(CollUtil.getFirst(pageResult.getRecords())); } } @@ -57,11 +58,6 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ browseHistoryMapper.updateUserDeletedByUserId(userId, spuIds, true); } - @Override - public Long getBrowseHistoryCount(Long userId, Boolean userDeleted) { - return browseHistoryMapper.selectCountByUserIdAndUserDeleted(userId, userDeleted); - } - @Override public PageResult getBrowseHistoryPage(ProductBrowseHistoryPageReqVO pageReqVO) { return browseHistoryMapper.selectPage(pageReqVO);