UpgradeChapterDynamicWeekly.php 5.0 KB

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