UpgradeProgress.php 4.8 KB

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