2
0

StatisticsExp.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\UserOperationDaily;
  4. use App\Tools\Tools;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Storage;
  8. class StatisticsExp extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'statistics:exp';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '统计 经验值 每月查询';
  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. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $file = 'public/statistics/exp-monthly.csv';
  42. Storage::disk('local')->put($file, '');
  43. // 按月获取数据
  44. $firstDay = UserOperationDaily::select('created_at')
  45. ->orderBy('created_at')
  46. ->first();
  47. $firstDay = strtotime($firstDay->created_at);
  48. $firstMonth = Carbon::create(date('Y-m', $firstDay));
  49. $now = Carbon::now();
  50. $current = $firstMonth;
  51. $sumTime = 0;
  52. while ($current <= $now) {
  53. // code...
  54. $start = Carbon::create($current)->startOfMonth();
  55. $end = Carbon::create($current)->endOfMonth();
  56. $date = $current->format('Y-m');
  57. $time = UserOperationDaily::whereDate('created_at', '>=', $start)
  58. ->whereDate('created_at', '<=', $end)
  59. ->sum('duration') / 1000;
  60. $sumTime += $time;
  61. $editor = UserOperationDaily::whereDate('created_at', '>=', $start)
  62. ->whereDate('created_at', '<=', $end)
  63. ->groupBy('user_id')
  64. ->select('user_id')->get();
  65. $info = $date.','.(int) ($time / 3600).','.(int) ($sumTime / 3600).','.count($editor);
  66. $this->info($info);
  67. Storage::disk('local')->append($file, $info);
  68. $current->addMonth(1);
  69. }
  70. return 0;
  71. }
  72. }