UpgradeProgress.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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=168 --para=916 --channel=19f53a65-81db-4b7d-8144-ac33f1217d34
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:progress {--book=} {--para=} {--channel=}';
  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 ($book && $para && $channelId) {
  49. $sentences = Sentence::where('strlen', '>', 0)
  50. ->where('book_id', $book)
  51. ->where('paragraph', $para)
  52. ->where('channel_uid', $channelId)
  53. ->groupby('book_id', 'paragraph', 'channel_uid')
  54. ->select('book_id', 'paragraph', 'channel_uid');
  55. } else {
  56. $sentences = Sentence::where('strlen', '>', 0)
  57. ->where('book_id', '<', 1000)
  58. ->where('channel_uid', '<>', '')
  59. ->groupby('book_id', 'paragraph', 'channel_uid')
  60. ->select('book_id', 'paragraph', 'channel_uid');
  61. }
  62. $count = $sentences->count();
  63. $sentences = $sentences->cursor();
  64. $this->info('sentences:' . $count);
  65. #第二步 更新段落表
  66. foreach ($sentences as $sentence) {
  67. # 第二步 生成para progress 1,2,15,zh-tw
  68. # 计算此段落完成时间
  69. $finalAt = Sentence::where('strlen', '>', 0)
  70. ->where('book_id', $sentence->book_id)
  71. ->where('paragraph', $sentence->paragraph)
  72. ->where('channel_uid', $sentence->channel_uid)
  73. ->max('created_at');
  74. $updateAt = Sentence::where('strlen', '>', 0)
  75. ->where('book_id', $sentence->book_id)
  76. ->where('paragraph', $sentence->paragraph)
  77. ->where('channel_uid', $sentence->channel_uid)
  78. ->max('updated_at');
  79. # 查询每个段落的等效巴利语字符数
  80. $result_sent = Sentence::where('strlen', '>', 0)
  81. ->where('book_id', $sentence->book_id)
  82. ->where('paragraph', $sentence->paragraph)
  83. ->where('channel_uid', $sentence->channel_uid)
  84. ->select('word_start')
  85. ->get();
  86. if (count($result_sent) > 0) {
  87. #查询这些句子的总共等效巴利语字符数
  88. $para_strlen = 0;
  89. foreach ($result_sent as $sent) {
  90. # code...
  91. $para_strlen += PaliSentence::where('book', $sentence->book_id)
  92. ->where('paragraph', $sentence->paragraph)
  93. ->where('word_begin', $sent->word_start)
  94. ->value('length');
  95. }
  96. $paraInfo = [
  97. 'book' => $sentence->book_id,
  98. 'para' => $sentence->paragraph,
  99. 'channel_id' => $sentence->channel_uid
  100. ];
  101. $paraData = [
  102. 'lang' => 'en',
  103. 'all_strlen' => $para_strlen,
  104. 'public_strlen' => $para_strlen,
  105. 'created_at' => $finalAt,
  106. 'updated_at' => $updateAt,
  107. ];
  108. Log::debug('Progress updateOrInsert', ['para' => $paraInfo, 'data' => $paraData]);
  109. Progress::updateOrInsert($paraInfo, $paraData);
  110. }
  111. }
  112. $time = time() - $startTime;
  113. $this->info("upgrade progress finished in {$time}s");
  114. return 0;
  115. }
  116. }