StatisticsExp.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $file = "public/statistics/exp-monthly.csv";
  41. Storage::disk('local')->put($file, "");
  42. #按月获取数据
  43. $firstDay = UserOperationDaily::select('created_at')
  44. ->orderBy('created_at')
  45. ->first();
  46. $firstDay = strtotime($firstDay->created_at);
  47. $firstMonth = Carbon::create(date("Y-m",$firstDay));
  48. $now = Carbon::now();
  49. $current = $firstMonth;
  50. $sumTime = 0;
  51. while ($current <= $now) {
  52. # code...
  53. $start = Carbon::create($current)->startOfMonth();
  54. $end = Carbon::create($current)->endOfMonth();
  55. $date = $current->format('Y-m');
  56. $time = UserOperationDaily::whereDate('created_at','>=',$start)
  57. ->whereDate('created_at','<=',$end)
  58. ->sum('duration')/1000;
  59. $sumTime += $time;
  60. $editor = UserOperationDaily::whereDate('created_at','>=',$start)
  61. ->whereDate('created_at','<=',$end)
  62. ->groupBy('user_id')
  63. ->select('user_id')->get();
  64. $info = $date.','.(int)($time/3600).','.(int)($sumTime/3600).','.count($editor);
  65. $this->info($info);
  66. Storage::disk('local')->append($file, $info);
  67. $current->addMonth(1);
  68. }
  69. return 0;
  70. }
  71. }