UpgradeProgressChapter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\MdRender;
  4. use App\Models\Channel;
  5. use App\Models\PaliSentence;
  6. use App\Models\PaliText;
  7. use App\Models\Progress;
  8. use App\Models\ProgressChapter;
  9. use App\Models\Sentence;
  10. use App\Models\TagMap;
  11. use App\Services\PaliTextService;
  12. use App\Tools\Markdown;
  13. use App\Tools\Tools;
  14. use Carbon\Carbon;
  15. use Illuminate\Console\Command;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\Validator;
  18. class UpgradeProgressChapter extends Command
  19. {
  20. protected $signature = 'upgrade:progress.chapter {--book=} {--para=} {--channel=} {--driver=str} {--fresh : 清除缓存断点,从头开始}';
  21. protected $description = '更新章节完成度(可重入:中断后重跑自动跳过已处理的 book)';
  22. const COMPLETION_RATE = 0.9;
  23. // 缓存键:记录最后处理完成的 book_id,48h 过期
  24. private const CACHE_KEY = 'upgrade-progress-chapter:cursor';
  25. public function handle(): int
  26. {
  27. if (Tools::isStop()) {
  28. return 0;
  29. }
  30. if ($this->option('fresh')) {
  31. Cache::forget(self::CACHE_KEY);
  32. $this->info('Cleared cached cursor.');
  33. }
  34. $paliTextService = app(PaliTextService::class);
  35. $this->info('upgrade:progress.chapter start.');
  36. $startTime = time();
  37. $book = $this->option('book');
  38. $para = $this->option('para');
  39. $channelId = $this->option('channel');
  40. if ($channelId) {
  41. $this->line('channel='.$channelId);
  42. }
  43. Markdown::driver($this->option('driver'));
  44. $tagCount = 0;
  45. // 第一步:查询有译文的 book 列表
  46. $books = $this->buildBookList($book);
  47. // 从缓存恢复断点:跳过上次已完成的 book
  48. $lastBookId = Cache::get(self::CACHE_KEY);
  49. if ($lastBookId && ! $book) {
  50. $books = $books->filter(fn ($b) => $b->book_id > $lastBookId)->values();
  51. $this->info("Resuming after book={$lastBookId}");
  52. }
  53. $totalBook = $books->count();
  54. foreach ($books as $bookIdx => $bookRow) {
  55. $this->info('['.($bookIdx + 1)."/{$totalBook}] book={$bookRow->book_id}");
  56. $chapters = $this->getChapters($bookRow->book_id, $para);
  57. foreach ($chapters as $chapter) {
  58. // 计算章节对应的巴利语总字符数
  59. $chapterEnd = $chapter->paragraph + $chapter->chapter_len - 1;
  60. $chapterStrlen = PaliSentence::where('book', $bookRow->book_id)
  61. ->whereBetween('paragraph', [$chapter->paragraph, $chapterEnd])
  62. ->sum('length');
  63. if ($chapterStrlen == 0) {
  64. $this->error("chapter_strlen=0 book:{$bookRow->book_id} para:{$chapter->paragraph}-{$chapterEnd}");
  65. continue;
  66. }
  67. // 按 channel 分组统计已翻译字符数
  68. $progressQuery = Progress::where('book', $bookRow->book_id)
  69. ->whereBetween('para', [$chapter->paragraph, $chapterEnd]);
  70. if ($channelId) {
  71. $progressQuery->where('channel_id', $channelId);
  72. }
  73. $channelProgress = $progressQuery->groupBy('channel_id')
  74. ->selectRaw('channel_id, sum(all_strlen) as cp_len')
  75. ->get();
  76. foreach ($channelProgress as $final) {
  77. $tagCount += $this->processChapterChannel(
  78. $bookRow->book_id,
  79. $chapter,
  80. $chapterEnd,
  81. $chapterStrlen,
  82. $final,
  83. $paliTextService,
  84. );
  85. }
  86. }
  87. // 每完成一本书,保存断点
  88. Cache::put(self::CACHE_KEY, $bookRow->book_id, now()->addHours(48));
  89. }
  90. // 全部完成,清除断点缓存
  91. Cache::forget(self::CACHE_KEY);
  92. $time = time() - $startTime;
  93. $this->info("upgrade:progress.chapter finished in {$time}s tag count:{$tagCount}");
  94. return 0;
  95. }
  96. /** 查询有译文的 book 列表,按 book_id 排序 */
  97. private function buildBookList(?string $book)
  98. {
  99. if ($book) {
  100. $table = Sentence::where('book_id', $book);
  101. } else {
  102. $table = Sentence::where('strlen', '>', 0)
  103. ->where('book_id', '<', 1000)
  104. ->whereNotNull('channel_uid');
  105. }
  106. return $table->groupBy('book_id')
  107. ->select('book_id')
  108. ->orderBy('book_id')
  109. ->get();
  110. }
  111. /** 获取某本书的章节列表(level 1-7) */
  112. private function getChapters(int $bookId, ?string $para)
  113. {
  114. $table = PaliText::where('book', $bookId);
  115. if ($para) {
  116. $table = $table->where('paragraph', '<=', $para);
  117. }
  118. return $table->where('level', '>', 0)
  119. ->where('level', '<', 8)
  120. ->select('paragraph', 'chapter_strlen', 'chapter_len')
  121. ->get();
  122. }
  123. /** 处理单个章节×channel 的进度更新,返回新增 tag 数 */
  124. private function processChapterChannel(
  125. int $bookId,
  126. $chapter,
  127. int $chapterEnd,
  128. int $chapterStrlen,
  129. $final,
  130. PaliTextService $paliTextService,
  131. ): int {
  132. $tagCount = 0;
  133. // 查询该 channel 在此章节范围内的完成时间
  134. $baseProgress = Progress::where('book', $bookId)
  135. ->whereBetween('para', [$chapter->paragraph, $chapterEnd])
  136. ->where('channel_id', $final->channel_id);
  137. $finalAt = (clone $baseProgress)->max('created_at');
  138. $updateAt = (clone $baseProgress)->max('updated_at');
  139. // 获取译文内容,用于生成摘要
  140. $transTexts = Sentence::where('book_id', $bookId)
  141. ->whereBetween('paragraph', [$chapter->paragraph + 1, $chapterEnd])
  142. ->where('channel_uid', $final->channel_id)
  143. ->select('content')
  144. ->orderBy('paragraph')
  145. ->orderBy('word_start')
  146. ->get();
  147. $mdRender = new MdRender(['format' => 'simple']);
  148. // 章节标题
  149. $title = Sentence::where('book_id', $bookId)
  150. ->where('paragraph', $chapter->paragraph)
  151. ->where('channel_uid', $final->channel_id)
  152. ->value('content');
  153. $title = $mdRender->convert($title, [$final->channel_id]);
  154. // 拼接摘要,最多 255 字符
  155. $summaryText = '';
  156. foreach ($transTexts as $text) {
  157. $textContent = $mdRender->convert($text->content, [$final->channel_id]);
  158. $summaryText .= str_replace("\n", '', $textContent);
  159. if (mb_strlen($summaryText, 'UTF-8') > 255) {
  160. break;
  161. }
  162. }
  163. // 查询 channel 语言
  164. $channelLang = Channel::where('uid', $final->channel_id)->value('lang');
  165. $lang = explode('-', $channelLang)[0];
  166. $attributes = [
  167. 'book' => $bookId,
  168. 'para' => $chapter->paragraph,
  169. 'channel_id' => $final->channel_id,
  170. ];
  171. $validator = Validator::make($attributes, [
  172. 'book' => 'integer',
  173. 'para' => 'integer',
  174. 'channel_id' => 'uuid',
  175. ]);
  176. if ($validator->fails()) {
  177. $this->error('Validator failed: '.json_encode($attributes));
  178. return 0;
  179. }
  180. // firstOrNew:存在则更新,不存在则新建
  181. $chapterData = ProgressChapter::firstOrNew($attributes);
  182. $progress = $final->cp_len / $chapterStrlen;
  183. $addChapter = false;
  184. // 进度 >= 90% 视为完成
  185. if ($progress >= self::COMPLETION_RATE && empty($chapterData->completed_at)) {
  186. $chapterData->completed_at = $finalAt;
  187. $addChapter = true;
  188. }
  189. $chapterData->lang = $lang;
  190. $chapterData->all_trans = $progress;
  191. $chapterData->public = $progress;
  192. $chapterData->progress = $progress;
  193. $chapterData->title = $title ? mb_substr($title, 0, 255, 'UTF-8') : '';
  194. $chapterData->summary = $summaryText ? mb_substr($summaryText, 0, 255, 'UTF-8') : '';
  195. $chapterData->created_at = $finalAt;
  196. $chapterData->updated_at = $updateAt;
  197. $chapterData->save();
  198. // 新完成的章节:向上更新父级目录的 last_chapter_completed_at
  199. if ($addChapter) {
  200. $this->updateParentChapters($bookId, $chapter->paragraph, $final->channel_id, $finalAt, $paliTextService);
  201. }
  202. // 更新标签映射
  203. $tagCount += $this->syncTags($bookId, $chapter->paragraph, $chapterData->uid);
  204. return $tagCount;
  205. }
  206. /** 向上遍历父章节,更新 last_chapter_completed_at 和 completed_chapters 计数 */
  207. private function updateParentChapters(int $bookId, int $para, string $channelId, $finalAt, PaliTextService $paliTextService): void
  208. {
  209. $currPara = $para;
  210. while ($parent = $paliTextService->getParent($bookId, $currPara)) {
  211. $parentChapter = ProgressChapter::where('book', $bookId)
  212. ->where('para', $parent->paragraph)
  213. ->where('channel_id', $channelId)
  214. ->first();
  215. if (! $parentChapter) {
  216. break;
  217. }
  218. $currPara = $parent->paragraph;
  219. if (
  220. is_null($parentChapter->last_chapter_completed_at) ||
  221. Carbon::parse($finalAt)->gt(Carbon::parse($parentChapter->last_chapter_completed_at))
  222. ) {
  223. $parentChapter->last_chapter_completed_at = $finalAt;
  224. $chapterEnd = $parent->paragraph + $parent->chapter_len - 1;
  225. $parentChapter->completed_chapters = ProgressChapter::where('book', $bookId)
  226. ->whereBetween('para', [$parent->paragraph, $chapterEnd])
  227. ->where('channel_id', $channelId)
  228. ->whereNotNull('completed_at')
  229. ->count();
  230. $parentChapter->save();
  231. }
  232. }
  233. }
  234. /** 同步章节的标签映射,返回新增 tag 数 */
  235. private function syncTags(int $bookId, int $para, string $chapterUid): int
  236. {
  237. $path = json_decode(
  238. PaliText::where('book', $bookId)
  239. ->where('paragraph', $para)
  240. ->value('path')
  241. );
  242. if (! $path) {
  243. return 0;
  244. }
  245. // 收集路径上所有层级的标签
  246. $tags = [];
  247. foreach ($path as $value) {
  248. if ($value->level > 0) {
  249. $paliTextUuid = PaliText::where('book', $value->book)
  250. ->where('paragraph', $value->paragraph)
  251. ->value('uid');
  252. $tagIds = TagMap::where('table_name', 'pali_texts')
  253. ->where('anchor_id', $paliTextUuid)
  254. ->pluck('tag_id');
  255. foreach ($tagIds as $tagId) {
  256. $tags[$tagId] = 1;
  257. }
  258. }
  259. }
  260. // 先删后建:重建标签映射
  261. TagMap::where('table_name', 'progress_chapters')
  262. ->where('anchor_id', $chapterUid)
  263. ->delete();
  264. $count = 0;
  265. foreach ($tags as $tagId => $_) {
  266. $tagmap = TagMap::create([
  267. 'table_name' => 'progress_chapters',
  268. 'anchor_id' => $chapterUid,
  269. 'tag_id' => $tagId,
  270. ]);
  271. if ($tagmap) {
  272. $count++;
  273. }
  274. }
  275. return $count;
  276. }
  277. }