StatisticsDict.php 2.3 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\UserOperationLog;
  7. class StatisticsDict extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'statistics:dict';
  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/lookup-monthly.csv";
  38. Storage::disk('local')->put($file, "");
  39. #按月获取数据
  40. $firstDay = UserOperationLog::where('op_type','dict_lookup')
  41. ->orderBy('created_at')
  42. ->select('created_at')
  43. ->first();
  44. $firstDay = strtotime($firstDay->created_at);
  45. $firstMonth = Carbon::create(date("Y-m",$firstDay));
  46. $now = Carbon::now();
  47. $current = $firstMonth;
  48. $sumCount = 0;
  49. while ($current <= $now) {
  50. # code...
  51. $start = Carbon::create($current)->startOfMonth();
  52. $end = Carbon::create($current)->endOfMonth();
  53. $date = $current->format('Y-m');
  54. $count = UserOperationLog::where('op_type','dict_lookup')
  55. ->whereDate('created_at','>=',$start)
  56. ->whereDate('created_at','<=',$end)
  57. ->count();
  58. $sumCount += $count;
  59. $editor = UserOperationLog::where('op_type','dict_lookup')
  60. ->whereDate('created_at','>=',$start)
  61. ->whereDate('created_at','<=',$end)
  62. ->groupBy('user_id')
  63. ->select('user_id')->get();
  64. $info = $date.','.$count.','.$sumCount.','.count($editor);
  65. $this->info($info);
  66. Storage::disk('local')->append($file, $info);
  67. $current->addMonth(1);
  68. }
  69. return 0;
  70. }
  71. }