UpgradeProgress.php 4.6 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. class UpgradeProgress extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan upgrade:progress --book=122 --para=244 --channel=5310999c-0b0c-4bb0-9bb9-9cdd176e9ef0
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:progress {--book=} {--para=} {--channel=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if(\App\Tools\Tools::isStop()){
  40. return 0;
  41. }
  42. $this->info('upgrade:progress start');
  43. $startTime = time();
  44. $book = $this->option('book');
  45. $para = $this->option('para');
  46. $channelId = $this->option('channel');
  47. if($book && $para && $channelId){
  48. $sentences = Sentence::where('strlen','>',0)
  49. ->where('book_id',$book)
  50. ->where('paragraph',$para)
  51. ->where('channel_uid',$channelId)
  52. ->groupby('book_id','paragraph','channel_uid')
  53. ->select('book_id','paragraph','channel_uid')
  54. ->cursor();
  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. ->cursor();
  62. }
  63. $this->info('sentences:',count($sentences));
  64. #第二步 更新段落表
  65. $bar = $this->output->createProgressBar(count($sentences));
  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'=>$channel->book_id,
  98. 'para'=>$channel->paragraph,
  99. 'channel_id'=>$channel->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. $bar->advance();
  112. }
  113. $bar->finish();
  114. $time = time() - $startTime;
  115. $this->info("upgrade progress finished in {$time}s");
  116. return 0;
  117. }
  118. }