|
@@ -0,0 +1,90 @@
|
|
|
+package cn.iocoder.yudao.module.product.controller.app.history;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
|
|
|
+import cn.iocoder.yudao.module.product.controller.admin.history.vo.ProductBrowseHistoryPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.app.history.vo.AppProductBrowseHistoryDeleteReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.app.history.vo.AppProductBrowseHistoryPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.product.controller.app.history.vo.AppProductBrowseHistoryRespVO;
|
|
|
+import cn.iocoder.yudao.module.product.dal.dataobject.history.ProductBrowseHistoryDO;
|
|
|
+import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
|
|
+import cn.iocoder.yudao.module.product.service.history.ProductBrowseHistoryService;
|
|
|
+import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import jakarta.validation.Valid;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
|
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
+
|
|
|
+@Tag(name = "用户 APP - 商品浏览记录")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/product/browse-history")
|
|
|
+public class AppProductBrowseHistoryController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ProductBrowseHistoryService productBrowseHistoryService;
|
|
|
+ @Resource
|
|
|
+ private ProductSpuService productSpuService;
|
|
|
+
|
|
|
+ @DeleteMapping(value = "/delete")
|
|
|
+ @Operation(summary = "删除商品浏览记录")
|
|
|
+ @PreAuthenticated
|
|
|
+ public CommonResult<Boolean> deleteBrowseHistory(@RequestBody @Valid AppProductBrowseHistoryDeleteReqVO reqVO) {
|
|
|
+ productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), reqVO.getSpuIds());
|
|
|
+ return success(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping(value = "/clean")
|
|
|
+ @Operation(summary = "清空商品浏览记录")
|
|
|
+ @PreAuthenticated
|
|
|
+ public CommonResult<Boolean> cleanBrowseHistory() {
|
|
|
+ productBrowseHistoryService.hideUserBrowseHistory(getLoginUserId(), null);
|
|
|
+ return success(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/get-count")
|
|
|
+ @Operation(summary = "获得商品浏览记录数量")
|
|
|
+ @PreAuthenticated
|
|
|
+ public CommonResult<Long> getBrowseHistoryCount() {
|
|
|
+ return success(productBrowseHistoryService.getBrowseHistoryCount(getLoginUserId(), false));
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/page")
|
|
|
+ @Operation(summary = "获得商品浏览记录分页")
|
|
|
+ @PreAuthenticated
|
|
|
+ public CommonResult<PageResult<AppProductBrowseHistoryRespVO>> getBrowseHistoryPage(AppProductBrowseHistoryPageReqVO reqVO) {
|
|
|
+ ProductBrowseHistoryPageReqVO pageReqVO = BeanUtils.toBean(reqVO, ProductBrowseHistoryPageReqVO.class);
|
|
|
+ pageReqVO.setUserId(getLoginUserId());
|
|
|
+ // 排除用户已删除的(隐藏的)
|
|
|
+ pageReqVO.setUserDeleted(false);
|
|
|
+ PageResult<ProductBrowseHistoryDO> pageResult = productBrowseHistoryService.getBrowseHistoryPage(pageReqVO);
|
|
|
+ if (CollUtil.isEmpty(pageResult.getList())) {
|
|
|
+ return success(PageResult.empty());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 得到商品 spu 信息
|
|
|
+ Set<Long> spuIds = convertSet(pageResult.getList(), ProductBrowseHistoryDO::getSpuId);
|
|
|
+ Map<Long, ProductSpuDO> spuMap = convertMap(productSpuService.getSpuList(spuIds), ProductSpuDO::getId);
|
|
|
+
|
|
|
+ // 转换 VO 结果
|
|
|
+ PageResult<AppProductBrowseHistoryRespVO> result = BeanUtils.toBean(pageResult, AppProductBrowseHistoryRespVO.class,
|
|
|
+ vo -> Optional.ofNullable(spuMap.get(vo.getSpuId())).ifPresent(spu -> {
|
|
|
+ vo.setSpuName(spu.getName());
|
|
|
+ vo.setPicUrl(spu.getPicUrl());
|
|
|
+ }));
|
|
|
+ return success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|