StatisticsNissaya.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Channel;
  5. use App\Models\Sentence;
  6. use Illuminate\Support\Facades\Storage;
  7. use Carbon\Carbon;
  8. class StatisticsNissaya extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'statistics:nissaya';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '统计nissaya 每月录入进度';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. $nissaya_channel = Channel::where('type','nissaya')->select('uid')->get();
  39. $this->info('channel:'.count($nissaya_channel));
  40. $maxDay = 360;
  41. $file = "public/statistics/nissaya-monthly.csv";
  42. Storage::disk('local')->put($file, "");
  43. #按月获取数据
  44. $firstDay = Sentence::whereIn('channel_uid',$nissaya_channel)
  45. ->orderBy('created_at')
  46. ->select('created_at')
  47. ->first();
  48. $firstDay = strtotime($firstDay->created_at);
  49. $firstMonth = Carbon::create(date("Y-m",$firstDay));
  50. $now = Carbon::now();
  51. $current = $firstMonth;
  52. $sumStrlen = 0;
  53. while ($current <= $now) {
  54. # code...
  55. $start = Carbon::create($current)->startOfMonth();
  56. $end = Carbon::create($current)->endOfMonth();
  57. $date = $current->format('Y-m');
  58. $strlen = Sentence::whereIn('channel_uid',$nissaya_channel)
  59. ->whereDate('created_at','>=',$start)
  60. ->whereDate('created_at','<=',$end)
  61. ->sum('strlen');
  62. $sumStrlen += $strlen;
  63. $editor = Sentence::whereIn('channel_uid',$nissaya_channel)
  64. ->whereDate('created_at','>=',$start)
  65. ->whereDate('created_at','<=',$end)
  66. ->groupBy('editor_uid')
  67. ->select('editor_uid')->get();
  68. $info = $date.','.$strlen.','.$sumStrlen.','.count($editor);
  69. $this->info($info);
  70. Storage::disk('local')->append($file, $info);
  71. $current->addMonth(1);
  72. }
  73. return 0;
  74. }
  75. }