ChapterService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace App\Services;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\MdRender;
  5. use App\Http\Resources\TocResource;
  6. use App\Models\Channel;
  7. use App\Models\PaliText;
  8. use App\Models\ProgressChapter;
  9. use App\Models\Sentence;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Str;
  12. class ChapterService
  13. {
  14. protected $result = [
  15. 'uid' => '',
  16. 'title' => '',
  17. 'path' => [],
  18. 'sub_title' => '',
  19. 'summary' => '',
  20. 'content' => '',
  21. 'content_type' => 'html',
  22. 'toc' => [],
  23. 'status' => 30,
  24. 'lang' => '',
  25. 'created_at' => '',
  26. 'updated_at' => '',
  27. ];
  28. protected $selectCol = [
  29. 'uid',
  30. 'book_id',
  31. 'paragraph',
  32. 'word_start',
  33. 'word_end',
  34. 'channel_uid',
  35. 'content',
  36. 'content_type',
  37. 'editor_uid',
  38. 'acceptor_uid',
  39. 'pr_edit_at',
  40. 'fork_at',
  41. 'create_time',
  42. 'modify_time',
  43. 'created_at',
  44. 'updated_at',
  45. ];
  46. private $MaxStrLen = 3000;
  47. public function setMaxSize(int $size)
  48. {
  49. $this->MaxStrLen = $size;
  50. }
  51. public function chapterWithContent(string $id)
  52. {
  53. // getChannels
  54. // chapter info
  55. // chapter rang
  56. // chapter content
  57. }
  58. public function paraWithContent(string $id) {}
  59. public function csParaWithContent(string $id) {}
  60. public function paraContent(int $book, int $from, int $to) {}
  61. private function currChapter() {}
  62. private function getChannels(?array $input, string $mode)
  63. {
  64. $channels = [];
  65. if (is_array($input)) {
  66. foreach ($input as $channel) {
  67. if (Str::isUuid($channel)) {
  68. $channels[] = $channel;
  69. }
  70. }
  71. }
  72. if ($mode === 'read') {
  73. // 阅读模式加载html格式原文
  74. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  75. } else {
  76. // 翻译模式加载json格式原文
  77. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  78. }
  79. if ($channelId !== false) {
  80. $channels[] = $channelId;
  81. }
  82. // 获取channel索引表
  83. $tranChannels = [];
  84. $channelInfo = Channel::whereIn('uid', $channels)
  85. ->select(['uid', 'type', 'lang', 'name'])->get();
  86. foreach ($channelInfo as $key => $value) {
  87. // code...
  88. if ($value->type === 'translation') {
  89. $tranChannels[] = $value->uid;
  90. }
  91. }
  92. $indexChannel = [];
  93. $paliService = app(PaliContentService::class);
  94. $indexChannel = $paliService->getChannelIndex($channels);
  95. return [
  96. 'channels' => $channels,
  97. 'index' => $indexChannel,
  98. 'translations' => $tranChannels,
  99. 'request' => $input,
  100. ];
  101. }
  102. private function chapterInfo(int $book, int $para, array $channelInfo)
  103. {
  104. $result = [];
  105. $chapter = PaliText::where('book', $book)->where('paragraph', $para)->first();
  106. if (! $chapter) {
  107. // FIXME throw
  108. }
  109. if (empty($chapter->toc)) {
  110. $this->result['title'] = 'unknown';
  111. } else {
  112. $result['title'] = $chapter->toc;
  113. $result['sub_title'] = $chapter->toc;
  114. $result['path'] = json_decode($chapter->path);
  115. }
  116. $title = Sentence::select($this->selectCol)
  117. ->where('book_id', $book)
  118. ->where('paragraph', $para)
  119. ->whereIn('channel_uid', $channelInfo['translation'])
  120. ->first();
  121. if ($title) {
  122. $result['title'] = MdRender::render($title->content, [$title->channel_uid]);
  123. $mdRender = new MdRender(['format' => 'simple']);
  124. $result['title_text'] = $mdRender->convert($title->content, [$title->channel_uid]);
  125. }
  126. return $result;
  127. }
  128. private function chapterContentRang($book, $para, $from, $to)
  129. {
  130. /**
  131. * 获取句子数据
  132. * 算法:
  133. * 1. 如果标题和下一级第一个标题之间有段落。只输出这些段落和子目录
  134. * 2. 如果标题和下一级第一个标题之间没有间隔 且 chapter 长度大于10000个字符 且有子目录,只输出子目录
  135. * 3. 如果二者都不是,lazy load
  136. */
  137. // 1. 计算 标题和下一级第一个标题之间 是否有间隔
  138. $chapter = PaliText::where('book', $book)->where('paragraph', $para)->first();
  139. $paraFrom = $para;
  140. $paraTo = $para + $chapter->chapter_len - 1;
  141. $nextChapter = PaliText::where('book', $book)
  142. ->where('paragraph', '>', $para)
  143. ->where('level', '<', 8)
  144. ->orderBy('paragraph')
  145. ->value('paragraph');
  146. $between = $nextChapter - $para;
  147. // 查找子目录
  148. $chapterLen = $chapter->chapter_len;
  149. $toc = PaliText::where('book', $book)
  150. ->whereBetween('paragraph', [$paraFrom + 1, $paraFrom + $chapterLen - 1])
  151. ->where('level', '<', 8)
  152. ->orderBy('paragraph')
  153. ->select(['book', 'paragraph', 'level', 'toc'])
  154. ->get();
  155. if ($between > 1) {
  156. // 有间隔
  157. $paraTo = $nextChapter - 1;
  158. } else {
  159. if ($chapter->chapter_strlen > $this->MaxStrLen) {
  160. if (count($toc) > 0) {
  161. // 有子目录只输出标题和目录
  162. $paraTo = $paraFrom;
  163. } else {
  164. // 没有子目录 全部输出
  165. }
  166. } else {
  167. // 章节小。全部输出 不输出子目录
  168. $toc = [];
  169. }
  170. }
  171. $pFrom = $from ?? $paraFrom;
  172. $pTo = $to ?? $paraTo;
  173. // 根据句子的长度找到这次应该加载的段落
  174. $paliText = PaliText::select(['paragraph', 'lenght'])
  175. ->where('book', $book)
  176. ->whereBetween('paragraph', [$pFrom, $pTo])
  177. ->orderBy('paragraph')
  178. ->get();
  179. $sumLen = 0;
  180. $currTo = $pTo;
  181. foreach ($paliText as $para) {
  182. $sumLen += $para->lenght;
  183. if ($sumLen > $this->MaxStrLen) {
  184. $currTo = $para->paragraph;
  185. break;
  186. }
  187. }
  188. return ['toc' => $toc, 'from' => $pFrom, 'to' => $currTo];
  189. }
  190. private function subChapterToc($toc)
  191. {
  192. // 第一次才显示toc
  193. return TocResource::collection($toc);
  194. }
  195. private function chapterContent($book, $para, $rang, $channelInfo, $mode)
  196. {
  197. $result = [];
  198. $paliService = app(PaliContentService::class);
  199. $record = Sentence::select($this->selectCol)
  200. ->where('book_id', $book)
  201. ->whereBetween('paragraph', [$rang['from'], $rang['to']])
  202. ->whereIn('channel_uid', $channelInfo['channels'])
  203. ->orderBy('paragraph')
  204. ->orderBy('word_start')
  205. ->get();
  206. if (count($record) === 0) {
  207. Log::warning('no data');
  208. }
  209. $result['content'] = json_encode($paliService->makeContentObj(
  210. $record,
  211. $mode,
  212. $channelInfo['index']
  213. ), JSON_UNESCAPED_UNICODE);
  214. $result['content_type'] = 'json';
  215. if ($rang['to'] < $para) {
  216. $result['from'] = $rang['to'] + 1;
  217. $result['to'] = $para;
  218. $result['paraId'] = "{$book}-{$para}";
  219. $result['channels'] = implode(',', $channelInfo['request']);
  220. $result['mode'] = $mode;
  221. }
  222. return $result;
  223. }
  224. public function publicChannels(int $book, int $para, ?string $type = null)
  225. {
  226. $channelIds = ProgressChapter::with('channel')
  227. ->where('book', $book)
  228. ->where('para', $para)
  229. ->whereHas('channel', function ($query) {
  230. $query->where('status', 30);
  231. })
  232. ->select('channel_id')
  233. ->get();
  234. $channels = [];
  235. foreach ($channelIds as $channel) {
  236. $channels[] = ChannelApi::getById($channel->channel_id);
  237. }
  238. return $channels;
  239. }
  240. public function getUidByChannel(int $book, int $para, string $channelId)
  241. {
  242. $uid = ProgressChapter::where('book', $book)
  243. ->where('para', $para)
  244. ->where('channel_id', $channelId)
  245. ->value('uid');
  246. return $uid;
  247. }
  248. }