PaliTextService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 getChildrenChapters(int $book, int $para) {}
  28. public function getBookPara(int $book, int $para)
  29. {
  30. $paragraph = PaliText::where('book', $book)
  31. ->where('paragraph', '<=', $para)
  32. ->where('level', 1)
  33. ->orderBy('paragraph', 'asc')->first();
  34. if (! $paragraph) {
  35. Log::warning('not found book ', ['book' => $book, 'para' => $para]);
  36. return null;
  37. }
  38. return $paragraph;
  39. }
  40. public function getParaCategoryTags(int $book, int $para): array
  41. {
  42. $bookPara = self::getBookPara($book, $para);
  43. if (! $bookPara) {
  44. return [];
  45. }
  46. if (Str::isUuid($bookPara->uid)) {
  47. return app(TagService::class)->getTagsName($bookPara->uid);
  48. } else {
  49. Log::error('book uid not uuid', [
  50. 'book' => $book,
  51. 'para' => $para,
  52. 'uid' => $bookPara->uid,
  53. ]);
  54. return [];
  55. }
  56. }
  57. public function getParaInfo(int $book, int $para)
  58. {
  59. return PaliText::where('book', $book)
  60. ->where('paragraph', $para)
  61. ->first();
  62. }
  63. public function getParaPathTitle(int $book, int $para)
  64. {
  65. $para = self::getParaInfo($book, $para);
  66. return array_map(function ($item) {
  67. return $item->title;
  68. }, json_decode($para->path));
  69. }
  70. }