|
@@ -1,6 +1,7 @@
|
|
|
package shop.alien.store.service.impl;
|
|
package shop.alien.store.service.impl;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -12,8 +13,10 @@ import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.LawyerUserSearchHistory;
|
|
import shop.alien.entity.store.LawyerUserSearchHistory;
|
|
|
import shop.alien.mapper.LawyerUserSearchHistoryMapper;
|
|
import shop.alien.mapper.LawyerUserSearchHistoryMapper;
|
|
|
import shop.alien.store.service.LawyerUserSearchHistoryService;
|
|
import shop.alien.store.service.LawyerUserSearchHistoryService;
|
|
|
|
|
+import shop.alien.util.myBaticsPlus.QueryBuilder;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 用户搜索历史 服务实现类
|
|
* 用户搜索历史 服务实现类
|
|
@@ -61,6 +64,43 @@ public class LawyerUserSearchHistoryServiceImpl extends ServiceImpl<LawyerUserSe
|
|
|
@Override
|
|
@Override
|
|
|
public R<LawyerUserSearchHistory> addUserSearchHistory(LawyerUserSearchHistory userSearchHistory) {
|
|
public R<LawyerUserSearchHistory> addUserSearchHistory(LawyerUserSearchHistory userSearchHistory) {
|
|
|
log.info("LawyerUserSearchHistoryServiceImpl.addUserSearchHistory?userSearchHistory={}", userSearchHistory);
|
|
log.info("LawyerUserSearchHistoryServiceImpl.addUserSearchHistory?userSearchHistory={}", userSearchHistory);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置搜索时间
|
|
|
|
|
+ if (userSearchHistory.getSearchTime() == null) {
|
|
|
|
|
+ userSearchHistory.setSearchTime(new java.util.Date());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查该用户的历史记录数量,如果超过10条则删除最老的记录
|
|
|
|
|
+ if (userSearchHistory.getClientUserId() != null) {
|
|
|
|
|
+ LambdaQueryWrapper<LawyerUserSearchHistory> countWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ countWrapper.eq(LawyerUserSearchHistory::getClientUserId, userSearchHistory.getClientUserId())
|
|
|
|
|
+ .eq(LawyerUserSearchHistory::getDeleteFlag, 0);
|
|
|
|
|
+ long count = this.count(countWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果已有10条或更多记录,删除最老的记录
|
|
|
|
|
+ if (count >= 10) {
|
|
|
|
|
+ // 查询需要删除的最老记录(保留9条,加上新的一条共10条)
|
|
|
|
|
+ LambdaQueryWrapper<LawyerUserSearchHistory> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ queryWrapper.eq(LawyerUserSearchHistory::getClientUserId, userSearchHistory.getClientUserId())
|
|
|
|
|
+ .eq(LawyerUserSearchHistory::getDeleteFlag, 0)
|
|
|
|
|
+ .orderByAsc(LawyerUserSearchHistory::getSearchTime) // 按搜索时间升序,最老的在前
|
|
|
|
|
+ .orderByAsc(LawyerUserSearchHistory::getCreatedTime) // 如果搜索时间相同,按创建时间升序
|
|
|
|
|
+ .last("LIMIT " + (count - 9)); // 查询需要删除的记录数
|
|
|
|
|
+ List<LawyerUserSearchHistory> toDeleteList = this.list(queryWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ // 批量删除最老的记录
|
|
|
|
|
+ if (!toDeleteList.isEmpty()) {
|
|
|
|
|
+ List<Integer> idsToDelete = toDeleteList.stream()
|
|
|
|
|
+ .map(LawyerUserSearchHistory::getId)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ this.removeByIds(idsToDelete);
|
|
|
|
|
+ log.info("删除用户{}的最老搜索历史记录{}条,当前记录数:{}",
|
|
|
|
|
+ userSearchHistory.getClientUserId(), idsToDelete.size(), count);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存新记录
|
|
|
boolean result = this.save(userSearchHistory);
|
|
boolean result = this.save(userSearchHistory);
|
|
|
if (result) {
|
|
if (result) {
|
|
|
return R.data(userSearchHistory);
|
|
return R.data(userSearchHistory);
|
|
@@ -90,5 +130,29 @@ public class LawyerUserSearchHistoryServiceImpl extends ServiceImpl<LawyerUserSe
|
|
|
}
|
|
}
|
|
|
return R.fail("清空失败");
|
|
return R.fail("清空失败");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<LawyerUserSearchHistory> getListWithOrder(LawyerUserSearchHistory userSearchHistory) {
|
|
|
|
|
+ log.info("LawyerUserSearchHistoryServiceImpl.getListWithOrder?userSearchHistory={}", userSearchHistory);
|
|
|
|
|
+ QueryWrapper<LawyerUserSearchHistory> queryWrapper = QueryBuilder.of(userSearchHistory)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .getWrapper();
|
|
|
|
|
+ // 添加排序:按创建时间降序(新的在前,老的在后)
|
|
|
|
|
+ queryWrapper.orderByDesc("created_time");
|
|
|
|
|
+ return this.list(queryWrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public IPage<LawyerUserSearchHistory> getPageWithOrder(LawyerUserSearchHistory userSearchHistory, int pageNum, int pageSize) {
|
|
|
|
|
+ log.info("LawyerUserSearchHistoryServiceImpl.getPageWithOrder?userSearchHistory={},pageNum={},pageSize={}",
|
|
|
|
|
+ userSearchHistory, pageNum, pageSize);
|
|
|
|
|
+ Page<LawyerUserSearchHistory> page = new Page<>(pageNum, pageSize);
|
|
|
|
|
+ QueryWrapper<LawyerUserSearchHistory> queryWrapper = QueryBuilder.of(userSearchHistory)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ .getWrapper();
|
|
|
|
|
+ // 添加排序:按创建时间降序(新的在前,老的在后)
|
|
|
|
|
+ queryWrapper.orderByDesc("created_time");
|
|
|
|
|
+ return this.page(page, queryWrapper);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|