UpgradeChapterDynamic.php 5.7 KB

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