UpgradeChapterDynamicWeekly.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #章节长度
  56. $paraEnd = PaliText::where('book',$chapter->book)
  57. ->where('paragraph',$chapter->para)
  58. ->value('chapter_len')+$chapter->para-1;
  59. $progress = [];
  60. for ($i=$weeks; $i > 0 ; $i--) {
  61. #这一周有多少次更新
  62. $currDay = $i*7+$this->option('offset',0);
  63. $count = SentHistory::whereBetween('sent_histories.created_at',
  64. [Carbon::today()->subDays($currDay)->startOfWeek(),
  65. Carbon::today()->subDays($currDay)->endOfWeek()])
  66. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  67. ->where('book_id',$chapter->book)
  68. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  69. ->count();
  70. $progress[] = $count;
  71. }
  72. Cache::put("/chapter_dynamic/{$chapter->book}/{$chapter->para}/global",$progress,3600*24*7);
  73. $bar->advance();
  74. if($this->option('test')){
  75. break; //调试代码
  76. }
  77. }
  78. $bar->finish();
  79. $time = time()- $startAt;
  80. $this->info("用时 {$time}");
  81. $startAt = time();
  82. $startAt = time();
  83. //更新chennel动态
  84. $this->info('更新chennel动态');
  85. $table = ProgressChapter::select('book','para','channel_id');
  86. if($this->option('book')){
  87. $table = $table->where('book',$this->option('book'));
  88. }
  89. $bar = $this->output->createProgressBar($table->count());
  90. foreach ($table->cursor() as $chapter) {
  91. # code...
  92. $max=0;
  93. #章节长度
  94. $paraEnd = PaliText::where('book',$chapter->book)
  95. ->where('paragraph',$chapter->para)
  96. ->value('chapter_len')+$chapter->para-1;
  97. $progress = [];
  98. for ($i=$weeks; $i > 0 ; $i--) {
  99. #这一周有多少次更新
  100. $currDay = $i*7+$this->option('offset',0);
  101. $count = SentHistory::whereBetween('sent_histories.created_at',
  102. [Carbon::today()->subDays($currDay)->startOfWeek(),
  103. Carbon::today()->subDays($currDay)->endOfWeek()])
  104. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  105. ->where('book_id',$chapter->book)
  106. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  107. ->where('sentences.channel_uid',$chapter->channel_id)
  108. ->count();
  109. $progress[] = $count;
  110. }
  111. Cache::put("/chapter_dynamic/{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}",$progress,3600*24*7);
  112. $bar->advance();
  113. if($this->option('test')){
  114. break; //调试代码
  115. }
  116. }
  117. $bar->finish();
  118. $time = time()- $startAt;
  119. $this->info("用时 {$time}");
  120. $this->info("upgrade:chapter.dynamic done");
  121. return 0;
  122. }
  123. }