UpgradeProgress.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:progress';
  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. $this->info('upgrade:progress start');
  40. $startTime = time();
  41. $channels = Sentence::where('strlen','>',0)
  42. ->where('book_id','<',1000)
  43. ->where('channel_uid','<>','')
  44. ->groupby('book_id','paragraph','channel_uid')
  45. ->select('book_id','paragraph','channel_uid')
  46. ->cursor();
  47. $this->info('channels:',count($channels));
  48. #第二步 更新段落表
  49. $bar = $this->output->createProgressBar(count($channels));
  50. foreach ($channels as $channel) {
  51. # 第二步 生成para progress 1,2,15,zh-tw
  52. # 计算此段落完成时间
  53. $finalAt = Sentence::where('strlen','>',0)
  54. ->where('book_id',$channel->book_id)
  55. ->where('paragraph',$channel->paragraph)
  56. ->where('channel_uid',$channel->channel_uid)
  57. ->max('created_at');
  58. $updateAt = Sentence::where('strlen','>',0)
  59. ->where('book_id',$channel->book_id)
  60. ->where('paragraph',$channel->paragraph)
  61. ->where('channel_uid',$channel->channel_uid)
  62. ->max('updated_at');
  63. # 查询每个段落的等效巴利语字符数
  64. $result_sent = Sentence::where('strlen','>',0)
  65. ->where('book_id',$channel->book_id)
  66. ->where('paragraph',$channel->paragraph)
  67. ->where('channel_uid',$channel->channel_uid)
  68. ->select('word_start')
  69. ->get();
  70. if (count($result_sent) > 0) {
  71. #查询这些句子的总共等效巴利语字符数
  72. $para_strlen = 0;
  73. foreach ($result_sent as $sent) {
  74. # code...
  75. $para_strlen += PaliSentence::where('book',$channel->book_id)
  76. ->where('paragraph',$channel->paragraph)
  77. ->where('word_begin',$sent->word_start)
  78. ->value('length');
  79. }
  80. Progress::updateOrInsert(
  81. [
  82. 'book'=>$channel->book_id,
  83. 'para'=>$channel->paragraph,
  84. 'channel_id'=>$channel->channel_uid
  85. ],
  86. [
  87. 'lang'=>'en',
  88. 'all_strlen'=>$para_strlen,
  89. 'public_strlen'=>$para_strlen,
  90. 'created_at'=>$finalAt,
  91. 'updated_at'=>$updateAt,
  92. ]);
  93. }
  94. $bar->advance();
  95. }
  96. $bar->finish();
  97. $time = time() - $startTime;
  98. $this->info("upgrade progress finished in {$time}s");
  99. return 0;
  100. }
  101. }