UpgradeChapterDynamic.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 {chapter?}';
  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. $img_width = 600;
  41. $img_height = 120;
  42. $days = 300; //统计多少天
  43. $min = 30;
  44. $linewidth = 2;
  45. $chapters = ProgressChapter::select('book','para')
  46. ->groupBy('book','para')
  47. ->orderBy('book')
  48. ->get();
  49. $bar = $this->output->createProgressBar(count($chapters));
  50. foreach ($chapters as $key => $chapter) {
  51. # code...
  52. $max=0;
  53. #章节长度
  54. $paraEnd = PaliText::where('book',$chapter->book)
  55. ->where('paragraph',$chapter->para)
  56. ->value('chapter_len')+$chapter->para-1;
  57. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'>";
  58. $svg .= "<polyline points='";
  59. for ($i=$days; $i >0 ; $i--) {
  60. # code...
  61. #这一天有多少次更新
  62. $count = SentHistory::whereDate('sent_histories.created_at', '=', Carbon::today()->subDays($i)->toDateString())
  63. ->leftJoin('sentences', 'sent_histories.sent_uid', '=', 'sentences.uid')
  64. ->where('book_id',$chapter->book)
  65. ->whereBetween('paragraph',[$chapter->para,$paraEnd])
  66. ->count();
  67. $x=($days-$i)*($img_width/$days);
  68. $y=(300-$count)*($img_height/300)-$linewidth;
  69. $svg .= "{$x},{$y} ";
  70. }
  71. $svg .= "' style='fill:none;stroke:green;stroke-width:{$linewidth}' /></svg>";
  72. $filename = "cd_{$chapter->book}_{$chapter->para}.svg";
  73. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  74. $bar->advance();
  75. }
  76. $bar->finish();
  77. $this->info('更新缺的章节空白图');
  78. // 更新缺的章节空白图
  79. $chapters = PaliText::select('book','paragraph')
  80. ->where('level', '<', 8)
  81. ->get();
  82. $bar = $this->output->createProgressBar(count($chapters));
  83. $svg = "<svg xmlns='http://www.w3.org/2000/svg' fill='currentColor' viewBox='0 0 $img_width $img_height'></svg>";
  84. foreach ($chapters as $key => $chapter) {
  85. $filename = "cd_{$chapter->book}_{$chapter->paragraph}.svg";
  86. if(!Storage::disk('local')->exists("public/images/chapter_dynamic/{$filename}")){
  87. Storage::disk('local')->put("public/images/chapter_dynamic/{$filename}", $svg);
  88. }
  89. $bar->advance();
  90. }
  91. $bar->finish();
  92. return 0;
  93. }
  94. }