StatisticsNissaya.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. $current = Carbon::now();
  51. while ($current >= $firstMonth) {
  52. # code...
  53. $start = Carbon::create($current)->startOfMonth();
  54. $end = Carbon::create($current)->endOfMonth();
  55. $date = $current->format('Y-m');
  56. $strlen = Sentence::whereIn('channel_uid',$nissaya_channel)
  57. ->whereDate('created_at','>=',$start)
  58. ->whereDate('created_at','<=',$end)
  59. ->sum('strlen');
  60. $editor = Sentence::whereIn('channel_uid',$nissaya_channel)
  61. ->whereDate('created_at','>=',$start)
  62. ->whereDate('created_at','<=',$end)
  63. ->groupBy('editor_uid')
  64. ->select('editor_uid')->get();
  65. $info = $date.','.$strlen.','.count($editor);
  66. $this->info($info);
  67. Storage::disk('local')->append($file, $info);
  68. $current->modify('-1 month');
  69. }
  70. return 0;
  71. }
  72. }