UpgradeProgress.php 4.7 KB

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