UpgradeChapterDynamicWeekly.php 5.1 KB

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