UpgradeChapterDynamicWeekly.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Support\Facades\Storage;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Carbon;
  6. use App\Models\SentHistory;
  7. use App\Models\Sentence;
  8. use App\Models\ProgressChapter;
  9. use App\Models\PaliText;
  10. use Illuminate\Support\Facades\Cache;
  11. class UpgradeChapterDynamicWeekly extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'upgrade:chapter.dynamic.weekly {--test} {--book=} {--offset=}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '更新章节活跃程度svg';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return int
  38. */
  39. public function handle()
  40. {
  41. $this->info('upgrade:chapter.dynamic.weekly start.');
  42. $startAt = time();
  43. $weeks = 60; //统计多少周
  44. //更新总动态
  45. $this->info("更新总动态");
  46. $table = ProgressChapter::select('book','para')
  47. ->groupBy('book','para')
  48. ->orderBy('book');
  49. if($this->option('book')){
  50. $table = $table->where('book',$this->option('book'));
  51. }
  52. $chapters = $table->get();
  53. $bar = $this->output->createProgressBar(count($chapters));
  54. foreach ($chapters as $key => $chapter) {
  55. # code...
  56. $max=0;
  57. #章节长度
  58. $paraEnd = PaliText::where('book',$chapter->book)
  59. ->where('paragraph',$chapter->para)
  60. ->value('chapter_len')+$chapter->para-1;
  61. $progress = [];
  62. for ($i=$weeks; $i > 0 ; $i--) {
  63. #这一周有多少次更新
  64. $currDay = $i*7+$this->option('offset',0);
  65. $count = SentHistory::whereBetween('sent_histories.created_at',
  66. [Carbon::today()->subDays($currDay)->startOfWeek(),
  67. Carbon::today()->subDays($currDay)->endOfWeek()])
  68. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  69. ->where('book_id',$chapter->book)
  70. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  71. ->count();
  72. $progress[] = $count;
  73. }
  74. Cache::put("/chapter_dynamic/{$chapter->book}/{$chapter->para}/global",$progress,3600*24*7);
  75. $bar->advance();
  76. if($this->option('test')){
  77. break; //调试代码
  78. }
  79. }
  80. $bar->finish();
  81. $time = time()- $startAt;
  82. $this->info("用时 {$time}");
  83. $startAt = time();
  84. $startAt = time();
  85. //更新chennel动态
  86. $this->info('更新chennel动态');
  87. $table = ProgressChapter::select('book','para','channel_id');
  88. if($this->option('book')){
  89. $table = $table->where('book',$this->option('book'));
  90. }
  91. $bar = $this->output->createProgressBar($table->count());
  92. foreach ($table->cursor() as $chapter) {
  93. # code...
  94. $max=0;
  95. #章节长度
  96. $paraEnd = PaliText::where('book',$chapter->book)
  97. ->where('paragraph',$chapter->para)
  98. ->value('chapter_len')+$chapter->para-1;
  99. $progress = [];
  100. for ($i=$weeks; $i > 0 ; $i--) {
  101. #这一周有多少次更新
  102. $currDay = $i*7+$this->option('offset',0);
  103. $count = SentHistory::whereBetween('sent_histories.created_at',
  104. [Carbon::today()->subDays($currDay)->startOfWeek(),
  105. Carbon::today()->subDays($currDay)->endOfWeek()])
  106. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  107. ->where('book_id',$chapter->book)
  108. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  109. ->where('sentences.channel_uid',$chapter->channel_id)
  110. ->count();
  111. $progress[] = $count;
  112. }
  113. Cache::put("/chapter_dynamic/{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}",$progress,3600*24*7);
  114. $bar->advance();
  115. if($this->option('test')){
  116. break; //调试代码
  117. }
  118. }
  119. $bar->finish();
  120. $time = time()- $startAt;
  121. $this->info("用时 {$time}");
  122. $this->info("upgrade:chapter.dynamic done");
  123. return 0;
  124. }
  125. }