InitCommentary.php 3.0 KB

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