StatisticsExp.php 2.2 KB

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