StatisticsDict.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $file = "public/statistics/lookup-monthly.csv";
  41. Storage::disk('local')->put($file, "");
  42. #按月获取数据
  43. $firstDay = UserOperationLog::where('op_type','dict_lookup')
  44. ->orderBy('created_at')
  45. ->select('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. $sumCount = 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. $count = UserOperationLog::where('op_type','dict_lookup')
  58. ->whereDate('created_at','>=',$start)
  59. ->whereDate('created_at','<=',$end)
  60. ->count();
  61. $sumCount += $count;
  62. $editor = UserOperationLog::where('op_type','dict_lookup')
  63. ->whereDate('created_at','>=',$start)
  64. ->whereDate('created_at','<=',$end)
  65. ->groupBy('user_id')
  66. ->select('user_id')->get();
  67. $info = $date.','.$count.','.$sumCount.','.count($editor);
  68. $this->info($info);
  69. Storage::disk('local')->append($file, $info);
  70. $current->addMonth(1);
  71. }
  72. return 0;
  73. }
  74. }