TipitakaChapterChannelService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Facades\RedisCache;
  4. use App\Models\GroupInfo;
  5. use App\Models\ProgressChapter;
  6. use App\Models\View;
  7. use App\Services\UserService;
  8. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Support\Collection;
  11. /**
  12. * 某一章有哪些 channel(译本 / 逐词 / 原文……)及各自的完成度。
  13. *
  14. * 取代 v2 `ProgressChapterController@index` 的 `view=chapter_channels` 分支。v2 那份
  15. * 原样保留、不共用(CLAUDE.md 铁律 2)。v3 端点用 {@see self::list()},Laravel library 的
  16. * 作品版本页用 {@see self::all()}(经 `TipitakaCatalogService::workChannels()`)。与 v2 的差别:
  17. *
  18. * - **只列看得见的 channel**:判定见 {@see ChannelAbilityService::scopeListable()}(公开的,加上登录用户
  19. * 自己的、所在团队名下的、共享给他或他所在团队的)。v2 不判断可见性,私有 channel 的名字与进度对所有人可见。
  20. * - 不再返回 likes(前端没用);views、studio 改成整页批量取,查询数与行数无关。
  21. */
  22. class TipitakaChapterChannelService
  23. {
  24. public function __construct(
  25. private readonly UserService $users,
  26. private readonly ChannelAbilityService $abilities,
  27. ) {}
  28. /**
  29. * 列出一章的 channel,按完成度从高到低(分页,给 v3 端点)。
  30. *
  31. * 返回的模型上已挂好 `views`、`studio`、`progress_line` 三个属性,Resource 直接读,不再查库。
  32. *
  33. * @param string|null $viewerUid 当前登录用户;未登录只看得到公开 channel
  34. * @return LengthAwarePaginator<int, ProgressChapter>
  35. */
  36. public function list(int $book, int $para, ?string $viewerUid, int $perPage): LengthAwarePaginator
  37. {
  38. $page = $this->query($book, $para, $viewerUid)->paginate($perPage);
  39. $this->enrich($page->getCollection());
  40. return $page;
  41. }
  42. /**
  43. * 同 {@see self::list()},但不分页、可排除一个 channel——给 Laravel library 的作品页用
  44. * (一章最多几十个 channel,整页渲染)。
  45. *
  46. * @param string|null $exceptChannel 不列的 channel(译本详情页的「其他版本」要排除当前译本)
  47. * @return Collection<int, ProgressChapter>
  48. */
  49. public function all(int $book, int $para, ?string $viewerUid, ?string $exceptChannel = null): Collection
  50. {
  51. $rows = $this->query($book, $para, $viewerUid)
  52. ->when($exceptChannel, fn (Builder $q) => $q->where('progress_chapters.channel_id', '<>', $exceptChannel))
  53. ->get();
  54. $this->enrich($rows);
  55. return $rows;
  56. }
  57. /**
  58. * 一章的进度行 join channel,已套可见性,按完成度降序。
  59. *
  60. * @return Builder<ProgressChapter>
  61. */
  62. private function query(int $book, int $para, ?string $viewerUid): Builder
  63. {
  64. return ProgressChapter::query()
  65. ->select([
  66. 'progress_chapters.uid',
  67. 'progress_chapters.book',
  68. 'progress_chapters.para',
  69. 'progress_chapters.channel_id',
  70. 'progress_chapters.progress',
  71. 'progress_chapters.updated_at',
  72. 'channels.name as channel_name',
  73. 'channels.type as channel_type',
  74. 'channels.lang as channel_lang',
  75. 'channels.owner_uid as channel_owner_uid',
  76. ])
  77. // inner join:channel 已删除的进度行没有意义,不列
  78. ->join('channels', 'progress_chapters.channel_id', '=', 'channels.uid')
  79. ->where('progress_chapters.book', $book)
  80. ->where('progress_chapters.para', $para)
  81. // 软删的 channel 不列(join 绕过了模型的软删作用域)
  82. ->whereNull('channels.deleted_at')
  83. // 版本列表不列系统 channel(巴利原文、系统逐词,每章都有):它们对谁都可读,
  84. // 不排除的话每章都会多出这两行;与 2026-11 迁移后 v3 一直以来的输出保持一致
  85. ->where('channels.is_system', false)
  86. ->where(fn (Builder $q) => $this->abilities->scopeListable($q, $viewerUid))
  87. ->orderByDesc('progress_chapters.progress')
  88. // 完成度相同时要稳定的 tiebreaker,否则跨页会漂移
  89. ->orderBy('progress_chapters.channel_id');
  90. }
  91. /**
  92. * 整批补上 views / studio / progress_line,查询数与行数无关。
  93. *
  94. * @param Collection<int, ProgressChapter> $rows
  95. */
  96. private function enrich(Collection $rows): void
  97. {
  98. $views = $this->viewCounts($rows->pluck('uid')->all());
  99. $studios = $this->studios($rows->pluck('channel_owner_uid')->all());
  100. foreach ($rows as $row) {
  101. $row->setAttribute('views', $views[$row->uid] ?? 0);
  102. $row->setAttribute('studio', $studios[$row->channel_owner_uid] ?? null);
  103. $row->setAttribute('progress_line', RedisCache::get(
  104. "/chapter_dynamic/{$row->book}/{$row->para}/ch_{$row->channel_id}"
  105. ));
  106. }
  107. }
  108. /**
  109. * 每个章节进度行被浏览的次数(views 表一行一次)。
  110. *
  111. * @param list<string> $uids progress_chapters.uid
  112. * @return array<string, int>
  113. */
  114. private function viewCounts(array $uids): array
  115. {
  116. if (! $uids) {
  117. return [];
  118. }
  119. return View::whereIn('target_id', $uids)
  120. ->groupBy('target_id')
  121. ->selectRaw('target_id, count(*) as n')
  122. ->pluck('n', 'target_id')
  123. ->map(fn ($n) => (int) $n)
  124. ->all();
  125. }
  126. /**
  127. * channel 所有者的摘要。所有者可能是用户,也可能是群组(v2 `StudioApi::getById` 的口径)。
  128. *
  129. * @param list<string|null> $ownerUids
  130. * @return array<string, array{id: string, nickName: string, realName: string, avatar?: string}>
  131. */
  132. private function studios(array $ownerUids): array
  133. {
  134. $output = [];
  135. foreach ($this->users->byUuids($ownerUids) as $uid => $profile) {
  136. $output[$uid] = array_filter([
  137. 'id' => $uid,
  138. 'nickName' => (string) ($profile['nickName'] ?? ''),
  139. 'realName' => (string) ($profile['realName'] ?? ''),
  140. 'avatar' => $profile['avatar'] ?? null,
  141. ], fn ($v) => $v !== null);
  142. }
  143. $groups = array_values(array_unique(array_diff(array_filter($ownerUids), array_keys($output))));
  144. if ($groups) {
  145. foreach (GroupInfo::whereIn('uid', $groups)->get(['uid', 'name']) as $group) {
  146. $output[$group->uid] = [
  147. 'id' => $group->uid,
  148. 'nickName' => (string) $group->name,
  149. 'realName' => (string) $group->uid,
  150. ];
  151. }
  152. }
  153. return $output;
  154. }
  155. }