Jelajahi Sumber

开发搜索历史查询接口
开发搜索历史清空接口
开发新增搜索历史接口(每个用户十条上限)

LuTong 1 bulan lalu
induk
melakukan
5bb213eadf

+ 5 - 11
alien-store/src/main/java/shop/alien/store/controller/LawyerUserSearchHistoryController.java

@@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.*;
 import shop.alien.entity.result.R;
 import shop.alien.entity.store.LawyerUserSearchHistory;
 import shop.alien.store.service.LawyerUserSearchHistoryService;
-import shop.alien.util.myBaticsPlus.QueryBuilder;
 
 import java.util.List;
 
@@ -29,7 +28,7 @@ public class LawyerUserSearchHistoryController {
 
     private final LawyerUserSearchHistoryService userSearchHistoryService;
 
-    @ApiOperation("新增用户搜索历史")
+    @ApiOperation("新增用户搜索历史(自动限制每个用户最多10条记录,超过则删除最老的记录)")
     @ApiOperationSupport(order = 1)
     @PostMapping("/addUserSearchHistory")
     public R<LawyerUserSearchHistory> addUserSearchHistory(@RequestBody LawyerUserSearchHistory userSearchHistory) {
@@ -53,7 +52,7 @@ public class LawyerUserSearchHistoryController {
         return userSearchHistoryService.clearUserSearchHistory(clientUserId);
     }
 
-    @ApiOperation("通用列表查询")
+    @ApiOperation("通用列表查询(按创建时间降序,新的在前)")
     @ApiOperationSupport(order = 4)
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query"),
@@ -65,13 +64,11 @@ public class LawyerUserSearchHistoryController {
     @GetMapping("/getList")
     public R<List<LawyerUserSearchHistory>> getList(@ModelAttribute LawyerUserSearchHistory userSearchHistory) {
         log.info("LawyerUserSearchHistoryController.getList?userSearchHistory={}", userSearchHistory);
-        List<LawyerUserSearchHistory> list = QueryBuilder.of(userSearchHistory)
-                .build()
-                .list(userSearchHistoryService);
+        List<LawyerUserSearchHistory> list = userSearchHistoryService.getListWithOrder(userSearchHistory);
         return R.data(list);
     }
 
-    @ApiOperation("通用分页查询")
+    @ApiOperation("通用分页查询(按创建时间降序,新的在前)")
     @ApiOperationSupport(order = 5)
     @ApiImplicitParams({
             @ApiImplicitParam(name = "page", value = "页数(默认1)", dataType = "int", paramType = "query"),
@@ -89,10 +86,7 @@ public class LawyerUserSearchHistoryController {
         log.info("LawyerUserSearchHistoryController.getPage?userSearchHistory={},page={},size={}", userSearchHistory, page, size);
         int pageNum = page > 0 ? page : 1;
         int pageSize = size > 0 ? size : 10;
-        IPage<LawyerUserSearchHistory> pageResult = QueryBuilder.of(userSearchHistory)
-                .page(pageNum, pageSize)
-                .build()
-                .page(userSearchHistoryService);
+        IPage<LawyerUserSearchHistory> pageResult = userSearchHistoryService.getPageWithOrder(userSearchHistory, pageNum, pageSize);
         return R.data(pageResult);
     }
 }

+ 18 - 0
alien-store/src/main/java/shop/alien/store/service/LawyerUserSearchHistoryService.java

@@ -57,5 +57,23 @@ public interface LawyerUserSearchHistoryService extends IService<LawyerUserSearc
      * @return R<Boolean>
      */
     R<Boolean> clearUserSearchHistory(Integer clientUserId);
+
+    /**
+     * 通用列表查询(按创建时间降序)
+     *
+     * @param userSearchHistory 查询条件
+     * @return List<LawyerUserSearchHistory>
+     */
+    List<LawyerUserSearchHistory> getListWithOrder(LawyerUserSearchHistory userSearchHistory);
+
+    /**
+     * 通用分页查询(按创建时间降序)
+     *
+     * @param userSearchHistory 查询条件
+     * @param pageNum 页码
+     * @param pageSize 每页数量
+     * @return IPage<LawyerUserSearchHistory>
+     */
+    IPage<LawyerUserSearchHistory> getPageWithOrder(LawyerUserSearchHistory userSearchHistory, int pageNum, int pageSize);
 }
 

+ 64 - 0
alien-store/src/main/java/shop/alien/store/service/impl/LawyerUserSearchHistoryServiceImpl.java

@@ -1,6 +1,7 @@
 package shop.alien.store.service.impl;
 
 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.extension.plugins.pagination.Page;
 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.mapper.LawyerUserSearchHistoryMapper;
 import shop.alien.store.service.LawyerUserSearchHistoryService;
+import shop.alien.util.myBaticsPlus.QueryBuilder;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 用户搜索历史 服务实现类
@@ -61,6 +64,43 @@ public class LawyerUserSearchHistoryServiceImpl extends ServiceImpl<LawyerUserSe
     @Override
     public R<LawyerUserSearchHistory> addUserSearchHistory(LawyerUserSearchHistory 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);
         if (result) {
             return R.data(userSearchHistory);
@@ -90,5 +130,29 @@ public class LawyerUserSearchHistoryServiceImpl extends ServiceImpl<LawyerUserSe
         }
         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);
+    }
 }