UpgradeChapterDynamic.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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:chapterdynamic {--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. $this->info('upgrade:chapterdynamic start.');
  41. $startAt = time();
  42. $img_width = 600;
  43. $img_height = 120;
  44. $days = 300; //统计多少天
  45. $min = 30;
  46. $linewidth = 2;
  47. //更新总动态
  48. $this->info("更新总动态");
  49. $chapters = ProgressChapter::select('book','para')
  50. ->groupBy('book','para')
  51. ->orderBy('book')
  52. ->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. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'>";
  62. $svg .= "<polyline points='";
  63. for ($i=$days; $i >0 ; $i--) {
  64. # code...
  65. #这一天有多少次更新
  66. $count = SentHistory::whereDate('sent_histories.created_at', '=', Carbon::today()->subDays($i)->toDateString())
  67. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  68. ->where('book_id',$chapter->book)
  69. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  70. ->count();
  71. $x=($days-$i)*($img_width/$days);
  72. $y=(300-$count)*($img_height/300)-$linewidth;
  73. $svg .= "{$x},{$y} ";
  74. }
  75. $svg .= "' style='fill:none;stroke:green;stroke-width:{$linewidth}' /></svg>";
  76. $filename = "{$chapter->book}/{$chapter->para}/globle.svg";
  77. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  78. $bar->advance();
  79. if($this->option('test')){
  80. break; //调试代码
  81. }
  82. }
  83. $bar->finish();
  84. $time = time()- $startAt;
  85. $this->info("用时 {$time}");
  86. $startAt = time();
  87. $this->info('更新缺的章节空白图');
  88. // 更新缺的章节空白图
  89. $chapters = PaliText::select('book','paragraph')
  90. ->where('level', '<', 8)
  91. ->get();
  92. $bar = $this->output->createProgressBar(count($chapters));
  93. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'></svg>";
  94. foreach ($chapters as $key => $chapter) {
  95. $filename = "{$chapter->book}/{$chapter->paragraph}/globle.svg";
  96. if(!Storage::disk('local')->exists("public/images/chapter_dynamic/{$filename}")){
  97. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  98. }
  99. $bar->advance();
  100. if($this->option('test')){
  101. break; //调试代码
  102. }
  103. }
  104. $bar->finish();
  105. $time = time()- $startAt;
  106. $this->info("用时 {$time}");
  107. $startAt = time();
  108. //更新chennel动态
  109. $this->info('更新chennel动态');
  110. $bar = $this->output->createProgressBar(ProgressChapter::count());
  111. foreach (ProgressChapter::select('book','para','channel_id')->cursor() as $chapter) {
  112. # code...
  113. $max=0;
  114. #章节长度
  115. $paraEnd = PaliText::where('book',$chapter->book)
  116. ->where('paragraph',$chapter->para)
  117. ->value('chapter_len')+$chapter->para-1;
  118. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'>";
  119. $svg .= "<polyline points='";
  120. for ($i=$days; $i >0 ; $i--) {
  121. # code...
  122. #这一天有多少次更新
  123. $count = SentHistory::whereDate('sent_histories.created_at', '=', Carbon::today()->subDays($i)->toDateString())
  124. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  125. ->where('book_id',$chapter->book)
  126. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  127. ->where('sentences.channel_uid',$chapter->channel_id)
  128. ->count();
  129. $x=($days-$i)*($img_width/$days);
  130. $y=(300-$count)*($img_height/300)-$linewidth;
  131. $svg .= "{$x},{$y} ";
  132. }
  133. $svg .= "' style='fill:none;stroke:green;stroke-width:{$linewidth}' /></svg>";
  134. $filename = "{$chapter->book}/{$chapter->para}/ch_{$chapter->channel_id}.svg";
  135. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  136. $bar->advance();
  137. if($this->option('test')){
  138. break; //调试代码
  139. }
  140. }
  141. $bar->finish();
  142. $time = time()- $startAt;
  143. $this->info("用时 {$time}");
  144. $this->info("upgrade:chapterdynamic done");
  145. return 0;
  146. }
  147. }