master:app用户搜索历史功能完善;

pull/1121/head
liujiang 2025-09-02 14:31:13 +08:00
parent 26f195d2bd
commit cfab581c70
3 changed files with 53 additions and 1 deletions

View File

@ -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<Integer> clearSearchHisRecord() {
return R.ok(userSearchHisService.clearSearchHisRecord());
}
}

View File

@ -16,4 +16,10 @@ public interface IUserSearchHistoryService {
* @return List<String>
*/
List<String> recordList();
/**
*
* @return Integer
*/
Integer clearSearchHisRecord();
}

View File

@ -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<String> recordList() {
List<UserSearchHistoryDTO> redisList = this.redisCache.getCacheObject(CacheConstants.USER_SEARCH_HISTORY + SecurityUtils.getUserId());
Long userId = SecurityUtils.getUserIdSafe();
if (ObjectUtils.isEmpty(userId)) {
return Collections.emptyList();
}
List<UserSearchHistoryDTO> 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<UserSearchHistory> searchHisList = this.userSearchHisMapper.selectList(new LambdaQueryWrapper<UserSearchHistory>()
.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();
}
}