From cfab581c7014b23185500f595a380ffc1ce1759e Mon Sep 17 00:00:00 2001 From: liujiang <569804566@qq.com> Date: Tue, 2 Sep 2025 14:31:13 +0800 Subject: [PATCH] =?UTF-8?q?master=EF=BC=9Aapp=E7=94=A8=E6=88=B7=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8E=86=E5=8F=B2=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkt/UserSearchHistoryController.java | 8 ++++ .../service/IUserSearchHistoryService.java | 6 +++ .../impl/UserSearchHistoryServiceImpl.java | 40 ++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/xkt/UserSearchHistoryController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/xkt/UserSearchHistoryController.java index 9b0449df4..3e00ae19e 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/xkt/UserSearchHistoryController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/xkt/UserSearchHistoryController.java @@ -6,6 +6,7 @@ import com.ruoyi.xkt.service.IUserSearchHistoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -32,4 +33,11 @@ public class UserSearchHistoryController extends XktBaseController { return R.ok(userSearchHisService.recordList()); } + + @ApiOperation(value = "删除用户搜索历史", httpMethod = "DELETE", response = R.class) + @DeleteMapping("") + public R clearSearchHisRecord() { + return R.ok(userSearchHisService.clearSearchHisRecord()); + } + } diff --git a/xkt/src/main/java/com/ruoyi/xkt/service/IUserSearchHistoryService.java b/xkt/src/main/java/com/ruoyi/xkt/service/IUserSearchHistoryService.java index 86c05e9a5..d8acfe2db 100644 --- a/xkt/src/main/java/com/ruoyi/xkt/service/IUserSearchHistoryService.java +++ b/xkt/src/main/java/com/ruoyi/xkt/service/IUserSearchHistoryService.java @@ -16,4 +16,10 @@ public interface IUserSearchHistoryService { * @return List */ List recordList(); + + /** + * 清空用户的搜索历史 + * @return Integer + */ + Integer clearSearchHisRecord(); } diff --git a/xkt/src/main/java/com/ruoyi/xkt/service/impl/UserSearchHistoryServiceImpl.java b/xkt/src/main/java/com/ruoyi/xkt/service/impl/UserSearchHistoryServiceImpl.java index ea81e4ce9..6ca12bc86 100644 --- a/xkt/src/main/java/com/ruoyi/xkt/service/impl/UserSearchHistoryServiceImpl.java +++ b/xkt/src/main/java/com/ruoyi/xkt/service/impl/UserSearchHistoryServiceImpl.java @@ -1,14 +1,21 @@ package com.ruoyi.xkt.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ruoyi.common.constant.CacheConstants; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.xkt.domain.UserSearchHistory; import com.ruoyi.xkt.dto.useSearchHistory.UserSearchHistoryDTO; +import com.ruoyi.xkt.mapper.UserSearchHistoryMapper; import com.ruoyi.xkt.service.IUserSearchHistoryService; import lombok.RequiredArgsConstructor; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -24,14 +31,45 @@ import java.util.stream.Collectors; public class UserSearchHistoryServiceImpl implements IUserSearchHistoryService { final RedisCache redisCache; + final UserSearchHistoryMapper userSearchHisMapper; @Override @Transactional(readOnly = true) public List recordList() { - List redisList = this.redisCache.getCacheObject(CacheConstants.USER_SEARCH_HISTORY + SecurityUtils.getUserId()); + Long userId = SecurityUtils.getUserIdSafe(); + if (ObjectUtils.isEmpty(userId)) { + return Collections.emptyList(); + } + List redisList = this.redisCache.getCacheObject(CacheConstants.USER_SEARCH_HISTORY + userId); + if (CollectionUtils.isEmpty(redisList)) { + return Collections.emptyList(); + } // 按照搜索时间倒序排列,最新的搜索数据展示在最前面 return redisList.stream().sorted(Comparator.comparing(UserSearchHistoryDTO::getSearchTime).reversed()) .map(UserSearchHistoryDTO::getSearchContent).collect(Collectors.toList()); } + /** + * 清空用户搜索历史 + * + * @return Integer + */ + @Override + @Transactional + public Integer clearSearchHisRecord() { + Long userId = SecurityUtils.getUserIdSafe(); + if (ObjectUtils.isEmpty(userId)) { + return 0; + } + this.redisCache.deleteObject(CacheConstants.USER_SEARCH_HISTORY + userId); + // 获取用户的浏览历史 + List searchHisList = this.userSearchHisMapper.selectList(new LambdaQueryWrapper() + .eq(UserSearchHistory::getUserId, userId).eq(UserSearchHistory::getDelFlag, Constants.UNDELETED)); + if (CollectionUtils.isEmpty(searchHisList)) { + return 0; + } + searchHisList.forEach(x -> x.setDelFlag(Constants.DELETED)); + return this.userSearchHisMapper.updateById(searchHisList).size(); + } + }