PaliTextService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PaliText;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Str;
  6. class PaliTextService
  7. {
  8. public function getParent(int $book, int $para)
  9. {
  10. $parent = PaliText::where('book', $book)
  11. ->where('paragraph', $para)->value('parent');
  12. return $parent ? PaliText::where('book', $book)
  13. ->where('paragraph', $parent)->first() : null;
  14. }
  15. public function getCurrChapter(int $book, int $para)
  16. {
  17. $paragraph = PaliText::where('book', $book)
  18. ->where('paragraph', '<=', $para)
  19. ->where('level', '<', 8)
  20. ->orderBy('paragraph', 'desc')->first();
  21. if ($paragraph) {
  22. return $paragraph;
  23. } else {
  24. return null;
  25. }
  26. }
  27. public function getBookPara(int $book, int $para)
  28. {
  29. $paragraph = PaliText::where('book', $book)
  30. ->where('paragraph', '<=', $para)
  31. ->where('level', 1)
  32. ->orderBy('paragraph', 'asc')->first();
  33. if (!$paragraph) {
  34. Log::warning('not found book ', ['book' => $book, 'para' => $para]);
  35. return null;
  36. }
  37. return $paragraph;
  38. }
  39. public function getParaCategoryTags(int $book, int $para): array
  40. {
  41. $bookPara = self::getBookPara($book, $para);
  42. if (!$bookPara) {
  43. return [];
  44. }
  45. if (Str::isUuid($bookPara->uid)) {
  46. return app(TagService::class)->getTagsName($bookPara->uid);
  47. } else {
  48. Log::error('book uid not uuid', [
  49. 'book' => $book,
  50. 'para' => $para,
  51. 'uid' => $bookPara->uid
  52. ]);
  53. return [];
  54. }
  55. }
  56. public function getParaInfo(int $book, int $para)
  57. {
  58. return PaliText::where('book', $book)
  59. ->where('paragraph', $para)
  60. ->first();
  61. }
  62. public function getParaPathTitle(int $book, int $para)
  63. {
  64. $para = self::getParaInfo($book, $para);
  65. return array_map(function ($item) {
  66. return $item->title;
  67. }, json_decode($para->path));
  68. }
  69. }