📖 MALL:code review 商品浏览记录

This commit is contained in:
YunaiV 2024-01-13 20:23:58 +08:00
parent bbee720710
commit 8b3bfcbd6b
4 changed files with 12 additions and 33 deletions

View File

@ -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.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
// TODO 芋艿后面再看
@Tag(name = "用户 APP - 商品浏览记录") @Tag(name = "用户 APP - 商品浏览记录")
@RestController @RestController
@RequestMapping("/product/browse-history") @RequestMapping("/product/browse-history")
@ -50,18 +49,11 @@ public class AppProductBrowseHistoryController {
@DeleteMapping(value = "/clean") @DeleteMapping(value = "/clean")
@Operation(summary = "清空商品浏览记录") @Operation(summary = "清空商品浏览记录")
@PreAuthenticated @PreAuthenticated
public CommonResult<Boolean> cleanBrowseHistory() { public CommonResult<Boolean> deleteBrowseHistory() {
productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), null); productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), null);
return success(Boolean.TRUE); return success(Boolean.TRUE);
} }
@GetMapping(value = "/get-count")
@Operation(summary = "获得商品浏览记录数量")
@PreAuthenticated
public CommonResult<Long> getBrowseHistoryCount() {
return success(productBrowseHistoryService.getBrowseHistoryCount(getLoginUserId(), false));
}
@GetMapping(value = "/page") @GetMapping(value = "/page")
@Operation(summary = "获得商品浏览记录分页") @Operation(summary = "获得商品浏览记录分页")
@PreAuthenticated @PreAuthenticated

View File

@ -20,6 +20,12 @@ import java.util.Collection;
@Mapper @Mapper
public interface ProductBrowseHistoryMapper extends BaseMapperX<ProductBrowseHistoryDO> { public interface ProductBrowseHistoryMapper extends BaseMapperX<ProductBrowseHistoryDO> {
default ProductBrowseHistoryDO selectByUserIdAndSpuId(Long userId, Long spuId) {
return selectOne(new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
.eq(ProductBrowseHistoryDO::getUserId, userId)
.eq(ProductBrowseHistoryDO::getSpuId, spuId));
}
default PageResult<ProductBrowseHistoryDO> selectPage(ProductBrowseHistoryPageReqVO reqVO) { default PageResult<ProductBrowseHistoryDO> selectPage(ProductBrowseHistoryPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<ProductBrowseHistoryDO>() return selectPage(reqVO, new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
.eqIfPresent(ProductBrowseHistoryDO::getUserId, reqVO.getUserId()) .eqIfPresent(ProductBrowseHistoryDO::getUserId, reqVO.getUserId())
@ -36,12 +42,6 @@ public interface ProductBrowseHistoryMapper extends BaseMapperX<ProductBrowseHis
.set(ProductBrowseHistoryDO::getUserDeleted, userDeleted)); .set(ProductBrowseHistoryDO::getUserDeleted, userDeleted));
} }
default Long selectCountByUserIdAndUserDeleted(Long userId, Boolean userDeleted) {
return selectCount(new LambdaQueryWrapperX<ProductBrowseHistoryDO>()
.eq(ProductBrowseHistoryDO::getUserId, userId)
.eqIfPresent(ProductBrowseHistoryDO::getUserDeleted, userDeleted));
}
default Page<ProductBrowseHistoryDO> selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) { default Page<ProductBrowseHistoryDO> selectPageByUserIdOrderByCreateTimeAsc(Long userId, Integer pageNo, Integer pageSize) {
Page<ProductBrowseHistoryDO> page = Page.of(pageNo, pageSize); Page<ProductBrowseHistoryDO> page = Page.of(pageNo, pageSize);
return selectPage(page, new LambdaQueryWrapperX<ProductBrowseHistoryDO>() return selectPage(page, new LambdaQueryWrapperX<ProductBrowseHistoryDO>()

View File

@ -31,15 +31,6 @@ public interface ProductBrowseHistoryService {
*/ */
void hideUserBrowseHistory(Long userId, Collection<Long> spuId); void hideUserBrowseHistory(Long userId, Collection<Long> spuId);
/**
* 获取用户记录数量
*
* @param userId 用户编号
* @param userDeleted 用户是否删除
* @return 数量
*/
Long getBrowseHistoryCount(Long userId, Boolean userDeleted);
/** /**
* 获得商品浏览记录分页 * 获得商品浏览记录分页
* *

View File

@ -20,6 +20,7 @@ import java.util.Collection;
@Service @Service
@Validated @Validated
public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryService { public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryService {
private static final int USER_STORE_MAXIMUM = 100; private static final int USER_STORE_MAXIMUM = 100;
@Resource @Resource
@ -33,14 +34,14 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ
} }
// 情况一同一个商品只保留最新的一条记录 // 情况一同一个商品只保留最新的一条记录
ProductBrowseHistoryDO historyDO = browseHistoryMapper.selectOne(ProductBrowseHistoryDO::getUserId, userId, ProductBrowseHistoryDO::getSpuId, spuId); ProductBrowseHistoryDO history = browseHistoryMapper.selectByUserIdAndSpuId(userId, spuId);
if (historyDO != null) { if (history != null) {
browseHistoryMapper.deleteById(historyDO); browseHistoryMapper.deleteById(history);
} else { } else {
// 情况二限制每个用户的浏览记录的条数只查一条最早地记录记录总数 // 情况二限制每个用户的浏览记录的条数只查一条最早地记录记录总数
// TODO @疯狂这里最好先查询一次数量如果发现超过了再删除主要考虑可能有部分不超过提前就多了一次 sql 查询了
Page<ProductBrowseHistoryDO> pageResult = browseHistoryMapper.selectPageByUserIdOrderByCreateTimeAsc(userId, 1, 1); Page<ProductBrowseHistoryDO> pageResult = browseHistoryMapper.selectPageByUserIdOrderByCreateTimeAsc(userId, 1, 1);
if (pageResult.getTotal() >= USER_STORE_MAXIMUM) { if (pageResult.getTotal() >= USER_STORE_MAXIMUM) {
// 删除最早的一条
browseHistoryMapper.deleteById(CollUtil.getFirst(pageResult.getRecords())); browseHistoryMapper.deleteById(CollUtil.getFirst(pageResult.getRecords()));
} }
} }
@ -57,11 +58,6 @@ public class ProductBrowseHistoryServiceImpl implements ProductBrowseHistoryServ
browseHistoryMapper.updateUserDeletedByUserId(userId, spuIds, true); browseHistoryMapper.updateUserDeletedByUserId(userId, spuIds, true);
} }
@Override
public Long getBrowseHistoryCount(Long userId, Boolean userDeleted) {
return browseHistoryMapper.selectCountByUserIdAndUserDeleted(userId, userDeleted);
}
@Override @Override
public PageResult<ProductBrowseHistoryDO> getBrowseHistoryPage(ProductBrowseHistoryPageReqVO pageReqVO) { public PageResult<ProductBrowseHistoryDO> getBrowseHistoryPage(ProductBrowseHistoryPageReqVO pageReqVO) {
return browseHistoryMapper.selectPage(pageReqVO); return browseHistoryMapper.selectPage(pageReqVO);