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