UpgradeProgress.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sentence;
  5. use App\Models\PaliSentence;
  6. use App\Models\Progress;
  7. use App\Models\ProgressChapter;
  8. use App\Models\PaliText;
  9. use Illuminate\Support\Facades\Log;
  10. class UpgradeProgress extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. * php artisan upgrade:progress --book=152 --channel=19f53a65-81db-4b7d-8144-ac33f1217d34
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:progress {--book=} {--para=} {--channel=} {--resume}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command description';
  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. if (\App\Tools\Tools::isStop()) {
  41. return 0;
  42. }
  43. $this->info('upgrade:progress start');
  44. $startTime = time();
  45. $book = $this->option('book');
  46. $para = $this->option('para');
  47. $channelId = $this->option('channel');
  48. if ($channelId) {
  49. $this->line('channel=' . $channelId);
  50. }
  51. $table = Sentence::where('strlen', '>', 0);
  52. if ($book || $para || $channelId) {
  53. if ($book) {
  54. $table = $table->where('book_id', $book);
  55. }
  56. if ($para) {
  57. $table = $table->where('paragraph', $para);
  58. }
  59. if ($channelId) {
  60. $table = $table->where('channel_uid', $channelId);
  61. }
  62. $sentences = $table->groupby('book_id', 'paragraph', 'channel_uid')
  63. ->select('book_id', 'paragraph', 'channel_uid');
  64. } else {
  65. if ($this->option('resume')) {
  66. $sentences = Sentence::where('strlen', '>', 0)
  67. ->whereBetween('book_id', [$book, 1000])
  68. ->where('paragraph', '>=', $para)
  69. ->whereNotNull('channel_uid')
  70. ->groupby('book_id', 'paragraph', 'channel_uid')
  71. ->select('book_id', 'paragraph', 'channel_uid');
  72. } else {
  73. $sentences = Sentence::where('strlen', '>', 0)
  74. ->where('book_id', '<', 1000)
  75. ->whereNotNull('channel_uid')
  76. ->groupby('book_id', 'paragraph', 'channel_uid')
  77. ->select('book_id', 'paragraph', 'channel_uid');
  78. }
  79. }
  80. $count = $sentences->count();
  81. $sentences = $sentences->cursor();
  82. $this->info('sentences:' . $count);
  83. #第二步 更新段落表
  84. foreach ($sentences as $sentence) {
  85. # 第二步 生成para progress 1,2,15,zh-tw
  86. # 计算此段落完成时间
  87. $finalAt = Sentence::where('strlen', '>', 0)
  88. ->where('book_id', $sentence->book_id)
  89. ->where('paragraph', $sentence->paragraph)
  90. ->where('channel_uid', $sentence->channel_uid)
  91. ->max('created_at');
  92. $updateAt = Sentence::where('strlen', '>', 0)
  93. ->where('book_id', $sentence->book_id)
  94. ->where('paragraph', $sentence->paragraph)
  95. ->where('channel_uid', $sentence->channel_uid)
  96. ->max('updated_at');
  97. # 查询每个段落的等效巴利语字符数
  98. $result_sent = Sentence::where('strlen', '>', 0)
  99. ->where('book_id', $sentence->book_id)
  100. ->where('paragraph', $sentence->paragraph)
  101. ->where('channel_uid', $sentence->channel_uid)
  102. ->select('word_start')
  103. ->get();
  104. if (count($result_sent) > 0) {
  105. #查询这些句子的总共等效巴利语字符数
  106. $para_strlen = 0;
  107. foreach ($result_sent as $sent) {
  108. # code...
  109. $para_strlen += PaliSentence::where('book', $sentence->book_id)
  110. ->where('paragraph', $sentence->paragraph)
  111. ->where('word_begin', $sent->word_start)
  112. ->value('length');
  113. }
  114. $paraInfo = [
  115. 'book' => $sentence->book_id,
  116. 'para' => $sentence->paragraph,
  117. 'channel_id' => $sentence->channel_uid
  118. ];
  119. $paraData = [
  120. 'lang' => 'en',
  121. 'all_strlen' => $para_strlen,
  122. 'public_strlen' => $para_strlen,
  123. 'created_at' => $finalAt,
  124. 'updated_at' => $updateAt,
  125. ];
  126. //Log::debug('Progress updateOrInsert', ['para' => $paraInfo, 'data' => $paraData]);
  127. $this->info('Progress updateOrInsert' . json_encode($paraInfo));
  128. Progress::updateOrInsert($paraInfo, $paraData);
  129. }
  130. }
  131. $time = time() - $startTime;
  132. $this->info("upgrade progress finished in {$time}s");
  133. return 0;
  134. }
  135. }