All files / modules/search search.service.ts

0% Statements 0/88
0% Branches 0/1
0% Functions 0/1
0% Lines 0/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                                                                                                                                                                                                                                                                                 
import { prisma } from '../../models';
 
/**
 * 搜索服务
 * 提供全局搜索功能(搜索书籍)
 */
export class SearchService {
  /**
   * 搜索书籍
   * @param query 搜索关键词
   * @param limit 返回结果数量限制
   */
  async searchAudio(query: string, limit = 20) {
    if (!query || query.trim() === '') {
      return [];
    }
 
    const searchQuery = query.trim();
 
    // 搜索书籍
    const results = await prisma.book.findMany({
      where: {
        OR: [
          { title: { contains: searchQuery } },
          { description: { contains: searchQuery } },
        ],
      },
      orderBy: {
        createdAt: 'desc',
      },
      take: limit,
    });
 
    return results;
  }
 
  /**
   * 获取热门搜索词
   * @param limit 返回数量限制
   */
  async getHotSearches(limit = 10) {
    const hotSearches = await prisma.hotSearch.findMany({
      orderBy: [
        { sort: 'desc' },
        { count: 'desc' },
      ],
      take: limit,
    });
    return hotSearches;
  }
 
  /**
   * 获取用户搜索历史
   * @param userId 用户ID
   * @param limit 返回数量限制
   */
  async getSearchHistory(userId: number, limit = 5) {
    const history = await prisma.searchHistory.findMany({
      where: { userId },
      orderBy: { createdAt: 'desc' },
      take: limit,
      distinct: ['keyword'], // 去重,相同关键词只保留最新一条
    });
    return history;
  }
 
  /**
   * 保存搜索历史
   * @param userId 用户ID
   * @param keyword 搜索关键词
   */
  async saveSearchHistory(userId: number, keyword: string) {
    if (!keyword || keyword.trim() === '') return;
 
    const trimmedKeyword = keyword.trim();
 
    // 删除同用户的相同关键词旧记录
    await prisma.searchHistory.deleteMany({
      where: { userId, keyword: trimmedKeyword },
    });
 
    // 创建新记录
    await prisma.searchHistory.create({
      data: {
        userId,
        keyword: trimmedKeyword,
      },
    });
 
    // 保持用户最多20条历史记录
    const allHistory = await prisma.searchHistory.findMany({
      where: { userId },
      orderBy: { createdAt: 'desc' },
    });
 
    if (allHistory.length > 20) {
      const toDelete = allHistory.slice(20);
      await prisma.searchHistory.deleteMany({
        where: { id: { in: toDelete.map((h) => h.id) } },
      });
    }
 
    // 更新热门搜索词计数
    const hotSearch = await prisma.hotSearch.findFirst({
      where: { keyword: trimmedKeyword },
    });
 
    if (hotSearch) {
      await prisma.hotSearch.update({
        where: { id: hotSearch.id },
        data: { count: { increment: 1 } },
      });
    } else {
      // 如果热门词不存在,创建新的(排序为0,count为1)
      await prisma.hotSearch.create({
        data: { keyword: trimmedKeyword, count: 1, sort: 0 },
      });
    }
  }
 
  /**
   * 删除用户搜索历史
   * @param userId 用户ID
   */
  async clearSearchHistory(userId: number) {
    await prisma.searchHistory.deleteMany({
      where: { userId },
    });
  }
 
  /**
   * 删除单条搜索历史
   * @param userId 用户ID
   * @param keyword 关键词
   */
  async deleteSearchHistoryItem(userId: number, keyword: string) {
    await prisma.searchHistory.deleteMany({
      where: { userId, keyword },
    });
  }
}
 
// 导出单例
export const searchService = new SearchService();