UpgradeChapterDynamic.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. class UpgradeChapterDynamic extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:chapter.dynamic {--test}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '更新章节活跃程度svg';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if(\App\Tools\Tools::isStop()){
  41. return 0;
  42. }
  43. $this->info('upgrade:chapterdynamic start.');
  44. $startAt = time();
  45. $img_width = 600;
  46. $img_height = 120;
  47. $days = 300; //统计多少天
  48. $min = 30;
  49. $linewidth = 2;
  50. //更新总动态
  51. $this->info("更新总动态");
  52. $chapters = ProgressChapter::select('book','para')
  53. ->groupBy('book','para')
  54. ->orderBy('book')
  55. ->get();
  56. $bar = $this->output->createProgressBar(count($chapters));
  57. foreach ($chapters as $key => $chapter) {
  58. # code...
  59. $max=0;
  60. #章节长度
  61. $paraEnd = PaliText::where('book',$chapter->book)
  62. ->where('paragraph',$chapter->para)
  63. ->value('chapter_len')+$chapter->para-1;
  64. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'>";
  65. $svg .= "<polyline points='";
  66. for ($i=$days; $i >0 ; $i--) {
  67. # code...
  68. #这一天有多少次更新
  69. $count = SentHistory::whereDate('sent_histories.created_at', '=', Carbon::today()->subDays($i)->toDateString())
  70. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  71. ->where('book_id',$chapter->book)
  72. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  73. ->count();
  74. $x=($days-$i)*($img_width/$days);
  75. $y=(300-$count)*($img_height/300)-$linewidth;
  76. $svg .= "{$x},{$y} ";
  77. }
  78. $svg .= "' style='fill:none;stroke:green;stroke-width:{$linewidth}' /></svg>";
  79. $filename = "{$chapter->book}/{$chapter->para}/globle.svg";
  80. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  81. $bar->advance();
  82. if($this->option('test')){
  83. break; //调试代码
  84. }
  85. }
  86. $bar->finish();
  87. $time = time()- $startAt;
  88. $this->info("用时 {$time}");
  89. $startAt = time();
  90. $this->info('更新缺的章节空白图');
  91. // 更新缺的章节空白图
  92. $chapters = PaliText::select('book','paragraph')
  93. ->where('level', '<', 8)
  94. ->get();
  95. $bar = $this->output->createProgressBar(count($chapters));
  96. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'></svg>";
  97. foreach ($chapters as $key => $chapter) {
  98. $filename = "{$chapter->book}/{$chapter->paragraph}/globle.svg";
  99. if(!Storage::disk('local')->exists("public/images/chapter_dynamic/{$filename}")){
  100. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  101. }
  102. $bar->advance();
  103. if($this->option('test')){
  104. break; //调试代码
  105. }
  106. }
  107. $bar->finish();
  108. $time = time()- $startAt;
  109. $this->info("用时 {$time}");
  110. $startAt = time();
  111. //更新chennel动态
  112. $this->info('更新chennel动态');
  113. $bar = $this->output->createProgressBar(ProgressChapter::count());
  114. foreach (ProgressChapter::select('book','para','channel_id')->cursor() as $chapter) {
  115. # code...
  116. $max=0;
  117. #章节长度
  118. $paraEnd = PaliText::where('book',$chapter->book)
  119. ->where('paragraph',$chapter->para)
  120. ->value('chapter_len')+$chapter->para-1;
  121. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'>";
  122. $svg .= "<polyline points='";
  123. for ($i=$days; $i >0 ; $i--) {
  124. # code...
  125. #这一天有多少次更新
  126. $count = SentHistory::whereDate('sent_histories.created_at', '=', Carbon::today()->subDays($i)->toDateString())
  127. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  128. ->where('book_id',$chapter->book)
  129. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  130. ->where('sentences.channel_uid',$chapter->channel_id)
  131. ->count();
  132. $x=($days-$i)*($img_width/$days);
  133. $y=(300-$count)*($img_height/300)-$linewidth;
  134. $svg .= "{$x},{$y} ";
  135. }
  136. $svg .= "' style='fill:none;stroke:green;stroke-width:{$linewidth}' /></svg>";
  137. $filename = "{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}.svg";
  138. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  139. $bar->advance();
  140. if($this->option('test')){
  141. break; //调试代码
  142. }
  143. }
  144. $bar->finish();
  145. $time = time()- $startAt;
  146. $this->info("用时 {$time}");
  147. $this->info("upgrade:chapterdynamic done");
  148. return 0;
  149. }
  150. }