InitCommentary.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Commentary;
  4. use App\Models\PaliSentence;
  5. use App\Models\PaliText;
  6. use App\Models\RelatedParagraph;
  7. use App\Models\Tag;
  8. use App\Models\TagMap;
  9. use Illuminate\Console\Command;
  10. class InitCommentary extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. * php artisan init:commentary
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'init:commentary {--book=}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'init commentary sentences';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. // 查询注释书标签
  42. $tags = Tag::whereIn('name', ['aṭṭhakathā', 'ṭīkā'])->select('id')->get();
  43. // 查询段落编号
  44. $paliId = TagMap::whereIn('tag_id', $tags)
  45. ->where('table_name', 'pali_texts')
  46. ->cursor();
  47. foreach ($paliId as $key => $paraId) {
  48. $book = PaliText::where('uid', $paraId->anchor_id)
  49. ->where('level', 1)->first();
  50. if (! $book) {
  51. continue;
  52. }
  53. $paragraphs = PaliText::where('book', $book->book)
  54. ->whereBetween('paragraph', [$book->paragraph, $book->paragraph + $book->chapter_len - 1])
  55. ->get();
  56. foreach ($paragraphs as $key => $para) {
  57. $this->info($para->book.'-'.$para->paragraph);
  58. $sentences = PaliSentence::where('book', $para->book)
  59. ->where('paragraph', $para->paragraph)
  60. ->get();
  61. $del = Commentary::where('book1', $para->book)
  62. ->where('paragraph1', $para->paragraph)
  63. ->where('owner_id', config('mint.admin.root_uuid'))
  64. ->delete();
  65. $csPara = RelatedParagraph::where('book', $para->book)
  66. ->where('para', $para->paragraph)
  67. ->first();
  68. if ($csPara) {
  69. foreach ($sentences as $key => $sentence) {
  70. $new = new Commentary;
  71. $new->book1 = $sentence->book;
  72. $new->paragraph1 = $sentence->paragraph;
  73. $new->start1 = $sentence->word_begin;
  74. $new->end1 = $sentence->word_end;
  75. $new->editor_id = config('mint.admin.root_uuid');
  76. $new->owner_id = config('mint.admin.root_uuid');
  77. $new->p_number = $csPara->book_name.'-'.$csPara->para;
  78. $new->save();
  79. }
  80. } else {
  81. $this->error('no relation paragraph');
  82. }
  83. }
  84. }
  85. $this->info('all done');
  86. return 0;
  87. }
  88. }